refactor: remove js2asar.py and port logic to JS in more readable / GN-style way (#16718)

* refactor: remove js2asar.py and port logic to JS in more readable / GN-style way

* refactor: further clean up ASAR impl, add new node_action GN template
This commit is contained in:
Samuel Attard 2019-02-05 12:10:15 -08:00 committed by GitHub
parent 8582325e85
commit b202ad1e24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 168 additions and 73 deletions

View file

@ -1,5 +1,6 @@
import("npm.gni")
import("node.gni")
# TODO(MarshallOfSound): Move to electron/node, this is the only place it is used now
# Run an action with a given working directory. Behaves identically to the
# action() target type, with the exception that it changes directory before
# running the script.
@ -32,19 +33,28 @@ template("chdir_action") {
template("asar") {
assert(defined(invoker.sources),
"Need sources in $target_name listing the JS files.")
"Need sources in $target_name listing the source files")
assert(defined(invoker.outputs),
"Need asar name (as 1-element array, e.g. \$root_out_dir/foo.asar)")
assert(defined(invoker.root), "Need asar root directory")
asar_root = invoker.root
assert(defined(invoker.root), "Need the base dir for generating the ASAR")
# js2asar.py expects relative paths to its inputs, so we must run it in a
# working directory in which those relative paths make sense.
chdir_action(target_name) {
sources = invoker.sources
outputs = invoker.outputs
script = "//electron/tools/js2asar.py"
cwd = rebase_path(get_path_info(".", "abspath"))
args = rebase_path(outputs, cwd) + [ asar_root ] + rebase_path(sources, ".")
node_action(target_name) {
forward_variables_from(invoker,
"*",
[
"script",
"args",
])
script = "//electron/script/gn-asar.js"
args = [
"--base",
rebase_path(root),
"--files",
] + rebase_path(sources) +
[
"--out",
rebase_path(outputs[0]),
]
}
}

21
build/node.gni Normal file
View file

@ -0,0 +1,21 @@
template("node_action") {
assert(defined(invoker.script), "Need script path to run")
assert(defined(invoker.args), "Need script argumets")
action(target_name) {
forward_variables_from(invoker,
[
"deps",
"public_deps",
"sources",
"inputs",
"outputs",
])
if (!defined(inputs)) {
inputs = []
}
inputs += [ invoker.script ]
script = "//electron/build/run-node.py"
args = [ rebase_path(invoker.script) ] + invoker.args
}
}

14
build/run-node.py Normal file
View file

@ -0,0 +1,14 @@
import os
import subprocess
import sys
SOURCE_ROOT = os.path.dirname(os.path.dirname(__file__))
def main():
# Proxy all args to node script
script = os.path.join(SOURCE_ROOT, sys.argv[1])
subprocess.check_call(['node', script] + [str(x) for x in sys.argv[2:]])
if __name__ == '__main__':
sys.exit(main())