Skip to content

Commit

Permalink
* Update grade to 4.1. (#9)
Browse files Browse the repository at this point in the history
* Add repositories attribute.
* Add omit attribute.
* Make require overwrite non-fatal.
  • Loading branch information
pcj authored Sep 6, 2017
1 parent 1cb680a commit 43d56ae
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ os:
- osx

env:
- V=HEAD
#- V=HEAD
- V=0.5.4
- V=0.5.3
- V=0.5.2
- V=0.4.5
- V=0.3.1

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ maven_repository(
Once the `transitive_deps` is stable (all transitive deps and their correct
sha1 values are listed), `rules_maven` will be silent.

> Note: make sure you leave out the artifact ID type, `rules_maven`
> will get confused about it. For example, don't say
> `com.google.inject:guice:jar:4.1.0` (leave out the `:jar`
> substring).
### 3. Load the `@guice//:rules.bzl` file in your WORKSPACE and invoke the desired macro configuration.

The `rules.bzl` file (a generated file) contains macro definitions
Expand Down Expand Up @@ -173,6 +178,8 @@ guice_compile()
| `transitive_deps` | `string_list` | `[]` | List of maven artifacts in the transitive set reachable from `deps`. The have the form `SHA1:NAME:GROUP:VERSION`, and are calculated by rules_maven via a generated `build.gradle` file. |
| `exclude` | `string_list_dict` | `{}` | List of artifacts to exclude, in the form `{ 'NAME:GROUP': ['EXCLUDED_GROUP:EXCLUDED_NAME']` |
| `force` | `string_list` | `[]` | List of artifacts to force, in the form `[ 'NAME:GROUP:VERSION']` |
| `omit` | `string_list` | `[]` | List of artifacts to skip, in the form `[ 'NAME:GROUP:VERSION']` |
| `repositories` | `string_list_dict` | `{}` | A mapping of artifact-id pattern to url (see below) |
| `configurations` | `string_list` | `["compile", "default", "runtime", "compileOnly", "compileClasspath"]` | List of configurations to generate a corresponding rule for. |
| `experimental_disambiguate_maven_jar_workspace_names` | `bool` | `False` | See Note |

Expand All @@ -186,6 +193,25 @@ guice_compile()
> workspace name includes the version specifier and becomes
> `com_google_guava_guava_20_0`.
Example use of the `repositories` attribute:


```python
# Load commons-imaging from adobe nexus repository.
# Load everything else (junit) from maven central.
maven_repository(
name = "maven",
repositories = {
"https://repo.adobe.com/nexus/content/repositories/public/": [
"org.apache.commons:commons-imaging",
],
},
deps = [
"junit:junit:4.12",
"org.apache.commons:commons-imaging:1.0-R1534292",
],
)
```

# Credits

Expand Down
2 changes: 1 addition & 1 deletion maven/internal/maven_repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("//maven:internal/require.bzl", "require")
load("//maven:internal/require_toolchain.bzl", "require_toolchain")

GRADLE_VERSION = "3.2.1"
GRADLE_VERSION = "4.1"

GRADLE_BUILD_FILE = """
java_import(
Expand Down
56 changes: 45 additions & 11 deletions maven/internal/maven_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ def _get_artifact_sha1(rtx, artifact):
ws_name = artifact["ws_name"]
output_file = ws_name + ".jar.sha1"

# Default url is maven central
url = _get_repository_url(rtx.attr.repositories, artifact) or "https://repo1.maven.org/maven2"

# TODO: make this configurable.
path = "https://repo1.maven.org/maven2/{group}/{name}/{version}/{name}-{version}.jar.sha1".format(
path = "{url}/{group}/{name}/{version}/{name}-{version}.jar.sha1".format(
url = url,
group = group,
name = name,
version = version,
Expand Down Expand Up @@ -249,8 +253,16 @@ def _format_java_library(name, artifacts):
lines.append(")")
return lines

def _get_repository_url(rules, artifact):
for url in rules.keys():
rule_list = rules[url]
for rule in rule_list:
match = rule.split(":")
if len(match) == 2 and match[0] == artifact["group"] and match[1] == artifact["name"]:
return url
return None

def _format_rules_file(rule_name, configs, all_artifacts):
def _format_rules_file(rtx, rule_name, configs, all_artifacts):
"""Generate the rules.bzl file content.
Args:
rule_name: string - The name of the rule (rtx.name)
Expand All @@ -263,32 +275,38 @@ def _format_rules_file(rule_name, configs, all_artifacts):
lines.append("load('@org_pubref_require_toolchain//:require.bzl', 'require')")
lines.append("DEPS = {")
for ws_name, artifact in all_artifacts.items():
lines += _format_maven_jar(ws_name, artifact)
lines += _format_maven_jar(rtx, ws_name, artifact)
lines.append("}")
for name, artifacts in configs.items():
lines += _format_config_def(rule_name + "_" + name, artifacts)
lines += _format_config_def(rtx, rule_name + "_" + name, artifacts)
return "\n".join(lines)


def _format_maven_jar(ws_name, artifact):
def _format_maven_jar(rtx, ws_name, artifact):
"""Format a maven_jar rule (in require form).
Args:
name: string - the workspace name.
artifacts: !dict<string,!Artifact>
Returns: !list<string>
"""
# If we match a different repository, populate the URL variable
url = _get_repository_url(rtx.attr.repositories, artifact)

lines = []
lines.append(" '%s': {" % ws_name)
lines.append(" 'rule': 'maven_jar',")
lines.append(" 'artifact': '%s'," % artifact["coordinate"])
lines.append(" 'sha1': '%s'," % artifact["sha1"])
if url:
lines.append(" 'repository': '%s'," % url)
lines.append(" },")
return lines


def _format_config_def(name, artifacts):
def _format_config_def(rtx, name, artifacts):
"""Format a macro function for a given configuration.
Args:
rtx: repository_ctx - the context.
name: string - the configuration name.
artifacts: !dict<string,!Artifact>
Returns: !list<string>
Expand All @@ -297,7 +315,12 @@ def _format_config_def(name, artifacts):
lines.append("def %s(deps = DEPS, excludes = [], overrides = {}):" % name)
lines.append(" require([")
for ws_name, artifact in artifacts.items():
lines.append(" '%s'," % ws_name)
should_include = True
for target in rtx.attr.omit:
if target == ws_name:
should_include = False
if should_include:
lines.append(" '%s'," % ws_name)
lines.append(" ], deps = deps, excludes = excludes, overrides = overrides)")
return lines

Expand Down Expand Up @@ -438,18 +461,25 @@ def _format_build_gradle_resolution_strategy(force):
return lines


def _format_build_gradle_repositories():
def _format_build_gradle_repositories(rtx):
lines = []
lines.append("repositories {")
lines.append(" mavenCentral()")
for url in rtx.attr.repositories.keys():
if url == 'jcenter':
lines.append(" jcenter()")
else:
lines.append(" maven {")
lines.append(' url = "%s"' % url)
lines.append(" }")
lines.append("}")
return lines


def _format_build_gradle_file(rtx):
lines = []
lines += _format_build_gradle_plugins()
lines += _format_build_gradle_repositories()
lines += _format_build_gradle_repositories(rtx)
lines += _format_build_gradle_resolution_strategy(rtx.attr.force)

lines.append("dependencies {")
Expand Down Expand Up @@ -498,7 +528,7 @@ def _maven_repository_impl(rtx):
print("\n# CLOSED-FORM RULE:\n# You can copy this to your WORKSPACE To suppress this message. \n%s\n" % "\n".join(lines))

rtx.file("BUILD", _format_build_file(configs));
rtx.file("rules.bzl", _format_rules_file(rtx.name, configs, transitive_artifacts));
rtx.file("rules.bzl", _format_rules_file(rtx, rtx.name, configs, transitive_artifacts));


maven_repository = repository_rule(
Expand All @@ -510,8 +540,12 @@ maven_repository = repository_rule(
),
"exclude": attr.string_list_dict(
),
"omit": attr.string_list(
),
"force": attr.string_list(
),
"repositories": attr.string_list_dict(
),
"transitive_deps": attr.string_list(
),
"_java": attr.label(
Expand All @@ -520,7 +554,7 @@ maven_repository = repository_rule(
cfg = "host",
),
"_gradle_launcher_jar": attr.label(
default = Label("@gradle_distribution//:lib/gradle-launcher-3.2.1.jar"),
default = Label("@gradle_distribution//:lib/gradle-launcher-4.1.jar"),
executable = True,
cfg = "host",
),
Expand Down
6 changes: 3 additions & 3 deletions maven/internal/require.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def _needs_install(name, dep, hkeys=["sha256", "sha1", "tag"], verbose=0):
if expected:
if expected != actual:
msg = """
An existing {0} rule '{1}' was already loaded with a {2} value of '{3}'. Refusing to overwrite this with the requested value ('{4}').
Either remove the pre-existing rule from your WORKSPACE or exclude it from loading by rules_protobuf.
An existing {0} rule '{1}' was already loaded with a {2} value of '{3}'. Overwriting this with the requested value ('{4}').
You *should* either remove the pre-existing rule from your WORKSPACE or exclude it from loading by rules_maven.
""".format(existing_rule["kind"], name, hkey, actual, expected)

fail(msg)
print(msg)
else:
if verbose > 1: print("Skip reload %s: %s = %s" % (name, hkey, actual))
return False
Expand Down

0 comments on commit 43d56ae

Please sign in to comment.