forked from amadornes/Artifactural
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
GradleRepositoryAdapter.java
344 lines (304 loc) · 18 KB
/
GradleRepositoryAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Artifactural
* Copyright (c) 2018-2021.
*
* This library 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 version 2.1
* of the License.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.artifactural.gradle;
import net.minecraftforge.artifactural.api.artifact.Artifact;
import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier;
import net.minecraftforge.artifactural.api.artifact.MissingArtifactException;
import net.minecraftforge.artifactural.api.repository.Repository;
import net.minecraftforge.artifactural.base.artifact.SimpleArtifactIdentifier;
import net.minecraftforge.artifactural.base.cache.LocatedArtifactCache;
import org.gradle.api.artifacts.ComponentMetadataSupplierDetails;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.internal.artifacts.BaseRepositoryFactory;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.ComponentResolvers;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.ConfiguredModuleComponentRepository;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.ModuleComponentRepositoryAccess;
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionParser;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvableArtifact;
import org.gradle.api.internal.artifacts.repositories.AbstractArtifactRepository;
import org.gradle.api.internal.artifacts.repositories.DefaultMavenLocalArtifactRepository;
import org.gradle.api.internal.artifacts.repositories.ResolutionAwareRepository;
import org.gradle.api.internal.artifacts.repositories.descriptor.FlatDirRepositoryDescriptor;
import org.gradle.api.internal.artifacts.repositories.descriptor.RepositoryDescriptor;
import org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceArtifactResolver;
import org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceResolver;
import org.gradle.api.internal.artifacts.repositories.resolver.MavenResolver;
import org.gradle.api.internal.artifacts.repositories.resolver.MetadataFetchingCost;
import org.gradle.api.internal.component.ArtifactType;
import org.gradle.api.model.ObjectFactory;
import org.gradle.internal.action.InstantiatingAction;
import org.gradle.internal.component.external.model.ModuleComponentResolveMetadata;
import org.gradle.internal.component.external.model.ModuleDependencyMetadata;
import org.gradle.internal.component.external.model.MutableModuleComponentResolveMetadata;
import org.gradle.internal.component.model.ComponentArtifactMetadata;
import org.gradle.internal.component.model.ComponentOverrideMetadata;
import org.gradle.internal.component.model.ComponentResolveMetadata;
import org.gradle.internal.component.model.ModuleSources;
import org.gradle.internal.nativeintegration.filesystem.FileSystem;
import org.gradle.internal.nativeintegration.services.FileSystems;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.internal.resolve.result.BuildableArtifactFileResolveResult;
import org.gradle.internal.resolve.result.BuildableArtifactSetResolveResult;
import org.gradle.internal.resolve.result.BuildableModuleComponentMetaDataResolveResult;
import org.gradle.internal.resolve.result.BuildableModuleVersionListingResolveResult;
import org.gradle.internal.resource.ExternalResourceName;
import org.gradle.internal.resource.ExternalResourceRepository;
import org.gradle.internal.resource.LocalBinaryResource;
import org.gradle.internal.resource.local.FileResourceRepository;
import org.gradle.internal.resource.local.LocalFileStandInExternalResource;
import org.gradle.internal.resource.local.LocallyAvailableExternalResource;
import org.gradle.internal.resource.metadata.ExternalResourceMetaData;
import org.gradle.internal.resource.transfer.DefaultCacheAwareExternalResourceAccessor;
import org.gradle.util.GradleVersion;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GradleRepositoryAdapter extends AbstractArtifactRepository implements ResolutionAwareRepository {
private static final Pattern URL_PATTERN = Pattern.compile(
"^(?<group>\\S+(?:/\\S+)*)/(?<name>\\S+)/(?<version>\\S+)/" +
"\\2-\\3(?:-(?<classifier>[^.\\s]+))?\\.(?<extension>\\S+)$");
public static GradleRepositoryAdapter add(RepositoryHandler handler, String name, File local, Repository repository) {
BaseRepositoryFactory factory = ReflectionUtils.get(handler, "repositoryFactory"); // We reflect here and create it manually so it DOESN'T get attached.
DefaultMavenLocalArtifactRepository maven = (DefaultMavenLocalArtifactRepository)factory.createMavenLocalRepository(); // We use maven local because it bypasses the caching and coping to .m2
maven.setUrl(local);
maven.setName(name);
maven.metadataSources(m -> {
m.mavenPom();
m.artifact();
});
final GradleRepositoryAdapter repo;
if (GradleVersion.current().compareTo(GradleVersion.version("7.6")) >= 0) {
// If we are on gradle 7.6+ we want to use the super constructor with 2 arguments (with the VersionParser)
repo = new GradleRepositoryAdapter(repository, maven, getVersionParser(maven));
} else {
// If we are on gradle 4.10 - 7.5 we use the super constructor with only the ObjectFactory parameter
repo = new GradleRepositoryAdapter(repository, maven);
}
repo.setName(name);
handler.add(repo);
return repo;
}
private final Repository repository;
private final DefaultMavenLocalArtifactRepository local;
private final String root;
private final LocatedArtifactCache cache;
// This constructor is modified via bytecode manipulation in 'build.gradle'
// DO NOT change this without modifying 'build.gradle'
// This constructor is used on Gradle 7.5.* and below
@Deprecated // TODO - remove this constructor when we can break ABI compatibility
private GradleRepositoryAdapter(Repository repository, DefaultMavenLocalArtifactRepository local) {
// This is replaced with a call to 'super(getObjectFactory(local))'
super(getObjectFactory(local), null);
this.repository = repository;
this.local = local;
this.root = cleanRoot(local.getUrl());
this.cache = new LocatedArtifactCache(new File(root));
}
// This constructor is used on Gradle 7.6 and above
private GradleRepositoryAdapter(Repository repository, DefaultMavenLocalArtifactRepository local, VersionParser versionParser) {
super(getObjectFactory(local), versionParser);
this.repository = repository;
this.local = local;
this.root = cleanRoot(local.getUrl());
this.cache = new LocatedArtifactCache(new File(root));
}
private static ObjectFactory getObjectFactory(DefaultMavenLocalArtifactRepository maven) {
return ReflectionUtils.get(maven, "objectFactory");
}
private static VersionParser getVersionParser(DefaultMavenLocalArtifactRepository maven) {
return ReflectionUtils.get(maven, "versionParser");
}
@Override
public String getDisplayName() {
return local.getDisplayName();
}
@Override
public ConfiguredModuleComponentRepository createResolver() {
MavenResolver resolver = (MavenResolver)local.createResolver();
GeneratingFileResourceRepository repo = new GeneratingFileResourceRepository();
ReflectionUtils.alter(resolver, "repository", prev -> repo); // ExternalResourceResolver.repository
//ReflectionUtils.alter(resolver, "metadataSources", ); //ExternalResourceResolver.metadataSources We need to fix these from returning 'missing'
// MavenResolver -> MavenMetadataLoader -> FileCacheAwareExternalResourceAccessor -> DefaultCacheAwareExternalResourceAccessor
DefaultCacheAwareExternalResourceAccessor accessor = ReflectionUtils.get(resolver, "mavenMetaDataLoader.cacheAwareExternalResourceAccessor.delegate");
ReflectionUtils.alter(accessor, "delegate", prev -> repo); // DefaultCacheAwareExternalResourceAccessor.delegate
ReflectionUtils.alter(accessor, "fileResourceRepository", prev -> repo); // DefaultCacheAwareExternalResourceAccessor.fileResourceRepository
ExternalResourceArtifactResolver extResolver = ReflectionUtils.invoke(resolver, ExternalResourceResolver.class, "createArtifactResolver"); //Makes the resolver and caches it.
ReflectionUtils.alter(extResolver, "repository", prev -> repo);
//File transport references, Would be better to get a reference to the transport and work from there, but don't see it stored anywhere.
ReflectionUtils.alter(resolver, "cachingResourceAccessor.this$0.repository", prev -> repo);
ReflectionUtils.alter(resolver, "cachingResourceAccessor.delegate.delegate", prev -> repo);
return new ConfiguredModuleComponentRepository() {
private final ModuleComponentRepositoryAccess local = wrap(resolver.getLocalAccess());
private final ModuleComponentRepositoryAccess remote = wrap(resolver.getRemoteAccess());
@Override public String getId() { return resolver.getId(); }
@Override public String getName() { return resolver.getName(); }
@Override public ModuleComponentRepositoryAccess getLocalAccess() { return local; }
@Override public ModuleComponentRepositoryAccess getRemoteAccess() { return remote; }
@Override public Map<ComponentArtifactIdentifier, ResolvableArtifact> getArtifactCache() { return resolver.getArtifactCache(); }
@Override public InstantiatingAction<ComponentMetadataSupplierDetails> getComponentMetadataSupplier() { return resolver.getComponentMetadataSupplier(); }
@Override public boolean isDynamicResolveMode() { return resolver.isDynamicResolveMode(); }
@Override public boolean isLocal() { return resolver.isLocal(); }
@Override
public void setComponentResolvers(ComponentResolvers resolver) { }
@Override
public Instantiator getComponentMetadataInstantiator() {
return resolver.getComponentMetadataInstantiator();
}
private ModuleComponentRepositoryAccess wrap(ModuleComponentRepositoryAccess delegate) {
return new ModuleComponentRepositoryAccess() {
@Override
public void resolveComponentMetaData(ModuleComponentIdentifier moduleComponentIdentifier, ComponentOverrideMetadata requestMetaData, BuildableModuleComponentMetaDataResolveResult result) {
delegate.resolveComponentMetaData(moduleComponentIdentifier, requestMetaData, result);
if (result.getState() == BuildableModuleComponentMetaDataResolveResult.State.Resolved) {
ModuleComponentResolveMetadata meta = result.getMetaData();
if (meta.isMissing()) {
MutableModuleComponentResolveMetadata mutable = meta.asMutable();
mutable.setChanging(true);
mutable.setMissing(false);
result.resolved(mutable.asImmutable());
}
}
}
@Override
public void listModuleVersions(ModuleDependencyMetadata dependency, BuildableModuleVersionListingResolveResult result) {
delegate.listModuleVersions(dependency, result);
}
@Override
public void resolveArtifactsWithType(ComponentResolveMetadata component, ArtifactType artifactType, BuildableArtifactSetResolveResult result) {
delegate.resolveArtifactsWithType(component, artifactType, result);
}
@Override
public void resolveArtifact(ComponentArtifactMetadata componentArtifactMetadata, ModuleSources moduleSources, BuildableArtifactFileResolveResult buildableArtifactFileResolveResult) {
delegate.resolveArtifact(componentArtifactMetadata, moduleSources, buildableArtifactFileResolveResult);
}
@Override
public MetadataFetchingCost estimateMetadataFetchingCost(ModuleComponentIdentifier moduleComponentIdentifier) {
return delegate.estimateMetadataFetchingCost(moduleComponentIdentifier);
}
};
}
};
}
public RepositoryDescriptor getDescriptor() {
return new FlatDirRepositoryDescriptor("ArtifacturalRepository", new ArrayList<>());
}
private static String cleanRoot(URI uri) {
String ret = uri.normalize().getPath().replace('\\', '/');
if (!ret.endsWith("/")) ret += '/';
return ret;
}
private class GeneratingFileResourceRepository implements FileResourceRepository {
private final FileSystem fileSystem = FileSystems.getDefault();
private void debug(String message) {
//System.out.println(message);
}
private void log(String message) {
System.out.println(message);
}
@Override
public ExternalResourceRepository withProgressLogging() {
return this;
}
@Override
public LocalBinaryResource localResource(File file) {
debug("localResource: " + file);
return null;
}
@Override
public LocallyAvailableExternalResource resource(File file) {
debug("resource(File): " + file);
return findArtifact(file.getAbsolutePath().replace('\\', '/'));
}
@Override
public LocallyAvailableExternalResource resource(ExternalResourceName location) {
return resource(location, false);
}
@Override
public LocallyAvailableExternalResource resource(ExternalResourceName location, boolean revalidate) {
debug("resource(ExternalResourceName,boolean): " + location + ", " + revalidate);
return findArtifact(location.getUri().getPath().replace('\\', '/'));
}
@Override
public LocallyAvailableExternalResource resource(File file, URI originUri, ExternalResourceMetaData originMetadata) {
debug("resource(File,URI,ExternalResourceMetaData): " + file + ", " + originUri + ", " + originMetadata);
return findArtifact(file.getAbsolutePath().replace('\\', '/'));
}
private LocallyAvailableExternalResource findArtifact(String path) {
if (path.startsWith(root)) {
String relative = path.substring(root.length());
debug(" Relative: " + relative);
Matcher matcher = URL_PATTERN.matcher(relative);
if (matcher.matches()) {
ArtifactIdentifier identifier = new SimpleArtifactIdentifier(
matcher.group("group").replace('/', '.'),
matcher.group("name"),
matcher.group("version"),
matcher.group("classifier"),
matcher.group("extension"));
Artifact artifact = repository.getArtifact(identifier);
return wrap(artifact, identifier);
} else if (relative.endsWith("maven-metadata.xml")) {
String tmp = relative.substring(0, relative.length() - "maven-metadata.xml".length() - 1);
int idx = tmp.lastIndexOf('/');
if (idx != -1) {
File ret = repository.getMavenMetadata(tmp.substring(0, idx - 1), tmp.substring(idx));
if (ret != null) {
return new LocalFileStandInExternalResource(ret, fileSystem);
}
}
} else if (relative.endsWith("/")) {
debug(" Directory listing not supported");
} else {
log(" Matcher Failed: " + relative);
}
} else {
log("Unknown root: " + path);
}
return new LocalFileStandInExternalResource(new File(path), fileSystem);
}
private LocallyAvailableExternalResource wrap(Artifact artifact, ArtifactIdentifier id) {
if (!artifact.isPresent())
return new LocalFileStandInExternalResource(cache.getPath(artifact), fileSystem);
Artifact.Cached cached = artifact.optionallyCache(cache);
try {
return new LocalFileStandInExternalResource(cached.asFile(), fileSystem);
} catch (MissingArtifactException | IOException e) {
throw new RuntimeException(e);
}
}
}
//TODO: Make this a artifact provider interface with a proper API so we dont have direct reference to GradleRepoAdapter in consumers.
public File getArtifact(ArtifactIdentifier identifier) {
Artifact art = repository.getArtifact(identifier);
if (!art.isPresent())
return null;
Artifact.Cached cached = art.optionallyCache(cache);
try {
return cached.asFile();
} catch (MissingArtifactException | IOException e) {
throw new RuntimeException(e);
}
}
}