3 # 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 # found in the LICENSE file.
8 Concatenates module scripts based on the module.json descriptor.
9 Optionally, minifies the result using rjsmin.
12 from cStringIO import StringIO
19 import simplejson as json
23 rjsmin_path = path.abspath(path.join(
24 path.dirname(__file__),
29 sys.path.append(rjsmin_path)
33 def read_file(filename):
34 with open(path.normpath(filename), 'rt') as file:
38 def write_file(filename, content):
39 # This is here to avoid overwriting source tree files due to hard links.
40 if path.exists(filename):
42 with open(filename, 'wt') as file:
46 def concatenate_scripts(file_names, module_dir, output_dir, output):
47 for file_name in file_names:
48 output.write('/* %s */\n' % file_name)
49 file_path = path.join(module_dir, file_name)
51 # This allows to also concatenate generated files found in output_dir.
52 if not path.isfile(file_path):
53 file_path = path.join(output_dir, path.basename(module_dir), file_name)
54 output.write(read_file(file_path))
60 print('Usage: %s module_json output_file no_minify' % argv[0])
63 module_json_file_name = argv[1]
64 output_file_name = argv[2]
65 no_minify = len(argv) > 3 and argv[3]
66 module_dir = path.dirname(module_json_file_name)
71 descriptor = json.loads(read_file(module_json_file_name))
73 print 'ERROR: Failed to load JSON from ' + module_json_file_name
76 # pylint: disable=E1103
77 scripts = descriptor.get('scripts')
79 output_root_dir = path.join(path.dirname(output_file_name), '..')
80 concatenate_scripts(scripts, module_dir, output_root_dir, output)
82 output_script = output.getvalue()
84 write_file(output_file_name, output_script if no_minify else rjsmin.jsmin(output_script))
86 if __name__ == '__main__':
87 sys.exit(main(sys.argv))