-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The rest of the updater functionality
- Loading branch information
1 parent
66f21e9
commit 435baf9
Showing
3 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/gtnewhorizons/gtnhgradle/tasks/SettingsUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters