Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: packagerChrome.py

Issue 29549786: Issue 5535 - Replace our module system with webpack (Closed)
Patch Set: Improved resolve paths comment Created Sept. 24, 2017, 12:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # This Source Code Form is subject to the terms of the Mozilla Public 1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 4
5 import errno 5 import errno
6 import io 6 import io
7 import json 7 import json
8 import os 8 import os
9 import re 9 import re
10 import shutil
10 from StringIO import StringIO 11 from StringIO import StringIO
11 import struct 12 import struct
13 import subprocess
12 import sys 14 import sys
13 import collections 15 import tempfile
14 16
17 from ensure_dependencies import read_deps
15 from packager import (readMetadata, getDefaultFileName, getBuildVersion, 18 from packager import (readMetadata, getDefaultFileName, getBuildVersion,
16 getTemplate, Files) 19 getTemplate, Files)
17 20
18 defaultLocale = 'en_US' 21 defaultLocale = 'en_US'
19 22
20 23
21 def getIgnoredFiles(params): 24 def getIgnoredFiles(params):
22 return {'store.description'} 25 return {'store.description'}
23 26
24 27
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 # Normalize JSON structure 135 # Normalize JSON structure
133 licenseComment = re.compile(r'/\*.*?\*/', re.S) 136 licenseComment = re.compile(r'/\*.*?\*/', re.S)
134 data = json.loads(re.sub(licenseComment, '', manifest, 1)) 137 data = json.loads(re.sub(licenseComment, '', manifest, 1))
135 if '_dummy' in data: 138 if '_dummy' in data:
136 del data['_dummy'] 139 del data['_dummy']
137 manifest = json.dumps(data, sort_keys=True, indent=2) 140 manifest = json.dumps(data, sort_keys=True, indent=2)
138 141
139 return manifest.encode('utf-8') 142 return manifest.encode('utf-8')
140 143
141 144
142 def convertJS(params, files): 145 def create_bundles(params, files):
143 output_files = collections.OrderedDict() 146 base_extension_path = params['baseDir']
144 args = {} 147 info_templates = {
148 'chrome': 'chromeInfo.js.tmpl',
149 'edge': 'edgeInfo.js.tmpl',
150 'gecko-webext': 'geckoInfo.js.tmpl'
151 }
152 info_module = None
145 153
146 for item in params['metadata'].items('convert_js'): 154 # Historically we didn't use relative paths when requiring modules, so in
147 name, value = item 155 # order for webpack to know where to find them we need to pass in a list of
148 filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups() 156 # resolve paths. Going forward we should always use relative paths, once we
149 if arg is None: 157 # do that consistently this can be removed. See issues 5760, 5761 and 5762.
150 output_files[filename] = (value.split(), item.source) 158 resolve_paths = ' '.join(
151 else: 159 [os.path.join(base_extension_path, dir, 'lib')
152 args.setdefault(filename, {})[arg] = value 160 for dir in ['', 'adblockpluscore', 'adblockplusui']]
161 )
153 162
154 template = getTemplate('modules.js.tmpl') 163 temp_dir = tempfile.mkdtemp()
164 try:
165 info_module = os.path.join(temp_dir, 'info.js')
166 template = getTemplate(info_templates[params['type']])
167 with open(info_module, 'w') as info_file:
168 info_file.write(
169 template.render(
170 basename=params['metadata'].get('general', 'basename'),
171 version=params['metadata'].get('general', 'version')
172 ).encode('utf-8')
173 )
155 174
156 for filename, (input_files, origin) in output_files.iteritems(): 175 for item in params['metadata'].items('bundles'):
157 if '/' in filename and not files.isIncluded(filename): 176 name, value = item
158 continue 177 base_item_path = os.path.dirname(item.source)
159 178
160 current_args = args.get(filename, {}) 179 bundle_file = os.path.relpath(os.path.join(base_item_path, name),
161 current_args['autoload'] = [module for module in 180 base_extension_path)
162 current_args.get('autoload', '').split(',') 181 entry_files = [
163 if module != ''] 182 os.path.join(
164 183 '.',
165 base_dir = os.path.dirname(origin) 184 os.path.relpath(os.path.join(base_item_path, module_path),
166 modules = [] 185 base_extension_path)
167 186 )
168 for input_filename in input_files: 187 for module_path in value.split()
169 module_name = os.path.splitext(os.path.basename(input_filename))[0] 188 ]
170 prefix = os.path.basename(os.path.dirname(input_filename)) 189 subprocess.check_call(
171 if prefix != 'lib': 190 ['npm', 'run-script', 'webpack', '--silent'],
172 module_name = '{}_{}'.format(prefix, module_name) 191 cwd=os.path.dirname(__file__),
173 with open(os.path.join(base_dir, input_filename), 'r') as file: 192 env={
174 modules.append((module_name, file.read().decode('utf-8'))) 193 'EXTENSION_PATH': base_extension_path,
175 files.pop(input_filename, None) 194 'ENTRY_POINTS': ' '.join(entry_files),
176 195 'OUTPUT_PATH': temp_dir,
177 files[filename] = template.render( 196 'BUNDLE_NAME': bundle_file,
178 args=current_args, 197 'RESOLVE_PATHS': resolve_paths,
179 basename=params['metadata'].get('general', 'basename'), 198 'INFO_PATH': info_module,
180 modules=modules, 199 'PATH': os.environ['PATH']
181 type=params['type'], 200 }
182 version=params['metadata'].get('general', 'version') 201 )
183 ).encode('utf-8') 202 for file_name in [bundle_file, bundle_file + '.map']:
203 with open(os.path.join(temp_dir, file_name), 'r') as f:
204 files[file_name] = f.read()
205 finally:
206 shutil.rmtree(temp_dir)
184 207
185 208
186 def toJson(data): 209 def toJson(data):
187 return json.dumps( 210 return json.dumps(
188 data, ensure_ascii=False, sort_keys=True, 211 data, ensure_ascii=False, sort_keys=True,
189 indent=2, separators=(',', ': ') 212 indent=2, separators=(',', ': ')
190 ).encode('utf-8') + '\n' 213 ).encode('utf-8') + '\n'
191 214
192 215
193 def import_string_webext(data, key, source): 216 def import_string_webext(data, key, source):
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 'metadata': metadata, 405 'metadata': metadata,
383 } 406 }
384 407
385 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] 408 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ]
386 files = Files(getPackageFiles(params), getIgnoredFiles(params), 409 files = Files(getPackageFiles(params), getIgnoredFiles(params),
387 process=lambda path, data: processFile(path, data, params)) 410 process=lambda path, data: processFile(path, data, params))
388 411
389 files.readMappedFiles(mapped) 412 files.readMappedFiles(mapped)
390 files.read(baseDir, skip=[opt for opt, _ in mapped]) 413 files.read(baseDir, skip=[opt for opt, _ in mapped])
391 414
392 if metadata.has_section('convert_js'): 415 if metadata.has_section('bundles'):
393 convertJS(params, files) 416 create_bundles(params, files)
394 417
395 if metadata.has_section('preprocess'): 418 if metadata.has_section('preprocess'):
396 files.preprocess( 419 files.preprocess(
397 [f for f, _ in metadata.items('preprocess')], 420 [f for f, _ in metadata.items('preprocess')],
398 {'needsExt': True} 421 {'needsExt': True}
399 ) 422 )
400 423
401 if metadata.has_section('import_locales'): 424 if metadata.has_section('import_locales'):
402 import_locales(params, files) 425 import_locales(params, files)
403 426
(...skipping 12 matching lines...) Expand all
416 params, 'testIndex.html.tmpl', ('general', 'testScripts') 439 params, 'testIndex.html.tmpl', ('general', 'testScripts')
417 ) 440 )
418 441
419 zipdata = files.zipToString() 442 zipdata = files.zipToString()
420 signature = None 443 signature = None
421 pubkey = None 444 pubkey = None
422 if keyFile != None: 445 if keyFile != None:
423 signature = signBinary(zipdata, keyFile) 446 signature = signBinary(zipdata, keyFile)
424 pubkey = getPublicKey(keyFile) 447 pubkey = getPublicKey(keyFile)
425 writePackage(outFile, pubkey, signature, zipdata) 448 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | webpack.config.js » ('J')

Powered by Google App Engine
This is Rietveld