Skip to content

Commit

Permalink
The rest of the updater functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenraven committed Feb 4, 2024
1 parent 66f21e9 commit 435baf9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Map;
import java.util.Properties;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A helper for accessing gradle Properties entries configuring the GTNH plugins.
Expand Down Expand Up @@ -765,11 +767,27 @@ public void initFromProperties(Map<?, ?> props, BiConsumer<String, Object> onMis
}
}

private static final Pattern BLOWDRYER_SETTINGS_PATTERN = Pattern
.compile("'GTNewHorizons/ExampleMod1\\.7\\.10'\\s*,\\s*'tag'\\s*,\\s*'([^']+)'\\s*\\)");

/**
* @param settingsPath Path to the settings.gradle file
* @param originalValues The old parsed properties
* @return An updated properties file contents
* @throws Throwable for convenience
*/
public String generateUpdatedProperties(Map<String, String> originalValues) {
public String generateUpdatedProperties(Path settingsPath, Map<String, String> originalValues) throws Throwable {
final String settingsContents = new String(Files.readAllBytes(settingsPath), StandardCharsets.UTF_8);

// Migrate from pre-GTNHGradle buildscript
final Matcher blowdryerMatch = BLOWDRYER_SETTINGS_PATTERN.matcher(settingsContents);
if (blowdryerMatch.find()) {
final String group = blowdryerMatch.group(1);
originalValues.put("gtnh.settings.blowdryerTag", group);
System.out
.println("Found old settings blowdryer tag pointing to " + group + ", migrating to gradle.properties");
}

final StringBuilder sb = new StringBuilder();
final String newline = System.lineSeparator();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.gtnewhorizons.gtnhgradle.tasks;

import com.gtnewhorizons.gtnhgradle.BuildConfig;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** The settings update task, loaded as an isolated class from the future when updating the buildscript. */
public class SettingsUpdater {

private static final Pattern BLOWDRYER_BLOCK_PATTERN = Pattern.compile("blowdryerSetup\\s*\\{[^}]*}");
private static final Pattern PLUGIN_VERSION_PATTERN = Pattern.compile(
"(['\"]com\\.gtnewhorizons\\.gtnhsettingsconvention['\"]\\s*\\)?\\s*version\\s*\\(?['\"])([^'\"]+)(['\"])");

/**
* Runs the update process
*
* @param settingsPath Path to the settings.gradle file
* @throws Throwable for convenience
*/
@SuppressWarnings("unused") // used by reflection
public void update(Path settingsPath) throws Throwable {
final String oldSettings = new String(Files.readAllBytes(settingsPath), StandardCharsets.UTF_8);
String newSettings = oldSettings;

final Matcher blowdryerBlock = BLOWDRYER_BLOCK_PATTERN.matcher(newSettings);
if (blowdryerBlock.find()) {
System.out.println("Removed the old blowdryerSetup block from settings.gradle");
newSettings = blowdryerBlock.replaceAll("");
}

final Matcher versionMatcher = PLUGIN_VERSION_PATTERN.matcher(newSettings);
if (versionMatcher.find()) {
final String preVersion = versionMatcher.group(1);
final String oldVersion = versionMatcher.group(2);
final String postVersion = versionMatcher.group(3);
final String newVersion = BuildConfig.VERSION;
if (oldVersion.equals(newVersion)) {
System.out.println("Settings.gradle plugin already at the newest version of " + newVersion);
} else {
System.out.println("Found plugin version update " + newVersion + " from " + oldVersion + ", applying");
}
newSettings = newSettings.substring(0, versionMatcher.start(2)) + newVersion
+ newSettings.substring(versionMatcher.end(2));
}

// noinspection StringEquality
if (newSettings != oldSettings) {
Files.write(settingsPath, newSettings.getBytes(StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ public void doUpdate() throws Throwable {
}
}

private void updateSettings(URLClassLoader newJarLoader) throws Exception {
//
}

private void updateProperties(URLClassLoader newJarLoader) throws Exception {
final Path settingsPath = getSettingsGradle().getAsFile()
.get()
.toPath();

final Class<?> propsClass = Class
.forName("com.gtnewhorizons.gtnhgradle.PropertiesConfiguration", true, newJarLoader);
final Object propsObject = propsClass.getConstructor()
.newInstance();
final Method generateUpdatedPropertiesMethod = propsClass.getMethod("generateUpdatedProperties", Map.class);
final Method generateUpdatedPropertiesMethod = propsClass
.getMethod("generateUpdatedProperties", Path.class, Map.class);

final Properties p = new Properties();
final Path propertiesPath = getPropertiesGradle().getAsFile()
Expand All @@ -122,7 +123,20 @@ private void updateProperties(URLClassLoader newJarLoader) throws Exception {
.toString());
}

final String newProps = (String) generateUpdatedPropertiesMethod.invoke(propsObject, originalProps);
final String newProps = (String) generateUpdatedPropertiesMethod
.invoke(propsObject, settingsPath, originalProps);
Files.write(propertiesPath, newProps.getBytes(StandardCharsets.UTF_8));
}

private void updateSettings(URLClassLoader newJarLoader) throws Exception {
final Path settingsPath = getSettingsGradle().getAsFile()
.get()
.toPath();
final Class<?> upClass = Class
.forName("com.gtnewhorizons.gtnhgradle.tasks.SettingsUpdater", true, newJarLoader);
final Object upInstance = upClass.getConstructor()
.newInstance();
final Method update = upClass.getMethod("update", Path.class);
update.invoke(upInstance, settingsPath);
}
}

0 comments on commit 435baf9

Please sign in to comment.