Skip to content

Commit 376fd83

Browse files
committed
update
1 parent da3a756 commit 376fd83

File tree

3 files changed

+40
-83
lines changed

3 files changed

+40
-83
lines changed

build.gradle.kts

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,32 @@ plugins {
1414

1515
group = "org.glavo.hmcl"
1616

17-
val buildDir = layout.buildDirectory
18-
val downloadDir = buildDir.dir("downloads")
19-
20-
val channel = project.findProperty("hmcl.channel")?.let { name ->
17+
val hmclChannel = project.findProperty("hmcl.channel")?.let { name ->
2118
UpdateChannel.entries.find { channel ->
2219
channel.name.contentEquals(name.toString(), true)
2320
} ?: throw GradleException("channel '$name' is not defined")
2421
} ?: throw GradleException("hmcl.channel is not defined")
2522

26-
val checkUpdate by tasks.registering(CheckUpdate::class) {
27-
channel.set(channel)
2823

29-
}
24+
CheckUpdate.apply(project, hmclChannel)
3025

31-
val hmclVersion by lazy {
32-
project.ext[CheckUpdate.HMCL_VERSION]?.toString()
33-
?: throw GradleException("${CheckUpdate.HMCL_VERSION} is not defined")
34-
}
26+
val hmclVersion: String = ext.get(CheckUpdate.HMCL_VERSION).toString()
27+
val hmclDownloadBaseUri: String = ext.get(CheckUpdate.HMCL_DOWNLOAD_BASE_URI).toString()
3528

36-
val checkExisting by tasks.registering(CheckExisting::class) {
37-
dependsOn(checkUpdate)
29+
version = hmclVersion
3830

39-
onlyIf {
40-
ext.has(CheckUpdate.HMCL_VERSION) && ext.has(CheckUpdate.HMCL_DOWNLOAD_BASE_URI)
41-
}
42-
43-
checkUpdate.get().doLast {
44-
hmclVersion.set(hmclVersion)
45-
}
31+
val downloadDir = layout.buildDirectory.dir("downloads")
4632

47-
channel.set(channel)
33+
val checkExisting by tasks.registering(CheckExisting::class) {
34+
version.set(hmclVersion)
35+
channel.set(hmclChannel)
4836
}
4937

50-
val needUpdate by lazy { ext.has(CheckExisting.NEED_UPDATE) }
51-
5238
val downloadArtifacts by tasks.registering(Download::class) {
5339
dependsOn(checkExisting)
5440

55-
onlyIf { needUpdate }
56-
57-
checkUpdate.get().doLast {
58-
val hmclDownloadUrlBase = project.ext[CheckUpdate.HMCL_DOWNLOAD_BASE_URI]?.toString()
59-
?: throw GradleException("${CheckUpdate.HMCL_DOWNLOAD_BASE_URI} is not defined")
60-
61-
src("$hmclDownloadUrlBase/HMCL-$hmclVersion.jar")
62-
src("$hmclDownloadUrlBase/HMCL-$hmclVersion.jar.sha256")
63-
}
41+
src("$hmclDownloadBaseUri/HMCL-$hmclVersion.jar")
42+
src("$hmclDownloadBaseUri/HMCL-$hmclVersion.jar.sha256")
6443

6544
overwrite(false)
6645
quiet(false)
@@ -71,8 +50,6 @@ val downloadArtifacts by tasks.registering(Download::class) {
7150
val verifyArtifacts by tasks.registering {
7251
dependsOn(downloadArtifacts)
7352

74-
onlyIf { needUpdate }
75-
7653
doLast {
7754
val dir = downloadDir.get().asFile.toPath()
7855
val expected = Files.readString(dir.resolve("HMCL-$hmclVersion.jar.sha256")).trim()
@@ -88,52 +65,45 @@ val verifyArtifacts by tasks.registering {
8865
}
8966

9067
val javadocJar by tasks.registering(Jar::class) {
91-
dependsOn(checkExisting)
92-
93-
onlyIf { needUpdate }
94-
9568
archiveBaseName.set("HMCL")
9669
archiveClassifier.set("javadoc")
9770

98-
checkUpdate.get().doLast {
99-
archiveVersion.set(hmclVersion)
100-
}
71+
archiveVersion.set(hmclVersion)
10172
}
10273

10374
val sourcesJar by tasks.registering(Jar::class) {
104-
dependsOn(checkExisting)
105-
106-
onlyIf { needUpdate }
107-
10875
archiveBaseName.set("HMCL")
10976
archiveClassifier.set("sources")
11077

111-
checkUpdate.get().doLast {
112-
archiveVersion.set(hmclVersion)
113-
}
78+
archiveVersion.set(hmclVersion)
11479
}
11580

11681
tasks.withType<GenerateModuleMetadata> {
11782
enabled = false
11883
}
11984

85+
tasks.withType<Sign> {
86+
dependsOn(verifyArtifacts, javadocJar, sourcesJar)
87+
}
88+
89+
tasks.withType<GenerateMavenPom> {
90+
dependsOn(verifyArtifacts, javadocJar, sourcesJar)
91+
}
92+
12093
val hmclPublication = publishing.publications.create<MavenPublication>("hmcl") {
12194
groupId = project.group.toString()
122-
artifactId = channel.mavenArtifactId
123-
124-
checkUpdate.get().doLast {
125-
if (ext.has(CheckUpdate.HMCL_VERSION)) {
126-
version = hmclVersion
127-
artifact(downloadDir.map { "HMCL-$hmclVersion.$ext" }) {
128-
this.extension = "jar"
129-
this.classifier = ""
130-
}
95+
artifactId = hmclChannel.mavenArtifactId
13196

132-
artifact(sourcesJar)
133-
artifact(javadocJar)
134-
}
97+
version = hmclVersion
98+
artifact(downloadDir.map { it.file("HMCL-$hmclVersion.jar") }) {
99+
builtBy(verifyArtifacts)
100+
this.extension = "jar"
101+
this.classifier = ""
135102
}
136103

104+
artifact(sourcesJar)
105+
artifact(javadocJar)
106+
137107
pom {
138108
name.set("Hello Minecraft! Launcher ")
139109
description.set("A Minecraft Launcher which is multi-functional, cross-platform and popular")

buildSrc/src/main/java/org/glavo/hmcl/update/CheckExisting.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.gradle.api.logging.Logging;
2323
import org.gradle.api.provider.Property;
2424
import org.gradle.api.tasks.Input;
25+
import org.gradle.api.tasks.StopExecutionException;
2526
import org.gradle.api.tasks.TaskAction;
2627
import org.jetbrains.annotations.NotNull;
2728
import org.w3c.dom.Document;
@@ -36,29 +37,27 @@
3637
import java.util.regex.Pattern;
3738

3839
public abstract class CheckExisting extends DefaultTask {
39-
public static final String NEED_UPDATE = "HMCL_NEED_UPDATE";
40-
4140
private static final Logger LOGGER = Logging.getLogger(CheckExisting.class);
4241
private static final Pattern PATTERN = Pattern.compile("^[0-9]+\\.[0-9]+\\.[0-9]+(\\.[0-9]+)?$");
4342

4443
@Input
4544
public abstract Property<@NotNull UpdateChannel> getChannel();
4645

4746
@Input
48-
public abstract Property<@NotNull String> getHMCLVersion();
47+
public abstract Property<@NotNull String> getVersion();
4948

5049
@TaskAction
5150
public void run() throws Exception {
5251
UpdateChannel channel = getChannel().get();
5352

54-
String version = System.getenv("HMCL_VERSION");
53+
String version = getVersion().get();
5554
if (!PATTERN.matcher(version).matches()) {
5655
throw new IllegalArgumentException("Bad HMCL version: " + version);
5756
}
5857

5958
Document document;
6059
try (InputStream body = Utils.fetch(
61-
URI.create("https://repo1.maven.org/maven2/org/glavo/hmcl/hmcl-%s/maven-metadata.xml".formatted(channel.getMavenArtifactId())),
60+
URI.create("https://repo1.maven.org/maven2/org/glavo/hmcl/%s/maven-metadata.xml".formatted(channel.getMavenArtifactId())),
6261
HttpResponse.BodyHandlers.ofInputStream()
6362
)) {
6463
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
@@ -72,11 +71,9 @@ public void run() throws Exception {
7271
Node item = versionList.item(i);
7372
if (version.equals(item.getFirstChild().getNodeValue())) {
7473
LOGGER.quiet("{} already exists, no update required", version);
75-
return;
74+
throw new StopExecutionException();
7675
}
7776
}
78-
79-
getProject().getExtensions().getExtraProperties().set(NEED_UPDATE, "true");
8077
}
8178

8279
}

buildSrc/src/main/java/org/glavo/hmcl/update/CheckUpdate.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@
2020

2121
import com.google.gson.JsonObject;
2222
import com.google.gson.reflect.TypeToken;
23-
import org.gradle.api.DefaultTask;
2423
import org.gradle.api.GradleException;
24+
import org.gradle.api.Project;
2525
import org.gradle.api.logging.Logger;
2626
import org.gradle.api.logging.Logging;
2727
import org.gradle.api.plugins.ExtraPropertiesExtension;
28-
import org.gradle.api.provider.Property;
29-
import org.gradle.api.tasks.Input;
30-
import org.gradle.api.tasks.TaskAction;
31-
import org.jetbrains.annotations.NotNull;
3228

3329
import java.io.IOException;
3430
import java.net.URI;
@@ -39,7 +35,7 @@
3935
import java.util.regex.Pattern;
4036

4137
/// @author Glavo
42-
public abstract class CheckUpdate extends DefaultTask {
38+
public final class CheckUpdate {
4339
public static final String HMCL_VERSION = "HMCL_VERSION";
4440
public static final String HMCL_DOWNLOAD_BASE_URI = "HMCL_DOWNLOAD_BASE_URI";
4541

@@ -49,9 +45,6 @@ public abstract class CheckUpdate extends DefaultTask {
4945
private static final String WORKFLOW_MULTI_BRANCH_PROJECT = "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject";
5046
private static final String FREE_STYLE_PROJECT = "hudson.model.FreeStyleProject";
5147

52-
@Input
53-
public abstract Property<@NotNull UpdateChannel> getChannel();
54-
5548
private static String concatUri(String base, String... others) {
5649
var builder = new StringBuilder(base);
5750
for (String other : others) {
@@ -63,12 +56,10 @@ private static String concatUri(String base, String... others) {
6356
return builder.toString();
6457
}
6558

66-
@TaskAction
67-
public void check() throws Exception {
68-
UpdateChannel channel = getChannel().get();
59+
public static void apply(Project project, UpdateChannel channel) throws Exception {
60+
LOGGER.quiet("CHANNEL: {}", channel);
6961

7062
URI apiUri = URI.create(concatUri(channel.getUri(), "api", "json"));
71-
LOGGER.quiet("Fetching metadata from {}", apiUri);
7263

7364
BuildMetadata buildMetadata;
7465

@@ -106,8 +97,7 @@ public void check() throws Exception {
10697

10798
LOGGER.quiet("Build metadata found: {}", buildMetadata);
10899

109-
ExtraPropertiesExtension ext = getProject().getExtensions().getExtraProperties();
110-
100+
ExtraPropertiesExtension ext = project.getExtensions().getExtraProperties();
111101
ext.set(HMCL_VERSION, buildMetadata.version);
112102
ext.set(HMCL_DOWNLOAD_BASE_URI, buildMetadata.downloadBaseUri);
113103
}

0 commit comments

Comments
 (0)