Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Unified Diff: src/caterpillar.py

Issue 1644913002: Caterpillar now generates app.info.js, a metadata script. Resolves #12. (Closed) Base URL: git@github.com:chromium/caterpillar.git@dependency-script-injection
Patch Set: Response to CR + whitespace fix Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/caterpillar_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/caterpillar.py
diff --git a/src/caterpillar.py b/src/caterpillar.py
index 4a0a8b5c38e4e035c8b1953a6cd4babbfe734961..9e19554d7fb077023131f7b3f7b82e82efdcb478 100755
--- a/src/caterpillar.py
+++ b/src/caterpillar.py
@@ -69,6 +69,9 @@ MAX_CACHE_VERSION = 1000000
# Where this file is located (so we can find resources).
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
+# Name of the app info script.
+INFO_SCRIPT_NAME = 'app.info.js'
+
# Maps dependency managers to the folder they install dependencies into.
DEPENDENCY_MANAGER_INSTALL_FOLDER = {
'bower': 'bower_components',
@@ -440,6 +443,24 @@ def add_service_worker(output_dir, ca_manifest, required_js_paths,
sw_file.write(surrogateescape.encode(sw_js))
+def add_app_info(output_dir, ca_manifest):
+ """Adds an app info script, containing metadata, to a web app.
+
+ Args:
+ output_dir: Path to web app to add app info script to.
+ ca_manifest: Chrome App manifest dictionary.
+ """
+ logging.debug('Generating app info script.')
+ js_manifest = json.dumps(ca_manifest, sort_keys=True, indent=2,
+ separators=(',', ': ')).replace('"', "'")
Matthew Alger 2016/01/28 23:25:32 Changed this to drop trailing spaces by explicitly
Matt Giuca 2016/01/29 02:34:27 Fair enough. Why is " being replaced with '? Isn'
Matt Giuca 2016/01/29 03:06:53 Acknowledged.
+ app_info_js = ('chrome.caterpillar.manifest = {manifest};\n').format(
+ manifest=js_manifest)
+ app_info_path = os.path.join(output_dir, INFO_SCRIPT_NAME)
+ logging.debug('Writing app info script to `%s`.', app_info_path)
+ with open(app_info_path, 'w') as app_info_file:
+ app_info_file.write(app_info_js.encode('utf-8'))
+
+
class InstallationError(Exception):
"""Exception raised when a dependency fails to install."""
@@ -626,6 +647,10 @@ def convert_app(input_dir, output_dir, config, force=False):
required_polyfill_paths = polyfill_paths(polyfillable)
+ # Additionally, we may generate some files we want to use in HTML script tags,
+ # but we don't want to install as a dependency or copy from a static JS file.
+ required_generated_paths = []
+
# Read in and check the manifest file.
try:
ca_manifest = chrome_app.manifest.get(input_dir)
@@ -646,6 +671,11 @@ def convert_app(input_dir, output_dir, config, force=False):
json.dump(pwa_manifest, pwa_manifest_file, indent=4, sort_keys=True)
logging.debug('Wrote `%s` to `%s`.', PWA_MANIFEST_FILENAME, pwa_manifest_path)
+ # Generate and write an app info file so we can access Chrome App metadata
+ # from polyfills and scripts.
+ add_app_info(output_dir, ca_manifest)
+ required_generated_paths.append(os.path.join('..', INFO_SCRIPT_NAME))
+
# Remove unnecessary files from the output web app. This must be done before
# the service worker is generated, or these files will be cached.
cleanup_output_dir(output_dir)
@@ -655,8 +685,8 @@ def convert_app(input_dir, output_dir, config, force=False):
# user code directly. This must be done before the static code is copied
# across, or the polyfills will have TODOs added to them.
# Order is significant here - always, then dependencies, then polyfills.
- required_script_paths = (required_always_paths + required_dependency_paths
- + required_polyfill_paths)
+ required_script_paths = (required_always_paths + required_generated_paths +
+ required_dependency_paths + required_polyfill_paths)
edit_code(output_dir, required_script_paths, ca_manifest, config)
# We want the static SW file to be copied in too, so we add it here.
« no previous file with comments | « no previous file | src/caterpillar_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698