Skip to content

Commit

Permalink
Merge pull request #131
Browse files Browse the repository at this point in the history
* Initial jarJar refactor

* Add missing file

* Reimplement missing extension methods

* Expose remaining public method

* More improvements

* Fix path in generated metadata

* Undo needless change to DynamicProjectExtension.java

* Merge branch 'NG_7.0' into jarjar-conf-cache

* Minor fixes and deprecate pin and ranged

* Specialize DependencyManagementObject#dependency to match gradle's De…

* Fix minor issues with jarJar and begin adding docs

* Change system to support library feature variants

* Fix some potential attribute edge cases

* Allow jarJar-ing of non-module artifacts with capabilities

* Use module name detection that prioritizes capabilities
  • Loading branch information
lukebemish authored Apr 16, 2024
1 parent 97ad49c commit 4340d14
Show file tree
Hide file tree
Showing 15 changed files with 722 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,60 @@
import net.neoforged.gradle.dsl.common.dependency.DependencyManagementObject;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.dsl.DependencyFactory;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.jetbrains.annotations.Nullable;

import java.util.regex.Pattern;
import javax.inject.Inject;
import java.util.Objects;

public class AbstractDependencyManagementObject implements DependencyManagementObject {
@SuppressWarnings("UnstableApiUsage")
public abstract class AbstractDependencyManagementObject implements DependencyManagementObject {
@Inject
protected abstract DependencyFactory getDependencyFactory();

protected final Project project;

public AbstractDependencyManagementObject(Project project) {
this.project = project;
protected static ArtifactIdentifier createArtifactIdentifier(final Dependency dependency) {
return new ArtifactIdentifier(dependency.getGroup(), dependency.getName(), dependency.getVersion());
}

protected static ArtifactIdentifier createArtifactIdentifier(final ResolvedDependency dependency) {
return new ArtifactIdentifier(dependency.getModuleGroup(), dependency.getModuleName(), dependency.getModuleVersion());
@Override
public Spec<? super ArtifactIdentifier> dependency(Project project) {
return dependency(getDependencyFactory().create(project));
}

protected static ArtifactIdentifier createArtifactIdentifier(final ModuleDependency dependency) {
return new ArtifactIdentifier(dependency.getGroup(), dependency.getName(), dependency.getVersion());
@Override
public Spec<? super ArtifactIdentifier> dependency(CharSequence dependencyNotation) {
return dependency(getDependencyFactory().create(dependencyNotation));
}

public Spec<? super ArtifactIdentifier> dependency(Object notation) {
return dependency(project.getDependencies().create(notation));
@Override
public Spec<? super ArtifactIdentifier> dependency(@Nullable String group, String name, @Nullable String version) {
return dependency(getDependencyFactory().create(group, name, version));
}

@Override
public Spec<? super ArtifactIdentifier> dependency(Dependency dependency) {
ArtifactIdentifier identifier = createArtifactIdentifier(dependency);

return this.dependency(new Closure<Boolean>(null) {

@SuppressWarnings("ConstantConditions")
@Override
public Boolean call(final Object it) {
if (it instanceof DependencyManagementObject.ArtifactIdentifier) {
final DependencyManagementObject.ArtifactIdentifier identifier = (DependencyManagementObject.ArtifactIdentifier) it;
return (dependency.getGroup() == null || Pattern.matches(dependency.getGroup(), identifier.getGroup())) &&
(dependency.getName() == null || Pattern.matches(dependency.getName(), identifier.getName())) &&
(dependency.getVersion() == null || Pattern.matches(dependency.getVersion(), identifier.getVersion()));
final DependencyManagementObject.ArtifactIdentifier id = (DependencyManagementObject.ArtifactIdentifier) it;
return (identifier.getGroup() == null || id.getGroup() == null || Objects.equals(identifier.getGroup(), id.getGroup())) &&
(identifier.getName() == null || id.getName() == null || Objects.equals(identifier.getName(), id.getName())) &&
(identifier.getVersion() == null || id.getVersion() == null || Objects.equals(identifier.getVersion(), id.getVersion()));
}

return false;
}
});
}

@Override
public Spec<? super ArtifactIdentifier> dependency(Closure<Boolean> spec) {
return Specs.convertClosureToSpec(spec);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package net.neoforged.gradle.common.dependency;

import net.neoforged.gradle.dsl.common.dependency.DependencyFilter;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.specs.Spec;

import java.util.ArrayList;
import java.util.List;

public class DefaultDependencyFilter extends AbstractDependencyManagementObject implements DependencyFilter {
public abstract class DefaultDependencyFilter extends AbstractDependencyManagementObject implements DependencyFilter {
protected final List<Spec<? super ArtifactIdentifier>> includeSpecs = new ArrayList<>();
protected final List<Spec<? super ArtifactIdentifier>> excludeSpecs = new ArrayList<>();

public DefaultDependencyFilter(Project project) {
super(project);
}

@Override
public DependencyFilter exclude(Spec<? super ArtifactIdentifier> spec) {
excludeSpecs.add(spec);
Expand All @@ -29,16 +22,6 @@ public DependencyFilter include(Spec<? super ArtifactIdentifier> spec) {
return this;
}

@Override
public boolean isIncluded(ResolvedDependency dependency) {
return isIncluded(createArtifactIdentifier(dependency));
}

@Override
public boolean isIncluded(ModuleDependency dependency) {
return isIncluded(createArtifactIdentifier(dependency));
}

@Override
public boolean isIncluded(ArtifactIdentifier dependency) {
boolean include = includeSpecs.isEmpty() || includeSpecs.stream().anyMatch(spec -> spec.isSatisfiedBy(dependency));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
import net.neoforged.gradle.dsl.common.dependency.DependencyVersionInformationHandler;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ModuleDependency;
import org.gradle.api.specs.Spec;

import java.util.Map;
import java.util.Optional;

public class DefaultDependencyVersionInformationHandler extends AbstractDependencyManagementObject implements DependencyVersionInformationHandler {
public abstract class DefaultDependencyVersionInformationHandler extends AbstractDependencyManagementObject implements DependencyVersionInformationHandler {

private final Map<Spec<? super ArtifactIdentifier>, String> rangedVersions = Maps.newHashMap();
private final Map<Spec<? super ArtifactIdentifier>, String> pinnedVersions = Maps.newHashMap();

public DefaultDependencyVersionInformationHandler(final Project project) {
super(project);
}

@Override
public void ranged(final Spec<? super ArtifactIdentifier> spec, final String range) {
rangedVersions.put(spec, range);
Expand All @@ -46,17 +40,15 @@ public void pin(final Spec<? super ArtifactIdentifier> spec, final ArtifactVersi
}

@Override
public Optional<String> getVersionRange(final ModuleDependency dependency) {
final ArtifactIdentifier identifier = createArtifactIdentifier(dependency);
public Optional<String> getVersionRange(final ArtifactIdentifier identifier) {
return rangedVersions.entrySet().stream()
.filter(entry -> entry.getKey().isSatisfiedBy(identifier))
.map(Map.Entry::getValue)
.findFirst();
}

@Override
public Optional<String> getVersion(final ModuleDependency dependency) {
final ArtifactIdentifier identifier = createArtifactIdentifier(dependency);
public Optional<String> getVersion(final ArtifactIdentifier identifier) {
return pinnedVersions.entrySet().stream()
.filter(entry -> entry.getKey().isSatisfiedBy(identifier))
.map(Map.Entry::getValue)
Expand Down
Loading

0 comments on commit 4340d14

Please sign in to comment.