Permalink
Please
sign in to comment.
Browse files
refactor: remove js2asar.py and port logic to JS in more readable / G…
…N-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
- Loading branch information...
Showing
with
168 additions
and 73 deletions.
- +45 −8 BUILD.gn
- +22 −12 build/asar.gni
- +21 −0 build/node.gni
- +14 −0 build/run-node.py
- +1 −1 default_app/index.html
- +1 −1 default_app/renderer.js
- +3 −0 filenames.gni
- +61 −0 script/gn-asar.js
- +0 −51 tools/js2asar.py
| @@ -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 | |||
| } | |||
| } | |||
| @@ -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()) | |||
| @@ -0,0 +1,61 @@ | |||
| const asar = require('asar') | |||
| const assert = require('assert') | |||
| const fs = require('fs-extra') | |||
| const os = require('os') | |||
| const path = require('path') | |||
|
|
|||
| const getArgGroup = (name) => { | |||
| const group = [] | |||
| let inGroup = false | |||
| for (const arg of process.argv) { | |||
| // At the next flag we stop being in the current group | |||
| if (arg.startsWith('--')) inGroup = false | |||
| // Push all args in the group | |||
| if (inGroup) group.push(arg) | |||
| // If we find the start flag, start pushing | |||
| if (arg === `--${name}`) inGroup = true | |||
| } | |||
|
|
|||
| return group | |||
| } | |||
|
|
|||
| const base = getArgGroup('base') | |||
| const files = getArgGroup('files') | |||
| const out = getArgGroup('out') | |||
|
|
|||
| assert(base.length === 1, 'should have a single base dir') | |||
| assert(files.length >= 1, 'should have at least one input file') | |||
| assert(out.length === 1, 'should have a single out path') | |||
|
|
|||
| // Ensure all files are inside the base dir | |||
| for (const file of files) { | |||
| if (!file.startsWith(base[0])) { | |||
| console.error(`Expected all files to be inside the base dir but "${file}" was not in "${base[0]}"`) | |||
| process.exit(1) | |||
| } | |||
| } | |||
|
|
|||
| const tmpPath = fs.mkdtempSync(path.resolve(os.tmpdir(), 'electron-gn-asar-')) | |||
|
|
|||
| try { | |||
| // Copy all files to a tmp dir to avoid including scrap files in the ASAR | |||
| for (const file of files) { | |||
| const newLocation = path.resolve(tmpPath, path.relative(base[0], file)) | |||
| fs.mkdirsSync(path.dirname(newLocation)) | |||
| fs.writeFileSync(newLocation, fs.readFileSync(file)) | |||
| } | |||
| } catch (err) { | |||
| console.error('Unexpected error while generating ASAR', err) | |||
| fs.removeSync(tmpPath) | |||
| process.exit(1) | |||
| } | |||
|
|
|||
| // Create the ASAR archive | |||
| asar.createPackageWithOptions(tmpPath, out[0], {}, (err) => { | |||
| fs.removeSync(tmpPath) | |||
|
|
|||
| if (err) { | |||
| console.error('Unexpected error while generating ASAR', err) | |||
| process.exit(1) | |||
| } | |||
| }) | |||
0 comments on commit
b202ad1