-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from menny/refactor-sources-locator
Refactor sources locator
- Loading branch information
Showing
12 changed files
with
428 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Auto-generated by https://github.com/menny/bazel-mvn-deps | ||
|
||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_androidx_fragment__fragment__1_1_0_alpha02-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_androidx_appcompat__appcompat__1_0_2-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_androidx_annotation__annotation__1_0_0-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_com_github_menny_Chauffeur__permissions__90e703256785b7baf9cd37370b62b1d7d3830406-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/bazel-mvn-deps/examples/android/program/main_deps/ --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true | ||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_androidx_fragment__fragment__1_1_0_alpha02-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_androidx_appcompat__appcompat__1_0_2-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_androidx_annotation__annotation__1_0_0-transitive-graph.json --graph_file=program/maven_dependency_graph_resolving_rule_com_github_menny_Chauffeur__permissions__90e703256785b7baf9cd37370b62b1d7d3830406-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/android/program/main_deps/ --fetch_srcjar=true --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Auto-generated by https://github.com/menny/bazel-mvn-deps | ||
|
||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_github_salomonbrys_kotson__kotson__2_5_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/kotlin/program/main_deps/ --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true | ||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_github_salomonbrys_kotson__kotson__2_5_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/kotlin/program/main_deps/ --fetch_srcjar=true --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Auto-generated by https://github.com/menny/bazel-mvn-deps | ||
|
||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_github_salomonbrys_kotson__kotson__2_5_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/kotlin_with_kt_rules/program/main_deps/ --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true | ||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_github_salomonbrys_kotson__kotson__2_5_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/kotlin_with_kt_rules/program/main_deps/ --fetch_srcjar=true --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Auto-generated by https://github.com/menny/bazel-mvn-deps | ||
|
||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_google_guava__guava__20_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/plain_java/program/main_deps/ --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true | ||
# Args: --graph_file=program/maven_dependency_graph_resolving_rule_com_google_guava__guava__20_0-transitive-graph.json --output_macro_file_path=dependencies.bzl --output_target_build_files_base_path=/Users/menny/dev/menny/mabel/examples/plain_java/program/main_deps/ --fetch_srcjar=true --package_path=program --rule_prefix=main_deps___ --create_deps_sub_folders=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
resolver/src/main/java/net/evendanan/bazel/mvn/merger/SourcesJarLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package net.evendanan.bazel.mvn.merger; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import net.evendanan.bazel.mvn.api.Dependency; | ||
|
||
public class SourcesJarLocator { | ||
|
||
private static final String SOURCES_CLASSIFIER = "sources"; | ||
|
||
private final ConnectionOpener mConnectionOpenner; | ||
private final Map<URI, URI> mURLCache = new HashMap<>(); | ||
|
||
public SourcesJarLocator() { | ||
this(url -> (HttpURLConnection) url.openConnection()); | ||
} | ||
|
||
@VisibleForTesting | ||
SourcesJarLocator(final ConnectionOpener opener) { | ||
mConnectionOpenner = opener; | ||
} | ||
|
||
public Collection<Dependency> fillSourcesAttribute(Collection<Dependency> dependencies) { | ||
return dependencies.stream() | ||
.map(dependency -> new Dependency(dependency.groupId(), dependency.artifactId(), dependency.version(), dependency.packaging(), | ||
fillSourcesAttribute(dependency.dependencies()), | ||
fillSourcesAttribute(dependency.exports()), | ||
fillSourcesAttribute(dependency.runtimeDependencies()), | ||
dependency.url(), uriWithClassifier(dependency.url()), dependency.javadocUrl(), dependency.licenses())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private URI uriWithClassifier(final URI uri) { | ||
return mURLCache.computeIfAbsent(uri, this::hotFetch); | ||
} | ||
|
||
private URI hotFetch(final URI uri) { | ||
System.out.print('.'); | ||
final String url = uri.toASCIIString(); | ||
final int extStartIndex = url.lastIndexOf("."); | ||
if (extStartIndex > 0) { | ||
try { | ||
//for example | ||
// https://repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0.jar | ||
// will become | ||
// https://repo1.maven.org/maven2/com/google/guava/guava/20.0/guava-20.0-sources.jar (always jar ext) | ||
final String urlWithClassifier = String.format(Locale.US, "%s-%s.jar", url.substring(0, extStartIndex), SOURCES_CLASSIFIER); | ||
final URL classifiedUrl = new URL(urlWithClassifier); | ||
HttpURLConnection con = mConnectionOpenner.openUrlConnection(classifiedUrl); | ||
con.setRequestMethod("HEAD"); | ||
final int responseCode = con.getResponseCode(); | ||
if (responseCode >= 200 && responseCode < 300) { | ||
return classifiedUrl.toURI(); | ||
} else { | ||
return URI.create(""); | ||
} | ||
} catch (Exception e) { | ||
return URI.create(""); | ||
} | ||
} else { | ||
return URI.create(""); | ||
} | ||
} | ||
|
||
interface ConnectionOpener { | ||
HttpURLConnection openUrlConnection(URL url) throws IOException; | ||
} | ||
} |
Oops, something went wrong.