-
Notifications
You must be signed in to change notification settings - Fork 775
优化更新版本后重命名原始文件的功能 #4196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Glavo
wants to merge
5
commits into
HMCL-dev:main
Choose a base branch
from
Glavo:rename
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
优化更新版本后重命名原始文件的功能 #4196
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ | |||||
import com.google.gson.JsonParseException; | ||||||
import javafx.application.Platform; | ||||||
|
||||||
import org.intellij.lang.annotations.RegExp; | ||||||
import org.jackhuang.hmcl.EntryPoint; | ||||||
import org.jackhuang.hmcl.Main; | ||||||
import org.jackhuang.hmcl.Metadata; | ||||||
|
@@ -35,6 +36,8 @@ | |||||
import org.jackhuang.hmcl.util.io.JarUtils; | ||||||
import org.jackhuang.hmcl.java.JavaRuntime; | ||||||
import org.jackhuang.hmcl.util.platform.OperatingSystem; | ||||||
import org.jetbrains.annotations.NotNull; | ||||||
import org.jetbrains.annotations.Nullable; | ||||||
|
||||||
import java.io.IOException; | ||||||
import java.nio.file.Files; | ||||||
|
@@ -147,24 +150,23 @@ public static void updateFrom(RemoteVersion version) { | |||||
private static void applyUpdate(Path target) throws IOException { | ||||||
LOG.info("Applying update to " + target); | ||||||
|
||||||
Path self = getCurrentLocation(); | ||||||
if (!IntegrityChecker.DISABLE_SELF_INTEGRITY_CHECK && !IntegrityChecker.isSelfVerified()) { | ||||||
throw new IOException("Self verification failed"); | ||||||
} | ||||||
ExecutableHeaderHelper.copyWithHeader(self, target); | ||||||
|
||||||
Optional<Path> newFilename = tryRename(target, Metadata.VERSION); | ||||||
if (newFilename.isPresent()) { | ||||||
LOG.info("Move " + target + " to " + newFilename.get()); | ||||||
Path self = getCurrentLocation(); | ||||||
Path newTarget = tryRename(target, UpdateChannel.getChannel(), Metadata.VERSION); | ||||||
ExecutableHeaderHelper.copyWithHeader(self, newTarget); | ||||||
if (!newTarget.equals(target)) { | ||||||
LOG.info("Rename " + target + " to " + newTarget); | ||||||
try { | ||||||
Files.move(target, newFilename.get()); | ||||||
target = newFilename.get(); | ||||||
Files.deleteIfExists(target); | ||||||
} catch (IOException e) { | ||||||
LOG.warning("Failed to move target", e); | ||||||
LOG.warning("Failed to delete old file " + target, e); | ||||||
} | ||||||
} | ||||||
|
||||||
startJava(target); | ||||||
startJava(newTarget); | ||||||
} | ||||||
|
||||||
private static void requestUpdate(Path updateTo, Path self) throws IOException { | ||||||
|
@@ -193,16 +195,33 @@ private static void startJava(Path jar, String... appArgs) throws IOException { | |||||
.start(); | ||||||
} | ||||||
|
||||||
private static Optional<Path> tryRename(Path path, String newVersion) { | ||||||
private static @NotNull Path tryRename(@NotNull Path path, @NotNull UpdateChannel newChannel, @NotNull String newVersion) { | ||||||
String filename = path.getFileName().toString(); | ||||||
Matcher matcher = Pattern.compile("^(?<prefix>[hH][mM][cC][lL][.-])(?<version>\\d+(?:\\.\\d+)*)(?<suffix>\\.[^.]+)$").matcher(filename); | ||||||
if (matcher.find()) { | ||||||
String newFilename = matcher.group("prefix") + newVersion + matcher.group("suffix"); | ||||||
if (!newFilename.equals(filename)) { | ||||||
return Optional.of(path.resolveSibling(newFilename)); | ||||||
} | ||||||
String newFileName = tryRename(filename, newChannel, newVersion); | ||||||
return newFileName != null ? path.resolveSibling(newFileName) : path; | ||||||
} | ||||||
|
||||||
static @Nullable String tryRename(@NotNull String fileName, @NotNull UpdateChannel newChannel, @NotNull String newVersion) { | ||||||
@RegExp final var prefixPattern = "[hH][mM][cC][lL]-"; | ||||||
@RegExp final var channelPattern = "(?:dev|stable)-"; | ||||||
@RegExp final var versionPattern = "\\d+(?:\\.\\d+)*(?:\\.(?:SNAPSHOT|[a-zA-Z0-9]+-[0-9a-f]{7}))?"; | ||||||
@RegExp final var suffixPattern = "\\.(?:jar|exe|sh)"; | ||||||
|
||||||
Matcher matcher = Pattern.compile(String.format( | ||||||
"^(?<prefix>%s)(?<channel>%s)?(?<version>%s)(?<suffix>%s)$", | ||||||
prefixPattern, channelPattern, versionPattern, suffixPattern)) | ||||||
.matcher(fileName); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Pattern.compile() call creates a new compiled pattern on every method invocation. Consider extracting this as a static final Pattern constant to avoid recompilation overhead.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
if (!matcher.matches()) { | ||||||
return null; | ||||||
} | ||||||
return Optional.empty(); | ||||||
|
||||||
StringBuilder builder = new StringBuilder(); | ||||||
builder.append(matcher.group("prefix")); | ||||||
if (matcher.group("channel") != null) | ||||||
builder.append(newChannel.channelName).append('-'); | ||||||
builder.append(newVersion); | ||||||
builder.append(matcher.group("suffix")); | ||||||
return builder.toString(); | ||||||
} | ||||||
|
||||||
private static Path getCurrentLocation() throws IOException { | ||||||
|
49 changes: 49 additions & 0 deletions
49
HMCL/src/test/java/org/jackhuang/hmcl/upgrade/UpdateHandlerTest.java
This file contains hidden or 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,49 @@ | ||
/* | ||
* Hello Minecraft! Launcher | ||
* Copyright (C) 2025 huangyuhui <[email protected]> and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.jackhuang.hmcl.upgrade; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
/** | ||
* @author Glavo | ||
*/ | ||
public final class UpdateHandlerTest { | ||
|
||
private static void assertRename(String expected, String fileName, UpdateChannel channel, String version) { | ||
for (var ext : new String[]{".jar", ".exe", ".sh"}) { | ||
assertEquals(expected + ext, UpdateHandler.tryRename(fileName + ext, channel, version)); | ||
} | ||
|
||
assertNull(UpdateHandler.tryRename(fileName, channel, version)); | ||
assertNull(UpdateHandler.tryRename(fileName + ".unknown", channel, version)); | ||
} | ||
|
||
@Test | ||
public void testRename() { | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.15.287", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.15", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.dev-3873459", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.dev-3873459", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.unofficial-3873459", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("HMCL-999.999.999", "HMCL-3.6.SNAPSHOT", UpdateChannel.STABLE, "999.999.999"); | ||
assertRename("hmcl-stable-999.999.999", "hmcl-dev-3.6.15.287", UpdateChannel.STABLE, "999.999.999"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The @regexp annotation is being used on local variables, but these are string literals that could be extracted as constants for better maintainability and reusability.
Copilot uses AI. Check for mistakes.