| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # -*- coding: UTF-8 -*- |
| 2 # | 3 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 4 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 5 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 6 # found in the LICENSE file. |
| 6 | 7 |
| 7 """ | 8 """ |
| 8 Release: | 9 Builds applications in release mode: |
| 9 - Concatenates autostart modules, application modules' module.json descriptors
, | 10 - Concatenates autostart modules, application modules' module.json descriptors, |
| 10 and the application loader into a single script. | 11 and the application loader into a single script. |
| 11 - Builds app.html referencing the application script. | 12 - Builds app.html referencing the application script. |
| 12 Debug: | |
| 13 - Copies the module directories into their destinations. | |
| 14 - Copies app.html as-is. | |
| 15 """ | 13 """ |
| 16 | 14 |
| 17 from cStringIO import StringIO | 15 from cStringIO import StringIO |
| 18 from os import path | 16 from os import path |
| 19 from os.path import join | 17 from os.path import join |
| 20 from modular_build import read_file, write_file, bail_error | 18 from modular_build import read_file, write_file, bail_error |
| 21 import copy | 19 import copy |
| 22 import modular_build | 20 import modular_build |
| 23 import os | 21 import os |
| 24 import re | 22 import re |
| 25 import shutil | 23 import shutil |
| 26 import sys | 24 import sys |
| 27 | 25 |
| 28 try: | 26 try: |
| 29 import simplejson as json | 27 import simplejson as json |
| 30 except ImportError: | 28 except ImportError: |
| 31 import json | 29 import json |
| 32 | 30 |
| 33 import rjsmin | 31 import rjsmin |
| 34 | 32 |
| 35 | 33 |
| 34 def main(argv): |
| 35 try: |
| 36 input_path_flag_index = argv.index('--input_path') |
| 37 input_path = argv[input_path_flag_index + 1] |
| 38 output_path_flag_index = argv.index('--output_path') |
| 39 output_path = argv[output_path_flag_index + 1] |
| 40 application_names = argv[1:input_path_flag_index] |
| 41 except: |
| 42 print('Usage: %s app_1 app_2 ... app_N --input_path <input_path> --outpu
t_path <output_path>' % argv[0]) |
| 43 raise |
| 44 |
| 45 loader = modular_build.DescriptorLoader(input_path) |
| 46 for app in application_names: |
| 47 descriptors = loader.load_application(app + '.json') |
| 48 builder = ReleaseBuilder(app, descriptors, input_path, output_path) |
| 49 builder.build_app() |
| 50 |
| 51 |
| 36 def resource_source_url(url): | 52 def resource_source_url(url): |
| 37 return '\n/*# sourceURL=' + url + ' */' | 53 return '\n/*# sourceURL=' + url + ' */' |
| 38 | 54 |
| 39 | 55 |
| 40 def minify_js(javascript): | 56 def minify_js(javascript): |
| 41 return rjsmin.jsmin(javascript) | 57 return rjsmin.jsmin(javascript) |
| 42 | 58 |
| 43 | 59 |
| 44 def concatenated_module_filename(module_name, output_dir): | 60 def concatenated_module_filename(module_name, output_dir): |
| 45 return join(output_dir, module_name + '/' + module_name + '_module.js') | 61 return join(output_dir, module_name + '/' + module_name + '_module.js') |
| (...skipping 14 matching lines...) Expand all Loading... |
| 60 for src_dir, dirs, files in os.walk(src): | 76 for src_dir, dirs, files in os.walk(src): |
| 61 subpath = path.relpath(src_dir, src) | 77 subpath = path.relpath(src_dir, src) |
| 62 dest_dir = path.normpath(join(dest, subpath)) | 78 dest_dir = path.normpath(join(dest, subpath)) |
| 63 os.mkdir(dest_dir) | 79 os.mkdir(dest_dir) |
| 64 for name in files: | 80 for name in files: |
| 65 src_name = join(os.getcwd(), src_dir, name) | 81 src_name = join(os.getcwd(), src_dir, name) |
| 66 dest_name = join(dest_dir, name) | 82 dest_name = join(dest_dir, name) |
| 67 symlink_or_copy_file(src_name, dest_name) | 83 symlink_or_copy_file(src_name, dest_name) |
| 68 | 84 |
| 69 | 85 |
| 70 class AppBuilder: | 86 # Outputs: |
| 87 # <app_name>.html |
| 88 # <app_name>.js |
| 89 # <module_name>_module.js |
| 90 class ReleaseBuilder(object): |
| 71 def __init__(self, application_name, descriptors, application_dir, output_di
r): | 91 def __init__(self, application_name, descriptors, application_dir, output_di
r): |
| 72 self.application_name = application_name | 92 self.application_name = application_name |
| 73 self.descriptors = descriptors | 93 self.descriptors = descriptors |
| 74 self.application_dir = application_dir | 94 self.application_dir = application_dir |
| 75 self.output_dir = output_dir | 95 self.output_dir = output_dir |
| 76 | 96 |
| 77 def app_file(self, extension): | 97 def app_file(self, extension): |
| 78 return self.application_name + '.' + extension | 98 return self.application_name + '.' + extension |
| 79 | 99 |
| 80 def core_resource_names(self): | 100 def core_resource_names(self): |
| 81 result = [] | 101 result = [] |
| 82 for module in self.descriptors.sorted_modules(): | 102 for module in self.descriptors.sorted_modules(): |
| 83 if self.descriptors.application[module].get('type') != 'autostart': | 103 if self.descriptors.application[module].get('type') != 'autostart': |
| 84 continue | 104 continue |
| 85 | 105 |
| 86 resources = self.descriptors.modules[module].get('resources') | 106 resources = self.descriptors.modules[module].get('resources') |
| 87 if not resources: | 107 if not resources: |
| 88 continue | 108 continue |
| 89 for resource_name in resources: | 109 for resource_name in resources: |
| 90 result.append(path.join(module, resource_name)) | 110 result.append(path.join(module, resource_name)) |
| 91 return result | 111 return result |
| 92 | 112 |
| 93 | |
| 94 # Outputs: | |
| 95 # <app_name>.html | |
| 96 # <app_name>.js | |
| 97 # <module_name>_module.js | |
| 98 class ReleaseBuilder(AppBuilder): | |
| 99 def __init__(self, application_name, descriptors, application_dir, output_di
r): | |
| 100 AppBuilder.__init__(self, application_name, descriptors, application_dir
, output_dir) | |
| 101 | |
| 102 def build_app(self): | 113 def build_app(self): |
| 103 if self.descriptors.has_html: | 114 if self.descriptors.has_html: |
| 104 self._build_html() | 115 self._build_html() |
| 105 self._build_app_script() | 116 self._build_app_script() |
| 106 for module in filter(lambda desc: (not desc.get('type') or desc.get('typ
e') == 'remote'), self.descriptors.application.values()): | 117 for module in filter(lambda desc: (not desc.get('type') or desc.get('typ
e') == 'remote'), self.descriptors.application.values()): |
| 107 self._concatenate_dynamic_module(module['name']) | 118 self._concatenate_dynamic_module(module['name']) |
| 108 | 119 |
| 109 def _build_html(self): | 120 def _build_html(self): |
| 110 html_name = self.app_file('html') | 121 html_name = self.app_file('html') |
| 111 output = StringIO() | 122 output = StringIO() |
| 112 with open(join(self.application_dir, html_name), 'r') as app_input_html: | 123 with open(join(self.application_dir, html_name), 'r') as app_input_html: |
| 113 for line in app_input_html: | 124 for line in app_input_html: |
| 114 if '<script ' in line or '<link ' in line: | 125 if '<script ' in line or '<link ' in line: |
| 115 continue | 126 continue |
| 116 if '</head>' in line: | 127 if '</head>' in line: |
| 117 output.write(self._generate_include_tag(self.app_file('js'))
) | 128 output.write(self._generate_include_tag(self.app_file('js'))
) |
| 118 output.write(line) | 129 output.write(line) |
| 119 | 130 |
| 120 write_file(join(self.output_dir, html_name), output.getvalue()) | 131 write_file(join(self.output_dir, html_name), output.getvalue()) |
| 121 output.close() | 132 output.close() |
| 122 | 133 |
| 123 def _build_app_script(self): | 134 def _build_app_script(self): |
| 124 script_name = self.app_file('js') | 135 script_name = self.app_file('js') |
| 125 output = StringIO() | 136 output = StringIO() |
| 126 self._concatenate_application_script(output) | 137 self._concatenate_application_script(output) |
| 127 write_file(join(self.output_dir, script_name), minify_js(output.getvalue
())) | 138 write_file(join(self.output_dir, script_name), minify_js(output.getvalue
())) |
| 128 output.close() | 139 output.close() |
| 129 | 140 |
| 130 def _generate_include_tag(self, resource_path): | 141 def _generate_include_tag(self, resource_path): |
| 131 if (resource_path.endswith('.js')): | 142 if resource_path.endswith('.js'): |
| 132 return ' <script type="text/javascript" src="%s"></script>\n' % r
esource_path | 143 return ' <script type="text/javascript" src="%s"></script>\n' % r
esource_path |
| 133 else: | 144 else: |
| 134 assert resource_path | 145 assert resource_path |
| 135 | 146 |
| 136 def _release_module_descriptors(self): | 147 def _release_module_descriptors(self): |
| 137 module_descriptors = self.descriptors.modules | 148 module_descriptors = self.descriptors.modules |
| 138 result = [] | 149 result = [] |
| 139 for name in module_descriptors: | 150 for name in module_descriptors: |
| 140 module = copy.copy(module_descriptors[name]) | 151 module = copy.copy(module_descriptors[name]) |
| 141 module_type = self.descriptors.application[name].get('type') | 152 module_type = self.descriptors.application[name].get('type') |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 output = StringIO() | 217 output = StringIO() |
| 207 if scripts: | 218 if scripts: |
| 208 modular_build.concatenate_scripts(scripts, module_dir, self.output_d
ir, output) | 219 modular_build.concatenate_scripts(scripts, module_dir, self.output_d
ir, output) |
| 209 if resources: | 220 if resources: |
| 210 self._write_module_resources(resources, output) | 221 self._write_module_resources(resources, output) |
| 211 output_file_path = concatenated_module_filename(module_name, self.output
_dir) | 222 output_file_path = concatenated_module_filename(module_name, self.output
_dir) |
| 212 write_file(output_file_path, minify_js(output.getvalue())) | 223 write_file(output_file_path, minify_js(output.getvalue())) |
| 213 output.close() | 224 output.close() |
| 214 | 225 |
| 215 | 226 |
| 216 # Outputs: | 227 if __name__ == '__main__': |
| 217 # <app_name>.html as-is | 228 sys.exit(main(sys.argv)) |
| 218 # <app_name>.js as-is | |
| 219 # <module_name>/<all_files> | |
| 220 class DebugBuilder(AppBuilder): | |
| 221 def __init__(self, application_name, descriptors, application_dir, output_di
r): | |
| 222 AppBuilder.__init__(self, application_name, descriptors, application_dir
, output_dir) | |
| 223 | |
| 224 def build_app(self): | |
| 225 if self.descriptors.has_html: | |
| 226 self._build_html() | |
| 227 js_name = self.app_file('js') | |
| 228 src_name = join(os.getcwd(), self.application_dir, js_name) | |
| 229 symlink_or_copy_file(src_name, join(self.output_dir, js_name), True) | |
| 230 for module_name in self.descriptors.modules: | |
| 231 module = self.descriptors.modules[module_name] | |
| 232 input_module_dir = join(self.application_dir, module_name) | |
| 233 output_module_dir = join(self.output_dir, module_name) | |
| 234 symlink_or_copy_dir(input_module_dir, output_module_dir) | |
| 235 | |
| 236 def _build_html(self): | |
| 237 html_name = self.app_file('html') | |
| 238 symlink_or_copy_file(join(os.getcwd(), self.application_dir, html_name),
join(self.output_dir, html_name), True) | |
| 239 | |
| 240 | |
| 241 def build_application(application_name, loader, application_dir, output_dir, rel
ease_mode): | |
| 242 descriptors = loader.load_application(application_name + '.json') | |
| 243 if release_mode: | |
| 244 builder = ReleaseBuilder(application_name, descriptors, application_dir,
output_dir) | |
| 245 else: | |
| 246 builder = DebugBuilder(application_name, descriptors, application_dir, o
utput_dir) | |
| 247 builder.build_app() | |
| OLD | NEW |