Allow overriding the Gradle installation

This commit is contained in:
Tad Fisher
2024-05-24 16:10:01 -07:00
parent 799fb3debc
commit f82c91be9f
3 changed files with 71 additions and 29 deletions

View File

@@ -95,14 +95,21 @@ nix run github:tadfisher/gradle2nix -- --help
#+begin_example #+begin_example
Usage: gradle2nix [<options>] [<args>]... Usage: gradle2nix [<options>] [<args>]...
Gradle installation:
Where to find Gradle. By default, use the project's wrapper.
--gradle-dist=<uri> Gradle distribution URI
--gradle-home=<dir> Gradle home path (e.g. `nix eval nixpkgs#gradle.outPath`/lib/gradle)
--gradle-wrapper=<value> Gradle wrapper version
Options: Options:
-t, --tasks=<tasks> Gradle tasks to run -t, --task=<task> Gradle tasks to run
-p, --project=<project-dir> Path to the project root (default: Current directory) -p, --project=<path> Path to the project root (default: Current directory)
-o, --out-dir=<dir> Path to write generated files (default: <project>) -o, --out-dir=<dir> Path to write generated files (default: <project>)
-l, --lock-file=<filename> Name of the generated lock file (default: gradle.lock) -l, --lock-file=<filename> Name of the generated lock file (default: gradle.lock)
-n, --nix-file=<filename> Name of the generated Nix file (default: gradle.nix) -n, --nix-file=<filename> Name of the generated Nix file (default: gradle.nix)
-g, --gradle-version=<version> Use a specific Gradle version -j, --gradle-jdk=<dir> JDK home to use for launching Gradle (e.g. nix eval nixpkgs#openjdk.home)
-j, --gradle-jdk=<dir> 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) --log=(debug|info|warn|error) Print messages with this priority or higher (default: error)
--dump-events Dump Gradle event logs to the output directory --dump-events Dump Gradle event logs to the output directory
--stacktrace Print a stack trace on error --stacktrace Print a stack trace on error
@@ -137,16 +144,21 @@ gradle2nix -t :app:installDist
/Note:/ This may be *required* if the build resolves configurations /Note:/ This may be *required* if the build resolves configurations
at execution time. at execution time.
*** Specifying the Gradle version *** Specifying the Gradle installation
By default, if the project has configured the Gradle wrapper, that By default, if the project has configured the Gradle wrapper, it will
version will be detected and pinned; otherwise, the version of Gradle be used; otherwise, the version of Gradle used to build gradle2nix
installed on your system will be pinned. You can override this with will be used. You can override this to use any of the following:
the =--gradle-version= argument, which also avoids the need to have
Gradle installed.
#+begin_example #+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 #+end_example
** Contributing ** Contributing

View File

@@ -16,8 +16,11 @@ import org.nixos.gradle2nix.model.RESOLVE_ALL_TASK
fun connect(config: Config, projectDir: File = config.projectDir): ProjectConnection = fun connect(config: Config, projectDir: File = config.projectDir): ProjectConnection =
GradleConnector.newConnector() GradleConnector.newConnector()
.apply { .apply {
if (config.gradleVersion != null) { when (val source = config.gradleSource) {
useGradleVersion(config.gradleVersion) is GradleSource.Distribution -> useDistribution(source.uri)
is GradleSource.Path -> useInstallation(source.path)
GradleSource.Project -> useBuildDistribution()
is GradleSource.Wrapper -> useGradleVersion(source.version)
} }
} }
.forProjectDirectory(projectDir) .forProjectDirectory(projectDir)

View File

@@ -5,26 +5,33 @@ import com.github.ajalt.clikt.core.context
import com.github.ajalt.clikt.output.MordantHelpFormatter import com.github.ajalt.clikt.output.MordantHelpFormatter
import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple 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.default
import com.github.ajalt.clikt.parameters.options.defaultLazy import com.github.ajalt.clikt.parameters.options.defaultLazy
import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.multiple import com.github.ajalt.clikt.parameters.options.multiple
import com.github.ajalt.clikt.parameters.options.option 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.options.validate
import com.github.ajalt.clikt.parameters.types.enum import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.file import com.github.ajalt.clikt.parameters.types.file
import java.io.File import java.io.File
import java.net.URI
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream import kotlinx.serialization.json.encodeToStream
import org.gradle.internal.service.scopes.Scopes.Gradle
import org.gradle.tooling.model.gradle.GradleBuild import org.gradle.tooling.model.gradle.GradleBuild
import org.nixos.gradle2nix.model.DependencySet import org.nixos.gradle2nix.model.DependencySet
data class Config( data class Config(
val appHome: File, val appHome: File,
val gradleHome: File, val gradleHome: File,
val gradleVersion: String?, val gradleSource: GradleSource,
val gradleJdk: File?, val gradleJdk: File?,
val gradleArgs: List<String>, val gradleArgs: List<String>,
val outDir: File, val outDir: File,
@@ -41,6 +48,13 @@ val JsonFormat = Json {
prettyPrintIndent = " " 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 { enum class LogLevel {
debug, debug,
info, info,
@@ -49,7 +63,7 @@ enum class LogLevel {
} }
class Gradle2Nix : CliktCommand( class Gradle2Nix : CliktCommand(
name = "gradle2nix" name = "gradle2nix",
) { ) {
private val tasks: List<String> by option( private val tasks: List<String> by option(
"--task", "-t", "--task", "-t",
@@ -88,16 +102,29 @@ class Gradle2Nix : CliktCommand(
help = "Name of the generated Nix file" help = "Name of the generated Nix file"
).default("gradle.nix") ).default("gradle.nix")
private val gradleVersion: String? by option( private val gradleSource: GradleSource by mutuallyExclusiveOptions(
"--gradle-version", "-g", option(
metavar = "VERSION", "--gradle-dist",
help = "Use a specific Gradle version" 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( private val gradleJdk: File? by option(
"--gradle-jdk", "-j", "--gradle-jdk", "-j",
metavar = "DIR", 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) ).file(canBeFile = false, canBeDir = true)
private val logLevel: LogLevel by option( private val logLevel: LogLevel by option(
@@ -138,7 +165,7 @@ class Gradle2Nix : CliktCommand(
val config = Config( val config = Config(
appHome, appHome,
gradleHome, gradleHome,
gradleVersion, gradleSource,
gradleJdk, gradleJdk,
gradleArgs, gradleArgs,
outDir ?: projectDir, outDir ?: projectDir,