Skip to content

Commit

Permalink
handle different project dependency configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur McGibbon committed Dec 5, 2023
1 parent 443b731 commit 30058b7
Show file tree
Hide file tree
Showing 44 changed files with 452 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
*/
public interface GradleProjectDependency extends Serializable {
public String getProjectPath();

public String getSourceSetName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Objects;

import com.microsoft.java.bs.gradle.model.GradleProjectDependency;
import com.microsoft.java.bs.gradle.model.GradleSourceSet;

/**
* Default implementation of {@link GradleProjectDependency}.
Expand All @@ -15,17 +16,26 @@ public class DefaultGradleProjectDependency implements GradleProjectDependency {

private String projectPath;

public DefaultGradleProjectDependency(String projectPath) {
private String sourceSetName;

public DefaultGradleProjectDependency(String projectPath, String sourceSetName) {
this.projectPath = projectPath;
this.sourceSetName = sourceSetName;
}

public DefaultGradleProjectDependency(GradleSourceSet sourceSet) {
this(sourceSet.getProjectPath(), sourceSet.getSourceSetName());
}


/**
* Copy constructor.
*
* @param projectDependency the other instance to copy from.
*/
public DefaultGradleProjectDependency(GradleProjectDependency projectDependency) {
this.projectPath = projectDependency.getProjectPath();
this.sourceSetName = projectDependency.getSourceSetName();
}

public String getProjectPath() {
Expand All @@ -36,9 +46,17 @@ public void setProjectPath(String projectPath) {
this.projectPath = projectPath;
}

public String getSourceSetName() {
return sourceSetName;
}

public void setSourceSetName(String sourceSetName) {
this.sourceSetName = sourceSetName;
}

@Override
public int hashCode() {
return Objects.hash(projectPath);
return Objects.hash(projectPath, sourceSetName);
}

@Override
Expand All @@ -53,6 +71,7 @@ public boolean equals(Object obj) {
return false;
}
DefaultGradleProjectDependency other = (DefaultGradleProjectDependency) obj;
return Objects.equals(projectPath, other.projectPath);
return Objects.equals(projectPath, other.projectPath)
&& Objects.equals(sourceSetName, other.sourceSetName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@
import java.util.regex.Pattern;

import org.gradle.api.Project;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.Directory;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.file.copy.DefaultCopySpec;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.plugins.ide.internal.tooling.java.DefaultInstalledJdk;
import org.gradle.tooling.provider.model.ToolingModelBuilder;
import org.gradle.util.GradleVersion;

import com.microsoft.java.bs.gradle.model.GradleProjectDependency;
import com.microsoft.java.bs.gradle.model.GradleSourceSet;
import com.microsoft.java.bs.gradle.model.GradleSourceSets;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleProjectDependency;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleSourceSet;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleSourceSets;
import com.microsoft.java.bs.gradle.plugin.dependency.DependencyCollector;
Expand Down Expand Up @@ -121,13 +127,63 @@ public Object buildAll(String modelName, Project rootProject) {

}

// map all output dirs to their source sets
Map<File, DefaultGradleSourceSet> outputsToSourceSet = new HashMap<>();
for (DefaultGradleSourceSet sourceSet : sourceSetMap.values()) {
if (sourceSet.getSourceOutputDir() != null) {
outputsToSourceSet.put(sourceSet.getSourceOutputDir(), sourceSet);
}
if (sourceSet.getResourceOutputDir() != null) {
outputsToSourceSet.put(sourceSet.getResourceOutputDir(), sourceSet);
}
}
// map all output jars to their source sets
for (Project project : allProject) {

SourceSetContainer sourceSets = getSourceSetContainer(project);
if (sourceSets == null || sourceSets.isEmpty()) {
continue;
}

// get all archive tasks for this project and find the dirs that are included in the archive
TaskCollection<AbstractArchiveTask> archiveTasks =
project.getTasks().withType(AbstractArchiveTask.class);
for (AbstractArchiveTask archiveTask : archiveTasks) {
Set<Object> archiveSourcePaths = getArchiveSourcePaths(archiveTask.getRootSpec());
for (Object sourcePath : archiveSourcePaths) {
sourceSets.forEach(sourceSet -> {
DefaultGradleSourceSet gradleSourceSet = sourceSetMap.get(sourceSet);
if (gradleSourceSet == null) {
return;
}

if (sourceSet.getOutput().equals(sourcePath)) {
outputsToSourceSet.put(archiveTask.getArchivePath(), gradleSourceSet);
}
});
}
}
}

// match any classpath entries to other project's output dirs/jars to create dependencies
for (DefaultGradleSourceSet sourceSet : sourceSetMap.values()) {
Set<GradleProjectDependency> dependencies = new HashSet<>();
for (File file : sourceSet.getCompileClasspath()) {
DefaultGradleSourceSet otherSourceSet = outputsToSourceSet.get(file);
if (otherSourceSet != null) {
dependencies.add(new DefaultGradleProjectDependency(otherSourceSet));
}
}
sourceSet.setProjectDependencies(dependencies);
}

for (Project project : allProject) {
SourceSetContainer sourceSets = getSourceSetContainer(project);
if (sourceSets == null || sourceSets.isEmpty()) {
continue;
}

// dependencies
// module dependencies
sourceSets.forEach(sourceSet -> {
DefaultGradleSourceSet gradleSourceSet = sourceSetMap.get(sourceSet);
if (gradleSourceSet == null) {
Expand All @@ -137,13 +193,24 @@ public Object buildAll(String modelName, Project rootProject) {
exclusionFromDependencies);
collector.collectByConfigurationNames(getClasspathConfigurationNames(sourceSet));
gradleSourceSet.setModuleDependencies(collector.getModuleDependencies());
gradleSourceSet.setProjectDependencies(collector.getProjectDependencies());
});
}

return new DefaultGradleSourceSets(gradleSourceSets);
}

private Set<Object> getArchiveSourcePaths(CopySpec copySpec) {
Set<Object> sourcePaths = new HashSet<>();
if (copySpec instanceof DefaultCopySpec) {
DefaultCopySpec defaultCopySpec = (DefaultCopySpec) copySpec;
sourcePaths.addAll(defaultCopySpec.getSourcePaths());
for (CopySpec child : defaultCopySpec.getChildren()) {
sourcePaths.addAll(getArchiveSourcePaths(child));
}
}
return sourcePaths;
}

private SourceSetContainer getSourceSetContainer(Project project) {
if (!project.getPlugins().hasPlugin("java")) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.artifacts.result.ArtifactResolutionResult;
import org.gradle.api.artifacts.result.ArtifactResult;
import org.gradle.api.artifacts.result.ComponentArtifactsResult;
Expand All @@ -31,10 +29,8 @@

import com.microsoft.java.bs.gradle.model.Artifact;
import com.microsoft.java.bs.gradle.model.GradleModuleDependency;
import com.microsoft.java.bs.gradle.model.GradleProjectDependency;
import com.microsoft.java.bs.gradle.model.impl.DefaultArtifact;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleModuleDependency;
import com.microsoft.java.bs.gradle.model.impl.DefaultGradleProjectDependency;

/**
* Collects dependencies from a {@link SourceSet}.
Expand All @@ -46,7 +42,6 @@ public class DependencyCollector {
private Project project;
private Set<File> exclusionFromDependencies;
private Set<GradleModuleDependency> moduleDependencies;
private Set<GradleProjectDependency> projectDependencies;

/**
* Instantiates a new dependency collector.
Expand All @@ -55,17 +50,12 @@ public DependencyCollector(Project project, Set<File> exclusionFromDependencies)
this.project = project;
this.exclusionFromDependencies = exclusionFromDependencies;
this.moduleDependencies = new LinkedHashSet<>();
this.projectDependencies = new LinkedHashSet<>();
}

public Set<GradleModuleDependency> getModuleDependencies() {
return moduleDependencies;
}

public Set<GradleProjectDependency> getProjectDependencies() {
return projectDependencies;
}

/**
* Resolve and collect dependencies from a {@link SourceSet}.
*/
Expand All @@ -85,8 +75,6 @@ public void collectByConfigurationNames(Set<String> configurationNames) {
resolveFileArtifactDependency((OpaqueComponentArtifactIdentifier) id, artifactResult);
} else if (id instanceof ComponentFileArtifactIdentifier) {
resolveFileArtifactDependency((ComponentFileArtifactIdentifier) id, artifactResult);
} else if (id.getComponentIdentifier() instanceof ProjectComponentIdentifier) {
resolveProjectDependency((ProjectComponentIdentifier) id.getComponentIdentifier());
}
}
}
Expand Down Expand Up @@ -182,14 +170,4 @@ private GradleModuleDependency getFileArtifactDependency(String displayName,
artifacts
);
}

private void resolveProjectDependency(ProjectComponentIdentifier id) {
if (Objects.equals(id.getProjectPath(), project.getPath())) {
return;
}

projectDependencies.add(new DefaultGradleProjectDependency(
id.getProjectPath()
));
}
}
Loading

0 comments on commit 30058b7

Please sign in to comment.