Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make workspace_refs optional for assemble_maven #182

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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