Skip to content

Commit

Permalink
chore(gpd): export aspect utils for reusability (bazel-contrib#3373)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyDev authored and jacqueline.lee committed Jul 19, 2023
1 parent 8efb85f commit 90e2b48
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions go/tools/gopackagesdriver/aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,38 @@ PROTO_COMPILER_ATTRS = [
"compilers",
]

def _is_file_external(f):
def is_file_external(f):
return f.owner.workspace_root != ""

def _file_path(f):
def file_path(f):
prefix = "__BAZEL_WORKSPACE__"
if not f.is_source:
prefix = "__BAZEL_EXECROOT__"
elif _is_file_external(f):
elif is_file_external(f):
prefix = "__BAZEL_OUTPUT_BASE__"
return paths.join(prefix, f.path)

def _go_archive_to_pkg(archive):
return struct(
ID = str(archive.data.label),
PkgPath = archive.data.importpath,
ExportFile = _file_path(archive.data.export_file),
ExportFile = file_path(archive.data.export_file),
GoFiles = [
_file_path(src)
file_path(src)
for src in archive.data.orig_srcs if src.path.endswith(".go")
],
CompiledGoFiles = [
_file_path(src)
file_path(src)
for src in archive.data.srcs if src.path.endswith(".go")
],
OtherFiles = [
_file_path(src)
file_path(src)
for src in archive.data.orig_srcs if not src.path.endswith(".go")
]
)

def _make_pkg_json(ctx, archive, pkg_info):
pkg_json_file = ctx.actions.declare_file(archive.data.name + ".pkg.json")
def make_pkg_json(ctx, name, pkg_info):
pkg_json_file = ctx.actions.declare_file(name + ".pkg.json")
ctx.actions.write(pkg_json_file, content = pkg_info.to_json())
return pkg_json_file

Expand Down Expand Up @@ -98,14 +98,14 @@ def _go_pkg_info_aspect_impl(target, ctx):
compiled_go_files.extend(archive.source.srcs)
export_files.append(archive.data.export_file)
pkg = _go_archive_to_pkg(archive)
pkg_json_files.append(_make_pkg_json(ctx, archive, pkg))
pkg_json_files.append(make_pkg_json(ctx, archive.data.name, pkg))

if ctx.rule.kind == "go_test":
for dep_archive in archive.direct:
# find the archive containing the test sources
if archive.data.label == dep_archive.data.label:
pkg = _go_archive_to_pkg(dep_archive)
pkg_json_files.append(_make_pkg_json(ctx, dep_archive, pkg))
pkg_json_files.append(make_pkg_json(ctx, dep_archive.data.name, pkg))
compiled_go_files.extend(dep_archive.source.srcs)
export_files.append(dep_archive.data.export_file)
break
Expand Down

0 comments on commit 90e2b48

Please sign in to comment.