Skip to content

Commit

Permalink
Merge pull request #22 from FlintMC/bugfix-and-annotation-processor-e…
Browse files Browse the repository at this point in the history
…xtension

Bugfix and annotation processor extension
  • Loading branch information
juliarn authored Mar 12, 2021
2 parents 598970d + a33b325 commit 65ee24c
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ repositories {
mavenCentral()
}

version = System.getenv().getOrDefault("VERSION", "2.9.0")
version = System.getenv().getOrDefault("VERSION", "2.9.1")

dependencies {
implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.11.1")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/flintmc/gradle/java/JarTaskProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void configureAnnotationProcessor(Project project, SourceSet sourceSet)
annotationProcessorArgs.put("net.flintmc.package.group", project.getGroup().toString());
annotationProcessorArgs.put("net.flintmc.package.name", project.getName());
annotationProcessorArgs.put("net.flintmc.package.version", project.getVersion().toString());
annotationProcessorArgs.put("net.flintmc.sourceSet", sourceSet.getName());

// Collect the arguments from the map above into the form of "-Akey=value"
task.getOptions().getCompilerArgumentProviders().add(() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@
import net.flintmc.gradle.manifest.dev.DevelopmentStaticFiles;
import net.flintmc.gradle.util.MaybeNull;
import net.flintmc.gradle.util.Util;
import net.flintmc.gradle.util.resource.ResourceFinder;
import net.flintmc.gradle.util.resource.ResourceLoader;
import net.flintmc.installer.impl.repository.models.PackageModel;
import net.flintmc.installer.impl.repository.models.install.DownloadFileDataModel;
import net.flintmc.installer.impl.repository.models.install.InstallInstructionModel;
import net.flintmc.installer.impl.repository.models.install.InstallInstructionTypes;
import okhttp3.OkHttpClient;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.*;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.io.*;
import java.net.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Task for downloading and installing static files.
Expand Down Expand Up @@ -89,7 +97,7 @@ public String getMinecraftVersion() {
*/
@Classpath
public Set<File> getClasspath() {
if (classpath == null) {
if(classpath == null) {
classpath = potentialClasspath.getRealClasspath(getProject(), minecraftVersion).getFiles();
}

Expand All @@ -100,47 +108,38 @@ public Set<File> getClasspath() {
* Computes the work this task has to do.
*/
private void compute() {
if (sources != null && classpath != null) {
if(sources != null && classpath != null) {
return;
}

// Convert the entire classpath to URLs
Set<File> classpath = getClasspath();
URL[] classpathAsURLs = new URL[classpath.size()];
// Set up a search for the entire classpath
ResourceLoader loader = new ResourceLoader(getClasspath());
ResourceFinder finder = loader.findAll("manifest.json");

int i = 0;
for (File file : classpath) {
try {
classpathAsURLs[i++] = file.toURI().toURL();
} catch (MalformedURLException e) {
throw new FlintGradleException("Failed to convert file " + file.getAbsolutePath() + " to URL", e);
}
}

// Set up the class loader, it will just be used to retrieve resources
ClassLoader loader = new URLClassLoader(classpathAsURLs);
Set<PackageModel> manifests = new HashSet<>();

try {
for (URL url : new HashSet<>(Collections.list(loader.getResources("manifest.json")))) {
try (InputStream manifestStream = Util.getURLStream(httpClient, url.toURI())) {
// Read the manifest
manifests.add(
JsonConverter.PACKAGE_MODEL_SERIALIZER.fromString(Util.readAll(manifestStream), PackageModel.class));
} catch (IOException e) {
throw new FlintGradleException("Failed to read manifest " + url.toExternalForm(), e);
while(true) {
try(InputStream stream = finder.streamNext()) {
if(stream == null) {
// No further streams found
break;
}

// Deserialize the stream into a package model
manifests.add(JsonConverter.PACKAGE_MODEL_SERIALIZER.fromString(
Util.readAll(stream), PackageModel.class
));
} catch(IOException e) {
throw new FlintGradleException("Failed to load manifests from classpath", e);
}
} catch (IOException | URISyntaxException e) {
throw new FlintGradleException("Failed to load manifests from classpath", e);
}

sources = new HashMap<>();
// Index all manifests
for (PackageModel manifest : manifests) {
for (InstallInstructionModel installInstruction : manifest.getInstallInstructions()) {
for(PackageModel manifest : manifests) {
for(InstallInstructionModel installInstruction : manifest.getInstallInstructions()) {
// Filter for DOWNLOAD_FILE instructions
if (installInstruction.getType().equals(InstallInstructionTypes.DOWNLOAD_FILE.toString())) {
if(installInstruction.getType().equals(InstallInstructionTypes.DOWNLOAD_FILE.toString())) {
// Try to retrieve a development environment override
DownloadFileDataModel data = installInstruction.getData();
File localFile = DevelopmentStaticFiles.getFor(
Expand All @@ -149,7 +148,7 @@ private void compute() {
// Compute where the file should be
File target = new File(workingDir, data.getPath());

if (localFile != null) {
if(localFile != null) {
// There is an override available
sources.put(target, new LocalSource(localFile));
} else {
Expand All @@ -167,7 +166,7 @@ private void compute() {
@TaskAction
public void performInstall() {
compute();
for (Map.Entry<File, StaticFileSource> entry : sources.entrySet()) {
for(Map.Entry<File, StaticFileSource> entry : sources.entrySet()) {
File target = entry.getKey();
StaticFileSource source = entry.getValue();

Expand Down Expand Up @@ -218,12 +217,12 @@ public LocalSource(File source) {

@Override
public void install(File target) throws IOException {
if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
if(!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) {
throw new IOException("Failed to create directory " + target.getParentFile().getAbsolutePath());
}

// Simply copy the file
try (
try(
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target)
) {
Expand Down Expand Up @@ -256,16 +255,16 @@ private RemoteSource(DownloadFileDataModel model) {
@Override
public void install(File target) throws IOException {
String localMD5 = null;
if (target.isFile()) {
if(target.isFile()) {
// File exists, check MD5
localMD5 = Util.md5Hex(Files.readAllBytes(target.toPath()));
if (localMD5.equals(model.getMd5())) {
if(localMD5.equals(model.getMd5())) {
// MD5 matches, skip download
return;
}
}

if (httpClient == null) {
if(httpClient == null) {
String errorMessage = target.isFile() ?
"the md5 checksums " + localMD5 + " of the static file " + model.getPath() + " (" + target.getAbsolutePath()
+ ") does not match the expected value " + model.getMd5() :
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* FlintMC
* Copyright (C) 2020-2021 LabyMedia GmbH and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package net.flintmc.gradle.util.resource;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* Input stream which when closed delegates the closing to its delegate stream and other closeables.
*/
public class MultiResourceInputStream extends InputStream {
private final Set<AutoCloseable> closeables;
private final InputStream delegate;

/**
* Constructs a new {@link MultiResourceInputStream} a delegate and other closeables.
*
* @param delegate The delegate to delegate stream operations to
* @param closeables The other closeables to close when the delegate has been closed
*/
public MultiResourceInputStream(InputStream delegate, AutoCloseable... closeables) {
this.closeables = new LinkedHashSet<>(Arrays.asList(closeables));
this.delegate = delegate;
}

@Override
public void close() throws IOException {
IOException ex = null;

try {
delegate.close();
} catch(IOException e) {
ex = e;
}

// Try to close as many closeables no matter what
for(AutoCloseable closeable : closeables) {
try {
closeable.close();
} catch(Exception e) {
if(e instanceof IOException && ex == null) {
// First exception, and its an I/O one, use it directly
ex = (IOException) e;
} else {
if(ex == null) {
// First exception, but not an I/O exception, wrap it
ex = new IOException("Exception while closing MultiResourceInputStream", e);
} else {
// Earlier an exception occurred already, suppress this one
ex.addSuppressed(e);
}
}
}
}

if(ex != null) {
// At least on exception was thrown
throw ex;
}
}

@Override
public int read() throws IOException {
return delegate.read();
}

@Override
public int read(@NotNull byte[] b) throws IOException {
return delegate.read(b);
}

@Override
public int read(@NotNull byte[] b, int off, int len) throws IOException {
return delegate.read(b, off, len);
}

@Override
public long skip(long n) throws IOException {
return delegate.skip(n);
}

@Override
public int available() throws IOException {
return delegate.available();
}

@Override
public synchronized void mark(int readlimit) {
delegate.mark(readlimit);
}

@Override
public synchronized void reset() throws IOException {
delegate.reset();
}

@Override
public boolean markSupported() {
return delegate.markSupported();
}
}
Loading

0 comments on commit 65ee24c

Please sign in to comment.