From 8e8a3e220b6080923d86e23dc8adb52d58c78796 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 1 Jan 2021 13:45:25 +0100 Subject: [PATCH 1/4] Remove duplicated logic in ts bzl files by using copy_select_files --- ts/copy.bzl | 20 ++++++++++++++++++++ ts/css-browser-selector.bzl | 20 ++++++++------------ ts/jquery-ui.bzl | 20 ++++++++------------ ts/jquery.bzl | 20 ++++++++------------ ts/mathjax.bzl | 34 ++++++++++++---------------------- ts/protobufjs.bzl | 20 ++++++++------------ 6 files changed, 64 insertions(+), 70 deletions(-) diff --git a/ts/copy.bzl b/ts/copy.bzl index e62d7a044..2b5302671 100644 --- a/ts/copy.bzl +++ b/ts/copy.bzl @@ -26,3 +26,23 @@ def copy_files(ctx, files): ) 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) diff --git a/ts/css-browser-selector.bzl b/ts/css-browser-selector.bzl index ad2c4578b..d03ee3315 100644 --- a/ts/css-browser-selector.bzl +++ b/ts/css-browser-selector.bzl @@ -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." @@ -9,17 +9,13 @@ _include = [ _unwanted_prefix = "external/npm/node_modules/css-browser-selector/" def _copy_css_browser_selector_impl(ctx): - wanted = [] - for f in ctx.attr.css_browser_selector.files.to_list(): - path = f.path - want = True - - for substr in _include: - if substr in path: - output = path.replace(_unwanted_prefix, "") - wanted.append((f, output)) - - return copy_files(ctx, wanted) + return copy_select_files( + ctx, + ctx.attr.css_browser_selector.files, + _include, + [], + _unwanted_prefix, + ) copy_css_browser_selector = rule( implementation = _copy_css_browser_selector_impl, diff --git a/ts/jquery-ui.bzl b/ts/jquery-ui.bzl index f99e1cef7..482aa6985 100644 --- a/ts/jquery-ui.bzl +++ b/ts/jquery-ui.bzl @@ -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." @@ -9,17 +9,13 @@ _include = [ _unwanted_prefix = "external/npm/node_modules/jquery-ui-dist/" def _copy_jquery_ui_impl(ctx): - wanted = [] - for f in ctx.attr.jquery_ui.files.to_list(): - path = f.path - want = True - - for substr in _include: - if substr in path: - output = path.replace(_unwanted_prefix, "") - wanted.append((f, output)) - - return copy_files(ctx, wanted) + return copy_select_files( + ctx, + ctx.attr.jquery_ui.files, + _include, + [], + _unwanted_prefix, + ) copy_jquery_ui = rule( implementation = _copy_jquery_ui_impl, diff --git a/ts/jquery.bzl b/ts/jquery.bzl index 86025d04a..f7a3542d8 100644 --- a/ts/jquery.bzl +++ b/ts/jquery.bzl @@ -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." @@ -9,17 +9,13 @@ _include = [ _unwanted_prefix = "external/npm/node_modules/jquery/dist/" def _copy_jquery_impl(ctx): - wanted = [] - for f in ctx.attr.jquery.files.to_list(): - path = f.path - want = True - - for substr in _include: - if substr in path: - output = path.replace(_unwanted_prefix, "") - wanted.append((f, output)) - - return copy_files(ctx, wanted) + return copy_select_files( + ctx, + ctx.attr.jquery.files, + _include, + [], + _unwanted_prefix, + ) copy_jquery = rule( implementation = _copy_jquery_impl, diff --git a/ts/mathjax.bzl b/ts/mathjax.bzl index 81f22365c..f50d3cf81 100644 --- a/ts/mathjax.bzl +++ b/ts/mathjax.bzl @@ -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." -_exclude = [ - "mathmaps_ie.js", -] - _include = [ "es5/tex-chtml.js", "es5/input/tex/extensions", @@ -14,26 +10,20 @@ _include = [ "es5/sre", ] +_exclude = [ + "mathmaps_ie.js", +] + _unwanted_prefix = "external/npm/node_modules/mathjax/es5/" def _copy_mathjax_impl(ctx): - wanted = [] - for f in ctx.attr.mathjax.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) + return copy_select_files( + ctx, + ctx.attr.mathjax.files, + _include, + _exclude, + _unwanted_prefix, + ) copy_mathjax = rule( implementation = _copy_mathjax_impl, diff --git a/ts/protobufjs.bzl b/ts/protobufjs.bzl index 218d2ac65..80a26f26c 100644 --- a/ts/protobufjs.bzl +++ b/ts/protobufjs.bzl @@ -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." @@ -9,17 +9,13 @@ _include = [ _unwanted_prefix = "external/npm/node_modules/protobufjs/dist/" def _copy_protobufjs_impl(ctx): - wanted = [] - for f in ctx.attr.protobufjs.files.to_list(): - path = f.path - want = True - - for substr in _include: - if substr in path: - output = path.replace(_unwanted_prefix, "") - wanted.append((f, output)) - - return copy_files(ctx, wanted) + return copy_select_files( + ctx, + ctx.attr.protobufjs.files, + _include, + [], + _unwanted_prefix, + ) copy_protobufjs = rule( implementation = _copy_protobufjs_impl, From 450ff60815750e4b58cd22a7b049bfdc28ef7213 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 1 Jan 2021 14:14:50 +0100 Subject: [PATCH 2/4] Fix input type for _drawFlag --- qt/aqt/data/web/js/reviewer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index 7827a2177..0ba011729 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -120,7 +120,7 @@ const _flagColours = { 4: "#77aaff", }; -function _drawFlag(flag: 0 | 1 | 2 | 3): void { +function _drawFlag(flag: 0 | 1 | 2 | 3 | 4): void { var elem = $("#_flag"); if (flag === 0) { elem.hide(); From 12dfb386092c0362c9c43b283fa2dfd3797dfffa Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 1 Jan 2021 14:16:52 +0100 Subject: [PATCH 3/4] Be a bit more precise when excluding mathmaps_ie.js * for documentation purposes --- ts/mathjax.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/mathjax.bzl b/ts/mathjax.bzl index f50d3cf81..fcfd8d517 100644 --- a/ts/mathjax.bzl +++ b/ts/mathjax.bzl @@ -11,7 +11,7 @@ _include = [ ] _exclude = [ - "mathmaps_ie.js", + "es5/sre/mathmaps/mathmaps_ie.js", ] _unwanted_prefix = "external/npm/node_modules/mathjax/es5/" From f97ad260bb655e196bb27dc6f33fef536f592187 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 1 Jan 2021 14:39:15 +0100 Subject: [PATCH 4/4] Add base argument to copy_select_files --- ts/copy.bzl | 15 ++++++++++----- ts/css-browser-selector.bzl | 4 +++- ts/jquery-ui.bzl | 4 +++- ts/jquery.bzl | 4 +++- ts/mathjax.bzl | 4 +++- ts/protobufjs.bzl | 4 +++- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ts/copy.bzl b/ts/copy.bzl index 2b5302671..525814259 100644 --- a/ts/copy.bzl +++ b/ts/copy.bzl @@ -27,22 +27,27 @@ def copy_files(ctx, files): return [DefaultInfo(files = depset(outputs))] -def copy_select_files(ctx, files, include, exclude, unwanted_prefix): +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 = f.path + path = remove_prefix(f.path, base) want = True for substr in exclude: - if substr in path: + if path.startswith(substr): want = False continue if not want: continue for substr in include: - if substr in path: - output = path.replace(unwanted_prefix, "") + if path.startswith(substr): + output = remove_prefix(path, unwanted_prefix) wanted.append((f, output)) return copy_files(ctx, wanted) diff --git a/ts/css-browser-selector.bzl b/ts/css-browser-selector.bzl index d03ee3315..db235118d 100644 --- a/ts/css-browser-selector.bzl +++ b/ts/css-browser-selector.bzl @@ -6,7 +6,8 @@ _include = [ "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): return copy_select_files( @@ -14,6 +15,7 @@ def _copy_css_browser_selector_impl(ctx): ctx.attr.css_browser_selector.files, _include, [], + _base, _unwanted_prefix, ) diff --git a/ts/jquery-ui.bzl b/ts/jquery-ui.bzl index 482aa6985..baf547270 100644 --- a/ts/jquery-ui.bzl +++ b/ts/jquery-ui.bzl @@ -6,7 +6,8 @@ _include = [ "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): return copy_select_files( @@ -14,6 +15,7 @@ def _copy_jquery_ui_impl(ctx): ctx.attr.jquery_ui.files, _include, [], + _base, _unwanted_prefix, ) diff --git a/ts/jquery.bzl b/ts/jquery.bzl index f7a3542d8..b7f1c4a84 100644 --- a/ts/jquery.bzl +++ b/ts/jquery.bzl @@ -6,7 +6,8 @@ _include = [ "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): return copy_select_files( @@ -14,6 +15,7 @@ def _copy_jquery_impl(ctx): ctx.attr.jquery.files, _include, [], + _base, _unwanted_prefix, ) diff --git a/ts/mathjax.bzl b/ts/mathjax.bzl index fcfd8d517..480e1f6e3 100644 --- a/ts/mathjax.bzl +++ b/ts/mathjax.bzl @@ -14,7 +14,8 @@ _exclude = [ "es5/sre/mathmaps/mathmaps_ie.js", ] -_unwanted_prefix = "external/npm/node_modules/mathjax/es5/" +_base = "external/npm/node_modules/mathjax/" +_unwanted_prefix = "es5/" def _copy_mathjax_impl(ctx): return copy_select_files( @@ -22,6 +23,7 @@ def _copy_mathjax_impl(ctx): ctx.attr.mathjax.files, _include, _exclude, + _base, _unwanted_prefix, ) diff --git a/ts/protobufjs.bzl b/ts/protobufjs.bzl index 80a26f26c..25c947251 100644 --- a/ts/protobufjs.bzl +++ b/ts/protobufjs.bzl @@ -6,7 +6,8 @@ _include = [ "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): return copy_select_files( @@ -14,6 +15,7 @@ def _copy_protobufjs_impl(ctx): ctx.attr.protobufjs.files, _include, [], + _base, _unwanted_prefix, )