Merge pull request #882 from hgiesel/copyfiles

Implement copy_select_files for removing duplication in js dep Bazel recipes
This commit is contained in:
Damien Elmes 2021-01-02 10:22:48 +10:00 committed by GitHub
commit 13994973cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 76 deletions

View file

@ -120,7 +120,7 @@ const _flagColours = {
4: "#77aaff", 4: "#77aaff",
}; };
function _drawFlag(flag: 0 | 1 | 2 | 3): void { function _drawFlag(flag: 0 | 1 | 2 | 3 | 4): void {
var elem = $("#_flag"); var elem = $("#_flag");
if (flag === 0) { if (flag === 0) {
elem.hide(); elem.hide();

View file

@ -26,3 +26,28 @@ def copy_files(ctx, files):
) )
return [DefaultInfo(files = depset(outputs))] return [DefaultInfo(files = depset(outputs))]
def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text
def copy_select_files(ctx, files, include, exclude, base, unwanted_prefix):
wanted = []
for f in files.to_list():
path = remove_prefix(f.path, base)
want = True
for substr in exclude:
if path.startswith(substr):
want = False
continue
if not want:
continue
for substr in include:
if path.startswith(substr):
output = remove_prefix(path, unwanted_prefix)
wanted.append((f, output))
return copy_files(ctx, wanted)

View file

@ -1,4 +1,4 @@
load("//ts:copy.bzl", "copy_files") load("//ts:copy.bzl", "copy_select_files")
"Rule to copy css-browser-selector subset from node_modules to vendor folder." "Rule to copy css-browser-selector subset from node_modules to vendor folder."
@ -6,20 +6,18 @@ _include = [
"css_browser_selector.min.js", "css_browser_selector.min.js",
] ]
_unwanted_prefix = "external/npm/node_modules/css-browser-selector/" _base = "external/npm/node_modules/css-browser-selector/"
_unwanted_prefix = ""
def _copy_css_browser_selector_impl(ctx): def _copy_css_browser_selector_impl(ctx):
wanted = [] return copy_select_files(
for f in ctx.attr.css_browser_selector.files.to_list(): ctx,
path = f.path ctx.attr.css_browser_selector.files,
want = True _include,
[],
for substr in _include: _base,
if substr in path: _unwanted_prefix,
output = path.replace(_unwanted_prefix, "") )
wanted.append((f, output))
return copy_files(ctx, wanted)
copy_css_browser_selector = rule( copy_css_browser_selector = rule(
implementation = _copy_css_browser_selector_impl, implementation = _copy_css_browser_selector_impl,

View file

@ -1,4 +1,4 @@
load("//ts:copy.bzl", "copy_files") load("//ts:copy.bzl", "copy_select_files")
"Rule to copy jquery-ui subset from node_modules to vendor folder." "Rule to copy jquery-ui subset from node_modules to vendor folder."
@ -6,20 +6,18 @@ _include = [
"jquery-ui.min.js", "jquery-ui.min.js",
] ]
_unwanted_prefix = "external/npm/node_modules/jquery-ui-dist/" _base = "external/npm/node_modules/jquery-ui-dist/"
_unwanted_prefix = ""
def _copy_jquery_ui_impl(ctx): def _copy_jquery_ui_impl(ctx):
wanted = [] return copy_select_files(
for f in ctx.attr.jquery_ui.files.to_list(): ctx,
path = f.path ctx.attr.jquery_ui.files,
want = True _include,
[],
for substr in _include: _base,
if substr in path: _unwanted_prefix,
output = path.replace(_unwanted_prefix, "") )
wanted.append((f, output))
return copy_files(ctx, wanted)
copy_jquery_ui = rule( copy_jquery_ui = rule(
implementation = _copy_jquery_ui_impl, implementation = _copy_jquery_ui_impl,

View file

@ -1,4 +1,4 @@
load("//ts:copy.bzl", "copy_files") load("//ts:copy.bzl", "copy_select_files")
"Rule to copy jquery subset from node_modules to vendor folder." "Rule to copy jquery subset from node_modules to vendor folder."
@ -6,20 +6,18 @@ _include = [
"dist/jquery.min.js" "dist/jquery.min.js"
] ]
_unwanted_prefix = "external/npm/node_modules/jquery/dist/" _base = "external/npm/node_modules/jquery/"
_unwanted_prefix = "dist/"
def _copy_jquery_impl(ctx): def _copy_jquery_impl(ctx):
wanted = [] return copy_select_files(
for f in ctx.attr.jquery.files.to_list(): ctx,
path = f.path ctx.attr.jquery.files,
want = True _include,
[],
for substr in _include: _base,
if substr in path: _unwanted_prefix,
output = path.replace(_unwanted_prefix, "") )
wanted.append((f, output))
return copy_files(ctx, wanted)
copy_jquery = rule( copy_jquery = rule(
implementation = _copy_jquery_impl, implementation = _copy_jquery_impl,

View file

@ -1,11 +1,7 @@
load("//ts:copy.bzl", "copy_files") load("//ts:copy.bzl", "copy_select_files")
"Rule to copy mathjax subset from node_modules to vendor folder." "Rule to copy mathjax subset from node_modules to vendor folder."
_exclude = [
"mathmaps_ie.js",
]
_include = [ _include = [
"es5/tex-chtml.js", "es5/tex-chtml.js",
"es5/input/tex/extensions", "es5/input/tex/extensions",
@ -14,26 +10,22 @@ _include = [
"es5/sre", "es5/sre",
] ]
_unwanted_prefix = "external/npm/node_modules/mathjax/es5/" _exclude = [
"es5/sre/mathmaps/mathmaps_ie.js",
]
_base = "external/npm/node_modules/mathjax/"
_unwanted_prefix = "es5/"
def _copy_mathjax_impl(ctx): def _copy_mathjax_impl(ctx):
wanted = [] return copy_select_files(
for f in ctx.attr.mathjax.files.to_list(): ctx,
path = f.path ctx.attr.mathjax.files,
want = True _include,
for substr in _exclude: _exclude,
if substr in path: _base,
want = False _unwanted_prefix,
continue )
if not want:
continue
for substr in _include:
if substr in path:
output = path.replace(_unwanted_prefix, "")
wanted.append((f, output))
return copy_files(ctx, wanted)
copy_mathjax = rule( copy_mathjax = rule(
implementation = _copy_mathjax_impl, implementation = _copy_mathjax_impl,

View file

@ -1,4 +1,4 @@
load("//ts:copy.bzl", "copy_files") load("//ts:copy.bzl", "copy_select_files")
"Rule to copy protobufjs subset from node_modules to vendor folder." "Rule to copy protobufjs subset from node_modules to vendor folder."
@ -6,20 +6,18 @@ _include = [
"dist/protobuf.min.js", "dist/protobuf.min.js",
] ]
_unwanted_prefix = "external/npm/node_modules/protobufjs/dist/" _base = "external/npm/node_modules/protobufjs/"
_unwanted_prefix = "dist/"
def _copy_protobufjs_impl(ctx): def _copy_protobufjs_impl(ctx):
wanted = [] return copy_select_files(
for f in ctx.attr.protobufjs.files.to_list(): ctx,
path = f.path ctx.attr.protobufjs.files,
want = True _include,
[],
for substr in _include: _base,
if substr in path: _unwanted_prefix,
output = path.replace(_unwanted_prefix, "") )
wanted.append((f, output))
return copy_files(ctx, wanted)
copy_protobufjs = rule( copy_protobufjs = rule(
implementation = _copy_protobufjs_impl, implementation = _copy_protobufjs_impl,