Skip to content

Commit

Permalink
Add include/excludeVersions to BranchSpec
Browse files Browse the repository at this point in the history
This acts as an addition/subtraction combo that is only used if the versions field is not set. Useful for April Fools snapshots.
Add settings.gradle for Gradle JDK resolution. Fixes case when user does not have the necessary JDK downloaded (like me not having J21 for 24w14a) and tries to import with Gradle.
Fix bug calculating target version if it is omitted
Change initial commit generation to only copy gradle/wrapper instead of gradle to account for a potential future use of libs.versions.toml by excluding it
Fix parsing of --cfg parameter when provided URI uses the file scheme
Update Gradle to 8.7
  • Loading branch information
SizableShrimp committed Apr 6, 2024
1 parent ec5cd26 commit d85c7b7
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 76 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ configurations {
}

dependencies {
compileOnly 'org.jetbrains:annotations:24.0.0'
compileOnly 'org.jetbrains:annotations:24.1.0'

// When updating shade dependencies, you must also run the generateDependencyHashes task
shade 'org.slf4j:slf4j-api:2.0.12'
Expand Down Expand Up @@ -104,8 +104,8 @@ tasks.named('idea').configure {
}

tasks.named('jar', Jar).configure {
from('gradle') {
into 'gradle'
from('gradle/wrapper') {
into 'gradle/wrapper'
}
manifest {
attributes([
Expand All @@ -119,8 +119,8 @@ tasks.named('shadowJar', ShadowJar).configure {
archiveClassifier = 'all'
configurations = [project.configurations.shade]

from('gradle') {
into 'gradle'
from('gradle/wrapper') {
into 'gradle/wrapper'
rename '(.+).jar', '$1.zip'
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0'
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
}

rootProject.name = 'Snowblower'
34 changes: 20 additions & 14 deletions src/main/java/net/neoforged/snowblower/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ public Generator setup(String branchName, @Nullable URL remoteUrl, boolean check

var cfgBranch = cfg.branches() == null ? null : cfg.branches().get(branchName);
if (cfgBranch == null) {
if (cliBranch.start() == null && cliBranch.end() == null && cliBranch.versions() == null)
if (cliBranch.start() == null && cliBranch.end() == null)
throw new IllegalArgumentException("Unknown branch config: " + branchName);
this.branch = cliBranch;
} else {
this.branch = new BranchSpec(
cfgBranch.type(),
cliBranch.start() == null ? cfgBranch.start() : cliBranch.start(),
cliBranch.end() == null ? cfgBranch.end() : cliBranch.end(),
cfgBranch.versions()
cfgBranch.versions(),
cfgBranch.includeVersions(),
cfgBranch.excludeVersions()
);
}

Expand Down Expand Up @@ -241,21 +243,23 @@ private void runInternal() throws IOException, GitAPIException {
if (manifest.versions() == null)
throw new IllegalStateException("Failed to find versions, manifest missing versions listing");

var versions = Arrays.asList(manifest.versions());
/* Sort the list by release time.. in case Mojang screwed it up?
Arrays.stream(manifest.versions())
.sorted((a,b) -> b.releaseTime().compareTo(a.releaseTime())) // b to a, so its in descending order
.toList();
*/
// Sorted from newest at index 0 to oldest at the end of the list
var versions = new ArrayList<>(Arrays.asList(manifest.versions()));

var targetVer = this.branch.end();
// If we have explicit filters, apply them
if (this.branch.versions() != null) {
versions = versions.stream().filter(v -> this.branch.versions().contains(v.id())).toList();
versions.removeIf(v -> !this.branch.versions().contains(v.id()));
if (targetVer == null)
targetVer = versions.get(0).id();
} else {
versions = versions.stream().filter(v -> !v.id().getType().isSpecial()).toList();
var exclude = versions.stream().filter(v -> v.id().getType().isSpecial()).map(VersionInfo::id).collect(Collectors.toList());
if (this.branch.includeVersions() != null)
exclude.removeAll(this.branch.includeVersions());
if (this.branch.excludeVersions() != null)
exclude.addAll(this.branch.excludeVersions());

versions.removeIf(v -> exclude.contains(v.id()));
}

// Find the latest version from the manifest
Expand All @@ -267,8 +271,8 @@ private void runInternal() throws IOException, GitAPIException {
if (this.branch.type().equals("release"))
targetVer = lat.release();
else {
var release = versions.stream().filter(e -> !lat.release().equals(e.id())).findFirst().orElse(null);
var snapshot = versions.stream().filter(e -> !lat.snapshot().equals(e.id())).findFirst().orElse(null);
var release = versions.stream().filter(e -> lat.release().equals(e.id())).findFirst().orElse(null);
var snapshot = versions.stream().filter(e -> lat.snapshot().equals(e.id())).findFirst().orElse(null);
if (release == null && snapshot == null)
throw new IllegalStateException("Failed to find latest, manifest specified " + lat.release() + " and " + lat.snapshot() + " and both are missing");
if (release == null)
Expand Down Expand Up @@ -314,8 +318,10 @@ else if (snapshot == null)
}
}

if (startIdx == -1 || endIdx == -1)
throw new IllegalStateException("Could not find start and/or end version in version manifest (or they were out of order)");
if (startIdx == -1)
throw new IllegalStateException("Could not find start version in version list. Was it excluded? Start: " + startVer + ", End: " + targetVer);
if (endIdx == -1)
throw new IllegalStateException("Could not find end version in version list (or end version is earlier than start version). Was it excluded? Start: " + startVer + ", End: " + targetVer);

List<VersionInfo> toGenerate = new ArrayList<>(versions.subList(endIdx, startIdx + 1));
if (this.branch.type().equals("release"))
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/net/neoforged/snowblower/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void main(String[] args) throws Exception {
var targetVer = options.has(targetVerO) ? MinecraftVersion.from(options.valueOf(targetVerO)) : null;
if (targetVer != null && targetVer.compareTo(startVer) < 0)
throw new IllegalArgumentException("Target version must be greater than or equal to start version");
var cliBranch = new BranchSpec(options.has(releasesOnlyO) ? "release" : "all", startVer, targetVer, null);
var cliBranch = new BranchSpec(options.has(releasesOnlyO) ? "release" : "all", startVer, targetVer);

String branchName = options.valueOf(branchNameO);

Expand All @@ -101,14 +101,18 @@ public static void main(String[] args) throws Exception {
URI configUri = options.valueOf(configO);
try {
URL url = configUri.toURL();
cfg = Util.downloadJson(url, Config.class);
if ("file".equals(configUri.getScheme())) {
cfg = Config.load(Util.getPath(configUri));
} else {
cfg = Util.downloadJson(url, Config.class);
}
} catch (MalformedURLException e) {
cfg = Config.load(Util.getPath(configUri));
throw new RuntimeException(e);
}
} else {
Map<String, BranchSpec> branches = new HashMap<>();
branches.put("release", new BranchSpec("release", null, null, null));
branches.put("dev", new BranchSpec("all", null, null, null));
branches.put("release", new BranchSpec("release", null, null));
branches.put("dev", new BranchSpec("all", null, null));
cfg = new Config(branches);
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/neoforged/snowblower/data/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public record BranchSpec(
String type,
MinecraftVersion start,
MinecraftVersion end,
List<MinecraftVersion> versions
) {}
List<MinecraftVersion> versions,
List<MinecraftVersion> includeVersions,
List<MinecraftVersion> excludeVersions
) {
public BranchSpec(String type, MinecraftVersion start, MinecraftVersion end) {
this(type, start, end, null, null, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
*/
package net.neoforged.snowblower.tasks.enhance;

import net.neoforged.snowblower.data.Version;
import net.neoforged.snowblower.util.HashFunction;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import net.neoforged.snowblower.data.Version;
import net.neoforged.snowblower.util.HashFunction;

public class EnhanceVersionTask {
// TODO: I moved this out to its own package even tho its only one function, because I'm curious if we can cache it.
// And doing that via a package level would be useful.
Expand All @@ -25,50 +25,63 @@ public class EnhanceVersionTask {
* This also includes dependencies with bad OS filtering rules when they are needed on all systems for compiling.
* Adding these to all generated Minecraft versions should be mostly safe.
*/
private static final List<String> EXTRA_DEPENDENCIES = List.of("org.jetbrains:annotations:24.0.0", "com.google.code.findbugs:jsr305:3.0.2", "ca.weblite:java-objc-bridge:1.1");
private static final String GRADLE_CONTENT = """
plugins {
id 'java'
}
private static final List<String> EXTRA_DEPENDENCIES = List.of("org.jetbrains:annotations:24.1.0", "com.google.code.findbugs:jsr305:3.0.2", "ca.weblite:java-objc-bridge:1.1");
private static final String BUILD_GRADLE_CONTENT = """
plugins {
id 'java'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(%java_version%)
}
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(%java_version%)
}
}
repositories {
mavenCentral()
maven {
name = 'Mojang'
url = 'https://libraries.minecraft.net/'
}
}
repositories {
mavenCentral()
maven {
name = 'Mojang'
url = 'https://libraries.minecraft.net/'
}
}
dependencies {
%deps%
}
""";
dependencies {
%deps%
}
""";
private static final String SETTINGS_GRADLE_CONTENT = """
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
}
""";

public static List<Path> enhance(Path output, Version version) throws IOException {
var data = GRADLE_CONTENT
.replace("%java_version%", Integer.toString(version.javaVersion().majorVersion())) // This assumes the minimum to be 8 (which it is)
.replace("%deps%", Stream.concat(version.libraries().stream()
.filter(Version.Library::isAllowed)
.map(Version.Library::name), EXTRA_DEPENDENCIES.stream())
.sorted()
.map(lib -> " implementation '" + lib + '\'')
.collect(Collectors.joining("\n")))
.getBytes(StandardCharsets.UTF_8);
var buildData = BUILD_GRADLE_CONTENT
.replace("%java_version%", Integer.toString(version.javaVersion().majorVersion())) // This assumes the minimum to be 8 (which it is)
.replace("%deps%", Stream.concat(version.libraries().stream()
.filter(Version.Library::isAllowed)
.map(Version.Library::name), EXTRA_DEPENDENCIES.stream())
.sorted()
.map(lib -> " implementation '" + lib + '\'')
.collect(Collectors.joining("\n")))
.getBytes(StandardCharsets.UTF_8);
var settingsData = SETTINGS_GRADLE_CONTENT.getBytes(StandardCharsets.UTF_8);

List<Path> added = new ArrayList<>();

writeCached(buildData, added, output.resolve("build.gradle"));
writeCached(settingsData, added, output.resolve("settings.gradle"));

var build = output.resolve("build.gradle");
var existing = Files.exists(build) ? HashFunction.MD5.hash(build) : "";
return added;
}

private static void writeCached(byte[] data, List<Path> added, Path path) throws IOException {
var existing = Files.exists(path) ? HashFunction.MD5.hash(path) : "";
var created = HashFunction.MD5.hash(data);

if (!existing.equals(created)) {
Files.write(build, data);
return List.of(build);
Files.write(path, data);
added.add(path);
}
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public boolean validate(MinecraftVersion start) throws IOException, GitAPIExcept

try {
Path copyParentFolder = Util.isDev() ? Util.getSourcePath() : Util.getPath(Main.class.getResource("/resource_root.txt").toURI()).getParent();
List<String> toCopy = List.of("gradlew", "gradlew.bat", "gradle");
List<String> toCopy = List.of("gradlew", "gradlew.bat", "gradle/wrapper");
AddCommand addCmd = git.add();

for (String filename : toCopy) {
Expand Down

0 comments on commit d85c7b7

Please sign in to comment.