Skip to content

Commit

Permalink
Make workspace_refs optional for assemble_maven (#182)
Browse files Browse the repository at this point in the history
## What is the goal of this PR?

Fix #176
`assemble_maven` should support a scenario where `workspace_refs` is not provided by users

## What are the changes implemented in this PR?

* Default to empty `workspace_refs` if no file is provided
  • Loading branch information
vmax authored Sep 23, 2019
1 parent d4a7864 commit 0065b12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
38 changes: 23 additions & 15 deletions maven/_pom_replace_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,33 @@
# under the License.
#

import argparse
import json
import sys

_, preprocessed_template_path, workspace_refs_path, version_file_path, pom_path = sys.argv
parser = argparse.ArgumentParser()
parser.add_argument('--template_file', type=argparse.FileType('rt'))
parser.add_argument('--version_file', type=argparse.FileType('rt'))
parser.add_argument('--pom_file', type=argparse.FileType('wt'))
parser.add_argument('--workspace_refs', required=False, type=argparse.FileType('rt'))
args = parser.parse_args()

with open(preprocessed_template_path, 'r') as template_file, \
open(workspace_refs_path, 'r') as refs_file, \
open(version_file_path, 'r') as version_file, \
open(pom_path, 'w') as pom_file:
refs = {
'commits': {},
'tags': {},
}

refs = json.loads(refs_file.read().strip())
template = template_file.read().strip()
version = version_file.read().strip()
if args.workspace_refs:
refs = json.loads(args.workspace_refs.read().strip())

pom = template
for workspace in refs['commits']:
pom = pom.replace(workspace, refs['commits'][workspace])
for workspace in refs['tags']:
pom = pom.replace(workspace, refs['tags'][workspace])
pom = pom.replace('{pom_version}', version)
template = args.template_file.read().strip()
version = args.version_file.read().strip()

pom_file.write(pom)
pom = template
for workspace in refs['commits']:
pom = pom.replace(workspace, refs['commits'][workspace])
for workspace in refs['tags']:
pom = pom.replace(workspace, refs['tags'][workspace])
pom = pom.replace('{pom_version}', version)

args.pom_file.write(pom)
16 changes: 13 additions & 3 deletions maven/templates/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,22 @@ def _generate_pom_xml(ctx, maven_coordinates):
else:
version_file = ctx.file.version_file

inputs = [preprocessed_template, version_file]

args = ctx.actions.args()
args.add('--template_file', preprocessed_template.path)
args.add('--version_file', version_file.path)
args.add('--pom_file', pom_file.path)

if ctx.attr.workspace_refs:
inputs.append(ctx.file.workspace_refs)
args.add('--workspace_refs', ctx.file.workspace_refs.path)

# Step 2: fill in {pom_version} from version_file
ctx.actions.run(
inputs = [preprocessed_template, ctx.file.workspace_refs, version_file],
inputs = inputs,
executable = ctx.file._pom_replace_version,
arguments = [preprocessed_template.path, ctx.file.workspace_refs.path, version_file.path, pom_file.path],
arguments = [args],
outputs = [pom_file],
)

Expand Down Expand Up @@ -297,7 +308,6 @@ assemble_maven = rule(
"""
),
"workspace_refs": attr.label(
mandatory = True,
allow_single_file = True,
doc = 'JSON file describing dependencies to other Bazel workspaces'
),
Expand Down

0 comments on commit 0065b12

Please sign in to comment.