Skip to content

Commit

Permalink
refactor: removed unused code in internal/common/module_mappings.bzl (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Mar 26, 2020
1 parent 9ecea34 commit f776854
Showing 1 changed file with 33 additions and 68 deletions.
101 changes: 33 additions & 68 deletions internal/common/module_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ _MODULE_MAPPINGS_DEPS_NAMES = (
["deps", "srcs"]
)

_DEBUG = False
def _debug(vars, *args):
if "VERBOSE_LOGS" in vars.keys():
print("[module_mappings.bzl]", *args)

def _debug(msg, values = ()):
if _DEBUG:
print(msg % values)

def get_module_mappings(label, attrs, srcs = [], workspace_name = None, mappings_attr = "es6_module_mappings"):
def _get_module_mappings(target, ctx):
"""Returns the module_mappings from the given attrs.
Collects a {module_name - module_root} hash from all transitive dependencies,
Expand All @@ -51,92 +49,59 @@ def get_module_mappings(label, attrs, srcs = [], workspace_name = None, mappings
`module_name`.
Args:
label: label
attrs: attributes
srcs: sources (defaults to [])
workspace_name: workspace name (defaults to None)
mappings_attr: mappings attribute to look for (defaults to "es6_module_mappings")
target: target
ctx: ctx
Returns:
The module mappings
"""
mappings = dict()
all_deps = _get_deps(attrs, names = _MODULE_MAPPINGS_DEPS_NAMES)
mappings_attr = "runfiles_module_mappings"
workspace_name = target.label.workspace_name if target.label.workspace_name else ctx.workspace_name
all_deps = _get_deps(ctx.rule.attr, names = _MODULE_MAPPINGS_DEPS_NAMES)
for dep in all_deps:
if not hasattr(dep, mappings_attr):
continue
for k, v in getattr(dep, mappings_attr).items():
if k in mappings and mappings[k] != v:
fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
(label, k, mappings[k], v)), "deps")
(target.label, k, mappings[k], v)), "deps")
mappings[k] = v
if ((hasattr(attrs, "module_name") and attrs.module_name) or
(hasattr(attrs, "module_root") and attrs.module_root)):
mn = attrs.module_name
if not mn:
mn = label.name
mr = label.package
if workspace_name:
mr = "%s/%s" % (workspace_name, mr)
elif label.workspace_root:
mr = "%s/%s" % (label.workspace_root, mr)
if hasattr(attrs, "module_root") and attrs.module_root and attrs.module_root != ".":
if attrs.module_root.endswith(".ts"):
if workspace_name:
# workspace_name is set only when doing module mapping for runtime.
# .d.ts module_root means we should be able to load in two ways:
# module_name -> module_path/module_root.js
# module_name/foo -> module_path/foo
# So we add two mappings. The one with the trailing slash is longer,
# so the loader should prefer it for any deep imports. The mapping
# without the trailing slash will be used only when importing from the
# bare module_name.
mappings[mn + "/"] = mr + "/"
mr = "%s/%s" % (mr, attrs.module_root.replace(".d.ts", ".js"))
else:
# This is the type-checking module mapping. Strip the trailing .d.ts
# as it doesn't belong in TypeScript's path mapping.
mr = "%s/%s" % (mr, attrs.module_root.replace(".d.ts", ""))

if hasattr(ctx.rule.attr, "module_name") and ctx.rule.attr.module_name:
mn = ctx.rule.attr.module_name

# When building a mapping for use at runtime, we need paths to be relative to
# the runfiles directory. This requires the workspace_name to be prefixed on
# each module root.
mr = "/".join([p for p in [workspace_name, target.label.package] if p])
if hasattr(ctx.rule.attr, "module_root") and ctx.rule.attr.module_root and ctx.rule.attr.module_root != ".":
if ctx.rule.attr.module_root.endswith(".ts"):
# Validate that sources are underneath the module root.
# module_roots ending in .ts are a special case, they are used to
# restrict what's exported from a build rule, e.g. only exports from a
# specific index.d.ts file. For those, not every source must be under the
# given module root.

#
# .d.ts module_root means we should be able to load in two ways:
# module_name -> module_path/module_root.js
# module_name/foo -> module_path/foo
# So we add two mappings. The one with the trailing slash is longer,
# so the loader should prefer it for any deep imports. The mapping
# without the trailing slash will be used only when importing from the
# bare module_name.
mappings[mn + "/"] = mr + "/"
mr = "%s/%s" % (mr, ctx.rule.attr.module_root.replace(".d.ts", ".js"))
else:
mr = "%s/%s" % (mr, attrs.module_root)
for s in srcs:
if not s.short_path.startswith(mr):
fail(("all sources must be under module root: %s, but found: %s" %
(mr, s.short_path)))
mr = "%s/%s" % (mr, ctx.rule.attr.module_root)
if mn in mappings and mappings[mn] != mr:
fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
(label, mn, mappings[mn], mr)), "deps")
(target.label, mn, mappings[mn], mr)), "deps")
mappings[mn] = mr
_debug("Mappings at %s: %s", (label, mappings))
_debug(ctx.var, "Mappings at %s: %s" % (target.label, mappings))
return mappings

def _module_mappings_aspect_impl(target, ctx):
mappings = get_module_mappings(target.label, ctx.rule.attr)
return struct(es6_module_mappings = mappings)

module_mappings_aspect = aspect(
_module_mappings_aspect_impl,
attr_aspects = _MODULE_MAPPINGS_DEPS_NAMES,
)

# When building a mapping for use at runtime, we need paths to be relative to
# the runfiles directory. This requires the workspace_name to be prefixed on
# each module root.
def _module_mappings_runtime_aspect_impl(target, ctx):
workspace_name = target.label.workspace_name if target.label.workspace_name else ctx.workspace_name
mappings = get_module_mappings(
target.label,
ctx.rule.attr,
workspace_name = workspace_name,
mappings_attr = "runfiles_module_mappings",
)
mappings = _get_module_mappings(target, ctx)
return struct(runfiles_module_mappings = mappings)

module_mappings_runtime_aspect = aspect(
Expand Down

0 comments on commit f776854

Please sign in to comment.