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
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:
-t, --tasks=<tasks> Gradle tasks to run
-p, --project=<project-dir> Path to the project root (default: Current directory)
-t, --task=<task> Gradle tasks to run
-p, --project=<path> Path to the project root (default: Current directory)
-o, --out-dir=<dir> Path to write generated files (default: <project>)
-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)
-g, --gradle-version=<version> Use a specific Gradle version
-j, --gradle-jdk=<dir> JDK home directory to use for launching Gradle (default: JAVA_HOME)
-j, --gradle-jdk=<dir> 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
@@ -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

View File

@@ -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)

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.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<String>,
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<String> 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,