Skip to content

Commit

Permalink
Merge pull request #28 from FlintMC/improvement/dependency-sha1-hashing
Browse files Browse the repository at this point in the history
Add sha1 hashing to manifests
  • Loading branch information
Janrupf authored Jun 1, 2021
2 parents 7bd833e + d379aa5 commit ba86ec0
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 61 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ dependencies {
implementation(group = "io.github.java-diff-utils", name = "java-diff-utils", version = "4.7")
implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.12.0-rc1")

implementation(group = "net.flintmc.installer", name = "logic-implementation", version = "1.1.11")
implementation(group = "net.flintmc.installer", name = "frontend-gui", version = "1.1.11")
implementation(group = "net.flintmc.installer", name = "logic", version = "1.1.11")
implementation(group = "net.flintmc.installer", name = "logic-implementation", version = "1.1.11")
implementation(group = "net.flintmc.installer", name = "logic", version = "1.1.11")
implementation(group = "net.flintmc.installer", name = "logic-implementation", version = "2.0.0")
implementation(group = "net.flintmc.installer", name = "frontend-gui", version = "2.0.0")
implementation(group = "net.flintmc.installer", name = "logic", version = "2.0.0")
implementation(group = "net.flintmc.installer", name = "logic-implementation", version = "2.0.0")
implementation(group = "net.flintmc.installer", name = "logic", version = "2.0.0")

implementation(group = "com.cloudbees", name = "diff4j", version = "1.2")
}
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/net/flintmc/gradle/FlintGradlePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

package net.flintmc.gradle;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
import javax.annotation.Nonnull;
import net.flintmc.gradle.environment.DeobfuscationEnvironment;
import net.flintmc.gradle.extension.FlintGradleExtension;
import net.flintmc.gradle.extension.FlintPatcherExtension;
Expand Down Expand Up @@ -53,6 +47,13 @@
import org.gradle.api.invocation.Gradle;
import org.gradle.api.tasks.Delete;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;

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

Expand Down Expand Up @@ -221,7 +222,7 @@ public void onExtensionConfigured() {
* Handles the given minecraft version and sets up all of the required steps for using it with gradle.
*
* @param version The minecraft version to handle
* @param type The environment type.
* @param type The environment type.
*/
private void handleVersion(String version, EnvironmentType type) {
DeobfuscationEnvironment environment = minecraftRepository.defaultEnvironment(version, type);
Expand All @@ -248,6 +249,16 @@ private void handleVersion(String version, EnvironmentType type) {
interaction.setupVersioned(compileArtifacts, runtimeArtifacts, version);
}

/**
* Retrieves the singleton instance of the internal flint maven repository.
* Usually at ~/.gradle/caches/flint-gradle/internal-repository.
*
* @return The internal flint maven repository
*/
public SimpleMavenRepository getInternalRepository() {
return internalRepository;
}

/**
* Retrieves the client artifact for the given version.
*
Expand Down Expand Up @@ -308,4 +319,13 @@ public MavenArtifactURLCache getMavenArtifactURLCache() {
public Project getProject() {
return project;
}

/**
* Retrieves the singleton instance of the maven artifact downloader.
*
* @return The singleton instance of the maven artifact downloader or null if gradle is in offline mode
*/
public MavenArtifactDownloader getDownloader() {
return this.downloader;
}
}
67 changes: 47 additions & 20 deletions src/main/java/net/flintmc/gradle/manifest/ManifestConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.credentials.HttpHeaderCredentials;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.Copy;
import org.gradle.authentication.http.HttpHeaderAuthentication;
import org.gradle.jvm.tasks.Jar;

import java.io.File;
import java.net.URI;
Expand All @@ -47,13 +46,15 @@ public class ManifestConfigurator {
private final Project project;
private final OkHttpClient httpClient;
private final MavenArtifactURLCache mavenArtifactURLCache;
private final FlintGradlePlugin flintGradlePlugin;

/**
* Constructs a new {@link ManifestConfigurator} for the given plugin.
*
* @param plugin The plugin to configure the manifest for
*/
public ManifestConfigurator(FlintGradlePlugin plugin) {
this.flintGradlePlugin = plugin;
this.project = plugin.getProject();
this.httpClient = plugin.getHttpClient();
this.mavenArtifactURLCache = plugin.getMavenArtifactURLCache();
Expand All @@ -66,13 +67,13 @@ public ManifestConfigurator(FlintGradlePlugin plugin) {
* Installs the required gradle tasks to generate the flint manifests.
*/
public void configure() {
if(!isValidProject(project)) {
if (!isValidProject(project)) {
return;
}

FlintGradleExtension extension = project.getExtensions().getByType(FlintGradleExtension.class);

if(extension.shouldAutoConfigurePublishing() && extension.shouldEnablePublishing()) {
if (extension.shouldAutoConfigurePublishing() && extension.shouldEnablePublishing()) {
// Auto configuration is enabled
PublishingExtension publishingExtension = project.getExtensions().findByType(PublishingExtension.class);

Expand All @@ -81,7 +82,7 @@ public void configure() {
"Set enablePublishing to false in the flint extension",
"Set shouldAutoConfigurePublishing to false in the flint extension");

if(publishingExtension != null) {
if (publishingExtension != null) {
// Found a publishing extension, automatically set the publish target
MavenPublication publication = publishingExtension.getPublications().create("flint", MavenPublication.class);

Expand Down Expand Up @@ -130,29 +131,54 @@ public void configure() {
generateStaticFileChecksumsTask.setGroup("publishing");
generateStaticFileChecksumsTask.setDescription("Calculates the checksums of all static files and caches them");

File manifestFile = new File(Util.getProjectCacheDir(project), "manifest.json");
File manifestFileJar = new File(Util.getProjectCacheDir(project), "manifestJar.json");

GenerateFlintManifestTask generateFlintManifestTask = project.getTasks().create(
"generateFlintManifest",
GenerateFlintManifestTask generateFlintManifestJarTask = project.getTasks().create(
"generateFlintManifestJar",
GenerateFlintManifestTask.class,
manifestFile,
this.flintGradlePlugin,
GenerateFlintManifestTask.ManifestType.JAR,
manifestFileJar,
staticFileInput,
packageDependencyInput,
resolveArtifactURLsTask.getCacheFile(),
generateStaticFileChecksumsTask.getCacheFile()
generateStaticFileChecksumsTask.getCacheFile(),
repositoryInput
);
generateFlintManifestTask.setGroup("publishing");
generateFlintManifestTask.setDescription("Generates the flint manifest.json and caches it");
generateFlintManifestTask.dependsOn(resolveArtifactURLsTask, generateStaticFileChecksumsTask);
generateFlintManifestJarTask.setGroup("publishing");
generateFlintManifestJarTask.setDescription("Generates the flint manifest.json to include in the jar file and caches it");
generateFlintManifestJarTask.dependsOn(resolveArtifactURLsTask, generateStaticFileChecksumsTask);

File manifestFileDistributor = new File(Util.getProjectCacheDir(project), "manifestDistributor.json");

GenerateFlintManifestTask generateFlintManifestDistributorTask = project.getTasks().create(
"generateFlintManifestDistributor",
GenerateFlintManifestTask.class,
this.flintGradlePlugin,
GenerateFlintManifestTask.ManifestType.DISTRIBUTOR,
manifestFileDistributor,
staticFileInput,
packageDependencyInput,
resolveArtifactURLsTask.getCacheFile(),
generateStaticFileChecksumsTask.getCacheFile(),
repositoryInput
);
Jar jar = (Jar) project.getTasks().getByName("jar");
generateFlintManifestDistributorTask.setGroup("publishing");
generateFlintManifestDistributorTask.setDescription("Generates the flint manifest.json to publish to the distributor and caches it");
generateFlintManifestDistributorTask.dependsOn(resolveArtifactURLsTask, generateStaticFileChecksumsTask, jar);


// Retrieve the process resources task so we can include the manifest
// The processResources task is a copy task, and as the ProcessResources class is marked unstable,
// we cast it to a copy task
Copy processResourcesTask = (Copy) project.getTasks().getByName("processResources");
processResourcesTask.from(manifestFile);
processResourcesTask.dependsOn(generateFlintManifestTask);
processResourcesTask
.from(manifestFileJar)
.rename("manifestJar.json", "manifest.json");
processResourcesTask.dependsOn(generateFlintManifestJarTask);

if(extension.shouldEnablePublishing()) {
if (extension.shouldEnablePublishing()) {
// Generate the URI to publish the manifest to
URI manifestURI = Util.concatURI(
getProjectPublishURI("Set enablePublishing to false in the flint extension"),
Expand All @@ -165,12 +191,13 @@ public void configure() {
PublishFileTask.class,
this,
new MaybeNull<>(httpClient),
manifestFile,
manifestFileDistributor,
manifestURI
);
publishManifestTask.setGroup("publishing");
publishManifestTask.setDescription("Publishes the flint manifest.json to the distributor");
publishManifestTask.dependsOn(generateFlintManifestTask);
publishManifestTask.dependsOn(generateFlintManifestJarTask);
publishManifestTask.dependsOn(generateFlintManifestDistributorTask);

// Create the static files publish task
PublishStaticFilesTask publishStaticFilesTask = project.getTasks().create(
Expand Down Expand Up @@ -214,7 +241,7 @@ private boolean isValidProject(Project project) {
* @return The base URI of the distributor publish endpoint including the project namespace
*/
public URI getProjectPublishURI(String... notAvailableSolutions) {
if(projectPublishURI == null) {
if (projectPublishURI == null) {
projectPublishURI = Util.concatURI(
FlintPluginProperties.DISTRIBUTOR_URL.require(project, notAvailableSolutions),
"api/v1/publish",
Expand All @@ -235,7 +262,7 @@ public URI getProjectPublishURI(String... notAvailableSolutions) {
* @return The base URI of the distributor repository including the project namespace
*/
public URI getProjectMavenURI(String... notAvailableSolution) {
if(projectMavenURI == null) {
if (projectMavenURI == null) {
URI distributorURI = Util.getDistributorMavenURI(project, notAvailableSolution);

projectMavenURI = Util.concatURI(
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.manifest.cache;

import net.flintmc.gradle.maven.pom.MavenArtifact;
import net.flintmc.gradle.util.Util;
import org.gradle.api.Project;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class MavenArtifactChecksums implements Externalizable {
/**
* Loads the cached static file checksums from a file.
*
* @param file The file to load the cached checksums from
* @return The loaded checksums
* @throws IOException If an I/O error occurs while loading the checksums
*/
public static MavenArtifactChecksums load(File file) throws IOException {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
return Util.forceCast(in.readObject());
} catch (ClassNotFoundException e) {
throw new IOException("Failed to read cached checksums of maven artifacts due to ClassNotFoundException", e);
}
}

private Map<MavenArtifact, String> artifactChecksums;

/**
* Constructs a new {@link MavenArtifactChecksums} with all values empty.
*/
public MavenArtifactChecksums() {
this.artifactChecksums = new HashMap<>();
}

/**
* Saves this instance to the given file.
*
* @param file The file to write the cache to
* @throws IOException If an I/O error occurs
*/
public void save(File file) throws IOException {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(this);
}
}

/**
* Adds a checksum to the map of URI checksums.
*
* @param mavenArtifact The maven artifact to add the checksum for
* @param checksum The checksum of the download
*/
public void add(MavenArtifact mavenArtifact, String checksum) {
this.artifactChecksums.put(mavenArtifact, checksum);
}

/**
* Checks if the checksums contain a checksum for the given URI.
*
* @param mavenArtifact The maven artifact to check for
* @return {@code true} if a checksum for the URI has been cached, {@code false} otherwise
*/
public boolean has(MavenArtifact mavenArtifact) {
return artifactChecksums.containsKey(mavenArtifact);
}

/**
* Retrieves the checksum for the given URI.
*
* @param mavenArtifact The maven artifact to retrieve the checksum for
* @return The checksum of the download behind the URI, or {@code null}, if the URI has not been cached
*/
public String get(MavenArtifact mavenArtifact) {
return artifactChecksums.get(mavenArtifact);
}

/**
* Clears all URI checksums.
*/
public void clearArtifacts() {
artifactChecksums.clear();
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.artifactChecksums);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.artifactChecksums = Util.forceCast(in.readObject());
}

/**
* Retrieves a project unique file to cache bound dependencies in.
*
* @param project The project to retrieve the cache file for
* @return The cache file
*/
public static File getCacheFile(Project project) {
return new File(Util.getProjectCacheDir(project), "artifact-checksums.bin");
}
}
Loading

0 comments on commit ba86ec0

Please sign in to comment.