Skip to content

Commit

Permalink
chore: refine codes a little (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Jan 20, 2025
1 parent 75fabce commit c567ae5
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 133 deletions.
10 changes: 8 additions & 2 deletions build/ten_common/general/glob.gni
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ template("glob") {
]
}

sources =
exec_script("//build/ten_common/general/glob_file.py", l, "list lines")
sources_info =
exec_script("//build/ten_common/general/glob_file.py", l, "json")

sources = []
foreach(source_info, sources_info) {
sources += [ source_info.path ]
}

if (defined(invoker.sources) && invoker.sources != []) {
sources += invoker.sources
}
Expand Down
26 changes: 22 additions & 4 deletions build/ten_common/general/glob_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,53 @@
import os
import sys
import glob
import json


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.dir: list[str]
self.dir_base: list[str] = []
self.recursive: bool
self.only_output_file: bool


def glob_file(dirs: list[str], recursive: bool, only_file: bool) -> None:
for dir in dirs:
def glob_file(
dirs: list[str], dir_bases: list[str], recursive: bool, only_file: bool
) -> None:
output = []

for index, dir in enumerate(dirs):
dir_base = dir_bases[index] if index < len(dir_bases) else ""
for v in glob.glob(dir, recursive=recursive):
if only_file:
if not os.path.isfile(v):
continue

sys.stdout.write(v.replace("\\", "/") + "\n")
relative_path = os.path.relpath(v, dir_base) if dir_base else ""

output.append(
{
"path": v.replace("\\", "/"),
"relative_path": relative_path,
}
)

json.dump(output, sys.stdout, indent=2)
sys.stdout.write("\n")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--dir", type=str, action="append", default=[])
parser.add_argument("--dir-base", type=str, action="append", default=[])
parser.add_argument("--recursive", action="store_true")
# Skip directory in the output.
parser.add_argument("--only-output-file", action="store_true")

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

glob_file(args.dir, args.recursive, args.only_output_file)
glob_file(args.dir, args.dir_base, args.recursive, args.only_output_file)

sys.exit(0)
26 changes: 20 additions & 6 deletions build/ten_common/rust/rust.gni
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ template("rust_target") {
rebase_path("${invoker.project_path}/**/*.rs"),
"--recursive",
],
"list lines")
sources = rust_source_files
"json")

sources = []
foreach(rust_source_file, rust_source_files) {
sources += [ rust_source_file.path ]
}

if (defined(invoker.extra_sources)) {
sources += invoker.extra_sources
}
Expand Down Expand Up @@ -332,8 +337,13 @@ template("rust_test") {
rebase_path("${invoker.project_path}/**/*.rs"),
"--recursive",
],
"list lines")
sources = rust_source_files
"json")

sources = []
foreach(rust_source_file, rust_source_files) {
sources += [ rust_source_file.path ]
}

if (defined(invoker.extra_sources)) {
sources += invoker.extra_sources
}
Expand All @@ -345,9 +355,13 @@ template("rust_test") {
rebase_path("${invoker.project_path}/**/Cargo.toml"),
"--recursive",
],
"list lines")
"json")

inputs = []
foreach(cargo_toml, cargo_tomls) {
inputs += [ cargo_toml.path ]
}

inputs = cargo_tomls
if (defined(invoker.extra_inputs)) {
inputs += invoker.extra_inputs
}
Expand Down
10 changes: 8 additions & 2 deletions build/ten_runtime/glob.gni
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ template("ten_runtime_glob") {
]
}

sources =
exec_script("//build/ten_common/general/glob_file.py", l, "list lines")
sources_info =
exec_script("//build/ten_common/general/glob_file.py", l, "json")

sources = []
foreach(source_info, sources_info) {
sources += [ source_info.path ]
}

if (defined(invoker.sources)) {
sources += invoker.sources
}
Expand Down
12 changes: 9 additions & 3 deletions build/ten_runtime/ten.gni
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,15 @@ template("ten_source_set") {
rebase_path(f),
]
}
sources = exec_script("//build/ten_common/general/glob_file.py",
rebase_files,
"list lines")

sources_info = exec_script("//build/ten_common/general/glob_file.py",
rebase_files,
"json")

foreach(source_info, sources_info) {
sources += [ source_info.path ]
}

if (defined(invoker.sources)) {
sources += invoker.sources
}
Expand Down
35 changes: 33 additions & 2 deletions core/src/ten_runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,43 @@ ten_package("ten_runtime_system_package") {
package_output_root_dir_name = "ten_runtime"

resources = [
"//core/include/ten_runtime=>include/ten_runtime",
"//core/include/ten_utils=>include/ten_utils",
"BUILD_release.gn=>BUILD.gn",
"manifest.json",
]

runtime_headers =
exec_script("//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/include/ten_runtime/**/*"),
"--dir-base",
rebase_path("//core/include/ten_runtime/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(runtime_header, runtime_headers) {
runtime_header_rel_path = runtime_header.relative_path
resources += [ "//core/include/ten_runtime/${runtime_header_rel_path}=>include/ten_runtime/${runtime_header_rel_path}" ]
}

utils_headers = exec_script("//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/include/ten_utils/**/*"),
"--dir-base",
rebase_path("//core/include/ten_utils/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(utils_header, utils_headers) {
utils_header_rel_path = utils_header.relative_path
resources += [ "//core/include/ten_utils/${utils_header_rel_path}=>include/ten_utils/${utils_header_rel_path}" ]
}

foreach(lib, ten_runtime_output_libs) {
libname = get_path_info(rebase_path(lib), "file")
resources += [ "${lib}=>lib/${libname}" ]
Expand Down
42 changes: 36 additions & 6 deletions core/src/ten_runtime/binding/go/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,42 @@ ten_package("ten_go_binding_system_package") {
package_kind = "system"
package_output_root_dir_name = "ten_runtime_go"

resources = [
"//core/src/ten_runtime/binding/go/interface=>interface",
"//core/src/ten_runtime/binding/go/tools=>tools",
"BUILD_release.gn=>BUILD.gn",
"manifest.json",
]
resources = [ "manifest.json" ]

interface_files =
exec_script("//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path(
"//core/src/ten_runtime/binding/go/interface/**/*"),
"--dir-base",
rebase_path("//core/src/ten_runtime/binding/go/interface/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(interface_file, interface_files) {
interface_file_rel_path = interface_file.relative_path
resources += [ "interface/${interface_file_rel_path}=>interface/${interface_file_rel_path}" ]
}

tools_files =
exec_script("//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/src/ten_runtime/binding/go/tools/**/*"),
"--dir-base",
rebase_path("//core/src/ten_runtime/binding/go/tools/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(tool_file, tools_files) {
tool_file_rel_path = tool_file.relative_path
resources += [ "tools/${tool_file_rel_path}=>tools/${tool_file_rel_path}" ]
}

foreach(lib, ten_runtime_go_output_libs) {
libname = get_path_info(rebase_path(lib), "file")
Expand Down
42 changes: 0 additions & 42 deletions core/src/ten_runtime/binding/go/BUILD_release.gn

This file was deleted.

18 changes: 17 additions & 1 deletion core/src/ten_runtime/binding/nodejs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ ten_package("ten_nodejs_binding_system_package") {
package_output_root_dir_name = "ten_runtime_nodejs"

resources = [
"//core/src/ten_runtime/binding/nodejs/interface=>interface",
"manifest.json",
"package.json",
"tsconfig.json",
]

interface_files = exec_script(
"//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/src/ten_runtime/binding/nodejs/interface/**/*"),
"--dir-base",
rebase_path("//core/src/ten_runtime/binding/nodejs/interface/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(interface_file, interface_files) {
interface_file_rel_path = interface_file.relative_path
resources += [ "interface/${interface_file_rel_path}=>interface/${interface_file_rel_path}" ]
}

foreach(lib, ten_runtime_nodejs_output_libs) {
libname = get_path_info(rebase_path(lib), "file")
resources += [ "${lib}=>lib/${libname}" ]
Expand Down
41 changes: 35 additions & 6 deletions core/src/ten_runtime/binding/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,41 @@ ten_package("ten_python_binding_system_package") {
package_kind = "system"
package_output_root_dir_name = "ten_runtime_python"

resources = [
"//core/src/ten_runtime/binding/python/interface=>interface",
"//core/src/ten_runtime/binding/python/tools=>tools",
"BUILD_release.gn=>BUILD.gn",
"manifest.json",
]
resources = [ "manifest.json" ]

interface_files = exec_script(
"//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/src/ten_runtime/binding/python/interface/**/*"),
"--dir-base",
rebase_path("//core/src/ten_runtime/binding/python/interface/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(interface_file, interface_files) {
interface_file_rel_path = interface_file.relative_path
resources += [ "interface/${interface_file_rel_path}=>interface/${interface_file_rel_path}" ]
}

tools_files = exec_script(
"//build/ten_common/general/glob_file.py",
[
"--dir",
rebase_path("//core/src/ten_runtime/binding/python/tools/**/*"),
"--dir-base",
rebase_path("//core/src/ten_runtime/binding/python/tools/"),
"--recursive",
"--only-output-file",
],
"json")

foreach(tool_file, tools_files) {
tool_file_rel_path = tool_file.relative_path
resources += [ "tools/${tool_file_rel_path}=>tools/${tool_file_rel_path}" ]
}

foreach(lib, ten_runtime_python_output_libs) {
libname = get_path_info(rebase_path(lib), "file")
Expand Down
Loading

0 comments on commit c567ae5

Please sign in to comment.