2018-11-04 21:28:46 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017, 2018 FabricMC
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
package net.fabricmc.loom.configuration;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2019-04-19 18:14:58 +00:00
|
|
|
import com.google.common.collect.Iterables;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
|
import org.gradle.api.InvalidUserDataException;
|
2018-11-04 21:28:46 +00:00
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.artifacts.Configuration;
|
|
|
|
import org.gradle.api.artifacts.Dependency;
|
2018-12-15 13:14:50 +00:00
|
|
|
import org.gradle.api.artifacts.ResolvedDependency;
|
2019-04-19 18:14:58 +00:00
|
|
|
import org.gradle.api.artifacts.SelfResolvingDependency;
|
2019-11-15 20:16:26 +00:00
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
|
|
|
public abstract class DependencyProvider {
|
|
|
|
private LoomDependencyManager dependencyManager;
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
private final Project project;
|
|
|
|
private final LoomGradleExtension extension;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
public DependencyProvider(Project project) {
|
|
|
|
this.project = project;
|
|
|
|
this.extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void provide(DependencyInfo dependency, Consumer<Runnable> postPopulationScheduler) throws Exception;
|
2018-11-04 21:28:46 +00:00
|
|
|
|
|
|
|
public abstract String getTargetConfig();
|
|
|
|
|
2020-10-10 20:13:12 +00:00
|
|
|
public Dependency addDependency(Object object, String target) {
|
2019-11-02 20:23:27 +00:00
|
|
|
if (object instanceof File) {
|
2018-11-04 21:28:46 +00:00
|
|
|
object = project.files(object);
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-10-10 20:13:12 +00:00
|
|
|
return project.getDependencies().add(target, object);
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public void register(LoomDependencyManager dependencyManager) {
|
2018-11-04 21:28:46 +00:00
|
|
|
this.dependencyManager = dependencyManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LoomDependencyManager getDependencyManager() {
|
|
|
|
return dependencyManager;
|
|
|
|
}
|
|
|
|
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
public Project getProject() {
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LoomGradleExtension getExtension() {
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:22:36 +00:00
|
|
|
public boolean isRefreshDeps() {
|
|
|
|
return getProject().getGradle().getStartParameter().isRefreshDependencies();
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:57:43 +00:00
|
|
|
public static class DependencyInfo {
|
2018-12-22 14:29:46 +00:00
|
|
|
final Project project;
|
2018-11-04 21:28:46 +00:00
|
|
|
final Dependency dependency;
|
|
|
|
final Configuration sourceConfiguration;
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
private String resolvedVersion = null;
|
|
|
|
|
2019-04-19 18:14:58 +00:00
|
|
|
public static DependencyInfo create(Project project, Dependency dependency, Configuration sourceConfiguration) {
|
|
|
|
if (dependency instanceof SelfResolvingDependency) {
|
|
|
|
return new FileDependencyInfo(project, (SelfResolvingDependency) dependency, sourceConfiguration);
|
|
|
|
} else {
|
|
|
|
return new DependencyInfo(project, dependency, sourceConfiguration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DependencyInfo(Project project, Dependency dependency, Configuration sourceConfiguration) {
|
2018-12-22 14:29:46 +00:00
|
|
|
this.project = project;
|
2018-11-04 21:28:46 +00:00
|
|
|
this.dependency = dependency;
|
|
|
|
this.sourceConfiguration = sourceConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Dependency getDependency() {
|
|
|
|
return dependency;
|
|
|
|
}
|
|
|
|
|
2018-12-15 13:14:50 +00:00
|
|
|
public String getResolvedVersion() {
|
2020-12-27 16:25:30 +00:00
|
|
|
if (resolvedVersion != null) {
|
|
|
|
return resolvedVersion;
|
|
|
|
}
|
|
|
|
|
2018-12-15 13:14:50 +00:00
|
|
|
for (ResolvedDependency rd : sourceConfiguration.getResolvedConfiguration().getFirstLevelModuleDependencies()) {
|
|
|
|
if (rd.getModuleGroup().equals(dependency.getGroup()) && rd.getModuleName().equals(dependency.getName())) {
|
2020-12-27 16:25:30 +00:00
|
|
|
resolvedVersion = rd.getModuleVersion();
|
|
|
|
return resolvedVersion;
|
2018-12-15 13:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
resolvedVersion = dependency.getVersion();
|
|
|
|
return resolvedVersion;
|
2018-12-15 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 21:28:46 +00:00
|
|
|
public Configuration getSourceConfiguration() {
|
|
|
|
return sourceConfiguration;
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:31:50 +00:00
|
|
|
public Set<File> resolve() {
|
2020-08-24 21:12:26 +00:00
|
|
|
if (dependency instanceof SelfResolvingDependency) {
|
|
|
|
return ((SelfResolvingDependency) dependency).resolve();
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:31:50 +00:00
|
|
|
return sourceConfiguration.files(dependency);
|
2018-12-22 14:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<File> resolveFile() {
|
2019-04-27 20:31:50 +00:00
|
|
|
Set<File> files = resolve();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
if (files.isEmpty()) {
|
|
|
|
return Optional.empty();
|
|
|
|
} else if (files.size() > 1) {
|
|
|
|
StringBuilder builder = new StringBuilder(this.toString());
|
|
|
|
builder.append(" resolves to more than one file:");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
for (File f : files) {
|
|
|
|
builder.append("\n\t-").append(f.getAbsolutePath());
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-22 14:29:46 +00:00
|
|
|
throw new RuntimeException(builder.toString());
|
|
|
|
} else {
|
|
|
|
return files.stream().findFirst();
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
2018-12-22 14:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return getDepString();
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public String getDepString() {
|
2018-11-04 21:28:46 +00:00
|
|
|
return dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion();
|
|
|
|
}
|
2018-12-23 08:37:54 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public String getResolvedDepString() {
|
2018-12-23 08:37:54 +00:00
|
|
|
return dependency.getGroup() + ":" + dependency.getName() + ":" + getResolvedVersion();
|
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|
2019-04-19 18:14:58 +00:00
|
|
|
|
|
|
|
public static class FileDependencyInfo extends DependencyInfo {
|
|
|
|
protected final Map<String, File> classifierToFile = new HashMap<>();
|
2020-08-26 07:31:42 +00:00
|
|
|
protected final Set<File> resolvedFiles;
|
2019-11-15 20:16:26 +00:00
|
|
|
protected final String group, name, version;
|
2019-04-19 18:14:58 +00:00
|
|
|
|
|
|
|
FileDependencyInfo(Project project, SelfResolvingDependency dependency, Configuration configuration) {
|
|
|
|
super(project, dependency, configuration);
|
|
|
|
|
|
|
|
Set<File> files = dependency.resolve();
|
2020-08-26 07:31:42 +00:00
|
|
|
this.resolvedFiles = files;
|
2019-04-19 18:14:58 +00:00
|
|
|
switch (files.size()) {
|
|
|
|
case 0: //Don't think Gradle would ever let you do this
|
|
|
|
throw new IllegalStateException("Empty dependency?");
|
|
|
|
|
|
|
|
case 1: //Single file dependency
|
|
|
|
classifierToFile.put("", Iterables.getOnlyElement(files));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: //File collection, try work out the classifiers
|
|
|
|
List<File> sortedFiles = files.stream().sorted(Comparator.comparing(File::getName, Comparator.comparingInt(String::length))).collect(Collectors.toList());
|
|
|
|
//First element in sortedFiles is the one with the shortest name, we presume all the others are different classifier types of this
|
|
|
|
File shortest = sortedFiles.remove(0);
|
|
|
|
String shortestName = FilenameUtils.removeExtension(shortest.getName()); //name.jar -> name
|
|
|
|
|
|
|
|
for (File file : sortedFiles) {
|
|
|
|
if (!file.getName().startsWith(shortestName)) {
|
|
|
|
//If there is another file which doesn't start with the same name as the presumed classifier-less one we're out of our depth
|
|
|
|
throw new IllegalArgumentException("Unable to resolve classifiers for " + this + " (failed to sort " + files + ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//We appear to be right, therefore this is the normal dependency file we want
|
|
|
|
classifierToFile.put("", shortest);
|
|
|
|
int start = shortestName.length();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-19 18:14:58 +00:00
|
|
|
for (File file : sortedFiles) {
|
|
|
|
//Now we just have to work out what classifier type the other files are, this shouldn't even return an empty string
|
|
|
|
String classifier = FilenameUtils.removeExtension(file.getName()).substring(start);
|
|
|
|
|
|
|
|
//The classifier could well be separated with a dash (thing name.jar and name-sources.jar), we don't want that leading dash
|
|
|
|
if (classifierToFile.put(classifier.charAt(0) == '-' ? classifier.substring(1) : classifier, file) != null) {
|
|
|
|
throw new InvalidUserDataException("Duplicate classifiers for " + this + " (\"" + file.getName().substring(start) + "\" in " + files + ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 20:16:26 +00:00
|
|
|
if (dependency.getGroup() != null && dependency.getVersion() != null) {
|
|
|
|
group = dependency.getGroup();
|
|
|
|
name = dependency.getName();
|
|
|
|
version = dependency.getVersion();
|
|
|
|
} else {
|
|
|
|
group = "net.fabricmc.synthetic";
|
|
|
|
File root = classifierToFile.get(""); //We've built the classifierToFile map, now to try find a name and version for our dependency
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-11-15 20:16:26 +00:00
|
|
|
if ("jar".equals(FilenameUtils.getExtension(root.getName())) && ZipUtil.containsEntry(root, "fabric.mod.json")) {
|
|
|
|
//It's a Fabric mod, see how much we can extract out
|
|
|
|
JsonObject json = new Gson().fromJson(new String(ZipUtil.unpackEntry(root, "fabric.mod.json"), StandardCharsets.UTF_8), JsonObject.class);
|
2019-04-19 18:14:58 +00:00
|
|
|
|
2019-11-15 20:16:26 +00:00
|
|
|
if (json == null || !json.has("id") || !json.has("version")) {
|
|
|
|
throw new IllegalArgumentException("Invalid Fabric mod jar: " + root + " (malformed json: " + json + ')');
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-11-15 20:16:26 +00:00
|
|
|
if (json.has("name")) { //Go for the name field if it's got one
|
|
|
|
name = json.get("name").getAsString();
|
|
|
|
} else {
|
|
|
|
name = json.get("id").getAsString();
|
|
|
|
}
|
|
|
|
|
|
|
|
version = json.get("version").getAsString();
|
2019-04-19 18:14:58 +00:00
|
|
|
} else {
|
2019-11-15 20:16:26 +00:00
|
|
|
//Not a Fabric mod, just have to make something up
|
|
|
|
name = FilenameUtils.removeExtension(root.getName());
|
|
|
|
version = "1.0";
|
2019-04-19 18:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getResolvedVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDepString() {
|
|
|
|
//Use our custom name and version with the dummy group rather than the null:unspecified:null it would otherwise return
|
|
|
|
return group + ':' + name + ':' + version;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getResolvedDepString() {
|
|
|
|
return getDepString();
|
|
|
|
}
|
2020-08-26 07:31:42 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<File> resolve() {
|
|
|
|
return this.resolvedFiles;
|
|
|
|
}
|
2019-04-19 18:14:58 +00:00
|
|
|
}
|
2018-11-04 21:28:46 +00:00
|
|
|
}
|