mirror of
https://github.com/tadfisher/gradle2nix.git
synced 2026-01-11 15:30:38 -05:00
Use project properties for plugin args
This commit is contained in:
@@ -17,7 +17,6 @@ dependencies {
|
||||
implementation("com.github.ajalt:clikt:latest.release")
|
||||
implementation("org.slf4j:slf4j-api:latest.release")
|
||||
runtimeOnly("org.slf4j:slf4j-simple:latest.release")
|
||||
implementation("com.squareup.moshi:moshi:latest.release")
|
||||
implementation("com.squareup.moshi:moshi-adapters:latest.release")
|
||||
implementation("com.squareup.moshi:moshi-kotlin:latest.release")
|
||||
kapt("com.squareup.moshi:moshi-kotlin-codegen:latest.release")
|
||||
|
||||
@@ -16,12 +16,12 @@ fun connect(config: Config): ProjectConnection =
|
||||
@Suppress("UnstableApiUsage")
|
||||
fun ProjectConnection.getBuildModel(config: Config, path: String): DefaultBuild {
|
||||
return model(Build::class.java).apply {
|
||||
addArguments("--init-script=$shareDir/init.gradle")
|
||||
if (path.isNotEmpty()) addArguments("--project-dir=$path")
|
||||
addJvmArguments(
|
||||
"-Dorg.nixos.gradle2nix.configurations='${config.configurations.joinToString(",")}'",
|
||||
"-Dorg.nixos.gradle2nix.subprojects='${config.subprojects.joinToString(",")}'"
|
||||
addArguments(
|
||||
"--init-script=$shareDir/init.gradle",
|
||||
"-Porg.nixos.gradle2nix.configurations=${config.configurations.joinToString(",")}",
|
||||
"-Porg.nixos.gradle2nix.subprojects=${config.subprojects.joinToString(",")}"
|
||||
)
|
||||
if (path.isNotEmpty()) addArguments("--project-dir=$path")
|
||||
if (!config.quiet) {
|
||||
setStandardOutput(System.err)
|
||||
setStandardError(System.err)
|
||||
|
||||
1333
gradle-env.json
1333
gradle-env.json
File diff suppressed because it is too large
Load Diff
@@ -4,11 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly("com.squareup.moshi:moshi:+")
|
||||
compileOnly("com.squareup.okio:okio:+")
|
||||
api("com.squareup.moshi:moshi:+")
|
||||
api("com.squareup.okio:okio:+")
|
||||
kapt("com.squareup.moshi:moshi-kotlin-codegen:+")
|
||||
|
||||
// https://github.com/gradle/gradle/issues/10697
|
||||
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41")
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41")
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -35,14 +35,12 @@ dependencies {
|
||||
|
||||
compatTestImplementation(embeddedKotlin("stdlib-jdk8"))
|
||||
compatTestImplementation(embeddedKotlin("test-junit5"))
|
||||
compatTestImplementation(embeddedKotlin("reflect"))
|
||||
compatTestImplementation("org.junit.jupiter:junit-jupiter-api:5.4+")
|
||||
compatTestRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4+")
|
||||
compatTestImplementation(gradleTestKit())
|
||||
compatTestImplementation(project(":model"))
|
||||
|
||||
// https://github.com/gradle/gradle/issues/10697
|
||||
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41")
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41")
|
||||
compatTestImplementation("io.strikt:strikt-core:latest.release")
|
||||
}
|
||||
|
||||
gradlePlugin {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
io.strikt:strikt-core:0.22.2
|
||||
org.apiguardian:apiguardian-api:1.0.0
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-test-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-test-junit5:1.3.41
|
||||
@@ -13,4 +17,4 @@ org.jetbrains.kotlin:kotlin-test:1.3.41
|
||||
org.jetbrains:annotations:13.0
|
||||
org.junit.jupiter:junit-jupiter-api:5.4.2
|
||||
org.junit.platform:junit-platform-commons:1.4.2
|
||||
org.opentest4j:opentest4j:1.1.1
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.christophsturm:filepeek:0.1.1
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
io.strikt:strikt-core:0.22.2
|
||||
org.apiguardian:apiguardian-api:1.0.0
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-test-annotations-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-test-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-test-junit5:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-test:1.3.41
|
||||
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1
|
||||
org.jetbrains:annotations:13.0
|
||||
org.junit.jupiter:junit-jupiter-api:5.4.2
|
||||
org.junit.jupiter:junit-jupiter-engine:5.4.2
|
||||
org.junit.platform:junit-platform-commons:1.4.2
|
||||
org.junit.platform:junit-platform-engine:1.4.2
|
||||
org.opentest4j:opentest4j:1.1.1
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
javax.inject:javax.inject:1
|
||||
org.apache.commons:commons-lang3:3.8.1
|
||||
org.apache.maven:maven-artifact:3.6.2
|
||||
@@ -12,8 +14,8 @@ org.codehaus.plexus:plexus-utils:3.2.1
|
||||
org.eclipse.sisu:org.eclipse.sisu.inject:0.3.3
|
||||
org.gradle:gradle-tooling-api:5.6.3
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
javax.inject:javax.inject:1
|
||||
org.apache.commons:commons-lang3:3.8.1
|
||||
org.apache.maven:maven-artifact:3.6.2
|
||||
@@ -10,3 +12,6 @@ org.apache.maven:maven-model:3.6.2
|
||||
org.codehaus.plexus:plexus-interpolation:1.25
|
||||
org.codehaus.plexus:plexus-utils:3.2.1
|
||||
org.eclipse.sisu:org.eclipse.sisu.inject:0.3.3
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
javax.inject:javax.inject:1
|
||||
org.apache.commons:commons-lang3:3.8.1
|
||||
org.apache.maven:maven-artifact:3.6.2
|
||||
@@ -11,8 +13,8 @@ org.codehaus.plexus:plexus-interpolation:1.25
|
||||
org.codehaus.plexus:plexus-utils:3.2.1
|
||||
org.eclipse.sisu:org.eclipse.sisu.inject:0.3.3
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.9.1
|
||||
com.squareup.okio:okio:2.4.1
|
||||
javax.inject:javax.inject:1
|
||||
org.apache.commons:commons-lang3:3.8.1
|
||||
org.apache.maven:maven-artifact:3.6.2
|
||||
@@ -11,8 +13,8 @@ org.codehaus.plexus:plexus-interpolation:1.25
|
||||
org.codehaus.plexus:plexus-utils:3.2.1
|
||||
org.eclipse.sisu:org.eclipse.sisu.inject:0.3.3
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.3.50
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.41
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.3.50
|
||||
org.jetbrains:annotations:13.0
|
||||
|
||||
@@ -2,6 +2,9 @@ package org.nixos.gradle2nix
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import strikt.api.expectThat
|
||||
import strikt.assertions.containsExactlyInAnyOrder
|
||||
import strikt.assertions.map
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@@ -415,5 +418,44 @@ class SubprojectsTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `includes subproject dependencies`() {
|
||||
root.resolve("child-a").also { it.mkdirs() }.resolve("build.gradle.kts").writeText("")
|
||||
root.resolve("child-b").also { it.mkdirs() }.resolve("build.gradle.kts").writeText("")
|
||||
root.resolve("child-c").also { it.mkdirs() }.resolve("build.gradle.kts").writeText("")
|
||||
root.resolve("child-d").also { it.mkdirs() }.resolve("build.gradle.kts").writeText("")
|
||||
|
||||
root.resolve("settings.gradle.kts").writeText("""
|
||||
include(":child-a", ":child-b", ":child-c", ":child-d")
|
||||
""".trimIndent())
|
||||
|
||||
val buildscript = """
|
||||
subprojects {
|
||||
apply(plugin = "java")
|
||||
}
|
||||
|
||||
project(":child-a") {
|
||||
dependencies {
|
||||
"implementation"(project(":child-b"))
|
||||
}
|
||||
}
|
||||
|
||||
project(":child-b") {
|
||||
dependencies {
|
||||
"implementation"(project(":child-c"))
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
with(root.buildKotlin(buildscript, subprojects = listOf(":child-a"))) {
|
||||
expectThat(rootProject.children).map { it.path }
|
||||
.containsExactlyInAnyOrder(":child-a", ":child-b", ":child-c")
|
||||
}
|
||||
|
||||
with(root.buildKotlin(buildscript, subprojects = listOf(":child-b"))) {
|
||||
expectThat(rootProject.children).map { it.path }.containsExactlyInAnyOrder(":child-b", ":child-c")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +1,33 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import org.gradle.api.internal.artifacts.dsl.ParsedModuleStringNotation
|
||||
import org.gradle.internal.classpath.DefaultClassPath
|
||||
import org.gradle.testkit.runner.GradleRunner
|
||||
import org.gradle.testkit.runner.internal.DefaultGradleRunner
|
||||
import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
|
||||
import org.gradle.tooling.GradleConnector
|
||||
import org.gradle.tooling.events.ProgressListener
|
||||
import java.io.File
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private val moshi = Moshi.Builder().build()
|
||||
|
||||
private fun File.initscript() = resolve("init.gradle").also {
|
||||
it.writer().use { out ->
|
||||
val classpath = DefaultClassPath.of(PluginUnderTestMetadataReading.readImplementationClasspath())
|
||||
.asFiles.joinToString(prefix = "'", postfix = "'")
|
||||
.asFiles.joinToString { n -> "'$n'" }
|
||||
out.appendln("""
|
||||
initscript {
|
||||
dependencies {
|
||||
classpath files($classpath)
|
||||
}
|
||||
}
|
||||
initscript {
|
||||
dependencies {
|
||||
classpath files($classpath)
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: org.nixos.gradle2nix.Gradle2NixPlugin
|
||||
""".trimIndent())
|
||||
apply plugin: org.nixos.gradle2nix.Gradle2NixPlugin
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,22 +53,43 @@ private fun File.build(
|
||||
configurations: List<String>,
|
||||
subprojects: List<String>
|
||||
): DefaultBuild {
|
||||
return GradleConnector.newConnector()
|
||||
.useGradleVersion(System.getProperty("compat.gradle.version"))
|
||||
.forProjectDirectory(this)
|
||||
.connect()
|
||||
.model(Build::class.java).apply {
|
||||
addArguments("--init-script=${initscript()}", "--stacktrace")
|
||||
addJvmArguments(
|
||||
"-Dorg.nixos.gradle2nix.configurations=${configurations.joinToString(",")}",
|
||||
"-Dorg.nixos.gradle2nix.subprojects=${subprojects.joinToString(",")}"
|
||||
)
|
||||
setStandardOutput(System.out)
|
||||
setStandardError(System.out)
|
||||
GradleRunner.create()
|
||||
.withGradleVersion(System.getProperty("compat.gradle.version"))
|
||||
.withProjectDir(this)
|
||||
.forwardOutput()
|
||||
.withArguments(
|
||||
"nixModel",
|
||||
"--init-script=${initscript()}",
|
||||
"--stacktrace",
|
||||
"-Porg.nixos.gradle2nix.configurations=${configurations.joinToString(",")}",
|
||||
"-Porg.nixos.gradle2nix.subprojects=${subprojects.joinToString(",")}"
|
||||
)
|
||||
.build()
|
||||
|
||||
return resolve("build/nix/model.json").run {
|
||||
println(readText())
|
||||
source().buffer().use { src ->
|
||||
checkNotNull(moshi.adapter(DefaultBuild::class.java).fromJson(src))
|
||||
}
|
||||
.get()
|
||||
.let { DefaultBuild(it) }
|
||||
}
|
||||
}
|
||||
//
|
||||
// return GradleConnector.newConnector()
|
||||
// .useGradleVersion(System.getProperty("compat.gradle.version"))
|
||||
// .forProjectDirectory(this)
|
||||
// .connect()
|
||||
// .model(Build::class.java).apply {
|
||||
// addArguments("--init-script=${initscript()}", "--stacktrace")
|
||||
// addJvmArguments(
|
||||
// "-Dorg.gradle.debug=true",
|
||||
// "-Dorg.nixos.gradle2nix.configurations=${configurations.joinToString(",")}",
|
||||
// "-Dorg.nixos.gradle2nix.subprojects=${subprojects.joinToString(",")}"
|
||||
// )
|
||||
// setStandardOutput(System.out)
|
||||
// setStandardError(System.out)
|
||||
// }
|
||||
// .get()
|
||||
// .let { DefaultBuild(it) }
|
||||
|
||||
fun jar(notation: String, sha256: String = ""): DefaultArtifact =
|
||||
artifact(notation, sha256, "jar")
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.artifacts.ArtifactRepositoryContainer
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.artifacts.ProjectDependency
|
||||
import org.gradle.api.artifacts.dsl.RepositoryHandler
|
||||
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
|
||||
import org.gradle.api.internal.GradleInternal
|
||||
@@ -10,6 +15,7 @@ import org.gradle.api.invocation.Gradle
|
||||
import org.gradle.api.tasks.wrapper.Wrapper
|
||||
import org.gradle.kotlin.dsl.getByName
|
||||
import org.gradle.kotlin.dsl.support.serviceOf
|
||||
import org.gradle.kotlin.dsl.withType
|
||||
import org.gradle.plugin.management.PluginRequest
|
||||
import org.gradle.tooling.provider.model.ToolingModelBuilder
|
||||
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||
@@ -19,56 +25,79 @@ import java.util.Locale
|
||||
|
||||
@Suppress("unused")
|
||||
open class Gradle2NixPlugin : Plugin<Gradle> {
|
||||
override fun apply(gradle: Gradle) {
|
||||
val configurationNames: List<String> = System.getProperty("org.nixos.gradle2nix.configurations")
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
?.split(",")
|
||||
?: emptyList()
|
||||
override fun apply(gradle: Gradle): Unit = gradle.run {
|
||||
val pluginRequests = collectPlugins()
|
||||
|
||||
val subprojects: List<String> = System.getProperty("org.nixos.gradle2nix.subprojects")
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
?.split(",")
|
||||
?: emptyList()
|
||||
|
||||
val pluginRequests = collectPlugins(gradle)
|
||||
|
||||
gradle.projectsLoaded {
|
||||
projectsLoaded {
|
||||
val modelProperties = rootProject.loadModelProperties()
|
||||
rootProject.serviceOf<ToolingModelBuilderRegistry>()
|
||||
.register(NixToolingModelBuilder(configurationNames, subprojects, pluginRequests))
|
||||
}
|
||||
}
|
||||
.register(NixToolingModelBuilder(modelProperties, pluginRequests))
|
||||
|
||||
private fun collectPlugins(gradle: Gradle): List<PluginRequest> {
|
||||
val pluginRequests = mutableListOf<PluginRequest>()
|
||||
gradle.settingsEvaluated {
|
||||
pluginManagement.resolutionStrategy.eachPlugin {
|
||||
if (requested.id.namespace != null && requested.id.namespace != "org.gradle") {
|
||||
pluginRequests.add(target)
|
||||
rootProject.tasks.register("nixModel") {
|
||||
doLast {
|
||||
val outFile = project.mkdir(project.buildDir.resolve("nix")).resolve("model.json")
|
||||
val model = project.buildModel(modelProperties, pluginRequests)
|
||||
outFile.sink().buffer().use { out ->
|
||||
Moshi.Builder().build()
|
||||
.adapter(DefaultBuild::class.java)
|
||||
.indent(" ")
|
||||
.toJson(out, model)
|
||||
out.flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return pluginRequests
|
||||
}
|
||||
}
|
||||
|
||||
private const val NIX_MODEL_NAME = "org.nixos.gradle2nix.Build"
|
||||
|
||||
private class NixToolingModelBuilder(
|
||||
private val explicitConfigurations: List<String>,
|
||||
private val explicitSubprojects: List<String>,
|
||||
private val modelProperties: ModelProperties,
|
||||
private val pluginRequests: List<PluginRequest>
|
||||
) : ToolingModelBuilder {
|
||||
override fun canBuild(modelName: String): Boolean {
|
||||
return modelName == "org.nixos.gradle2nix.Build"
|
||||
return modelName == NIX_MODEL_NAME
|
||||
}
|
||||
|
||||
override fun buildAll(modelName: String, project: Project): Build = project.run {
|
||||
val plugins = buildPlugins(pluginRequests)
|
||||
DefaultBuild(
|
||||
gradle = buildGradle(),
|
||||
pluginDependencies = plugins,
|
||||
rootProject = buildProject(explicitConfigurations, explicitSubprojects, plugins),
|
||||
includedBuilds = includedBuilds()
|
||||
)
|
||||
override fun buildAll(modelName: String, project: Project): Build =
|
||||
project.buildModel(modelProperties, pluginRequests)
|
||||
}
|
||||
|
||||
private fun Gradle.collectPlugins(): List<PluginRequest> {
|
||||
val pluginRequests = mutableListOf<PluginRequest>()
|
||||
gradle.settingsEvaluated {
|
||||
pluginManagement.resolutionStrategy.eachPlugin {
|
||||
if (requested.id.namespace != null && requested.id.namespace != "org.gradle") {
|
||||
pluginRequests.add(target)
|
||||
}
|
||||
}
|
||||
}
|
||||
return pluginRequests
|
||||
}
|
||||
|
||||
private fun Project.buildModel(
|
||||
modelProperties: ModelProperties,
|
||||
pluginRequests: List<PluginRequest>
|
||||
): DefaultBuild {
|
||||
val plugins = buildPlugins(pluginRequests)
|
||||
|
||||
val subprojects = if (modelProperties.subprojects.isEmpty()) {
|
||||
project.subprojects
|
||||
} else {
|
||||
project.subprojects
|
||||
.filter { it.path in modelProperties.subprojects }
|
||||
// Include dependent subprojects as well
|
||||
.flatMap { setOf(it) + it.dependentSubprojects(modelProperties.configurations) }
|
||||
.toSet()
|
||||
}
|
||||
|
||||
return DefaultBuild(
|
||||
gradle = buildGradle(),
|
||||
pluginDependencies = plugins,
|
||||
rootProject = buildProject(modelProperties.configurations, subprojects, plugins),
|
||||
includedBuilds = includedBuilds()
|
||||
)
|
||||
}
|
||||
|
||||
private fun Project.buildGradle(): DefaultGradle =
|
||||
@@ -104,20 +133,20 @@ private fun Project.includedBuilds(): List<DefaultIncludedBuild> =
|
||||
|
||||
private fun Project.buildProject(
|
||||
explicitConfigurations: List<String>,
|
||||
explicitSubprojects: List<String>,
|
||||
explicitSubprojects: Collection<Project>,
|
||||
plugins: DefaultDependencies
|
||||
): DefaultProject =
|
||||
DefaultProject(
|
||||
): DefaultProject {
|
||||
logger.lifecycle(" Subproject: $path")
|
||||
return DefaultProject(
|
||||
name = name,
|
||||
version = version.toString(),
|
||||
path = path,
|
||||
projectDir = projectDir.toRelativeString(rootProject.projectDir),
|
||||
buildscriptDependencies = buildscriptDependencies(plugins),
|
||||
projectDependencies = projectDependencies(explicitConfigurations),
|
||||
children = subprojects
|
||||
.filter { explicitSubprojects.isEmpty() || it.path in explicitSubprojects }
|
||||
.map { it.buildProject(explicitConfigurations, emptyList(), plugins) }
|
||||
children = explicitSubprojects.map { it.buildProject(explicitConfigurations, emptyList(), plugins) }
|
||||
)
|
||||
}
|
||||
|
||||
private fun Project.buildscriptDependencies(plugins: DefaultDependencies): DefaultDependencies =
|
||||
with(DependencyResolver(buildscript.configurations, buildscript.dependencies)) {
|
||||
@@ -133,12 +162,7 @@ private fun Project.buildscriptDependencies(plugins: DefaultDependencies): Defau
|
||||
|
||||
private fun Project.projectDependencies(explicitConfigurations: List<String>): DefaultDependencies =
|
||||
with(DependencyResolver(configurations, dependencies)) {
|
||||
val toResolve = if (explicitConfigurations.isEmpty()) {
|
||||
configurations.filter { it.isCanBeResolved }
|
||||
} else {
|
||||
configurations.filter { it.name in explicitConfigurations }
|
||||
}
|
||||
|
||||
val toResolve = collectConfigurations(explicitConfigurations)
|
||||
DefaultDependencies(
|
||||
repositories = repositories.repositories(),
|
||||
artifacts = toResolve.flatMap { resolveDependencies(it) + resolvePoms(it) }
|
||||
@@ -147,14 +171,24 @@ private fun Project.projectDependencies(explicitConfigurations: List<String>): D
|
||||
)
|
||||
}
|
||||
|
||||
private fun fetchDistSha256(url: String): String {
|
||||
return URL("$url.sha256").openConnection().run {
|
||||
connect()
|
||||
getInputStream().reader().use { it.readText() }
|
||||
}
|
||||
private fun Project.dependentSubprojects(explicitConfigurations: List<String>): Set<Project> {
|
||||
return collectConfigurations(explicitConfigurations)
|
||||
.flatMap { it.allDependencies.withType<ProjectDependency>() }
|
||||
.map { it.dependencyProject }
|
||||
.toSet()
|
||||
.flatMap { setOf(it) + it.dependentSubprojects(explicitConfigurations) }
|
||||
.toSet()
|
||||
}
|
||||
|
||||
private val nativePlatformJarRegex = Regex("""native-platform-([\d.]+)\.jar""")
|
||||
private fun Project.collectConfigurations(
|
||||
explicitConfigurations: List<String>
|
||||
): Set<Configuration> {
|
||||
return if (explicitConfigurations.isEmpty()) {
|
||||
configurations.filter { it.isCanBeResolved }.toSet()
|
||||
} else {
|
||||
configurations.filter { it.name in explicitConfigurations }.toSet()
|
||||
}
|
||||
}
|
||||
|
||||
private val excludedRepoNames = setOf(
|
||||
"Embedded Kotlin Repository",
|
||||
@@ -169,6 +203,15 @@ internal fun RepositoryHandler.repositories() = DefaultRepositories(
|
||||
}
|
||||
)
|
||||
|
||||
private fun fetchDistSha256(url: String): String {
|
||||
return URL("$url.sha256").openConnection().run {
|
||||
connect()
|
||||
getInputStream().reader().use { it.readText() }
|
||||
}
|
||||
}
|
||||
|
||||
private val nativePlatformJarRegex = Regex("""native-platform-([\d.]+)\.jar""")
|
||||
|
||||
private val Wrapper.sha256: String
|
||||
get() {
|
||||
return if (GradleVersion.current() < GradleVersion.version("4.5")) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
import java.util.Properties
|
||||
import org.gradle.api.Project
|
||||
|
||||
data class ModelProperties(
|
||||
val configurations: List<String>,
|
||||
val subprojects: List<String>
|
||||
)
|
||||
|
||||
internal fun Project.loadModelProperties(): ModelProperties {
|
||||
return ModelProperties(
|
||||
configurations = this["org.nixos.gradle2nix.configurations"]?.split(",") ?: emptyList(),
|
||||
subprojects = this["org.nixos.gradle2nix.subprojects"]?.split(",") ?: emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
private operator fun Project.get(key: String): String? {
|
||||
return System.getProperty(key)?.takeIf { it.isNotEmpty() }
|
||||
?: (properties[key] as? String)?.takeIf { it.isNotEmpty() }
|
||||
}
|
||||
Reference in New Issue
Block a user