Skip to content

Commit b82d554

Browse files
Cypher121mezz
authored andcommitted
Implement fg.deobf to allow deobf for all dependency types (MinecraftForge#577)
1 parent d42ca6c commit b82d554

File tree

7 files changed

+569
-288
lines changed

7 files changed

+569
-288
lines changed

src/common/java/net/minecraftforge/gradle/common/util/Artifact.java

+66-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020

2121
package net.minecraftforge.gradle.common.util;
2222

23-
import java.io.File;
24-
import java.util.Locale;
25-
import org.apache.maven.artifact.versioning.ComparableVersion;
26-
2723
import com.amadornes.artifactural.api.artifact.ArtifactIdentifier;
2824
import com.google.common.base.Splitter;
2925
import com.google.common.collect.Iterables;
26+
import org.apache.maven.artifact.versioning.ComparableVersion;
27+
import org.gradle.api.artifacts.Dependency;
28+
import org.gradle.api.artifacts.ResolvedArtifact;
29+
import org.gradle.api.specs.Spec;
30+
31+
import java.io.File;
32+
import java.util.Locale;
33+
import java.util.function.Predicate;
3034

3135
public class Artifact implements ArtifactIdentifier, Comparable<Artifact> {
3236
//Descriptor parts: group:name:version[:classifier][@extension]
@@ -69,11 +73,32 @@ public static Artifact from(String descriptor) {
6973
if (ret.classifier != null) ret.file += '-' + ret.classifier;
7074
ret.file += '.' + ret.ext;
7175

72-
ret.path = ret.group.replace('.', '/') + '/' + ret.name + '/' + ret.version + '/' + ret.file;
76+
ret.path = String.join("/", ret.group.replace('.', '/'), ret.name, ret.version, ret.file);
7377

7478
return ret;
7579
}
7680

81+
public static Artifact from(ArtifactIdentifier identifier) {
82+
if (identifier instanceof Artifact) {
83+
return (Artifact) identifier;
84+
}
85+
86+
StringBuilder builder = new StringBuilder();
87+
builder.append(identifier.getGroup()).append(':').append(identifier.getName()).append(':').append(identifier.getVersion());
88+
if (identifier.getClassifier() != null && !identifier.getClassifier().isEmpty()) {
89+
builder.append(':').append(identifier.getClassifier());
90+
}
91+
92+
builder.append('@');
93+
if (identifier.getExtension() == null || identifier.getExtension().isEmpty()) {
94+
builder.append("jar");
95+
} else {
96+
builder.append(identifier.getExtension());
97+
}
98+
99+
return from(builder.toString());
100+
}
101+
77102
public static Artifact from(String group, String name, String version, String classifier, String ext) {
78103
StringBuilder buf = new StringBuilder();
79104
buf.append(group).append(':').append(name).append(':').append(version);
@@ -84,8 +109,12 @@ public static Artifact from(String group, String name, String version, String cl
84109
return from(buf.toString());
85110
}
86111

87-
public File getLocalPath(File base) {
88-
return new File(base, path.replace('/', File.separatorChar));
112+
public File getLocalFile(File base) {
113+
return new File(base, getLocalPath());
114+
}
115+
116+
public String getLocalPath() {
117+
return path.replace('/', File.separatorChar);
89118
}
90119

91120
public String getDescriptor(){ return descriptor; }
@@ -101,12 +130,42 @@ public File getLocalPath(File base) {
101130
@Override
102131
public String getExtension() { return ext; }
103132
public String getFilename() { return file; }
133+
104134
public boolean isSnapshot() { return isSnapshot; }
135+
136+
public Artifact withVersion(String version) {
137+
return Artifact.from(group, name, version, classifier, ext);
138+
}
139+
105140
@Override
106141
public String toString() {
107142
return getDescriptor();
108143
}
109144

145+
public Spec<Dependency> asDependencySpec() {
146+
return (dep) -> group.equals(dep.getGroup()) && name.equals(dep.getName()) && version.equals(dep.getVersion());
147+
}
148+
149+
public Predicate<ResolvedArtifact> asArtifactMatcher() {
150+
return (art) -> {
151+
String theirClassifier;
152+
if (art.getClassifier() == null) {
153+
theirClassifier = "";
154+
} else {
155+
theirClassifier = art.getClassifier();
156+
}
157+
158+
String theirExt;
159+
if (art.getExtension().isEmpty()) {
160+
theirExt = "jar";
161+
} else {
162+
theirExt = art.getExtension();
163+
}
164+
165+
return (classifier == null || classifier.equals(theirClassifier)) && (ext == null || ext.equals(theirExt));
166+
};
167+
}
168+
110169
@Override
111170
public int compareTo(Artifact o) {
112171
int ret = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* ForgeGradle
3+
* Copyright (C) 2018 Forge Development LLC
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
*/
20+
21+
package net.minecraftforge.gradle.userdev;
22+
23+
import groovy.lang.GroovyObjectSupport;
24+
import net.minecraftforge.gradle.userdev.util.DependencyRemapper;
25+
import org.gradle.api.Project;
26+
import org.gradle.api.artifacts.Dependency;
27+
28+
public class DependencyManagementExtension extends GroovyObjectSupport {
29+
public static final String EXTENSION_NAME = "fg";
30+
private final Project project;
31+
private final DependencyRemapper remapper;
32+
33+
public DependencyManagementExtension(Project project, DependencyRemapper remapper) {
34+
this.project = project;
35+
this.remapper = remapper;
36+
}
37+
38+
@SuppressWarnings("unused")
39+
public Dependency deobf(Object dependency) {
40+
Dependency baseDependency = project.getDependencies().create(dependency);
41+
project.getConfigurations().getByName(UserDevPlugin.OBF).getDependencies().add(baseDependency);
42+
43+
return remapper.remap(baseDependency);
44+
}
45+
}

0 commit comments

Comments
 (0)