Skip to content

Commit

Permalink
Use a more straightforward argument detection method
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 30, 2024
1 parent 9c51828 commit 55c71f2
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 30 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# File generated by the GradleUtils `setupGitHubActionsWorkflows` task, avoid modifying it directly
# The template can be found at https://github.com/neoforged/GradleUtils/blob/72be0f55ce3f6e91f89cfd7847143b9ef1dcc3e9/src/actionsTemplate/resources/.github/workflows/build-prs.yml

name: Build and test PRs

on:
pull_request:
types:
- synchronize
- opened
- ready_for_review
- reopened
push:
branches:
- 'feature/**'
workflow_dispatch:

jobs:
build:
uses: neoforged/actions/.github/workflows/build-prs.yml@main
with:
java: 17
gradle_tasks: build
jar_compatibility: false
20 changes: 20 additions & 0 deletions .github/workflows/publish-jcc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# File generated by the GradleUtils `setupGitHubActionsWorkflows` task, avoid modifying it directly
# The template can be found at https://github.com/neoforged/GradleUtils/blob/72be0f55ce3f6e91f89cfd7847143b9ef1dcc3e9/src/actionsTemplate/resources/.github/workflows/publish-jcc.yml

name: Publish PR JCC output

on:
workflow_run:
workflows: [Build and test PRs]
types:
- completed

jobs:
publish-jcc:
if: false # Option not enabled when the workflows were generated
uses: neoforged/actions/.github/workflows/publish-jcc.yml@main
with:
beta_version_pattern: .* # Change this line if there's a clear point at which a version no longer accepts breaking changes
secrets:
JCC_GH_APP_ID: ${{ secrets.JCC_GH_APP_ID }}
JCC_GH_APP_KEY: ${{ secrets.JCC_GH_APP_KEY }}
29 changes: 29 additions & 0 deletions .github/workflows/publish-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# File generated by the GradleUtils `setupGitHubActionsWorkflows` task, avoid modifying it directly
# The template can be found at https://github.com/neoforged/GradleUtils/blob/72be0f55ce3f6e91f89cfd7847143b9ef1dcc3e9/src/actionsTemplate/resources/.github/workflows/publish-prs.yml

name: Publish PRs to GitHub Packages

on:
workflow_run:
workflows: [Build and test PRs]
types:
- completed
issue_comment:
types:
- edited
pull_request_target:
types:
- opened

permissions:
packages: write

jobs:
publish-prs:
if: false # Option not enabled when the workflows were generated
uses: neoforged/actions/.github/workflows/publish-prs.yml@main
with:
artifact_base_path: net/neoforged/server/
secrets:
PR_PUBLISHING_GH_APP_ID: ${{ secrets.PR_PUBLISHING_GH_APP_ID }}
PR_PUBLISHING_GH_APP_KEY: ${{ secrets.PR_PUBLISHING_GH_APP_KEY }}
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# File generated by the GradleUtils `setupGitHubActionsWorkflows` task, avoid modifying it directly
# The template can be found at https://github.com/neoforged/GradleUtils/blob/72be0f55ce3f6e91f89cfd7847143b9ef1dcc3e9/src/actionsTemplate/resources/.github/workflows/release.yml

name: Release

on:
push:
branches: [ "main" ]

permissions:
contents: write
statuses: write

jobs:
release:
uses: neoforged/actions/.github/workflows/gradle-publish.yml@main
with:
java: 17
pre_gradle_tasks: build
gradle_tasks: publishMods
secrets:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
MAVEN_USER: ${{ secrets.GITHUB_TOKEN }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
GPG_SUBKEY: ${{ secrets.GPG_SUBKEY }}
GPG_SUBKEY_ID: ${{ secrets.GPG_SUBKEY_ID }}
GPG_SUBKEY_PASSWORD: ${{ secrets.GPG_SUBKEY_PASSWORD }}
26 changes: 25 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
plugins {
id 'java'
id 'maven-publish'
id 'net.neoforged.gradleutils' version '3.0.0'
id 'me.modmuss50.mod-publish-plugin' version '0.5.1'
}

group = 'net.neoforged'
version = gradleutils.version

base {
archivesName = 'server'
}

java {
toolchain {
Expand All @@ -16,6 +24,22 @@ jar {
'Premain-Class' : 'net.neoforged.serverstarterjar.Agent',
'Launcher-Agent-Class': 'net.neoforged.serverstarterjar.Agent',
'Add-Exports' : 'java.base/jdk.internal.loader',
'Add-Opens' : 'java.base/java.lang'
'Add-Opens' : 'java.base/java.lang',
])
}

var lastCommitMessage = new ByteArrayOutputStream()
project.exec {
commandLine('git log -1 --pretty=%B'.split(' '))
standardOutput = lastCommitMessage
}
publishMods {
getChangelog().set(lastCommitMessage.toString().trim())
github {
accessToken = providers.environmentVariable('MAVEN_USER')
repository = 'neoforged/serverstarterjar'
commitish = 'main'
}
}

test.enabled = false
64 changes: 35 additions & 29 deletions src/main/java/net/neoforged/serverstarterjar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
public static final char ESCAPE = (char) 92; // \\
Expand All @@ -39,16 +38,20 @@ public class Main {

public static void main(String[] $) throws Throwable {
final var argsFile = getArgsFile();
final var args = Files.readAllLines(argsFile);
final var copy = new ArrayList<>(args);
final var args = Files.readAllLines(argsFile).stream()
.flatMap(arg -> toArgs(arg).stream()).collect(Collectors.toCollection(ArrayList::new));

final var pathArg = findValue(args, "-p");
if (pathArg == null) {
System.err.println("Could not find module path (specified by -p)");
System.exit(1);
return;
}

final var pathArg = find(args, copy, arg -> arg.startsWith("-p "))
.findFirst().orElseThrow().substring("-p ".length());
final var bootPath = installModulePath(getModulePath(pathArg));

copy.indexOf("-p");
find(args, copy, arg -> arg.startsWith("--add-opens "))
.map(arg -> arg.substring("--add-opens ".length()).split("="))
findValues(args, "--add-opens").stream()
.map(arg -> arg.split("="))
.forEach(toOpen -> Agent.instrumentation.redefineModule(
bootPath.layer().findModule(toOpen[0].split("/")[0]).orElseThrow(),
Set.of(),
Expand All @@ -57,8 +60,8 @@ public static void main(String[] $) throws Throwable {
Set.of(),
Map.of()
));
find(args, copy, arg -> arg.startsWith("--add-exports "))
.map(arg -> arg.substring("--add-exports ".length()).split("="))
findValues(args, "--add-exports").stream()
.map(arg -> arg.split("="))
.forEach(toExport -> Agent.instrumentation.redefineModule(
bootPath.layer().findModule(toExport[0].split("/")[0]).orElseThrow(),
Set.of(),
Expand All @@ -70,38 +73,41 @@ public static void main(String[] $) throws Throwable {

BOOT_LAYER.invokeExact(bootPath.layer());

find(args, copy, arg -> arg.startsWith("-D"))
var sysProps = args.stream().filter(arg -> arg.startsWith("-D")).toList();
sysProps.forEach(args::remove);
sysProps.stream()
.map(arg -> arg.substring("-D".length()).split("=", 2))
.forEach(arg -> System.setProperty(arg[0], arg[1]));

// The args file specifies "--add-modules ALL-MODULE-PATH" which is completely useless now
copy.remove("--add-modules ALL-MODULE-PATH");
// The args file specifies "--add-modules ALL-MODULE-PATH" which is completely useless now, so we ignore it
findValue(args, "--add-modules");

final var mainName = copy.remove(0);
final var mainSplit = mainName.split("\\.");
final var mainSplitByDot = new ArrayList<>(Arrays.asList(mainSplit));
mainSplitByDot.remove(mainSplitByDot.size() - 1);
final var mainPkg = String.join(".", mainSplitByDot);
final var mainName = args.remove(0);

final Method main;
try {
final var mainModule = bootPath.layer().modules()
.stream().filter(m -> m.getPackages().contains(mainPkg))
.findFirst()
.orElseThrow();
final var mainClass = mainModule.getClassLoader().loadClass(mainName);
main = mainClass.getDeclaredMethod("main", String[].class);
main = Class.forName(mainName).getDeclaredMethod("main", String[].class);
} catch (Exception e) {
throw new Exception("Failed to find main class \"" + mainName + "\"", e);
}

main.invoke(null, new Object[] { copy.stream().flatMap(arg -> toArgs(arg).stream()).toArray(String[]::new) });
main.invoke(null, new Object[] { args.toArray(String[]::new) });
}

private static List<String> findValues(List<String> args, String argument) {
var lst = new ArrayList<String>();
String val;
while ((val = findValue(args, argument)) != null) lst.add(val);
return lst;
}

private static Stream<String> find(List<String> args, List<String> copy, Predicate<String> test) {
return args.stream()
.filter(test)
.peek(copy::remove);
private static String findValue(List<String> args, String argument) {
int idx = args.indexOf(argument);
if (idx >= 0) {
args.remove(idx);
return args.remove(idx);
}
return null;
}

private static ModuleLayer.Controller installModulePath(Path[] path) throws Throwable {
Expand Down

0 comments on commit 55c71f2

Please sign in to comment.