diff --git a/README.org b/README.org index 9803d1d..07f9b87 100644 --- a/README.org +++ b/README.org @@ -95,18 +95,25 @@ nix run github:tadfisher/gradle2nix -- --help #+begin_example Usage: gradle2nix [] []... +Gradle installation: + + Where to find Gradle. By default, use the project's wrapper. + + --gradle-dist= Gradle distribution URI + --gradle-home= Gradle home path (e.g. `nix eval nixpkgs#gradle.outPath`/lib/gradle) + --gradle-wrapper= Gradle wrapper version + Options: - -t, --tasks= Gradle tasks to run - -p, --project= Path to the project root (default: Current directory) - -o, --out-dir= Path to write generated files (default: ) - -l, --lock-file= Name of the generated lock file (default: gradle.lock) - -n, --nix-file= Name of the generated Nix file (default: gradle.nix) - -g, --gradle-version= Use a specific Gradle version - -j, --gradle-jdk= JDK home directory to use for launching Gradle (default: JAVA_HOME) - --log=(debug|info|warn|error) Print messages with this priority or higher (default: error) - --dump-events Dump Gradle event logs to the output directory - --stacktrace Print a stack trace on error - -h, --help Show this message and exit + -t, --task= Gradle tasks to run + -p, --project= Path to the project root (default: Current directory) + -o, --out-dir= Path to write generated files (default: ) + -l, --lock-file= Name of the generated lock file (default: gradle.lock) + -n, --nix-file= Name of the generated Nix file (default: gradle.nix) + -j, --gradle-jdk= JDK home to use for launching Gradle (e.g. nix eval nixpkgs#openjdk.home) + --log=(debug|info|warn|error) Print messages with this priority or higher (default: error) + --dump-events Dump Gradle event logs to the output directory + --stacktrace Print a stack trace on error + -h, --help Show this message and exit Arguments: Extra arguments to pass to Gradle @@ -137,16 +144,21 @@ gradle2nix -t :app:installDist /Note:/ This may be *required* if the build resolves configurations at execution time. -*** Specifying the Gradle version +*** Specifying the Gradle installation -By default, if the project has configured the Gradle wrapper, that -version will be detected and pinned; otherwise, the version of Gradle -installed on your system will be pinned. You can override this with -the =--gradle-version= argument, which also avoids the need to have -Gradle installed. +By default, if the project has configured the Gradle wrapper, it will +be used; otherwise, the version of Gradle used to build gradle2nix +will be used. You can override this to use any of the following: #+begin_example -gradle2nix -g 6.1 +# Gradle distribution URL: +gradle2nix --gradle-dist='https://services.gradle.org/distributions/gradle-8.7-bin.zip' + +# Path to a local Gradle installation: +gradle2nix --gradle-home=`nix eval nixpkgs#gradle.outPath`/lib/gradle + +# A specific wrapper version: +gradle2nix --gradle-wrapper=8.7 #+end_example ** Contributing diff --git a/app/src/main/kotlin/org/nixos/gradle2nix/GradleRunner.kt b/app/src/main/kotlin/org/nixos/gradle2nix/GradleRunner.kt index 0ca87ce..fc92bb7 100644 --- a/app/src/main/kotlin/org/nixos/gradle2nix/GradleRunner.kt +++ b/app/src/main/kotlin/org/nixos/gradle2nix/GradleRunner.kt @@ -16,8 +16,11 @@ import org.nixos.gradle2nix.model.RESOLVE_ALL_TASK fun connect(config: Config, projectDir: File = config.projectDir): ProjectConnection = GradleConnector.newConnector() .apply { - if (config.gradleVersion != null) { - useGradleVersion(config.gradleVersion) + when (val source = config.gradleSource) { + is GradleSource.Distribution -> useDistribution(source.uri) + is GradleSource.Path -> useInstallation(source.path) + GradleSource.Project -> useBuildDistribution() + is GradleSource.Wrapper -> useGradleVersion(source.version) } } .forProjectDirectory(projectDir) diff --git a/app/src/main/kotlin/org/nixos/gradle2nix/Main.kt b/app/src/main/kotlin/org/nixos/gradle2nix/Main.kt index 46a48b0..b459802 100644 --- a/app/src/main/kotlin/org/nixos/gradle2nix/Main.kt +++ b/app/src/main/kotlin/org/nixos/gradle2nix/Main.kt @@ -5,26 +5,33 @@ import com.github.ajalt.clikt.core.context import com.github.ajalt.clikt.output.MordantHelpFormatter import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.multiple +import com.github.ajalt.clikt.parameters.groups.default +import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions +import com.github.ajalt.clikt.parameters.groups.single +import com.github.ajalt.clikt.parameters.options.convert import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.defaultLazy import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.multiple import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.options.optionalValue import com.github.ajalt.clikt.parameters.options.validate import com.github.ajalt.clikt.parameters.types.enum import com.github.ajalt.clikt.parameters.types.file import java.io.File +import java.net.URI import kotlinx.coroutines.runBlocking import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.json.encodeToStream +import org.gradle.internal.service.scopes.Scopes.Gradle import org.gradle.tooling.model.gradle.GradleBuild import org.nixos.gradle2nix.model.DependencySet data class Config( val appHome: File, val gradleHome: File, - val gradleVersion: String?, + val gradleSource: GradleSource, val gradleJdk: File?, val gradleArgs: List, val outDir: File, @@ -41,6 +48,13 @@ val JsonFormat = Json { prettyPrintIndent = " " } +sealed interface GradleSource { + data class Distribution(val uri: URI) : GradleSource + data class Path(val path: File) : GradleSource + data object Project : GradleSource + data class Wrapper(val version: String) : GradleSource +} + enum class LogLevel { debug, info, @@ -49,7 +63,7 @@ enum class LogLevel { } class Gradle2Nix : CliktCommand( - name = "gradle2nix" + name = "gradle2nix", ) { private val tasks: List by option( "--task", "-t", @@ -88,16 +102,29 @@ class Gradle2Nix : CliktCommand( help = "Name of the generated Nix file" ).default("gradle.nix") - private val gradleVersion: String? by option( - "--gradle-version", "-g", - metavar = "VERSION", - help = "Use a specific Gradle version" - ) + private val gradleSource: GradleSource by mutuallyExclusiveOptions( + option( + "--gradle-dist", + metavar = "URI", + help = "Gradle distribution URI" + ).convert { GradleSource.Distribution(URI(it)) }, + option( + "--gradle-home", + metavar = "DIR", + help = "Gradle home path (e.g. \\`nix eval nixpkgs#gradle.outPath\\`/lib/gradle)" + ).file(mustExist = true, canBeFile = false).convert { GradleSource.Path(it) }, + option( + "--gradle-wrapper", + help = "Gradle wrapper version" + ).convert { GradleSource.Wrapper(it) }, + name = "Gradle installation", + help = "Where to find Gradle. By default, use the project's wrapper." + ).single().default(GradleSource.Project) private val gradleJdk: File? by option( "--gradle-jdk", "-j", metavar = "DIR", - help = "JDK home directory to use for launching Gradle (default: ${System.getProperty("java.home")})" + help = "JDK home to use for launching Gradle (e.g. `nix eval nixpkgs#openjdk.home`)", ).file(canBeFile = false, canBeDir = true) private val logLevel: LogLevel by option( @@ -138,7 +165,7 @@ class Gradle2Nix : CliktCommand( val config = Config( appHome, gradleHome, - gradleVersion, + gradleSource, gradleJdk, gradleArgs, outDir ?: projectDir,