20
20
21
21
package net .minecraftforge .gradle .common .util ;
22
22
23
- import java .io .File ;
24
- import java .util .Locale ;
25
- import org .apache .maven .artifact .versioning .ComparableVersion ;
26
-
27
23
import com .amadornes .artifactural .api .artifact .ArtifactIdentifier ;
28
24
import com .google .common .base .Splitter ;
29
25
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 ;
30
34
31
35
public class Artifact implements ArtifactIdentifier , Comparable <Artifact > {
32
36
//Descriptor parts: group:name:version[:classifier][@extension]
@@ -69,11 +73,32 @@ public static Artifact from(String descriptor) {
69
73
if (ret .classifier != null ) ret .file += '-' + ret .classifier ;
70
74
ret .file += '.' + ret .ext ;
71
75
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 ) ;
73
77
74
78
return ret ;
75
79
}
76
80
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
+
77
102
public static Artifact from (String group , String name , String version , String classifier , String ext ) {
78
103
StringBuilder buf = new StringBuilder ();
79
104
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
84
109
return from (buf .toString ());
85
110
}
86
111
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 );
89
118
}
90
119
91
120
public String getDescriptor (){ return descriptor ; }
@@ -101,12 +130,42 @@ public File getLocalPath(File base) {
101
130
@ Override
102
131
public String getExtension () { return ext ; }
103
132
public String getFilename () { return file ; }
133
+
104
134
public boolean isSnapshot () { return isSnapshot ; }
135
+
136
+ public Artifact withVersion (String version ) {
137
+ return Artifact .from (group , name , version , classifier , ext );
138
+ }
139
+
105
140
@ Override
106
141
public String toString () {
107
142
return getDescriptor ();
108
143
}
109
144
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
+
110
169
@ Override
111
170
public int compareTo (Artifact o ) {
112
171
int ret = 0 ;
0 commit comments