Skip to content

Commit

Permalink
refactor(bazel): removed BazelCodeLocationBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Billings committed Oct 7, 2019
1 parent 7fb9b4d commit de24e0e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 113 deletions.

This file was deleted.

Expand Up @@ -24,11 +24,18 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.synopsys.integration.bdio.graph.MutableDependencyGraph;
import com.synopsys.integration.bdio.graph.MutableMapDependencyGraph;
import com.synopsys.integration.bdio.model.dependency.Dependency;
import com.synopsys.integration.bdio.model.externalid.ExternalId;
import com.synopsys.integration.bdio.model.externalid.ExternalIdFactory;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
Expand All @@ -41,13 +48,13 @@
public class BazelExtractor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final ExecutableRunner executableRunner;
private final BazelCodeLocationBuilder codeLocationGenerator;
private final ExternalIdFactory externalIdFactory;
private final WorkspaceRuleChooser workspaceRuleChooser;

public BazelExtractor(final ExecutableRunner executableRunner,
final BazelCodeLocationBuilder codeLocationGenerator, final WorkspaceRuleChooser workspaceRuleChooser) {
final ExternalIdFactory externalIdFactory, final WorkspaceRuleChooser workspaceRuleChooser) {
this.executableRunner = executableRunner;
this.codeLocationGenerator = codeLocationGenerator;
this.externalIdFactory = externalIdFactory;
this.workspaceRuleChooser = workspaceRuleChooser;
}

Expand All @@ -68,11 +75,9 @@ public Extraction extract(final File bazelExe, final File workspaceDir, final Ba
pipelineData = pipelineStep.process(pipelineData);
}
// final pipelineData is a list of group:artifact:version strings
for (String artifactString : pipelineData) {
final String[] gavParts = artifactString.split(":");
codeLocationGenerator.addDependency(gavParts[0], gavParts[1], gavParts[2]);
}
final List<CodeLocation> codeLocations = codeLocationGenerator.build();
final MutableDependencyGraph dependencyGraph = gavStringsToDependencyGraph(pipelineData);
final CodeLocation codeLocation = new CodeLocation(dependencyGraph);
final List<CodeLocation> codeLocations = Arrays.asList(codeLocation);
final String projectName = bazelProjectNameGenerator.generateFromBazelTarget(bazelTarget);
final Extraction.Builder builder = new Extraction.Builder()
.success(codeLocations)
Expand All @@ -84,4 +89,24 @@ public Extraction extract(final File bazelExe, final File workspaceDir, final Ba
return new Extraction.Builder().failure(msg).build();
}
}

@NotNull
private MutableDependencyGraph gavStringsToDependencyGraph(final List<String> gavStrings) {
final MutableDependencyGraph dependencyGraph = new MutableMapDependencyGraph();
for (String artifactString : gavStrings) {
final String[] gavParts = artifactString.split(":");
final String group = gavParts[0];
final String artifact = gavParts[1];
final String version = gavParts[2];
try {
logger.debug(String.format("Adding dependency from external id: %s:%s:%s", group, artifact, version));
final ExternalId externalId = externalIdFactory.createMavenExternalId(group, artifact, version);
final Dependency artifactDependency = new Dependency(artifact, version, externalId);
dependencyGraph.addChildToRoot(artifactDependency);
} catch (final Exception e) {
logger.error(String.format("Unable to create dependency from %s:%s:%s", group, artifact, version));
}
}
return dependencyGraph;
}
}

This file was deleted.

Expand Up @@ -21,7 +21,6 @@
import com.synopsys.integration.detectable.detectables.bazel.BazelProjectNameGenerator;
import com.synopsys.integration.detectable.detectables.bazel.BazelWorkspace;
import com.synopsys.integration.detectable.detectables.bazel.WorkspaceRule;
import com.synopsys.integration.detectable.detectables.bazel.BazelCodeLocationBuilder;
import com.synopsys.integration.detectable.detectables.bazel.pipeline.WorkspaceRuleChooser;
import com.synopsys.integration.detectable.detectables.bazel.pipeline.Pipelines;
import com.synopsys.integration.detectable.detectables.bazel.pipeline.stepexecutor.BazelCommandExecutor;
Expand Down Expand Up @@ -57,12 +56,11 @@ public void testMavenJar() throws ExecutableRunnerException, IntegrationExceptio
final File workspaceDir = new File(".");
final ExecutableRunner executableRunner = Mockito.mock(ExecutableRunner.class);
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final BazelCodeLocationBuilder codeLocationBuilder = new BazelCodeLocationBuilder(externalIdFactory);
final BazelWorkspace bazelWorkspace = Mockito.mock(BazelWorkspace.class);
Mockito.when(bazelWorkspace.getDependencyRule()).thenReturn(WorkspaceRule.MAVEN_JAR);
final WorkspaceRuleChooser workspaceRuleChooser = Mockito.mock(WorkspaceRuleChooser.class);
Mockito.when(workspaceRuleChooser.choose(Mockito.eq(WorkspaceRule.MAVEN_JAR), Mockito.isNull())).thenReturn(WorkspaceRule.MAVEN_JAR);
final BazelExtractor bazelExtractor = new BazelExtractor(executableRunner, codeLocationBuilder, workspaceRuleChooser);
final BazelExtractor bazelExtractor = new BazelExtractor(executableRunner, externalIdFactory, workspaceRuleChooser);
final File bazelExe = new File("/usr/bin/bazel");

// bazel query 'filter("@.*:jar", deps(//:ProjectRunner))'
Expand Down Expand Up @@ -126,12 +124,11 @@ public void testMavenInstall() throws ExecutableRunnerException, IntegrationExce
final File workspaceDir = new File(".");
final ExecutableRunner executableRunner = Mockito.mock(ExecutableRunner.class);
final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
final BazelCodeLocationBuilder codeLocationBuilder = new BazelCodeLocationBuilder(externalIdFactory);
final BazelWorkspace bazelWorkspace = Mockito.mock(BazelWorkspace.class);
Mockito.when(bazelWorkspace.getDependencyRule()).thenReturn(WorkspaceRule.MAVEN_INSTALL);
final WorkspaceRuleChooser workspaceRuleChooser = Mockito.mock(WorkspaceRuleChooser.class);
Mockito.when(workspaceRuleChooser.choose(Mockito.eq(WorkspaceRule.MAVEN_INSTALL), Mockito.isNull())).thenReturn(WorkspaceRule.MAVEN_INSTALL);
final BazelExtractor bazelExtractor = new BazelExtractor(executableRunner, codeLocationBuilder, workspaceRuleChooser);
final BazelExtractor bazelExtractor = new BazelExtractor(executableRunner, externalIdFactory, workspaceRuleChooser);
final File bazelExe = new File("/usr/bin/bazel");

// bazel cquery --noimplicit_deps "kind(j.*import, deps(//:ProjectRunner))" --output build
Expand Down
Expand Up @@ -65,7 +65,6 @@
import com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorResolver;
import com.synopsys.integration.detectable.detectables.bazel.BazelDetectable;
import com.synopsys.integration.detectable.detectables.bazel.BazelExtractor;
import com.synopsys.integration.detectable.detectables.bazel.BazelCodeLocationBuilder;
import com.synopsys.integration.detectable.detectables.bazel.pipeline.WorkspaceRuleChooser;
import com.synopsys.integration.detectable.detectables.bitbake.BitbakeDetectable;
import com.synopsys.integration.detectable.detectables.bitbake.BitbakeExtractor;
Expand Down Expand Up @@ -241,9 +240,8 @@ public DetectableFactory detectableFactory() {

@Bean
public BazelExtractor bazelExtractor() {
final BazelCodeLocationBuilder codeLocationGenerator = new BazelCodeLocationBuilder(externalIdFactory);
final WorkspaceRuleChooser workspaceRuleChooser = new WorkspaceRuleChooser();
return new BazelExtractor(executableRunner, codeLocationGenerator, workspaceRuleChooser);
return new BazelExtractor(executableRunner, externalIdFactory, workspaceRuleChooser);
}

public FilePathGenerator filePathGenerator() {
Expand Down

0 comments on commit de24e0e

Please sign in to comment.