diff options
author | 2017-07-24 21:21:03 +0200 | |
---|---|---|
committer | 2017-07-25 13:15:39 +0200 | |
commit | 7a1b4e311b594f93ae87409496da9c6bf553f2dd (patch) | |
tree | 4bba4d0c52bb885fd92f21830ce4e90e0f8a9a37 /src | |
parent | 9e62187df84ae425a5d7226b6baf1bef576f0a10 (diff) |
Remove generate_workspace from Bazel
It now exists as a separate project at
https://github.com/bazelbuild/migration-tooling.
PiperOrigin-RevId: 162970509
Diffstat (limited to 'src')
18 files changed, 0 insertions, 1889 deletions
@@ -15,7 +15,6 @@ filegroup( }), visibility = [ "//src/test/java:__pkg__", - "//src/tools/generate_workspace:__pkg__", "//src/tools/remote_worker:__subpackages__", ], ) @@ -385,7 +384,6 @@ filegroup( "//src/test/shell:srcs", "//src/tools/android/java/com/google/devtools/build/android:srcs", "//src/tools/benchmark:srcs", - "//src/tools/generate_workspace:srcs", "//src/tools/launcher:srcs", "//src/tools/xcode/actoolwrapper:srcs", "//src/tools/xcode/environment:srcs", diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index fe94b75d0b..360b71790f 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -305,16 +305,6 @@ sh_test( ) sh_test( - name = "generate_workspace_test", - size = "large", - srcs = ["generate_workspace_test.sh"], - data = [ - ":test-deps", - "//src/tools/generate_workspace", - ], -) - -sh_test( name = "workspace_test", size = "large", srcs = ["workspace_test.sh"], diff --git a/src/test/shell/bazel/generate_workspace_test.sh b/src/test/shell/bazel/generate_workspace_test.sh deleted file mode 100755 index f7638fe9b8..0000000000 --- a/src/test/shell/bazel/generate_workspace_test.sh +++ /dev/null @@ -1,306 +0,0 @@ -#!/bin/bash -# -# Copyright 2015 The Bazel Authors. All rights reserved. -# -# Licensed 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. -# -# Test //external mechanisms -# - -# Load the test setup defined in the parent directory -CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "${CURRENT_DIR}/../integration_test_setup.sh" \ - || { echo "integration_test_setup.sh not found!" >&2; exit 1; } -source "${CURRENT_DIR}/remote_helpers.sh" \ - || { echo "remote_helpers.sh not found!" >&2; exit 1; } - -function set_up() { - # Set up custom repository directory. - m2=$TEST_TMPDIR/my-m2 - rm -rf $m2 - mkdir -p $m2 - startup_server $m2 -} - -function tear_down() { - shutdown_server - rm -rf $m2 -} - -function generate_workspace() { - ${bazel_data}/src/tools/generate_workspace/generate_workspace $@ -} - -# Takes: groupId, artifactId, and version, extra arguments are dependencies. -function make_artifact() { - local groupId=$1; - local artifactId=$2; - local version=$3; - - shift; shift; shift; - - local pkg_dir=${m2}/${groupId}/${artifactId}/${version} - local pom_file=${pkg_dir}/${artifactId}-${version}.pom - mkdir -p ${pkg_dir} - # Make the pom.xml. - cat > ${pom_file} <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <groupId>${groupId}</groupId> - <artifactId>${artifactId}</artifactId> - <version>${version}</version> -EOF - - if [[ ${#@} > 0 ]]; then - echo ' <dependencies>' >> ${pom_file} - - for artifact in $@; do - IFS=':' read -r -a dep <<< "$artifact" - cat >> ${pom_file} << EOF - <dependency> - <groupId>${dep[0]}</groupId> - <artifactId>${dep[1]}</artifactId> - <version>${dep[2]}</version> - </dependency> -EOF - done - - echo ' </dependencies>' >> ${pom_file} - fi - - echo "</project>" >> ${pom_file} - - # Make the jar with one class (we use the groupId for the classname). - cat > $TEST_TMPDIR/$groupId.java <<EOF -public class $groupId { - public static void print() { - System.out.println("$artifactId"); - } -} -EOF - local jar_path=$pkg_dir/$artifactId-$version.jar - ${bazel_javabase}/bin/javac $TEST_TMPDIR/$groupId.java - ${bazel_javabase}/bin/jar cf $jar_path $TEST_TMPDIR/$groupId.class - - local sha1=$(shasum $jar_path | awk '{print $1}') - echo -n $sha1 > $jar_path.sha1 - echo $sha1 -} - -function get_workspace_file() { - cat $TEST_log | tail -n 2 | head -n 1 -} - -function get_build_file() { - cat $TEST_log | tail -n 1 -} - -function test_pom() { - # Create a maven repo - local sha1=$(make_artifact blorp glorp 1.2.3) - - # Create a pom that references the artifacts. - cat > $TEST_TMPDIR/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <groupId>my</groupId> - <artifactId>thing</artifactId> - <version>1.0</version> - <repositories> - <repository> - <id>my-repo1</id> - <name>a custom repo</name> - <url>http://localhost:$fileserver_port/</url> - </repository> - </repositories> - - <dependencies> - <dependency> - <groupId>blorp</groupId> - <artifactId>glorp</artifactId> - <version>1.2.3</version> - </dependency> - </dependencies> -</project> -EOF - - generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \ - || fail "generating workspace failed" - - cat $(cat $TEST_log | tail -n 2 | head -n 1) > ws - cat $(cat $TEST_log | tail -n 1) > build - - assert_contains "artifact = \"blorp:glorp:1.2.3\"," ws - assert_contains "repository = \"http://localhost:$fileserver_port/\"," ws - assert_contains "sha1 = \"$sha1\"," ws - assert_contains "\"@blorp_glorp//jar\"," build - assert_contains "name = \"blorp_glorp\"," build -} - -function test_pom_exclusions() { - # Create a maven repo - local sha1_guppy=$(make_artifact fish guppy 2.0) - local sha1_trout=$(make_artifact fish trout 4.2) - - local sha1_glorp=$(make_artifact blorp glorp 1.2.3 fish:guppy:2.0) - local sha1_mlorp=$(make_artifact blorp mlorp 3.2.1 fish:trout:4.2) - - # Create a pom that references the artifacts. - cat > $TEST_TMPDIR/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <groupId>my</groupId> - <artifactId>thing</artifactId> - <version>1.0</version> - <repositories> - <repository> - <id>my-repo1</id> - <name>a custom repo</name> - <url>http://localhost:$fileserver_port/</url> - </repository> - </repositories> - - <dependencies> - <dependency> - <groupId>blorp</groupId> - <artifactId>glorp</artifactId> - <version>1.2.3</version> - </dependency> - <dependency> - <groupId>blorp</groupId> - <artifactId>mlorp</artifactId> - <version>3.2.1</version> - <exclusions> - <exclusion> - <groupId>fish</groupId> - <artifactId>trout</artifactId> - </exclusion> - </exclusions> - </dependency> - </dependencies> -</project> -EOF - - generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \ - || fail "generating workspace failed" - - cat $(cat $TEST_log | tail -n 2 | head -n 1) > ws - cat $(cat $TEST_log | tail -n 1) > build - - assert_contains "artifact = \"blorp:glorp:1.2.3\"," ws - assert_contains "repository = \"http://localhost:$fileserver_port/\"," ws - assert_contains "sha1 = \"$sha1_glorp\"," ws - assert_contains "sha1 = \"$sha1_mlorp\"," ws - assert_contains "sha1 = \"$sha1_guppy\"," ws - assert_not_contains "sha1 = \"$sha1_trout\"," ws - assert_contains "\"@blorp_glorp//jar\"," build - assert_contains "\"@blorp_mlorp//jar\"," build - assert_contains "name = \"blorp_glorp\"," build - assert_contains "name = \"blorp_mlorp\"," build - assert_contains "\"@fish_guppy//jar\"," build - assert_not_contains "\"@fish_trout//jar\"," build -} - -function test_invalid_pom() { - # No pom file. - rm -f $TEST_TMPDIR/pom.xml - generate_workspace -m $TEST_TMPDIR &> $TEST_log - expect_log "Non-readable POM $TEST_TMPDIR/pom.xml" - - # Invalid XML. - cat > $TEST_TMPDIR/pom.xml <<EOF -<project> -EOF - generate_workspace -m $TEST_TMPDIR &> $TEST_log - expect_log "expected end tag </project>" -} - -function test_profile() { - cat > $TEST_TMPDIR/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <groupId>my</groupId> - <artifactId>thing</artifactId> - <version>1.0</version> - <profiles> - <profile> - <id>my-profile</id> - <activation> - <property> - <name>makeThing</name> - <value>thing</value> - </property> - </activation> - </profile> - </profiles> -</project> -EOF - - generate_workspace --maven_project=$TEST_TMPDIR &> $TEST_log \ - || fail "generating workspace failed" -} - -function test_submodules() { - cat > $TEST_TMPDIR/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <groupId>xyz</groupId> - <artifactId>a</artifactId> - <version>1.0</version> - <packaging>pom</packaging> - <modules> - <module>b1</module> - <module>b2</module> - </modules> -</project> -EOF - - # Create submodules, version and group are inherited from parent. - mkdir -p $TEST_TMPDIR/{b1,b2} - cat > $TEST_TMPDIR/b1/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <artifactId>b1</artifactId> - <parent> - <groupId>xyz</groupId> - <artifactId>a</artifactId> - <version>1.0</version> - </parent> - <dependencies> - <dependency> - <groupId>xyz</groupId> - <artifactId>b2</artifactId> - <version>1.0</version> - </dependency> - </dependencies> -</project> -EOF - - cat > $TEST_TMPDIR/b2/pom.xml <<EOF -<project> - <modelVersion>4.0.0</modelVersion> - <artifactId>b2</artifactId> - <parent> - <groupId>xyz</groupId> - <artifactId>a</artifactId> - <version>1.0</version> - </parent> -</project> -EOF - - generate_workspace -m $TEST_TMPDIR/b1 &> $TEST_log || fail "generate failed" - expect_log "xyz_b2 was defined in $TEST_TMPDIR/b2/pom.xml which isn't a repository URL" - assert_contains "artifact = \"xyz:b2:1.0\"," $(get_workspace_file) -} - -run_suite "maven tests" diff --git a/src/tools/generate_workspace/BUILD b/src/tools/generate_workspace/BUILD deleted file mode 100644 index 96bba5c293..0000000000 --- a/src/tools/generate_workspace/BUILD +++ /dev/null @@ -1,17 +0,0 @@ -java_binary( - name = "generate_workspace", - data = ["//src:libunix"], - jvm_flags = ["-Djava.library.path=$(GENDIR)/src/main/native"], - main_class = "com.google.devtools.build.workspace.GenerateWorkspace", - visibility = ["//visibility:public"], - runtime_deps = ["//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace"], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]) + [ - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace:srcs", - "//src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace:srcs", - ], - visibility = ["//src:__pkg__"], -) diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/BUILD b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/BUILD deleted file mode 100644 index ee318dc912..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/BUILD +++ /dev/null @@ -1,27 +0,0 @@ -java_library( - name = "workspace", - srcs = glob(["*.java"]), - visibility = ["//src/tools/generate_workspace:__subpackages__"], - deps = [ - "//src/main/java/com/google/devtools/build/lib:bazel-main", - "//src/main/java/com/google/devtools/build/lib:bazel-rules", - "//src/main/java/com/google/devtools/build/lib:build-base", - "//src/main/java/com/google/devtools/build/lib:events", - "//src/main/java/com/google/devtools/build/lib:os_util", - "//src/main/java/com/google/devtools/build/lib:packages", - "//src/main/java/com/google/devtools/build/lib:runtime", - "//src/main/java/com/google/devtools/build/lib:unix", - "//src/main/java/com/google/devtools/build/lib:vfs", - "//src/main/java/com/google/devtools/common/options", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven:rule", - "//third_party:guava", - "//third_party:maven_model", - ], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]) + ["//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven:srcs"], - visibility = ["//src/tools/generate_workspace:__pkg__"], -) diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspace.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspace.java deleted file mode 100644 index 1700dc1f5b..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspace.java +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace; - -import com.google.common.collect.Lists; -import com.google.common.io.Files; -import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.events.StoredEventHandler; -import com.google.devtools.build.lib.packages.Package; -import com.google.devtools.build.lib.unix.UnixFileSystem; -import com.google.devtools.build.lib.util.OS; -import com.google.devtools.build.lib.vfs.FileSystem; -import com.google.devtools.build.lib.vfs.JavaIoFileSystem; -import com.google.devtools.build.lib.vfs.Path; -import com.google.devtools.build.workspace.maven.DefaultModelResolver; -import com.google.devtools.build.workspace.maven.Resolver; -import com.google.devtools.common.options.OptionsParser; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Paths; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -/** - * Generates a WORKSPACE file for Bazel from other types of dependency trackers. - */ -public class GenerateWorkspace { - - private final EventHandler handler; - private final FileSystem fileSystem; - private final Resolver resolver; - private final List<String> inputs; - private final Path outputDir; - - public static void main(String[] args) throws InterruptedException { - OptionsParser parser = OptionsParser.newOptionsParser(GenerateWorkspaceOptions.class); - parser.parseAndExitUponError(args); - GenerateWorkspaceOptions options = parser.getOptions(GenerateWorkspaceOptions.class); - if (options.mavenProjects.isEmpty() - && options.bazelProjects.isEmpty() - && options.artifacts.isEmpty()) { - printUsage(parser); - return; - } - - GenerateWorkspace workspaceFileGenerator = new GenerateWorkspace(options.outputDir); - workspaceFileGenerator.generateFromWorkspace(options.bazelProjects); - workspaceFileGenerator.generateFromPom(options.mavenProjects); - workspaceFileGenerator.generateFromArtifacts(options.artifacts); - if (!workspaceFileGenerator.hasErrors()) { - workspaceFileGenerator.writeResults(); - } - workspaceFileGenerator.cleanup(); - if (workspaceFileGenerator.hasErrors()) { - System.exit(1); - } - } - - private static void printUsage(OptionsParser parser) { - System.out.println("Usage: generate_workspace (-b PATH|-m PATH|-a coord)+ [-o PATH]\n\n" - + "Generates a WORKSPACE file from the given projects and a BUILD file with a rule that " - + "contains all of the transitive dependencies. At least one bazel_project, " - + "maven_project, or artifact coordinate must be specified. If output_dir is not " - + "specified, the generated files will be written to a temporary directory.\n"); - System.out.println(parser.describeOptions(Collections.<String, String>emptyMap(), - OptionsParser.HelpVerbosity.LONG)); - } - - private GenerateWorkspace(String outputDir) { - this.handler = new EventHandler(); - this.fileSystem = getFileSystem(); - this.resolver = new Resolver(handler, new DefaultModelResolver()); - this.inputs = Lists.newArrayList(); - if (outputDir.isEmpty()) { - this.outputDir = fileSystem.getPath(Files.createTempDir().toString()); - } else { - this.outputDir = fileSystem.getPath(outputDir); - } - } - - static FileSystem getFileSystem() { - return OS.getCurrent() == OS.WINDOWS - ? new JavaIoFileSystem() : new UnixFileSystem(); - } - - private void generateFromWorkspace(List<String> projects) throws InterruptedException { - for (String project : projects) { - WorkspaceResolver workspaceResolver = new WorkspaceResolver(resolver, handler); - Path projectPath = fileSystem.getPath(getAbsolute(project)).getRelative("WORKSPACE"); - Package externalPackage = workspaceResolver.parse(projectPath); - inputs.add(projectPath.getPathString()); - workspaceResolver.resolveTransitiveDependencies(externalPackage); - } - } - - private void generateFromPom(List<String> projects) { - for (String project : projects) { - String pomFile = resolver.resolvePomDependencies(getAbsolute(project)); - if (pomFile != null) { - inputs.add(pomFile); - } - } - } - - private void generateFromArtifacts(List<String> artifacts) { - for (String artifactCoord : artifacts) { - inputs.add(artifactCoord); - resolver.resolveArtifact(artifactCoord); - } - } - - private String getAbsolute(String path) { - return Paths.get(System.getProperty("user.dir")).resolve(path).toString(); - } - - /** - * Returns if there were any errors generating the WORKSPACE and BUILD files. - */ - private boolean hasErrors() { - return handler.hasErrors(); - } - - private void writeResults() { - String date = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); - File workspaceFile = outputDir.getRelative("WORKSPACE").getPathFile(); - File buildFile = outputDir.getRelative("BUILD").getPathFile(); - - // Don't overwrite existing files with generated ones. - if (workspaceFile.exists()) { - workspaceFile = outputDir.getRelative(date + ".WORKSPACE").getPathFile(); - } - if (buildFile.exists()) { - buildFile = outputDir.getRelative(date + ".BUILD").getPathFile(); - } - - try (PrintStream workspaceStream = new PrintStream(workspaceFile); - PrintStream buildStream = new PrintStream(buildFile)) { - ResultWriter writer = new ResultWriter(inputs, resolver.getRules()); - writer.writeWorkspace(workspaceStream); - writer.writeBuild(buildStream); - } catch (IOException e) { - handler.handle(Event.error( - "Could not write WORKSPACE and BUILD files to " + outputDir + ": " + e.getMessage())); - return; - } - System.err.println("Wrote:\n" + workspaceFile + "\n" + buildFile); - } - - private void cleanup() { - for (Event event : handler.getEvents()) { - System.err.println(event); - } - } - - private class EventHandler extends StoredEventHandler { - @Override - public void handle(Event event) { - System.err.println(event.getKind() + ": " + event.getMessage()); - } - } -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspaceOptions.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspaceOptions.java deleted file mode 100644 index 8b01420484..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/GenerateWorkspaceOptions.java +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace; - -import com.google.devtools.common.options.Option; -import com.google.devtools.common.options.OptionDocumentationCategory; -import com.google.devtools.common.options.OptionEffectTag; -import com.google.devtools.common.options.OptionsBase; -import java.util.List; - -/** - * Command-line options for generate_workspace tool. - */ -public class GenerateWorkspaceOptions extends OptionsBase { - @Option( - name = "help", - abbrev = 'h', - help = "Prints usage info.", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "true" - ) - public boolean help; - - @Option( - name = "bazel_project", - abbrev = 'b', - help = "Directory contains a Bazel project.", - allowMultiple = true, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "" - ) - public List<String> bazelProjects; - - @Option( - name = "maven_project", - abbrev = 'm', - help = "Directory containing a Maven project.", - allowMultiple = true, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "" - ) - public List<String> mavenProjects; - - @Option( - name = "artifact", - abbrev = 'a', - help = "Maven artifact coordinates (e.g. groupId:artifactId:version).", - allowMultiple = true, - category = "input", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "" - ) - public List<String> artifacts; - - @Option( - name = "output_dir", - abbrev = 'o', - help = - "Output directory to store the WORKSPACE and BUILD files. If unspecified, a temporary" - + " directory is used.", - category = "output", - documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, - effectTags = {OptionEffectTag.UNKNOWN}, - defaultValue = "" - ) - public String outputDir; -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/ResultWriter.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/ResultWriter.java deleted file mode 100644 index b9323ce753..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/ResultWriter.java +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2016 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace; - -import com.google.devtools.build.workspace.maven.Rule; - -import java.io.PrintStream; -import java.util.Collection; -import java.util.List; - -/** - * Write a set of rules to a WORKSPACE and BUILD file. - */ -public class ResultWriter { - private final List<String> sources; - private final Collection<Rule> rules; - - public ResultWriter(List<String> sources, Collection<Rule> rules) { - this.sources = sources; - this.rules = rules; - } - - /** - * Writes all resolved dependencies in WORKSPACE file format to the outputStream. - */ - public void writeWorkspace(PrintStream outputStream) { - writeHeader(outputStream); - for (Rule rule : rules) { - outputStream.println(rule + "\n"); - } - } - - /** - * Write library rules to depend on the transitive closure of all of these rules. - */ - public void writeBuild(PrintStream outputStream) { - writeHeader(outputStream); - for (Rule rule : rules) { - outputStream.println("java_library("); - outputStream.println(" name = \"" + rule.name() + "\","); - outputStream.println(" visibility = [\"//visibility:public\"],"); - outputStream.println(" exports = ["); - outputStream.println(" \"@" + rule.name() + "//jar\","); - for (Rule r : rule.getDependencies()) { - outputStream.println(" \"@" + r.name() + "//jar\","); - } - outputStream.println(" ],"); - outputStream.println(")"); - outputStream.println(); - } - } - - private void writeHeader(PrintStream outputStream) { - outputStream.println("# The following dependencies were calculated from:"); - for (String header : sources) { - outputStream.println("# " + header); - } - outputStream.print("\n\n"); - } -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/WorkspaceResolver.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/WorkspaceResolver.java deleted file mode 100644 index fdf12cd2d9..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/WorkspaceResolver.java +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Sets; -import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; -import com.google.devtools.build.lib.bazel.BazelMain; -import com.google.devtools.build.lib.cmdline.LabelSyntaxException; -import com.google.devtools.build.lib.cmdline.RepositoryName; -import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.events.EventHandler; -import com.google.devtools.build.lib.events.Location; -import com.google.devtools.build.lib.packages.AggregatingAttributeMapper; -import com.google.devtools.build.lib.packages.AttributeMap; -import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; -import com.google.devtools.build.lib.packages.NoSuchPackageException; -import com.google.devtools.build.lib.packages.Package; -import com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension; -import com.google.devtools.build.lib.packages.RuleClassProvider; -import com.google.devtools.build.lib.packages.Target; -import com.google.devtools.build.lib.packages.WorkspaceFactory; -import com.google.devtools.build.lib.runtime.BlazeModule; -import com.google.devtools.build.lib.runtime.BlazeRuntime; -import com.google.devtools.build.lib.runtime.ServerBuilder; -import com.google.devtools.build.lib.syntax.Mutability; -import com.google.devtools.build.lib.syntax.ParserInputSource; -import com.google.devtools.build.lib.syntax.Type; -import com.google.devtools.build.lib.util.AbruptExitException; -import com.google.devtools.build.lib.vfs.Path; -import com.google.devtools.build.workspace.maven.DefaultModelResolver; -import com.google.devtools.build.workspace.maven.Resolver; -import com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException; -import com.google.devtools.build.workspace.maven.Rule; -import java.io.IOException; -import java.util.List; -import org.apache.maven.model.building.ModelSource; -import org.apache.maven.model.resolution.UnresolvableModelException; - -/** - * Finds the transitive dependencies of a WORKSPACE file. - */ -public class WorkspaceResolver { - - private final RuleClassProvider ruleClassProvider; - private final ImmutableList<EnvironmentExtension> environmentExtensions; - private final EventHandler handler; - private final com.google.devtools.build.workspace.maven.Resolver resolver; - - WorkspaceResolver(Resolver resolver, EventHandler handler) { - this.resolver = resolver; - this.handler = handler; - ServerBuilder serverBuilder = new ServerBuilder(); - ConfiguredRuleClassProvider.Builder ruleClassBuilder = - new ConfiguredRuleClassProvider.Builder(); - List<BlazeModule> blazeModules = BlazeRuntime.createModules(BazelMain.BAZEL_MODULES); - for (BlazeModule blazeModule : blazeModules) { - try { - blazeModule.serverInit(null, serverBuilder); - } catch (AbruptExitException e) { - throw new RuntimeException(e); - } - blazeModule.initializeRuleClasses(ruleClassBuilder); - } - this.ruleClassProvider = ruleClassBuilder.build(); - this.environmentExtensions = serverBuilder.getEnvironmentExtensions(); - } - - /** Converts the WORKSPACE file content into an ExternalPackage. */ - public Package parse(Path workspacePath) throws InterruptedException { - Package.Builder builder = Package.newExternalPackageBuilder( - Package.Builder.DefaultHelper.INSTANCE, - workspacePath, - ruleClassProvider.getRunfilesPrefix()); - try (Mutability mutability = Mutability.create("External Package %s", workspacePath)) { - new WorkspaceFactory(builder, ruleClassProvider, environmentExtensions, mutability) - .parse(ParserInputSource.create(workspacePath)); - } catch (IOException | BuildFileContainsErrorsException | InterruptedException e) { - handler.handle(Event.error(Location.fromFile(workspacePath), e.getMessage())); - } - - try { - return builder.build(); - } catch (NoSuchPackageException e) { - // TODO(kchodorow): delete this entire method. - throw new IllegalStateException("No globs expected in WORKSPACE files", e); - } - } - - /** - * Calculates transitive dependencies of the given //external package. - */ - public void resolveTransitiveDependencies(Package externalPackage) { - Location location = Location.fromFile(externalPackage.getFilename()); - for (Target target : externalPackage.getTargets().values()) { - // Targets are //external:foo. - if (target.getTargetKind().startsWith("maven_jar ")) { - RepositoryName repositoryName; - try { - repositoryName = RepositoryName.create("@" + target.getName()); - } catch (LabelSyntaxException e) { - handler.handle(Event.error(location, "Invalid repository name for " + target + ": " - + e.getMessage())); - return; - } - com.google.devtools.build.lib.packages.Rule workspaceRule = - externalPackage.getRule(repositoryName.strippedName()); - - DefaultModelResolver modelResolver = resolver.getModelResolver(); - AttributeMap attributeMap = AggregatingAttributeMapper.of(workspaceRule); - Rule rule; - try { - rule = new Rule(Resolver.getArtifact(attributeMap.get("artifact", Type.STRING))); - } catch (InvalidArtifactCoordinateException e) { - handler.handle(Event.error(location, "Couldn't get attribute: " + e.getMessage())); - return; - } - if (attributeMap.isAttributeValueExplicitlySpecified("repository")) { - modelResolver.addUserRepository(attributeMap.get("repository", Type.STRING)); - rule.setRepository(attributeMap.get("repository", Type.STRING), handler); - } - if (attributeMap.isAttributeValueExplicitlySpecified("sha1")) { - rule.setSha1(attributeMap.get("sha1", Type.STRING)); - } else { - rule.setSha1(resolver.downloadSha1(rule)); - } - - ModelSource modelSource; - try { - modelSource = modelResolver.resolveModel( - rule.groupId(), rule.artifactId(), rule.version()); - } catch (UnresolvableModelException e) { - handler.handle(Event.error( - "Could not resolve model for " + target + ": " + e.getMessage())); - continue; - } - resolver.addArtifact(rule, modelSource.getLocation()); - resolver.resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule); - } else if (!target.getTargetKind().startsWith("bind") - && !target.getTargetKind().startsWith("source ")) { - handler.handle(Event.warn(location, "Cannot fetch transitive dependencies for " + target - + " yet, skipping")); - } - } - } -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/BUILD b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/BUILD deleted file mode 100644 index a54bed5bad..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/BUILD +++ /dev/null @@ -1,48 +0,0 @@ -java_library( - name = "maven", - srcs = [ - "DefaultModelResolver.java", - "Resolver.java", - ], - visibility = [ - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace:__pkg__", - "//src/tools/generate_workspace/src/test:__subpackages__", - ], - runtime_deps = [ - "//third_party:maven_model", - "//third_party:plexus_interpolation", - "//third_party:plexus_utils", - ], - deps = [ - ":rule", - "//src/main/java/com/google/devtools/build/lib:events", - "//src/main/java/com/google/devtools/build/lib:maven-connector", - "//third_party:aether", - "//third_party:guava", - "//third_party:jsr305", - "//third_party:maven_model", - "//third_party:plexus_component_annotations", - ], -) - -java_library( - name = "rule", - srcs = ["Rule.java"], - visibility = [ - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace:__pkg__", - "//src/tools/generate_workspace/src/test:__subpackages__", - ], - deps = [ - "//src/main/java/com/google/devtools/build/lib:events", - "//src/main/java/com/google/devtools/build/lib:maven-connector", - "//third_party:aether", - "//third_party:guava", - "//third_party:maven_model", - ], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]), - visibility = ["//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace:__pkg__"], -) diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/DefaultModelResolver.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/DefaultModelResolver.java deleted file mode 100644 index 752039fe0e..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/DefaultModelResolver.java +++ /dev/null @@ -1,231 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace.maven; - -import com.google.common.base.Joiner; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import com.google.devtools.build.lib.bazel.repository.MavenConnector; -import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.events.EventHandler; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; -import org.apache.maven.model.Repository; -import org.apache.maven.model.building.DefaultModelBuilder; -import org.apache.maven.model.building.DefaultModelBuilderFactory; -import org.apache.maven.model.building.DefaultModelBuildingRequest; -import org.apache.maven.model.building.FileModelSource; -import org.apache.maven.model.building.ModelBuildingException; -import org.apache.maven.model.building.ModelBuildingResult; -import org.apache.maven.model.building.ModelSource; -import org.apache.maven.model.building.UrlModelSource; -import org.apache.maven.model.composition.DefaultDependencyManagementImporter; -import org.apache.maven.model.management.DefaultDependencyManagementInjector; -import org.apache.maven.model.management.DefaultPluginManagementInjector; -import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander; -import org.apache.maven.model.profile.DefaultProfileSelector; -import org.apache.maven.model.resolution.ModelResolver; -import org.apache.maven.model.resolution.UnresolvableModelException; -import org.eclipse.aether.artifact.Artifact; - -/** - * Resolver to find the repository a given Maven artifact should be fetched - * from. - */ -public class DefaultModelResolver implements ModelResolver { - - private final Set<Repository> repositories; - private final Map<String, ModelSource> ruleNameToModelSource; - private final DefaultModelBuilder modelBuilder; - - public DefaultModelResolver() { - repositories = Sets.newHashSet(); - repositories.add(MavenConnector.getMavenCentral()); - ruleNameToModelSource = Maps.newHashMap(); - modelBuilder = new DefaultModelBuilderFactory().newInstance() - .setProfileSelector(new DefaultProfileSelector()) - .setPluginConfigurationExpander(new DefaultPluginConfigurationExpander()) - .setPluginManagementInjector(new DefaultPluginManagementInjector()) - .setDependencyManagementImporter(new DefaultDependencyManagementImporter()) - .setDependencyManagementInjector(new DefaultDependencyManagementInjector()); - } - - private DefaultModelResolver( - Set<Repository> repositories, Map<String, ModelSource> ruleNameToModelSource, - DefaultModelBuilder modelBuilder) { - this.repositories = repositories; - this.ruleNameToModelSource = ruleNameToModelSource; - this.modelBuilder = modelBuilder; - } - - public ModelSource resolveModel(Artifact artifact) throws UnresolvableModelException { - return resolveModel(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()); - } - - @Override - public ModelSource resolveModel(String groupId, String artifactId, String version) - throws UnresolvableModelException { - String ruleName = Rule.name(groupId, artifactId); - if (ruleNameToModelSource.containsKey(ruleName)) { - return ruleNameToModelSource.get(ruleName); - } - for (Repository repository : repositories) { - UrlModelSource modelSource = getModelSource( - repository.getUrl(), groupId, artifactId, version); - if (modelSource != null) { - return modelSource; - } - } - - // TODO(kchodorow): use Java 8 features to make this a one-liner. - List<String> attemptedUrls = Lists.newArrayList(); - for (Repository repository : repositories) { - attemptedUrls.add(repository.getUrl()); - } - throw new UnresolvableModelException("Could not find any repositories that knew how to " - + "resolve " + groupId + ":" + artifactId + ":" + version + " (checked " - + Joiner.on(", ").join(attemptedUrls) + ")", groupId, artifactId, version); - } - - // TODO(kchodorow): make this work with local repositories. - private UrlModelSource getModelSource( - String url, String groupId, String artifactId, String version) - throws UnresolvableModelException { - try { - if (!url.endsWith("/")) { - url += "/"; - } - URL urlUrl = new URL(url - + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId - + "-" + version + ".pom"); - if (pomFileExists(urlUrl)) { - UrlModelSource urlModelSource = new UrlModelSource(urlUrl); - ruleNameToModelSource.put(Rule.name(groupId, artifactId), urlModelSource); - return urlModelSource; - } - } catch (MalformedURLException e) { - throw new UnresolvableModelException("Bad URL " + url + ": " + e.getMessage(), groupId, - artifactId, version, e); - } - return null; - } - - private boolean pomFileExists(URL url) { - try { - URLConnection urlConnection = url.openConnection(); - if (!(urlConnection instanceof HttpURLConnection)) { - return false; - } - - HttpURLConnection connection = (HttpURLConnection) urlConnection; - connection.setRequestMethod("HEAD"); - connection.setInstanceFollowRedirects(true); - connection.connect(); - - int code = connection.getResponseCode(); - if (code == 200) { - return true; - } - } catch (IOException e) { - // Something went wrong, fall through. - } - return false; - } - - // For compatibility with older versions of ModelResolver which don't have this method, - // don't add @Override. - public ModelSource resolveModel(Parent parent) throws UnresolvableModelException { - return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()); - } - - // For compatibility with older versions of ModelResolver which don't have this method, - // don't add @Override. - public void addRepository(Repository repository) { - repositories.add(repository); - } - - @Override - public void addRepository(Repository repository, boolean replace) { - addRepository(repository); - } - - @Override - public ModelResolver newCopy() { - return new DefaultModelResolver(repositories, ruleNameToModelSource, modelBuilder); - } - - /** - * Adds a user-specified repository to the list. - */ - public void addUserRepository(String url) { - Repository repository = new Repository(); - repository.setUrl(url); - repository.setId("user-defined repository"); - repository.setName("default"); - addRepository(repository); - } - - public boolean putModelSource(String groupId, String artifactId, ModelSource modelSource) { - String key = Rule.name(groupId, artifactId); - if (!ruleNameToModelSource.containsKey(key)) { - ruleNameToModelSource.put(key, modelSource); - return true; - } - return false; - } - - public Model getEffectiveModel(ModelSource modelSource, EventHandler handler) { - DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); - request.setModelResolver(this); - request.setModelSource(modelSource); - Model model; - try { - ModelBuildingResult result = modelBuilder.build(request); - model = result.getEffectiveModel(); - } catch (ModelBuildingException | IllegalArgumentException e) { - // IllegalArg can be thrown if the parent POM cannot be resolved. - handler.handle(Event.error("Unable to resolve Maven model from " + modelSource.getLocation() - + ": " + e.getMessage())); - return null; - } - return model; - } - - public Model getRawModel(FileModelSource fileModelSource, EventHandler handler) { - DefaultModelBuildingRequest request = new DefaultModelBuildingRequest(); - request.setModelResolver(this); - request.setModelSource(fileModelSource); - Model model; - try { - ModelBuildingResult result = modelBuilder.build(request); - model = result.getRawModel(); - } catch (ModelBuildingException | IllegalArgumentException e) { - // IllegalArg can be thrown if the parent POM cannot be resolved. - handler.handle(Event.error("Unable to resolve raw Maven model from " - + fileModelSource.getLocation() + ": " + e.getMessage())); - return null; - } - return model; - } -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Resolver.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Resolver.java deleted file mode 100644 index fd19b03729..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Resolver.java +++ /dev/null @@ -1,287 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace.maven; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import com.google.common.io.CharStreams; -import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.events.EventHandler; - -import org.apache.maven.model.Dependency; -import org.apache.maven.model.Model; -import org.apache.maven.model.Parent; -import org.apache.maven.model.Repository; -import org.apache.maven.model.building.DefaultModelProcessor; -import org.apache.maven.model.building.FileModelSource; -import org.apache.maven.model.building.ModelSource; -import org.apache.maven.model.io.DefaultModelReader; -import org.apache.maven.model.locator.DefaultModelLocator; -import org.apache.maven.model.resolution.UnresolvableModelException; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.artifact.DefaultArtifact; - -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import javax.annotation.Nullable; - -/** - * Resolves Maven dependencies. - */ -public class Resolver { - - /** - * Exception thrown if an artifact coordinate could not be parsed. - */ - public static class InvalidArtifactCoordinateException extends Exception { - InvalidArtifactCoordinateException(String message) { - super(message); - } - } - - public static Artifact getArtifact(String atrifactCoords) - throws InvalidArtifactCoordinateException { - try { - return new DefaultArtifact(atrifactCoords); - } catch (IllegalArgumentException e) { - throw new InvalidArtifactCoordinateException(e.getMessage()); - } - } - - public static Artifact getArtifact(Dependency dependency) - throws InvalidArtifactCoordinateException { - return getArtifact(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" - + dependency.getVersion()); - } - - private static final String COMPILE_SCOPE = "compile"; - - private final EventHandler handler; - private final DefaultModelResolver modelResolver; - - // Mapping of maven_jar name to Rule. - private final Map<String, Rule> deps; - - public Resolver(EventHandler handler, DefaultModelResolver resolver) { - this.handler = handler; - this.deps = Maps.newHashMap(); - this.modelResolver = resolver; - } - - /** - * Returns all maven_jars. - */ - public Collection<Rule> getRules() { - return deps.values(); - } - - public DefaultModelResolver getModelResolver() { - return modelResolver; - } - - /** - * Given a local path to a Maven project, this attempts to find the transitive dependencies of - * the project. - * @param projectPath The path to search for Maven projects. - */ - public String resolvePomDependencies(String projectPath) { - DefaultModelProcessor processor = new DefaultModelProcessor(); - processor.setModelLocator(new DefaultModelLocator()); - processor.setModelReader(new DefaultModelReader()); - File pom = processor.locatePom(new File(projectPath)); - FileModelSource pomSource = new FileModelSource(pom); - // First resolve the model source locations. - resolveSourceLocations(pomSource); - // Next, fully resolve the models. - resolveEffectiveModel(pomSource, Sets.<String>newHashSet(), null); - return pom.getAbsolutePath(); - } - - /** - * Resolves an artifact as a root of a dependency graph. - */ - public void resolveArtifact(String artifactCoord) { - Artifact artifact; - ModelSource modelSource; - try { - artifact = getArtifact(artifactCoord); - modelSource = modelResolver.resolveModel(artifact); - } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) { - handler.handle(Event.error(e.getMessage())); - return; - } - - Rule rule = new Rule(artifact); - deps.put(rule.name(), rule); // add the artifact rule to the workspace - resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule); - } - - /** - * Resolves all dependencies from a given "model source," which could be either a URL or a local - * file. - * @return the model. - */ - @Nullable - public Model resolveEffectiveModel(ModelSource modelSource, Set<String> exclusions, Rule parent) { - Model model = modelResolver.getEffectiveModel(modelSource, handler); - if (model == null) { - return null; - } - for (Repository repo : model.getRepositories()) { - modelResolver.addRepository(repo); - } - - for (Dependency dependency : model.getDependencies()) { - if (!dependency.getScope().equals(COMPILE_SCOPE)) { - continue; - } - if (dependency.isOptional()) { - continue; - } - if (exclusions.contains(dependency.getGroupId() + ":" + dependency.getArtifactId())) { - continue; - } - try { - Rule artifactRule = new Rule(getArtifact(dependency), dependency.getExclusions()); - HashSet<String> localDepExclusions = new HashSet<>(exclusions); - localDepExclusions.addAll(artifactRule.getExclusions()); - - boolean isNewDependency = addArtifact(artifactRule, model.toString()); - if (isNewDependency) { - ModelSource depModelSource = modelResolver.resolveModel( - dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); - if (depModelSource != null) { - artifactRule.setRepository(depModelSource.getLocation(), handler); - artifactRule.setSha1(downloadSha1(artifactRule)); - resolveEffectiveModel(depModelSource, localDepExclusions, artifactRule); - } else { - handler.handle(Event.error("Could not get a model for " + dependency)); - } - } - - if (parent != null) { - parent.addDependency(artifactRule); - parent.getDependencies().addAll(artifactRule.getDependencies()); - } else { - addArtifact(artifactRule, modelSource.getLocation()); - } - } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) { - handler.handle(Event.error("Could not resolve dependency " + dependency.getGroupId() - + ":" + dependency.getArtifactId() + ":" + dependency.getVersion() + ": " - + e.getMessage())); - } - } - return model; - } - - /** - * Find the POM files for a given pom's parent(s) and submodules. - */ - private void resolveSourceLocations(FileModelSource fileModelSource) { - Model model = modelResolver.getRawModel(fileModelSource, handler); - if (model == null) { - return; - } - - // Self. - Parent parent = model.getParent(); - if (model.getGroupId() == null) { - model.setGroupId(parent.getGroupId()); - } - if (!modelResolver.putModelSource( - model.getGroupId(), model.getArtifactId(), fileModelSource)) { - return; - } - - // Parent. - File pomDirectory = new File(fileModelSource.getLocation()).getParentFile(); - if (parent != null && !parent.getArtifactId().equals(model.getArtifactId())) { - File parentPom; - try { - parentPom = new File(pomDirectory, parent.getRelativePath()).getCanonicalFile(); - } catch (IOException e) { - handler.handle(Event.error("Unable to get canonical path of " + pomDirectory + " and " - + parent.getRelativePath())); - return; - } - if (parentPom.exists()) { - resolveSourceLocations(new FileModelSource(parentPom)); - } - } - - // Submodules. - for (String module : model.getModules()) { - resolveSourceLocations(new FileModelSource(new File(pomDirectory, module + "/pom.xml"))); - } - } - - /** - * Adds the artifact to the map of deps, if it is not already there. Returns if the artifact - * was newly added. If the artifact was in the list at a different version, adds an comment - * about the desired version. - */ - public boolean addArtifact(Rule dependency, String parent) { - String artifactName = dependency.name(); - if (deps.containsKey(artifactName)) { - Rule existingDependency = deps.get(artifactName); - // Check that the versions are the same. - if (!existingDependency.version().equals(dependency.version())) { - existingDependency.addParent(parent + " wanted version " + dependency.version()); - } else { - existingDependency.addParent(parent); - } - return false; - } - - deps.put(artifactName, dependency); - dependency.addParent(parent); - return true; - } - - static String getSha1Url(String url, String extension) { - return url.replaceAll(".pom$", "." + extension + ".sha1"); - } - - /** - * Downloads the SHA-1 for the given artifact. - */ - public String downloadSha1(Rule rule) { - String sha1Url = getSha1Url(rule.getUrl(), rule.getArtifact().getExtension()); - try { - HttpURLConnection connection = (HttpURLConnection) new URL(sha1Url).openConnection(); - connection.setInstanceFollowRedirects(true); - connection.connect(); - return extractSha1( - CharStreams.toString( - new InputStreamReader(connection.getInputStream(), Charset.defaultCharset()))); - } catch (IOException e) { - handler.handle(Event.warn("Failed to download the sha1 at " + sha1Url)); - } - return null; - } - - static String extractSha1(String sha1Contents) { - return sha1Contents.split("\\s+")[0]; - } -} diff --git a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Rule.java b/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Rule.java deleted file mode 100644 index 6a85e727cc..0000000000 --- a/src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven/Rule.java +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace.maven; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; -import com.google.devtools.build.lib.bazel.repository.MavenConnector; -import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.events.EventHandler; - -import org.apache.maven.model.Exclusion; -import org.eclipse.aether.artifact.Artifact; - -import java.util.List; -import java.util.Objects; -import java.util.Set; - -/** - * A struct representing the fields of maven_jar to be written to the WORKSPACE file. - */ -public final class Rule implements Comparable<Rule> { - - private final Artifact artifact; - private final Set<String> parents; - private final Set<String> exclusions; - private final Set<Rule> dependencies; - private String repository; - private String sha1; - - public Rule(Artifact artifact) { - this.artifact = artifact; - this.parents = Sets.newHashSet(); - this.dependencies = Sets.newTreeSet(); - this.exclusions = Sets.newHashSet(); - this.repository = MavenConnector.MAVEN_CENTRAL_URL; - } - - public Rule(Artifact artifact, List<Exclusion> exclusions) { - this(artifact); - - for (Exclusion exclusion : exclusions) { - String coord = String.format("%s:%s", exclusion.getGroupId(), exclusion.getArtifactId()); - this.exclusions.add(coord); - } - } - - public void addParent(String parent) { - parents.add(parent); - } - - public void addDependency(Rule dependency) { - dependencies.add(dependency); - } - - public Set<Rule> getDependencies() { - return dependencies; - } - - public String artifactId() { - return artifact.getArtifactId(); - } - - public Set<String> getExclusions() { - return exclusions; - } - - public String groupId() { - return artifact.getGroupId(); - } - - public String version() { - return artifact.getVersion(); - } - - /** - * A unique name for this artifact to use in maven_jar's name attribute. - */ - public String name() { - return Rule.name(groupId(), artifactId()); - } - - /** - * A unique name for this artifact to use in maven_jar's name attribute. - */ - public static String name(String groupId, String artifactId) { - return groupId.replaceAll("[.-]", "_") + "_" + artifactId.replaceAll("[.-]", "_"); - } - - public Artifact getArtifact() { - return artifact; - } - - public String toMavenArtifactString() { - return groupId() + ":" + artifactId() + ":" + version(); - } - - public void setRepository(String url, EventHandler handler) { - // url is of the form repository/group/artifact/version/artifact-version.pom. Strip off - // everything after repository/. - int uriStart = url.indexOf(getUri()); - if (uriStart == -1) { - // If url is actually a path to a file, it won't match the URL pattern described above. - // However, in that case we also have no way of fetching the artifact, so we'll print a - // warning. - handler.handle(Event.warn(name() + " was defined in " + url - + " which isn't a repository URL, so we couldn't figure out how to fetch " - + toMavenArtifactString() + " in a general way. You will need to set the \"repository\"" - + " attribute manually")); - } else { - this.repository = url.substring(0, uriStart); - } - } - - public void setSha1(String sha1) { - this.sha1 = sha1; - } - - private String getUri() { - return groupId().replaceAll("\\.", "/") + "/" + artifactId() + "/" + version() + "/" - + artifactId() + "-" + version() + ".pom"; - } - - /** - * @return The artifact's URL. - */ - public String getUrl() { - Preconditions.checkState(repository.endsWith("/")); - return repository + getUri(); - } - - /** - * The way this jar should be stringified for the WORKSPACE file. - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - for (String parent : parents) { - builder.append("# " + parent + "\n"); - } - builder.append("maven_jar(\n" - + " name = \"" + name() + "\",\n" - + " artifact = \"" + toMavenArtifactString() + "\",\n" - + (hasCustomRepository() ? " repository = \"" + repository + "\",\n" : "") - + (sha1 != null ? " sha1 = \"" + sha1 + "\",\n" : "") - + ")"); - return builder.toString(); - } - - private boolean hasCustomRepository() { - return !MavenConnector.MAVEN_CENTRAL_URL.equals(repository); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Rule rule = (Rule) o; - - return Objects.equals(groupId(), rule.groupId()) - && Objects.equals(artifactId(), rule.artifactId()); - } - - @Override - public int hashCode() { - return Objects.hash(groupId(), artifactId()); - } - - @Override - public int compareTo(Rule o) { - return name().compareTo(o.name()); - } -} diff --git a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/BUILD b/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/BUILD deleted file mode 100644 index 93e5986937..0000000000 --- a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/BUILD +++ /dev/null @@ -1,22 +0,0 @@ -licenses(["notice"]) # Apache 2.0 - -filegroup( - name = "srcs", - srcs = glob(["**"]) + [ - "//src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven:srcs", - ], - visibility = ["//src/tools/generate_workspace:__pkg__"], -) - -java_test( - name = "ResultWriterTest", - srcs = ["ResultWriterTest.java"], - deps = [ - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven:rule", - "//third_party:aether", - "//third_party:guava", - "//third_party:junit4", - "//third_party:truth", - ], -) diff --git a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/ResultWriterTest.java b/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/ResultWriterTest.java deleted file mode 100644 index ce3b3aaf96..0000000000 --- a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/ResultWriterTest.java +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2016 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; -import com.google.devtools.build.workspace.maven.Rule; - -import org.eclipse.aether.artifact.DefaultArtifact; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Set; - -/** - * Tests for @{link ResultWriter}. - */ -@RunWith(JUnit4.class) -public class ResultWriterTest { - - public String getWorkspaceFileContent(List<String> sources, Set<Rule> rules) throws Exception{ - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - ResultWriter writer = new ResultWriter(sources, rules); - writer.writeWorkspace(ps); - return baos.toString(String.valueOf(Charset.defaultCharset())); - } - - public String getBuildFileContent(List<String> sources, Set<Rule> rules) throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream(baos); - ResultWriter writer = new ResultWriter(sources, rules); - writer.writeBuild(ps); - return baos.toString(String.valueOf(Charset.defaultCharset())); - } - - @Test - public void testHeaders() throws Exception { - String content = getWorkspaceFileContent( - ImmutableList.of("foo", "bar"), Sets.<Rule>newHashSet()); - assertThat(content).contains("# foo\n# bar"); - } - - @Test - public void testArtifacts() throws Exception { - Set<Rule> rules = ImmutableSet.of( - new Rule(new DefaultArtifact("x:y:1.2.3"))); - String content = getWorkspaceFileContent(ImmutableList.<String>of(), rules); - assertThat(content).contains("maven_jar(\n" - + " name = \"x_y\",\n" - + " artifact = \"x:y:1.2.3\",\n" - + ")" - ); - } - - @Test - public void testParents() throws Exception { - Rule rule = new Rule(new DefaultArtifact("x:y:1.2.3")); - rule.addParent("some parent"); - Set<Rule> rules = ImmutableSet.of(rule); - String content = getWorkspaceFileContent(ImmutableList.<String>of(), rules); - assertThat(content).contains("# some parent\n" - + "maven_jar(\n" - + " name = \"x_y\",\n" - + " artifact = \"x:y:1.2.3\",\n" - + ")" - ); - } - - @Test - public void testBuildFile() throws Exception { - Rule rule = new Rule(new DefaultArtifact("x:y:1.2.3")); - Rule dep1 = new Rule(new DefaultArtifact("dep:dep1:4.5.6")); - rule.addDependency(dep1); - Rule dep2 = new Rule(new DefaultArtifact("dep:dep2:7.8.9")); - rule.addDependency(dep2); - Set<Rule> rules = ImmutableSet.of(rule, dep1, dep2); - String content = getBuildFileContent(ImmutableList.<String>of(), rules); - assertThat(content).contains("java_library(\n" - + " name = \"x_y\",\n" - + " visibility = [\"//visibility:public\"],\n" - + " exports = [\n" - + " \"@x_y//jar\",\n" - + " \"@dep_dep1//jar\",\n" - + " \"@dep_dep2//jar\",\n" - + " ],\n" - + ")" - ); - } -} diff --git a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/BUILD b/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/BUILD deleted file mode 100644 index 8db2e7381a..0000000000 --- a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/BUILD +++ /dev/null @@ -1,30 +0,0 @@ -filegroup( - name = "srcs", - srcs = glob(["**"]), - visibility = ["//src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace:__pkg__"], -) - -java_test( - name = "RuleTest", - srcs = ["RuleTest.java"], - deps = [ - "//src/main/java/com/google/devtools/build/lib:events", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven:rule", - "//third_party:junit4", - "//third_party:truth", - ], -) - -java_test( - name = "ResolverTest", - srcs = ["ResolverTest.java"], - deps = [ - "//src/main/java/com/google/devtools/build/lib:events", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven", - "//src/tools/generate_workspace/src/main/java/com/google/devtools/build/workspace/maven:rule", - "//third_party:junit4", - "//third_party:mockito", - "//third_party:truth", - ], -) diff --git a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/ResolverTest.java b/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/ResolverTest.java deleted file mode 100644 index 82046eea2a..0000000000 --- a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/ResolverTest.java +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2016 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace.maven; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.devtools.build.lib.events.StoredEventHandler; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import org.mockito.Mockito; - -import java.util.Collection; - -/** - * Tests for {@link Resolver}. - */ -@RunWith(JUnit4.class) -public class ResolverTest { - @Test - public void testGetSha1Url() throws Exception { - assertThat(Resolver.getSha1Url("http://example.com/foo.pom", "jar")) - .isEqualTo("http://example.com/foo.jar.sha1"); - assertThat(Resolver.getSha1Url("http://example.com/foo.pom", "aar")) - .isEqualTo("http://example.com/foo.aar.sha1"); - } - - @Test - public void testGetSha1UrlOnlyAtEOL() throws Exception { - assertThat(Resolver.getSha1Url("http://example.pom/foo.pom", "jar")) - .isEqualTo("http://example.pom/foo.jar.sha1"); - } - - @Test - public void testArtifactResolution() throws Exception { - StoredEventHandler handler = new StoredEventHandler(); - DefaultModelResolver modelResolver = Mockito.mock(DefaultModelResolver.class); - Resolver resolver = new Resolver(handler, modelResolver); - resolver.resolveArtifact("x:y:1.2.3"); - Collection<Rule> rules = resolver.getRules(); - assertThat(rules).hasSize(1); - Rule rule = rules.iterator().next(); - assertThat(rule.name()).isEqualTo("x_y"); - } - - @Test - public void testExtractSha1() { - assertThat(Resolver.extractSha1("5fe28b9518e58819180a43a850fbc0dd24b7c050")) - .isEqualTo("5fe28b9518e58819180a43a850fbc0dd24b7c050"); - - assertThat(Resolver.extractSha1("5fe28b9518e58819180a43a850fbc0dd24b7c050\n")) - .isEqualTo("5fe28b9518e58819180a43a850fbc0dd24b7c050"); - - assertThat(Resolver.extractSha1( - "83cd2cd674a217ade95a4bb83a8a14f351f48bd0 /home/maven/repository-staging/to-ibiblio/maven2/antlr/antlr/2.7.7/antlr-2.7.7.jar")) - .isEqualTo("83cd2cd674a217ade95a4bb83a8a14f351f48bd0"); - } -} diff --git a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/RuleTest.java b/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/RuleTest.java deleted file mode 100644 index 2782aba7da..0000000000 --- a/src/tools/generate_workspace/src/test/java/com/google/devtools/build/workspace/maven/RuleTest.java +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2016 The Bazel Authors. All rights reserved. -// -// Licensed 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.google.devtools.build.workspace.maven; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.devtools.build.lib.events.StoredEventHandler; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for {@link Rule}. - */ -@RunWith(JUnit4.class) -public class RuleTest { - - private StoredEventHandler handler; - - @Before - public void createEventHandler() { - handler = new StoredEventHandler(); - } - - @Test - public void testUrl() throws Exception { - Rule rule = new Rule(Resolver.getArtifact("foo:bar:1.2.3")); - assertThat(rule.getUrl()) - .isEqualTo("https://repo1.maven.org/maven2/foo/bar/1.2.3/bar-1.2.3.pom"); - rule.setRepository("http://myrepo.com/foo/bar/1.2.3/bar-1.2.3.pom", handler); - assertThat(handler.getEvents()).isEmpty(); - assertThat(rule.getUrl()).isEqualTo("http://myrepo.com/foo/bar/1.2.3/bar-1.2.3.pom"); - } -} |