Files
ZomboidForge/zomboidforge.jsh

168 lines
7.8 KiB
Java

// Sets up or updates the ZomboidForge mod project in the current directory, whose
// settings.gradle.kts applies the plugin dev.overlord.zomboidforge. Runs with JShell (JDK 25),
// the same way on Linux, macOS, and Windows:
//
// curl -fsSL https://git.security-command.org/Overlord/ZomboidForge/raw/branch/master/zomboidforge.jsh | jshell -
// curl -fsSL https://git.security-command.org/Overlord/ZomboidForge/raw/branch/master/zomboidforge.jsh | jshell -R-Dzomboidforge.version=0.7.0 -
//
// Steps: sets the plugin version in settings.gradle.kts, writes the Gradle wrapper of that
// ZomboidForge version, then runs "gradlew zomboidForgeSetup build".
//
// System properties (-R-D<name>=<value>):
// zomboidforge.version the version to set; default: the newest published one
// zomboidforge.repository Maven repository with the plugin; default: the ZomboidForge registry
// zomboidforge.files base URL of the wrapper files, as <base>/v<version>/<path>;
// default: the release tags of the ZomboidForge repository
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class ZomboidForgeUpdater
{
static final String WEBSITE = "https://git.security-command.org/Overlord/ZomboidForge";
static final String REPOSITORY = ZomboidForgeUpdater.property("zomboidforge.repository", "https://git.security-command.org/api/packages/overlord/maven");
static final String FILES = ZomboidForgeUpdater.property("zomboidforge.files", ZomboidForgeUpdater.WEBSITE + "/raw/tag");
static final String METADATA = "/dev/overlord/zomboidforge/dev.overlord.zomboidforge.gradle.plugin/maven-metadata.xml";
static final Path SETTINGS = Path.of("settings.gradle.kts");
static final Pattern PLUGIN = Pattern.compile("id\\(\"dev\\.overlord\\.zomboidforge\"\\)\\s*version\\s*\"([^\"]*)\"");
static final Pattern VERSION = Pattern.compile("<version>([^<]+)</version>");
static final Pattern RELEASE = Pattern.compile("<release>([^<]+)</release>");
static final List<String> WRAPPER = List.of("gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.jar", "gradle/wrapper/gradle-wrapper.properties");
static final int TIMEOUT = 30_000;
static int run()
{
try
{
ZomboidForgeUpdater.update();
return 0;
}
catch (IOException e)
{
System.err.println("[zomboidforge] " + e.getMessage());
return 1;
}
catch (Exception e)
{
System.err.println("[zomboidforge] Unable to update: " + e);
return 1;
}
}
static void update() throws Exception
{
if (!Files.isRegularFile(ZomboidForgeUpdater.SETTINGS)) throw new IOException("Unable to update: no 'settings.gradle.kts' in '" + Path.of("").toAbsolutePath() + "'.");
String settings = Files.readString(ZomboidForgeUpdater.SETTINGS);
Matcher plugin = ZomboidForgeUpdater.PLUGIN.matcher(settings);
if (!plugin.find()) throw new IOException("Unable to update: 'settings.gradle.kts' has no 'id(\"dev.overlord.zomboidforge\") version \"...\"'.");
String current = plugin.group(1);
String target = ZomboidForgeUpdater.target();
// Every download finishes before the first file changes.
Map<String, byte[]> wrapper = new LinkedHashMap<>();
for (String path : ZomboidForgeUpdater.WRAPPER) wrapper.put(path, ZomboidForgeUpdater.download(ZomboidForgeUpdater.FILES + "/v" + target + "/" + path));
if (!current.equals(target)) Files.writeString(ZomboidForgeUpdater.SETTINGS, settings.substring(0, plugin.start(1)) + target + settings.substring(plugin.end(1)));
for (Map.Entry<String, byte[]> file : wrapper.entrySet())
{
Path path = Path.of(file.getKey());
if (path.getParent() != null) Files.createDirectories(path.getParent());
Files.write(path, file.getValue());
}
//noinspection ResultOfMethodCallIgnored
Path.of("gradlew").toFile().setExecutable(true);
System.out.println("[zomboidforge] ZomboidForge " + target + (current.equals(target) ? "" : " (was " + current + ")") + ".");
System.out.println("[zomboidforge] Documentation: " + ZomboidForgeUpdater.WEBSITE + "/src/tag/v" + target + "/MOD_DEVELOPMENT.md");
if (!current.equals(target)) System.out.println("[zomboidforge] Changes: " + ZomboidForgeUpdater.WEBSITE + "/compare/v" + current + "...v" + target);
int exit = ZomboidForgeUpdater.gradle("zomboidForgeSetup", "build");
if (exit != 0) throw new IOException("Unable to build the mod: 'gradlew zomboidForgeSetup build' exited with " + exit + ".");
}
/** The version from {@code zomboidforge.version}, else the newest one; either has to be published. */
static String target() throws IOException
{
String metadata = new String(ZomboidForgeUpdater.download(ZomboidForgeUpdater.REPOSITORY + ZomboidForgeUpdater.METADATA), StandardCharsets.UTF_8);
List<String> published = new ArrayList<>();
Matcher versions = ZomboidForgeUpdater.VERSION.matcher(metadata);
Matcher release = ZomboidForgeUpdater.RELEASE.matcher(metadata);
while (versions.find()) published.add(versions.group(1));
if (published.isEmpty()) throw new IOException("Unable to update: no published versions in '" + ZomboidForgeUpdater.REPOSITORY + ZomboidForgeUpdater.METADATA + "'.");
String newest = release.find() ? release.group(1) : published.getLast();
String target = ZomboidForgeUpdater.property("zomboidforge.version", newest);
if (!published.contains(target)) throw new IOException("Unable to update: version '" + target + "' is not published; published: " + published + ".");
return target;
}
static byte[] download(String url) throws IOException
{
try
{
URLConnection connection = URI.create(url).toURL().openConnection();
connection.setConnectTimeout(ZomboidForgeUpdater.TIMEOUT);
connection.setReadTimeout(ZomboidForgeUpdater.TIMEOUT);
try (InputStream in = connection.getInputStream()) { return in.readAllBytes(); }
}
catch (FileNotFoundException e)
{
throw new IOException("Unable to download '" + url + "': not found.", e);
}
catch (IOException e)
{
throw new IOException("Unable to download '" + url + "': " + e, e);
}
}
/** Runs the wrapper in the current directory and copies its output; JShell drops inherited output. */
static int gradle(String... tasks) throws IOException, InterruptedException
{
boolean windows = System.getProperty("os.name").startsWith("Windows");
List<String> command = new ArrayList<>();
command.add(Path.of(windows ? "gradlew.bat" : "gradlew").toAbsolutePath().toString());
command.addAll(List.of(tasks));
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
process.getOutputStream().close();
try (InputStream out = process.getInputStream()) { out.transferTo(System.out); }
return process.waitFor();
}
static String property(String name, String fallback)
{
String value = System.getProperty(name, "").strip();
return value.isEmpty() ? fallback : value.replaceAll("/+$", "");
}
}
int status = ZomboidForgeUpdater.run();
/exit status