Skip to content

Commit 05e7842

Browse files
authored
Merge pull request #17 from FlintMC/usability-patch
Usability patch (2.8.1)
2 parents 19e8c3b + 76590e6 commit 05e7842

8 files changed

+169
-116
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ repositories {
6666
mavenCentral()
6767
}
6868

69-
version = System.getenv().getOrDefault("VERSION", "2.8.0")
69+
version = System.getenv().getOrDefault("VERSION", "2.8.1")
7070

7171
dependencies {
7272
implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.11.1")

src/main/java/net/flintmc/gradle/FlintGradlePlugin.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@
4444
import net.flintmc.gradle.minecraft.data.environment.EnvironmentType;
4545
import net.flintmc.gradle.minecraft.data.environment.MinecraftVersion;
4646
import net.flintmc.gradle.minecraft.yggdrasil.YggdrasilAuthenticator;
47+
import net.flintmc.gradle.util.JavaClosure;
48+
import net.flintmc.gradle.util.Util;
4749
import okhttp3.OkHttpClient;
4850
import org.gradle.api.GradleException;
4951
import org.gradle.api.Plugin;
5052
import org.gradle.api.Project;
5153
import org.gradle.api.invocation.Gradle;
54+
import org.gradle.api.tasks.Delete;
5255

5356
public class FlintGradlePlugin implements Plugin<Project> {
5457
public static final String MINECRAFT_TASK_GROUP = "minecraft";
5558

5659
private static final String MINECRAFT_MAVEN = "https://libraries.minecraft.net";
5760
private static final String MAVEN_CENTRAL = "https://repo.maven.apache.org/maven2/";
58-
private static final String FLINT_MAVEN = "https://dist.labymod.net/api/v1/maven/release/";
5961

6062
private Project project;
6163

@@ -64,7 +66,6 @@ public class FlintGradlePlugin implements Plugin<Project> {
6466
private MavenArtifactURLCache mavenArtifactURLCache;
6567

6668
private FlintGradleExtension extension;
67-
private FlintPatcherExtension patcherExtension;
6869
private JavaPluginInteraction interaction;
6970
private MinecraftRepository minecraftRepository;
7071
private SimpleMavenRepository internalRepository;
@@ -105,7 +106,7 @@ public void apply(@Nonnull Project project) {
105106
}
106107

107108
this.extension = project.getExtensions().create(FlintGradleExtension.NAME, FlintGradleExtension.class, this);
108-
this.patcherExtension = project.getExtensions().create(FlintPatcherExtension.NAME, FlintPatcherExtension.class, this);
109+
project.getExtensions().create(FlintPatcherExtension.NAME, FlintPatcherExtension.class, this);
109110

110111
Path flintGradlePath = gradle.getGradleUserHomeDir().toPath().resolve("caches/flint-gradle");
111112
Path minecraftCache = flintGradlePath.resolve("minecraft-cache");
@@ -139,6 +140,7 @@ public void apply(@Nonnull Project project) {
139140
} catch (IOException e) {
140141
throw new UncheckedIOException("Failed to setup artifact URL cache", e);
141142
}
143+
142144
} else {
143145
this.httpClient = parentPlugin.httpClient;
144146
this.downloader = parentPlugin.downloader;
@@ -154,9 +156,25 @@ public void apply(@Nonnull Project project) {
154156

155157
this.manifestConfigurator = new ManifestConfigurator(this);
156158
interaction.setup();
157-
project.afterEvaluate((p) -> {
158-
extension.ensureConfigured();
159-
FlintResolutionStrategy.getInstance().forceResolutionStrategy(p);
159+
project.getTasks().getByName("clean").configure(
160+
JavaClosure.of((Delete task) -> task.delete(Util.getProjectCacheDir(project))));
161+
162+
project.afterEvaluate((p) -> extension.ensureConfigured());
163+
164+
project.getRepositories().maven(repo -> {
165+
repo.setName("Mojang");
166+
repo.setUrl(MINECRAFT_MAVEN);
167+
});
168+
169+
project.getRepositories().maven(repo -> {
170+
repo.setName("Flint");
171+
repo.setUrl(Util.getDistributorMavenURI(project));
172+
Util.applyDistributorCredentials(project, repo, false);
173+
});
174+
175+
project.getRepositories().maven((repo) -> {
176+
repo.setName("Internal minecraft");
177+
repo.setUrl(minecraftRepository.getBaseDir());
160178
});
161179
}
162180

@@ -179,20 +197,7 @@ public void onExtensionConfigured() {
179197
}
180198
}
181199

182-
project.getRepositories().maven(repo -> {
183-
repo.setName("Mojang");
184-
repo.setUrl(MINECRAFT_MAVEN);
185-
});
186-
187-
project.getRepositories().maven(repo -> {
188-
repo.setName("Flint");
189-
repo.setUrl(FLINT_MAVEN);
190-
});
191-
192-
project.getRepositories().maven((repo) -> {
193-
repo.setName("Internal minecraft");
194-
repo.setUrl(minecraftRepository.getBaseDir());
195-
});
200+
FlintResolutionStrategy.getInstance().forceResolutionStrategy(project, extension);
196201

197202
for (Project subProject : project.getSubprojects()) {
198203
if (!extension.getProjectFilter().test(subProject)) {
@@ -300,10 +305,6 @@ public MavenArtifactURLCache getMavenArtifactURLCache() {
300305
return mavenArtifactURLCache;
301306
}
302307

303-
public FlintPatcherExtension getPatcherExtension() {
304-
return patcherExtension;
305-
}
306-
307308
public Project getProject() {
308309
return project;
309310
}

src/main/java/net/flintmc/gradle/manifest/ManifestConfigurator.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import net.flintmc.gradle.manifest.data.ManifestStaticFileInput;
2828
import net.flintmc.gradle.manifest.tasks.*;
2929
import net.flintmc.gradle.maven.cache.MavenArtifactURLCache;
30-
import net.flintmc.gradle.minecraft.InstallStaticFilesTask;
3130
import net.flintmc.gradle.property.FlintPluginProperties;
3231
import net.flintmc.gradle.util.MaybeNull;
3332
import net.flintmc.gradle.util.Util;
@@ -61,7 +60,6 @@ public ManifestConfigurator(FlintGradlePlugin plugin) {
6160
}
6261

6362
private URI projectPublishURI;
64-
private URI distributorMavenURI;
6563
private URI projectMavenURI;
6664

6765
/**
@@ -79,7 +77,7 @@ public void configure() {
7977
PublishingExtension publishingExtension = project.getExtensions().findByType(PublishingExtension.class);
8078

8179
// Build the distributor URL in form of <host>/maven/<channel>
82-
URI distributorUrl = getDistributorMavenURI(
80+
URI distributorUrl = Util.getDistributorMavenURI(project,
8381
"Set enablePublishing to false in the flint extension",
8482
"Set shouldAutoConfigurePublishing to false in the flint extension");
8583

@@ -100,18 +98,8 @@ public void configure() {
10098
publishingExtension.getRepositories().maven((repo) -> repo.setName("FlintDistributor"));
10199
repository.setUrl(distributorUrl);
102100

103-
// Try to retrieve authentication, it might not be available
104-
HttpHeaderCredentials values = Util.getPublishCredentials(project, false);
105-
106-
if(values != null) {
107-
// Gradle does not allow setting the credentials instance directly, so copy it
108-
HttpHeaderCredentials credentials = repository.getCredentials(HttpHeaderCredentials.class);
109-
credentials.setName(values.getName());
110-
credentials.setValue(values.getValue());
111-
}
112-
113-
// Set the authentication, no further configuration required
114-
repository.getAuthentication().create("header", HttpHeaderAuthentication.class);
101+
// Apply the access credentials if available
102+
Util.applyDistributorCredentials(project, repository, false);
115103
}
116104
}
117105

@@ -240,26 +228,6 @@ public URI getProjectPublishURI(String... notAvailableSolutions) {
240228
return projectPublishURI;
241229
}
242230

243-
/**
244-
* Retrieves the base URI of the distributor repository.
245-
*
246-
* @param notAvailableSolution Messages to display as a solution in case URI can't be computed
247-
* @return The base URI of the distributor repository
248-
*/
249-
public URI getDistributorMavenURI(String... notAvailableSolution) {
250-
if(distributorMavenURI == null) {
251-
distributorMavenURI = Util.concatURI(
252-
FlintPluginProperties.DISTRIBUTOR_URL
253-
.require(project, notAvailableSolution),
254-
"api/v1/maven",
255-
FlintPluginProperties.DISTRIBUTOR_CHANNEL
256-
.require(project, notAvailableSolution)
257-
);
258-
}
259-
260-
return distributorMavenURI;
261-
}
262-
263231
/**
264232
* Retrieves the base URI of the distributor repository including the project namespace.
265233
*
@@ -268,7 +236,7 @@ public URI getDistributorMavenURI(String... notAvailableSolution) {
268236
*/
269237
public URI getProjectMavenURI(String... notAvailableSolution) {
270238
if(projectMavenURI == null) {
271-
URI distributorURI = getDistributorMavenURI(notAvailableSolution);
239+
URI distributorURI = Util.getDistributorMavenURI(project, notAvailableSolution);
272240

273241
projectMavenURI = Util.concatURI(
274242
distributorURI,

src/main/java/net/flintmc/gradle/manifest/tasks/PublishTaskBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected final void publish(URI uri, RequestBody requestBody) {
6767
.put(requestBody);
6868

6969

70-
HttpHeaderCredentials credentials = Util.getPublishCredentials(
70+
HttpHeaderCredentials credentials = Util.getDistributorCredentials(
7171
getProject(),
7272
true,
7373
"Set enablePublishing to false in the flint extension");

src/main/java/net/flintmc/gradle/maven/FlintResolutionStrategy.java

+54-28
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
package net.flintmc.gradle.maven;
2121

22-
import com.google.common.base.Objects;
23-
import java.util.ArrayList;
24-
import java.util.List;
22+
import net.flintmc.gradle.extension.FlintGradleExtension;
2523
import org.gradle.api.Project;
2624
import org.gradle.api.artifacts.ModuleVersionSelector;
2725

26+
import java.util.ArrayList;
27+
import java.util.List;
28+
import java.util.Objects;
29+
2830
public class FlintResolutionStrategy {
2931

3032
private static final FlintResolutionStrategy instance = new FlintResolutionStrategy();
@@ -54,8 +56,8 @@ public static FlintResolutionStrategy getInstance() {
5456
}
5557

5658
/**
57-
* Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
58-
* forced modules to be considered when resolving dependencies. It accepts following notations:
59+
* Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
60+
* be considered when resolving dependencies. It accepts following notations:
5961
*
6062
* <ul>
6163
* <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -67,22 +69,22 @@ public static FlintResolutionStrategy getInstance() {
6769
public void forceDependency(Object moduleVersionSelectorNotation) {
6870
ForcedDependency forcedDependency = this.createForcedDependency(moduleVersionSelectorNotation);
6971

70-
if (forcedDependency == null) {
72+
if(forcedDependency == null) {
7173
return;
7274
}
7375

74-
for (int index = 0; index < this.forcedDependencies.size(); index++) {
76+
for(int index = 0; index < this.forcedDependencies.size(); index++) {
7577
ForcedDependency dependency = this.createForcedDependency(this.forcedDependencies.get(index));
7678

77-
if (dependency == null) {
79+
if(dependency == null) {
7880
return;
7981
}
8082

81-
if (dependency.equals(forcedDependency)) {
83+
if(dependency.equals(forcedDependency)) {
8284
return;
8385
}
8486

85-
if (dependency.nonVersionEquals(forcedDependency)) {
87+
if(dependency.nonVersionEquals(forcedDependency)) {
8688
this.forcedDependencies.set(index, moduleVersionSelectorNotation);
8789
return;
8890
}
@@ -92,8 +94,8 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
9294
}
9395

9496
/**
95-
* Allows forcing certain versions of dependencies, including transitive dependencies. Appends new
96-
* forced modules to be considered when resolving dependencies. It accepts following notations:
97+
* Allows forcing certain versions of dependencies, including transitive dependencies. Appends new forced modules to
98+
* be considered when resolving dependencies. It accepts following notations:
9799
*
98100
* <ul>
99101
* <li>String in a format of: 'group:name:version', for example: 'org.gradle:gradle-core:1.0'
@@ -104,47 +106,63 @@ public void forceDependency(Object moduleVersionSelectorNotation) {
104106
* @param moduleVersionSelectorNotations Typically group:name:version notations to append.
105107
*/
106108
public void forceDependencies(Object... moduleVersionSelectorNotations) {
107-
for (Object moduleVersionSelectorNotation : moduleVersionSelectorNotations) {
109+
for(Object moduleVersionSelectorNotation : moduleVersionSelectorNotations) {
108110
this.forceDependency(moduleVersionSelectorNotation);
109111
}
110112
}
111113

112114
/**
113115
* Forces a collection of dependencies on all configurations of the given {@code project}.
114116
*
115-
* @param project The project for which the dependencies are to be enforced.
117+
* @param project The project for which the dependencies are to be enforced.
118+
* @param extension The flint extension for the project
116119
*/
117-
public void forceResolutionStrategy(Project project) {
120+
public void forceResolutionStrategy(Project project, FlintGradleExtension extension) {
121+
String flintVersion = extension.getFlintVersion();
122+
118123
project
119124
.getConfigurations()
120125
.forEach(
121126
configuration ->
122127
configuration.resolutionStrategy(
123128
resolutionStrategy -> {
124129
forcedDependencies.forEach(resolutionStrategy::force);
130+
resolutionStrategy.eachDependency((details) -> {
131+
ModuleVersionSelector selector = details.getRequested();
132+
133+
if(selector.getGroup().equals("net.flintmc") && !Objects.equals(selector.getVersion(), flintVersion)) {
134+
135+
// Force the flint version to what is specified in the project
136+
details.useVersion(flintVersion);
137+
details.because(
138+
"Forcing net.flintmc dependency from " + selector.getVersion() + " to " +
139+
flintVersion + " to ensure a consistent framework version " +
140+
"[Automatically done by flint-gradle]");
141+
}
142+
});
125143
}));
126144
}
127145

128146
private ForcedDependency createForcedDependency(Object moduleVersionSelectorNotation) {
129147

130-
if (moduleVersionSelectorNotation instanceof String) {
148+
if(moduleVersionSelectorNotation instanceof String) {
131149
String notation = (String) moduleVersionSelectorNotation;
132150

133-
if (!notation.contains(":")) {
151+
if(!notation.contains(":")) {
134152
return null;
135153
}
136154

137155
String[] split = notation.split(":");
138156

139-
if (split.length == 3) {
157+
if(split.length == 3) {
140158

141159
String group = split[0];
142160
String name = split[1];
143161
String version = split[2];
144162

145163
return new ForcedDependency(group, name, version);
146164
}
147-
} else if (moduleVersionSelectorNotation instanceof ModuleVersionSelector) {
165+
} else if(moduleVersionSelectorNotation instanceof ModuleVersionSelector) {
148166
ModuleVersionSelector moduleVersionSelector =
149167
(ModuleVersionSelector) moduleVersionSelectorNotation;
150168
return new ForcedDependency(
@@ -180,25 +198,33 @@ public String getVersion() {
180198
}
181199

182200
public boolean nonVersionEquals(Object o) {
183-
if (this == o) return true;
184-
if (o == null || getClass() != o.getClass()) return false;
201+
if(this == o) {
202+
return true;
203+
}
204+
if(o == null || getClass() != o.getClass()) {
205+
return false;
206+
}
185207
ForcedDependency that = (ForcedDependency) o;
186-
return Objects.equal(group, that.group) && Objects.equal(name, that.name);
208+
return Objects.equals(group, that.group) && Objects.equals(name, that.name);
187209
}
188210

189211
@Override
190212
public boolean equals(Object o) {
191-
if (this == o) return true;
192-
if (o == null || getClass() != o.getClass()) return false;
213+
if(this == o) {
214+
return true;
215+
}
216+
if(o == null || getClass() != o.getClass()) {
217+
return false;
218+
}
193219
ForcedDependency that = (ForcedDependency) o;
194-
return Objects.equal(group, that.group)
195-
&& Objects.equal(name, that.name)
196-
&& Objects.equal(version, that.version);
220+
return Objects.equals(group, that.group)
221+
&& Objects.equals(name, that.name)
222+
&& Objects.equals(version, that.version);
197223
}
198224

199225
@Override
200226
public int hashCode() {
201-
return Objects.hashCode(group, name, version);
227+
return Objects.hash(group, name, version);
202228
}
203229
}
204230
}

src/main/java/net/flintmc/gradle/patch/context/LocalPatchContextProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void writeFile(PatchSingle patch, List<String> lines) throws IOException
125125
if (patch.getHunks().length == 0) {
126126
patch.getTargetFile().delete();
127127
} else {
128-
byte[] content = Util.deserializeLines(patch.getHunks()[0].lines);
128+
byte[] content = Util.decodeBase64Lines(patch.getHunks()[0].lines);
129129
this.copyStreamsCloseAll(
130130
new FileOutputStream(patch.getTargetFile()), new ByteArrayInputStream(content));
131131
}

0 commit comments

Comments
 (0)