Remove duplicated logic in ts bzl files by using copy_select_files

This commit is contained in:
Henrik Giesel 2021-01-01 13:45:25 +01:00
parent eecdc07c68
commit 8e8a3e220b
6 changed files with 64 additions and 70 deletions

View File

@ -26,3 +26,23 @@ def copy_files(ctx, files):
) )
return [DefaultInfo(files = depset(outputs))] return [DefaultInfo(files = depset(outputs))]
def copy_select_files(ctx, files, include, exclude, unwanted_prefix):
wanted = []
for f in files.to_list():
path = f.path
want = True
for substr in exclude:
if substr in path:
want = False
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)

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."
@ -9,17 +9,13 @@ _include = [
_unwanted_prefix = "external/npm/node_modules/css-browser-selector/" _unwanted_prefix = "external/npm/node_modules/css-browser-selector/"
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: _unwanted_prefix,
if substr in path: )
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."
@ -9,17 +9,13 @@ _include = [
_unwanted_prefix = "external/npm/node_modules/jquery-ui-dist/" _unwanted_prefix = "external/npm/node_modules/jquery-ui-dist/"
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: _unwanted_prefix,
if substr in path: )
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."
@ -9,17 +9,13 @@ _include = [
_unwanted_prefix = "external/npm/node_modules/jquery/dist/" _unwanted_prefix = "external/npm/node_modules/jquery/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: _unwanted_prefix,
if substr in path: )
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,20 @@ _include = [
"es5/sre", "es5/sre",
] ]
_exclude = [
"mathmaps_ie.js",
]
_unwanted_prefix = "external/npm/node_modules/mathjax/es5/" _unwanted_prefix = "external/npm/node_modules/mathjax/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: _unwanted_prefix,
want = False )
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."
@ -9,17 +9,13 @@ _include = [
_unwanted_prefix = "external/npm/node_modules/protobufjs/dist/" _unwanted_prefix = "external/npm/node_modules/protobufjs/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: _unwanted_prefix,
if substr in path: )
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,