From de24e0e0ee02a02801e1c6f7c337a973fef299e4 Mon Sep 17 00:00:00 2001 From: Steve Billings Date: Mon, 7 Oct 2019 18:55:40 -0400 Subject: [PATCH] refactor(bazel): removed BazelCodeLocationBuilder --- .../bazel/BazelCodeLocationBuilder.java | 66 ------------------- .../detectables/bazel/BazelExtractor.java | 41 +++++++++--- .../bazel/BazelCodeLocationBuilderTest.java | 31 --------- .../functional/bazel/BazelExtractorTest.java | 7 +- .../detect/DetectableBeanConfiguration.java | 4 +- 5 files changed, 36 insertions(+), 113 deletions(-) delete mode 100644 detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelCodeLocationBuilder.java delete mode 100644 detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelCodeLocationBuilderTest.java diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelCodeLocationBuilder.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelCodeLocationBuilder.java deleted file mode 100644 index 06a6446f00..0000000000 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelCodeLocationBuilder.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * detectable - * - * Copyright (c) 2019 Synopsys, Inc. - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.synopsys.integration.detectable.detectables.bazel; - -import java.util.ArrayList; -import java.util.List; - -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.detectable.codelocation.CodeLocation; - -public class BazelCodeLocationBuilder { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private final ExternalIdFactory externalIdFactory; - private final MutableDependencyGraph dependencyGraph; - - public BazelCodeLocationBuilder(final ExternalIdFactory externalIdFactory) { - this.externalIdFactory = externalIdFactory; - dependencyGraph = new MutableMapDependencyGraph(); - } - - public BazelCodeLocationBuilder addDependency(final String group, final String artifact, final String version) { - 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 this; - } - - public List build() { - final CodeLocation codeLocation = new CodeLocation(dependencyGraph); - final List codeLocations = new ArrayList<>(1); - codeLocations.add(codeLocation); - return codeLocations; - } -} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelExtractor.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelExtractor.java index 1a45540843..22d7e0829e 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelExtractor.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/bazel/BazelExtractor.java @@ -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; @@ -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; } @@ -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 codeLocations = codeLocationGenerator.build(); + final MutableDependencyGraph dependencyGraph = gavStringsToDependencyGraph(pipelineData); + final CodeLocation codeLocation = new CodeLocation(dependencyGraph); + final List codeLocations = Arrays.asList(codeLocation); final String projectName = bazelProjectNameGenerator.generateFromBazelTarget(bazelTarget); final Extraction.Builder builder = new Extraction.Builder() .success(codeLocations) @@ -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 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; + } } diff --git a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelCodeLocationBuilderTest.java b/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelCodeLocationBuilderTest.java deleted file mode 100644 index 7733513345..0000000000 --- a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelCodeLocationBuilderTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.synopsys.integration.detectable.detectables.bazel.functional.bazel; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.junit.Test; - -import com.synopsys.integration.bdio.model.dependency.Dependency; -import com.synopsys.integration.bdio.model.externalid.ExternalIdFactory; -import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation; -import com.synopsys.integration.detectable.detectables.bazel.BazelCodeLocationBuilder; - -public class BazelCodeLocationBuilderTest { - - @Test - public void test() { - BazelCodeLocationBuilder bdioBuilder = new BazelCodeLocationBuilder(new ExternalIdFactory()); - final List codeLocations = bdioBuilder - .addDependency("testGroup", "testArtifact", "testVersion") - .build(); - - assertEquals(1, codeLocations.size()); - assertEquals(1, codeLocations.get(0).getDependencyGraph().getRootDependencies().size()); - - final Dependency dep = codeLocations.get(0).getDependencyGraph().getRootDependencies().iterator().next(); - assertEquals("testArtifact", dep.name); - assertEquals("testVersion", dep.version); - assertEquals("testGroup", dep.externalId.group); - } -} diff --git a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelExtractorTest.java b/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelExtractorTest.java index 2e09c8b270..e903e7ccd7 100644 --- a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelExtractorTest.java +++ b/detectable/src/test/java/com/synopsys/integration/detectable/detectables/bazel/functional/bazel/BazelExtractorTest.java @@ -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; @@ -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))' @@ -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 diff --git a/src/main/java/com/synopsys/integration/detect/DetectableBeanConfiguration.java b/src/main/java/com/synopsys/integration/detect/DetectableBeanConfiguration.java index a593fcb33c..2362cbf273 100644 --- a/src/main/java/com/synopsys/integration/detect/DetectableBeanConfiguration.java +++ b/src/main/java/com/synopsys/integration/detect/DetectableBeanConfiguration.java @@ -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; @@ -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() {