diff options
author | khoden <khoden@yandex-team.com> | 2023-09-08 15:29:00 +0300 |
---|---|---|
committer | khoden <khoden@yandex-team.com> | 2023-09-08 15:56:50 +0300 |
commit | 1327369eb8af90ffcedd1ea4f256c374c52097e1 (patch) | |
tree | a9c6154771777732ad4d1e568ca0de9b38dfab12 /build | |
parent | 2a4344f50e5d190be68407dca1cada777cb04c45 (diff) | |
download | ydb-1327369eb8af90ffcedd1ea4f256c374c52097e1.tar.gz |
Изменить логику дискаверинга исходников для TS_*
### Принцип работы:
1. В переменную с помощью `_GLOB` записываем все файлы (*.ts *.tsx *.js *.jsx *.json) проекта, кроме дефолтного списка исключений (типа `node_modules`, `dist/build`, `a.yaml`, `ya.make`, `package.json`, `pnpm-lock.yaml` и подобные)
2. Вызываем плагин (`_TS_CONFIGURE`), где удаляем из этого списка файлы, не покрываемые `tsconfig.json`
3. Получившееся значение используем в качестве источника инпутов для таргета
Diffstat (limited to 'build')
-rw-r--r-- | build/conf/ts/ts.conf | 34 | ||||
-rw-r--r-- | build/conf/ts/ts_bundle.conf | 18 | ||||
-rw-r--r-- | build/conf/ts/ts_library.conf | 16 | ||||
-rw-r--r-- | build/conf/ts/ts_next.conf | 23 | ||||
-rw-r--r-- | build/conf/ts/ts_vite_bundle.conf | 18 | ||||
-rw-r--r-- | build/plugins/lib/nots/typescript/tests/test_ts_glob.py | 75 | ||||
-rw-r--r-- | build/plugins/lib/nots/typescript/tests/ya.make | 1 | ||||
-rw-r--r-- | build/plugins/lib/nots/typescript/ts_config.py | 23 | ||||
-rw-r--r-- | build/plugins/lib/nots/typescript/ts_glob.py | 77 | ||||
-rw-r--r-- | build/plugins/lib/nots/typescript/ya.make | 1 | ||||
-rw-r--r-- | build/plugins/nots.py | 23 |
11 files changed, 278 insertions, 31 deletions
diff --git a/build/conf/ts/ts.conf b/build/conf/ts/ts.conf index 2812035788..dbf05d48b0 100644 --- a/build/conf/ts/ts.conf +++ b/build/conf/ts/ts.conf @@ -6,6 +6,8 @@ NOTS_TOOL_BASE_ARGS=--build-root $ARCADIA_BUILD_ROOT --bindir $BINDIR --curdir $ NOTS_TOOL_NODE_MODULES_BUNDLE=$BINDIR/node_modules.tar ERM_PACKAGES_PATH=devtools/frontend_build_platform/erm/erm-packages.json +TS_CONFIG_PATH=tsconfig.json + module _TS_BASE_UNIT: _BARE_UNIT { # Propagates peers to related modules .PEERDIR_POLICY=as_build_from @@ -44,6 +46,38 @@ macro _TS_CONFIG_EPILOGUE() { _SETUP_EXTRACT_NODE_MODULES_RECIPE(${MODDIR}) } + +# List of the inputs, filled in _TS_GLOB. Will be reduced in _TS_CONFIGURE macro. +# Used as inputs in TS_COMPILE through `$_TS_GLOB_AS_INPUTS(IN $TS_GLOB_FILES)` +TS_GLOB_FILES= + +# Hardcoded "include" list (all other files will be ignored) +TS_GLOB_INCLUDE=**/* + +# Hardcoded "exclude" list (reasonable default). +TS_GLOB_EXCLUDE=$TS_CONFIG_PATH \ + ya.make a.yaml \ + (.code|.idea)/**/* \ + (build|dist|bundle|.*|$TS_NEXT_OUTPUT_DIR)/**/* \ + node_modules/**/* package.json pnpm-lock.yaml .* \ + tests/**/* **/*.(test|spec).(ts|tsx|js|jsx) + + +### _TS_GLOB() # internal +### +### Fill $TS_GLOB_FILES with potential inputs. +### It will be reduced later in _TS_CONFIGURE based on `tsconfig.json` rules. +### So it is important to call _TS_CONFIGURE() with _TS_GLOB()! +macro _TS_GLOB() { + _GLOB(TS_GLOB_FILES $TS_GLOB_INCLUDE EXCLUDE $TS_GLOB_EXCLUDE) +} + +# Ugly hack for using inputs from the variable +macro _TS_GLOB_AS_INPUTS(IN{input}[]) { + .CMD=${input;hide:IN} +} + + _TS_FILES_COPY_CMD= ### TS_FILES(Files...) diff --git a/build/conf/ts/ts_bundle.conf b/build/conf/ts/ts_bundle.conf index 370e5be750..0c455cff20 100644 --- a/build/conf/ts/ts_bundle.conf +++ b/build/conf/ts/ts_bundle.conf @@ -6,12 +6,17 @@ WEBPACK_CONFIG_PATH=webpack.config.js TS_BUNDLE_WEBPACK=$TOUCH_UNIT \ && $_TS_FILES_COPY_CMD \ && $ADD_VCS_INFO_FILE_CMD \ - && ${cwd:BINDIR} $NOTS_TOOL bundle-webpack $NOTS_TOOL_BASE_ARGS --webpack-resource $WEBPACK_ROOT \ - --moddir $MODDIR --webpack-config ${input:WEBPACK_CONFIG_PATH} --ts-config ${input:TS_CONFIG_PATH} \ - --vcs-info "${VCS_INFO_FILE}" --output-dir ${WEBPACK_OUTPUT_DIR} \ - --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ - ${input;hide:"./package.json"} ${TS_CONFIG_FILES} ${output;hide:"output.tar"} \ - ${output;hide:"package.json"} ${kv;hide:"p TSWP"} ${kv;hide:"pc magenta"} + && ${cwd:BINDIR} $NOTS_TOOL bundle-webpack $NOTS_TOOL_BASE_ARGS \ + --moddir $MODDIR \ + --ts-config ${input:TS_CONFIG_PATH} \ + --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ + --webpack-resource $WEBPACK_ROOT \ + --webpack-config ${input:WEBPACK_CONFIG_PATH} \ + --vcs-info "${VCS_INFO_FILE}" \ + --output-dir ${WEBPACK_OUTPUT_DIR} \ + ${input;hide:"package.json"} ${TS_CONFIG_FILES} $_TS_GLOB_AS_INPUTS(IN $TS_GLOB_FILES) \ + ${output;hide:"package.json"} ${output;hide:"output.tar"} \ + ${kv;hide:"p TSWP"} ${kv;hide:"pc magenta"} ### @usage: WEBPACK_OUTPUT(DirName) ### @@ -48,6 +53,7 @@ multimodule TS_BUNDLE { SET_APPEND(_MAKEFILE_INCLUDE_LIKE_DEPS ${CURDIR}/${TS_CONFIG_PATH} ${CURDIR}/package.json) SET(TS_CONFIG_DEDUCE_OUT no) + _TS_GLOB() _TS_CONFIGURE($TS_CONFIG_PATH) # we should set NODE_MODULES_BUNDLE_AS_OUTPUT conditionally, diff --git a/build/conf/ts/ts_library.conf b/build/conf/ts/ts_library.conf index d5675daf29..ed3a59fe96 100644 --- a/build/conf/ts/ts_library.conf +++ b/build/conf/ts/ts_library.conf @@ -1,12 +1,16 @@ # TYPESCRIPT_ROOT is defined by _PEERDIR_TS_RESOURCE(typescript) TYPESCRIPT_ROOT= -TS_CONFIG_PATH=tsconfig.json + TS_COMPILE=$TOUCH_UNIT \ && $_TS_FILES_COPY_CMD \ - && ${cwd:BINDIR} $NOTS_TOOL compile-ts $NOTS_TOOL_BASE_ARGS --tsc-resource $TYPESCRIPT_ROOT \ - --moddir $MODDIR --ts-config ${input:TS_CONFIG_PATH} --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE \ - $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} ${input;hide:"./package.json"} ${TS_CONFIG_FILES} \ - ${output;hide:"package.json"} ${output;hide:"output.tar"} ${kv;hide:"p TSC"} ${kv;hide:"pc magenta"} + && ${cwd:BINDIR} $NOTS_TOOL compile-ts $NOTS_TOOL_BASE_ARGS \ + --moddir $MODDIR \ + --ts-config ${input:TS_CONFIG_PATH} \ + --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ + --tsc-resource $TYPESCRIPT_ROOT \ + ${input;hide:"package.json"} ${TS_CONFIG_FILES} $_TS_GLOB_AS_INPUTS(IN $TS_GLOB_FILES) \ + ${output;hide:"package.json"} ${output;hide:"output.tar"} \ + ${kv;hide:"p TSC"} ${kv;hide:"pc magenta"} ### @usage: TS_LIBRARY([name]) ### @@ -40,6 +44,8 @@ multimodule TS_LIBRARY { # based on whether module has deps or doesn't have _SET_NODE_MODULES_BUNDLE_AS_OUTPUT() + _TS_GLOB() + _TS_CONFIGURE($TS_CONFIG_PATH) } diff --git a/build/conf/ts/ts_next.conf b/build/conf/ts/ts_next.conf index c9158d773c..bc66e176dc 100644 --- a/build/conf/ts/ts_next.conf +++ b/build/conf/ts/ts_next.conf @@ -2,18 +2,21 @@ NEXT_ROOT= TS_NEXT_OUTPUT_DIR=.next TS_NEXT_CONFIG_PATH=next.config.js -TS_NEXT_SRC_FILES= TS_NEXT_CMD=$TOUCH_UNIT \ && $_TS_FILES_COPY_CMD \ && $ADD_VCS_INFO_FILE_CMD \ && ${cwd:BINDIR} $NOTS_TOOL build-nextjs $NOTS_TOOL_BASE_ARGS \ - --moddir $MODDIR --nextjs-resource $NEXT_ROOT \ - --nextjs-config ${input:TS_NEXT_CONFIG_PATH} --ts-config ${input:TS_CONFIG_PATH} \ - --vcs-info "${VCS_INFO_FILE}" --output-dir ${TS_NEXT_OUTPUT_DIR} \ + --moddir $MODDIR \ + --ts-config ${input:TS_CONFIG_PATH} \ --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ - ${input;hide:"./package.json"} ${TS_CONFIG_FILES} ${output;hide:"output.tar"} \ - ${output;hide:"package.json"} ${kv;hide:"p TSNEXT"} ${kv;hide:"pc magenta"} + --nextjs-resource $NEXT_ROOT \ + --nextjs-config ${input:TS_NEXT_CONFIG_PATH} \ + --vcs-info "${VCS_INFO_FILE}" \ + --output-dir ${TS_NEXT_OUTPUT_DIR} \ + ${input;hide:"package.json"} ${TS_CONFIG_FILES} $_TS_GLOB_AS_INPUTS(IN $TS_GLOB_FILES) \ + ${output;hide:"output.tar"} ${output;hide:"package.json"} \ + ${kv;hide:"p TSNEXT"} ${kv;hide:"pc magenta"} ### @usage: TS_NEXT() ### @@ -31,7 +34,7 @@ multimodule TS_NEXT { .CMD=TS_NEXT_CMD .PEERDIRSELF=NODE_MODULES # epilogue is not inherited from TS_LIBRARY - .EPILOGUE=_TS_NEXT_EPILOG + .EPILOGUE=_TS_CONFIG_EPILOGUE # by default multimodule overrides inherited MODULE_TAG to submodule name (BUILD in this case) # but we have to set it to TS for include processor to work @@ -41,6 +44,7 @@ multimodule TS_NEXT { SET_APPEND(_MAKEFILE_INCLUDE_LIKE_DEPS ${CURDIR}/${TS_CONFIG_PATH} ${CURDIR}/package.json) SET(TS_CONFIG_DEDUCE_OUT no) + _TS_GLOB() _TS_CONFIGURE($TS_CONFIG_PATH) # we should set NODE_MODULES_BUNDLE_AS_OUTPUT conditionally, @@ -56,11 +60,6 @@ macro TS_NEXT_CONFIG(Path) { SET(TS_NEXT_CONFIG_PATH $Path) } -macro _TS_NEXT_EPILOG() { - _TS_CONFIG_EPILOGUE() - _GLOB(TS_NEXT_SRC_FILES ${CURDIR}/src/pages/**/* ${CURDIR}/pages/**/* ${CURDIR}/src/app/**/* ${CURDIR}/app/**/* ${CURDIR}/public/**/*) - SRCS($TS_NEXT_SRC_FILES) -} ### @usage: TS_NEXT_OUTPUT(DirName) ### diff --git a/build/conf/ts/ts_vite_bundle.conf b/build/conf/ts/ts_vite_bundle.conf index 74feb6cfd6..4c6590b687 100644 --- a/build/conf/ts/ts_vite_bundle.conf +++ b/build/conf/ts/ts_vite_bundle.conf @@ -6,12 +6,17 @@ VITE_CONFIG_PATH=vite.config.ts TS_BUNDLE_VITE=$TOUCH_UNIT \ && $_TS_FILES_COPY_CMD \ && $ADD_VCS_INFO_FILE_CMD \ - && ${cwd:BINDIR} $NOTS_TOOL bundle-vite $NOTS_TOOL_BASE_ARGS --vite-resource $VITE_ROOT \ - --moddir $MODDIR --vite-config ${input:VITE_CONFIG_PATH} --ts-config ${input:TS_CONFIG_PATH} \ - --vcs-info "${VCS_INFO_FILE}" --output-dir ${VITE_OUTPUT_DIR} \ - --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ - ${input;hide:"./package.json"} ${TS_CONFIG_FILES} ${output;hide:"output.tar"} \ - ${output;hide:"package.json"} ${kv;hide:"p VB"} ${kv;hide:"pc magenta"} + && ${cwd:BINDIR} $NOTS_TOOL bundle-vite $NOTS_TOOL_BASE_ARGS \ + --moddir $MODDIR \ + --ts-config ${input:TS_CONFIG_PATH} \ + --node-modules-bundle $NOTS_TOOL_NODE_MODULES_BUNDLE $NODE_MODULES_BUNDLE_AS_OUTPUT ${hide:PEERS} \ + --vite-resource $VITE_ROOT \ + --vite-config ${input:VITE_CONFIG_PATH} \ + --vcs-info "${VCS_INFO_FILE}" \ + --output-dir ${VITE_OUTPUT_DIR} \ + ${input;hide:"package.json"} ${TS_CONFIG_FILES} $_TS_GLOB_AS_INPUTS(IN $TS_GLOB_FILES) \ + ${output;hide:"package.json"} ${output;hide:"output.tar"} \ + ${kv;hide:"p TSVB"} ${kv;hide:"pc magenta"} ### @usage: VITE_OUTPUT(DirName) ### @@ -48,6 +53,7 @@ multimodule TS_VITE_BUNDLE { SET_APPEND(_MAKEFILE_INCLUDE_LIKE_DEPS ${CURDIR}/${TS_CONFIG_PATH} ${CURDIR}/package.json) SET(TS_CONFIG_DEDUCE_OUT no) + _TS_GLOB() _TS_CONFIGURE($TS_CONFIG_PATH) # we should set NODE_MODULES_BUNDLE_AS_OUTPUT conditionally, diff --git a/build/plugins/lib/nots/typescript/tests/test_ts_glob.py b/build/plugins/lib/nots/typescript/tests/test_ts_glob.py new file mode 100644 index 0000000000..9d38a8d80f --- /dev/null +++ b/build/plugins/lib/nots/typescript/tests/test_ts_glob.py @@ -0,0 +1,75 @@ +from build.plugins.lib.nots.typescript.ts_glob import ts_glob, TsGlobConfig + + +class TestTsGlobIncluding: + ts_glob_config = TsGlobConfig( + root_dir="src", out_dir="build", include=["src/module_a/**/*", "src/module_b/**/*", "src/module_x"] + ) + + def test_dir_include(self): + # arrange + all_files = [ + "src/module_x/index.ts", + ] + + # act + arrange + assert ts_glob(self.ts_glob_config, all_files) == [ + "src/module_x/index.ts", + ] + + def test_deep_include(self): + # arrange + all_files = [ + "src/module_a/index.ts", + "src/module_b/index.ts", + "src/module_c/index.ts", + ] + + # act + arrange + assert ts_glob(self.ts_glob_config, all_files) == [ + "src/module_a/index.ts", + "src/module_b/index.ts", + ] + + +class TestTsGlobExcluding: + ts_glob_config = TsGlobConfig(root_dir="src", out_dir="build", include=["src/**/*"]) + + def test_only_in_root_dir(self): + # arrange + all_files = [ + "CHANGELOG.md", + "fake-src/one-more-src.ts", + "src/index.ts", + ] + + # act + assert + assert ts_glob(self.ts_glob_config, all_files) == ["src/index.ts"] + + def test_exclude_out_dir(self): + # arrange + all_files = ["build/index.js"] + + # act + assert + assert ts_glob(self.ts_glob_config, all_files) == [] + + def test_exclude_out_dir_none(self): + # arrange + all_files = ["build/index.js"] + ts_glob_config = TsGlobConfig(root_dir=".", out_dir=None) + + # act + assert + assert ts_glob(ts_glob_config, all_files) == ["build/index.js"] + + def test_complex(self): + # arrange + all_files = [ + "CHANGELOG.md", + "fake-src/one-more-src.ts", + "src/baz.ts", + "src/index.ts", + "src/required_file.ts", + ] + + # act + assert + assert ts_glob(self.ts_glob_config, all_files) == ["src/baz.ts", "src/index.ts", "src/required_file.ts"] diff --git a/build/plugins/lib/nots/typescript/tests/ya.make b/build/plugins/lib/nots/typescript/tests/ya.make index 44798138bc..2e038f6c96 100644 --- a/build/plugins/lib/nots/typescript/tests/ya.make +++ b/build/plugins/lib/nots/typescript/tests/ya.make @@ -4,6 +4,7 @@ OWNER(g:frontend-build-platform) TEST_SRCS( ts_config.py + test_ts_glob.py ) PEERDIR( diff --git a/build/plugins/lib/nots/typescript/ts_config.py b/build/plugins/lib/nots/typescript/ts_config.py index c54121a9d1..b4ad9c3d3f 100644 --- a/build/plugins/lib/nots/typescript/ts_config.py +++ b/build/plugins/lib/nots/typescript/ts_config.py @@ -1,9 +1,9 @@ import copy -import os import json +import os from .ts_errors import TsError, TsValidationError - +from .ts_glob import ts_glob, TsGlobConfig from ..package_manager.base import utils DEFAULT_TS_CONFIG_FILE = "tsconfig.json" @@ -249,3 +249,22 @@ class TsConfig(object): with open(path, "w") as f: json.dump(self.data, f, indent=indent) + + def filter_files(self, all_files): + # type: (list[str]) -> list[str] + """ + Filters all the files by the rules from this tsconig.json. The result will be used as input entries in `ya make`. + + Known limits: + + - `files` nots supported, use `include` (see `self.validate()`) + - `exclude` not implemented, because `tsc` still uses "excluded" files as declaration files (for typing and referencing) + """ + + ts_glob_config = TsGlobConfig( + root_dir=self.compiler_option("rootDir"), + out_dir=self.compiler_option("outDir"), + include=self.data.get("include"), + ) + + return ts_glob(ts_glob_config, all_files) diff --git a/build/plugins/lib/nots/typescript/ts_glob.py b/build/plugins/lib/nots/typescript/ts_glob.py new file mode 100644 index 0000000000..b81e8774a5 --- /dev/null +++ b/build/plugins/lib/nots/typescript/ts_glob.py @@ -0,0 +1,77 @@ +import fnmatch +import os.path + + +class TsGlobConfig: + def __init__(self, root_dir, out_dir=None, include=None): + # type: (TsGlobConfig, str, str, list[str], list[str]) -> None + + self.root_dir = root_dir # Required + self.out_dir = out_dir + + self.include = include or ["**/*"] + + +def __path_to_match_rule(path): + # type: (str) -> str + + # already a rule + + # convert "**/*" to "*" (python compatible with fnmatch) + if path.endswith('**/*'): + return path[:-3] # /**/* -> /* + + if path.endswith("*") or ('*' in path or '?' in path): + return path + + # special cases + if path == ".": + return "*" + + # filename + root, ext = os.path.splitext(path) + if ext: + return path + + # dirname ? + return os.path.join(path, '*') + + +def __filter_files(files, path_or_rule): + # type: (set[str], str) -> set[str] + + rule = __path_to_match_rule(path_or_rule) + + result = set() + for path in files: + py_rule = __path_to_match_rule(rule) + if path == rule or fnmatch.fnmatch(path, py_rule): + result.add(path) + + return result + + +def ts_glob(glob_config, all_files): + # type: (TsGlobConfig, list[str]) -> list[str] + + result = set(all_files) + + # only in `root_dir` + result &= __filter_files(result, glob_config.root_dir) + + # only listed by `include` option + include_only = set() + for include_path in glob_config.include: + include_only |= __filter_files(result, include_path) + + result &= include_only # keep only intersection (common in both sets) + + skip_files = set() + + # exclude out_dir + if glob_config.out_dir: + skip_files |= __filter_files(result, glob_config.out_dir) + + result -= skip_files # keep only differences (the elements in `result` that not exist in `skip_files`) + + return sorted(result) diff --git a/build/plugins/lib/nots/typescript/ya.make b/build/plugins/lib/nots/typescript/ya.make index 8847f9bbd3..ac31196472 100644 --- a/build/plugins/lib/nots/typescript/ya.make +++ b/build/plugins/lib/nots/typescript/ya.make @@ -5,6 +5,7 @@ OWNER(g:frontend-build-platform) PY_SRCS( __init__.py ts_errors.py + ts_glob.py ts_config.py ) diff --git a/build/plugins/nots.py b/build/plugins/nots.py index 740b042376..f6363dfd6f 100644 --- a/build/plugins/nots.py +++ b/build/plugins/nots.py @@ -200,6 +200,29 @@ def on_ts_configure(unit, tsconfig_path): unit.set(["TS_CONFIG_PRESERVE_JSX", to_yesno(tsconfig.compiler_option("jsx") == "preserve")]) _setup_eslint(unit) + _filter_inputs_by_rules_from_tsconfig(unit, tsconfig) + + +def __strip_prefix(prefix, line): + # type: (str, str) -> str + if line.startswith(prefix): + prefix_len = len(prefix) + return line[prefix_len:] + + return line + + +def _filter_inputs_by_rules_from_tsconfig(unit, tsconfig): + """ + Reduce file list from the TS_GLOB_FILES variable following tsconfig.json rules + """ + mod_dir = unit.get("MODDIR") + target_path = "${ARCADIA_ROOT}/" + mod_dir + "/" + + all_files = [__strip_prefix(target_path, f) for f in unit.get("TS_GLOB_FILES").split(" ")] + filtered_files = tsconfig.filter_files(all_files) + + unit.set(["TS_GLOB_FILES", ' '.join([target_path + f for f in filtered_files])]) def _get_ts_test_data_dirs(unit): |