mirror of
https://github.com/tadfisher/gradle2nix.git
synced 2026-01-12 07:50:53 -05:00
Dogfood v2
This commit is contained in:
@@ -21,7 +21,6 @@ dependencies {
|
||||
isTransitive = false
|
||||
}
|
||||
|
||||
//testRuntimeOnly(kotlin("reflect"))
|
||||
testImplementation(libs.kotest.assertions)
|
||||
testImplementation(libs.kotest.runner)
|
||||
testImplementation(libs.ktor.server.core)
|
||||
|
||||
292
app/src/dist/share/gradle.nix
vendored
Normal file
292
app/src/dist/share/gradle.nix
vendored
Normal file
@@ -0,0 +1,292 @@
|
||||
# This file is generated by gradle2nix.
|
||||
#
|
||||
# Example usage (e.g. in default.nix):
|
||||
#
|
||||
# with (import <nixpkgs> {});
|
||||
# let
|
||||
# buildGradle = callPackage ./gradle.nix {};
|
||||
# in
|
||||
# buildGradle {
|
||||
# lockFile = ./gradle.lock;
|
||||
#
|
||||
# src = ./.;
|
||||
#
|
||||
# gradleFlags = [ "installDist" ];
|
||||
#
|
||||
# installPhase = ''
|
||||
# mkdir -p $out
|
||||
# cp -r app/build/install/myproject $out
|
||||
# '';
|
||||
# }
|
||||
|
||||
{ lib
|
||||
, stdenv
|
||||
, buildEnv
|
||||
, fetchs3
|
||||
, fetchurl
|
||||
, gradle
|
||||
, maven
|
||||
, runCommandLocal
|
||||
, symlinkJoin
|
||||
, writeText
|
||||
, writeTextDir
|
||||
}:
|
||||
|
||||
{
|
||||
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
|
||||
lockFile
|
||||
, pname ? "project"
|
||||
, version ? null
|
||||
, enableParallelBuilding ? true
|
||||
# Arguments to Gradle used to build the project in buildPhase.
|
||||
, gradleFlags ? [ "build" ]
|
||||
# Enable debugging for the Gradle build; this will cause Gradle to run a debug server
|
||||
# and wait for a JVM debugging client to attach.
|
||||
, enableDebug ? false
|
||||
# Additional code to run in the Gradle init script (init.gradle).
|
||||
, extraInit ? ""
|
||||
# Override the default JDK used to run Gradle itself.
|
||||
, buildJdk ? null
|
||||
# Override functions which fetch dependency artifacts.
|
||||
# Keys in this set are URL schemes such as "https" or "s3".
|
||||
# Values are functions which take a dependency in the form
|
||||
# `{ urls, hash }` and fetch into the Nix store. For example:
|
||||
#
|
||||
# {
|
||||
# s3 = { name, urls, hash }: fetchs3 {
|
||||
# s3url = builtins.head urls;
|
||||
# # TODO This doesn't work without patching fetchs3 to accept SRI hashes
|
||||
# inherit name hash;
|
||||
# region = "us-west-2";
|
||||
# credentials = {
|
||||
# access_key_id = "foo";
|
||||
# secret_access_key = "bar";
|
||||
# };
|
||||
# };
|
||||
# }
|
||||
, fetchers ? { }
|
||||
, ... } @ args:
|
||||
|
||||
let
|
||||
inherit (builtins)
|
||||
attrValues concatStringsSep elemAt filter fromJSON getAttr hasAttr head length match
|
||||
removeAttrs replaceStrings sort;
|
||||
|
||||
inherit (lib)
|
||||
assertMsg concatMapStringsSep findFirst foldl' groupBy' hasSuffix hasPrefix last mapAttrs
|
||||
mapAttrsToList optionalAttrs optionalString readFile removeSuffix unique versionAtLeast
|
||||
versionOlder;
|
||||
|
||||
inherit (lib.strings) sanitizeDerivationName;
|
||||
|
||||
lockedDeps = fromJSON (readFile lockFile);
|
||||
|
||||
toCoordinates = id:
|
||||
let
|
||||
coords = builtins.split ":" id;
|
||||
in rec {
|
||||
group = elemAt coords 0;
|
||||
module = elemAt coords 2;
|
||||
version = elemAt coords 4;
|
||||
versionParts = parseVersion version;
|
||||
};
|
||||
|
||||
parseVersion = version:
|
||||
let
|
||||
parts = builtins.split ":" version;
|
||||
base = elemAt parts 0;
|
||||
in
|
||||
{
|
||||
inherit base;
|
||||
exact = base;
|
||||
}
|
||||
// optionalAttrs (length parts >= 2) (
|
||||
let
|
||||
snapshot = elemAt parts 2;
|
||||
exact = replaceStrings [ "-SNAPSHOT" ] [ "-${snapshot}" ] base;
|
||||
parts = builtins.split "-" timestamp;
|
||||
timestamp = findFirst (match "[0-9]{8}\.[0-9]{6}") parts;
|
||||
buildNumber = let lastPart = last parts; in if match "[0-9]+" lastPart then lastPart else null;
|
||||
in
|
||||
{ inherit snapshot exact timestamp buildNumber; }
|
||||
);
|
||||
|
||||
fetchers' = {
|
||||
http = fetchurl;
|
||||
https = fetchurl;
|
||||
} // fetchers;
|
||||
|
||||
# Fetch urls using the scheme for the first entry only; there isn't a
|
||||
# straightforward way to tell Nix to try multiple fetchers in turn
|
||||
# and short-circuit on the first successful fetch.
|
||||
fetch = name: { urls, hash }:
|
||||
let
|
||||
first = head urls;
|
||||
scheme = head (builtins.match "([a-z0-9+.-]+)://.*" first);
|
||||
fetch' = getAttr scheme fetchers';
|
||||
urls' = filter (hasPrefix scheme) urls;
|
||||
in
|
||||
fetch' { urls = urls'; inherit hash; };
|
||||
|
||||
mkModule = id: artifacts:
|
||||
let
|
||||
coords = toCoordinates id;
|
||||
modulePath = "${replaceStrings ["."] ["/"] coords.group}/${coords.module}/${coords.version}";
|
||||
in
|
||||
stdenv.mkDerivation {
|
||||
pname = sanitizeDerivationName "${coords.group}-${coords.module}";
|
||||
version = coords.versionParts.exact;
|
||||
|
||||
srcs = mapAttrsToList fetch artifacts;
|
||||
|
||||
dontPatch = true;
|
||||
dontConfigure = true;
|
||||
dontBuild = true;
|
||||
dontFixup = true;
|
||||
dontInstall = true;
|
||||
|
||||
preUnpack = ''
|
||||
mkdir -p "$out/${modulePath}"
|
||||
'';
|
||||
|
||||
unpackCmd = ''
|
||||
cp "$curSrc" "$out/${modulePath}/$(stripHash "$curSrc")"
|
||||
'';
|
||||
|
||||
sourceRoot = ".";
|
||||
|
||||
preferLocalBuild = true;
|
||||
allowSubstitutes = false;
|
||||
};
|
||||
|
||||
offlineRepo = symlinkJoin {
|
||||
name = if version != null then "${pname}-${version}-gradle-repo" else "${pname}-gradle-repo";
|
||||
paths = mapAttrsToList mkModule lockedDeps;
|
||||
};
|
||||
|
||||
initScript =
|
||||
let
|
||||
inSettings = pred: script:
|
||||
optionalString pred (
|
||||
if versionAtLeast gradle.version "6.0" then ''
|
||||
gradle.beforeSettings {
|
||||
${script}
|
||||
}
|
||||
'' else ''
|
||||
gradle.settingsEvaluated {
|
||||
${script}
|
||||
}
|
||||
''
|
||||
);
|
||||
in
|
||||
writeText "init.gradle" ''
|
||||
static def offlineRepo(RepositoryHandler repositories) {
|
||||
repositories.clear()
|
||||
repositories.mavenLocal {
|
||||
url 'file:${offlineRepo}'
|
||||
metadataSources {
|
||||
gradleMetadata()
|
||||
mavenPom()
|
||||
artifact()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
${inSettings (versionAtLeast gradle.version "6.0") ''
|
||||
offlineRepo(it.buildscript.repositories)
|
||||
''}
|
||||
|
||||
${inSettings true ''
|
||||
offlineRepo(it.pluginManagement.repositories)
|
||||
''}
|
||||
|
||||
gradle.projectsLoaded {
|
||||
allprojects {
|
||||
buildscript {
|
||||
offlineRepo(repositories)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
${if versionAtLeast gradle.version "6.8"
|
||||
then ''
|
||||
gradle.beforeSettings {
|
||||
it.dependencyResolutionManagement {
|
||||
offlineRepo(repositories)
|
||||
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
|
||||
}
|
||||
}
|
||||
''
|
||||
else ''
|
||||
gradle.projectsLoaded {
|
||||
allprojects {
|
||||
offlineRepo(repositories)
|
||||
}
|
||||
}
|
||||
''
|
||||
}
|
||||
|
||||
${extraInit}
|
||||
'';
|
||||
|
||||
buildProject = flags: ''
|
||||
gradle --offline --no-daemon --no-build-cache \
|
||||
--info --full-stacktrace --warning-mode=all \
|
||||
--no-configuration-cache \
|
||||
-Dmaven.repo.local=${offlineRepo} \
|
||||
${optionalString enableParallelBuilding "--parallel"} \
|
||||
${optionalString enableDebug "-Dorg.gradle.debug=true"} \
|
||||
${optionalString (buildJdk != null) "-Dorg.gradle.java.home=${buildJdk.home}"} \
|
||||
--init-script ${initScript} \
|
||||
${concatStringsSep " " flags}
|
||||
'';
|
||||
|
||||
in stdenv.mkDerivation ({
|
||||
|
||||
dontStrip = true;
|
||||
|
||||
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ gradle ];
|
||||
|
||||
buildPhase = args.buildPhase or ''
|
||||
runHook preBuild
|
||||
|
||||
(
|
||||
set -eux
|
||||
|
||||
${optionalString (versionOlder gradle.version "8.0") ''
|
||||
# Work around https://github.com/gradle/gradle/issues/1055
|
||||
TMPHOME="$(mktemp -d)"
|
||||
mkdir -p "$TMPHOME/init.d"
|
||||
export GRADLE_USER_HOME="$TMPHOME"
|
||||
cp ${initScript} $TMPHOME/
|
||||
''}
|
||||
|
||||
gradle --offline --no-daemon --no-build-cache \
|
||||
--info --full-stacktrace --warning-mode=all \
|
||||
--no-configuration-cache --console=plain \
|
||||
-Dmaven.repo.local=${offlineRepo} \
|
||||
${optionalString enableParallelBuilding "--parallel"} \
|
||||
${optionalString enableDebug "-Dorg.gradle.debug=true"} \
|
||||
${optionalString (buildJdk != null) "-Dorg.gradle.java.home=${buildJdk.home}"} \
|
||||
--init-script ${initScript} \
|
||||
${concatStringsSep " " gradleFlags}
|
||||
)
|
||||
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
passthru = (args.passthru or {}) // {
|
||||
inherit offlineRepo;
|
||||
};
|
||||
|
||||
} // (removeAttrs args [
|
||||
"nativeBuildInputs"
|
||||
"passthru"
|
||||
"lockFile"
|
||||
"gradleFlags"
|
||||
"gradle"
|
||||
"enableDebug"
|
||||
"extraInit"
|
||||
"buildJdk"
|
||||
"fetchers"
|
||||
]))
|
||||
@@ -2,7 +2,7 @@ package org.nixos.gradle2nix
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.core.context
|
||||
import com.github.ajalt.clikt.output.CliktHelpFormatter
|
||||
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.options.default
|
||||
@@ -10,7 +10,6 @@ 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.validate
|
||||
import com.github.ajalt.clikt.parameters.types.choice
|
||||
import com.github.ajalt.clikt.parameters.types.enum
|
||||
import com.github.ajalt.clikt.parameters.types.file
|
||||
import java.io.File
|
||||
@@ -69,14 +68,20 @@ class Gradle2Nix : CliktCommand(
|
||||
help = "Path to write generated files (default: PROJECT-DIR)")
|
||||
.file(canBeFile = false, canBeDir = true)
|
||||
|
||||
val envFile: String by option(
|
||||
"--env", "-e",
|
||||
val lockFile: String by option(
|
||||
"--lock-file", "-l",
|
||||
metavar = "FILENAME",
|
||||
help = "Prefix for environment files (.json and .nix)")
|
||||
.default("gradle-env")
|
||||
help = "Name of the generated lock file"
|
||||
).default("gradle.lock")
|
||||
|
||||
val nixFile: String by option(
|
||||
"--nix-file", "-n",
|
||||
metavar = "FILENAME",
|
||||
help = "Name of the generated Nix file"
|
||||
).default("gradle.nix")
|
||||
|
||||
private val logLevel: LogLevel by option(
|
||||
"--log", "-l",
|
||||
"--log",
|
||||
metavar = "LEVEL",
|
||||
help = "Print messages with priority of at least LEVEL")
|
||||
.enum<LogLevel>()
|
||||
@@ -118,22 +123,20 @@ class Gradle2Nix : CliktCommand(
|
||||
|
||||
init {
|
||||
context {
|
||||
helpFormatter = CliktHelpFormatter(showDefaultValues = true)
|
||||
helpFormatter = { MordantHelpFormatter(it, showDefaultValues = true) }
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
override fun run() {
|
||||
val appHome = System.getProperty("org.nixos.gradle2nix.share")
|
||||
if (appHome == null) {
|
||||
System.err.println("Error: could not locate the /share directory in the gradle2nix installation")
|
||||
}
|
||||
val gradleHome =
|
||||
System.getenv("GRADLE_USER_HOME")?.let(::File) ?: File("${System.getProperty("user.home")}/.gradle")
|
||||
val logger = Logger(logLevel = logLevel, stacktrace = stacktrace)
|
||||
|
||||
val appHome = System.getProperty("org.nixos.gradle2nix.share")?.let(::File)
|
||||
?: error("could not locate the /share directory in the gradle2nix installation")
|
||||
val gradleHome =
|
||||
System.getenv("GRADLE_USER_HOME")?.let(::File) ?: File("${System.getProperty("user.home")}/.gradle")
|
||||
val config = Config(
|
||||
File(appHome),
|
||||
appHome,
|
||||
gradleHome,
|
||||
gradleVersion,
|
||||
gradleJdk,
|
||||
@@ -156,6 +159,8 @@ class Gradle2Nix : CliktCommand(
|
||||
} else {
|
||||
metadata.deleteOnExit()
|
||||
}
|
||||
} else {
|
||||
metadata.deleteOnExit()
|
||||
}
|
||||
|
||||
val buildSrcs = connect(config).use { connection ->
|
||||
@@ -187,11 +192,19 @@ class Gradle2Nix : CliktCommand(
|
||||
logger.error("dependency parsing failed", e)
|
||||
}
|
||||
|
||||
val json = config.outDir.resolve("$envFile.json")
|
||||
logger.info("Writing environment to $json")
|
||||
json.outputStream().buffered().use { output ->
|
||||
config.outDir.mkdirs()
|
||||
|
||||
val outLockFile = config.outDir.resolve(lockFile)
|
||||
logger.info("Writing lock file to $outLockFile")
|
||||
outLockFile.outputStream().buffered().use { output ->
|
||||
JsonFormat.encodeToStream(env, output)
|
||||
}
|
||||
|
||||
val inNixFile = config.appHome.resolve("gradle.nix").takeIf { it.exists() }
|
||||
?: error("Couldn't locate gradle.nix in the the gradle2nix installation: ${config.appHome}")
|
||||
val outNixFile = config.outDir.resolve(nixFile)
|
||||
logger.info("Writing Nix builder to $outNixFile")
|
||||
inNixFile.copyTo(outNixFile, overwrite = true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user