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: Use absolute paths for entry points Created Sept. 28, 2017, 1:09 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
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 = [os.path.join(base_item_path, module_path)
163 if module != ''] 182 for module_path in value.split()]
164 183 subprocess.check_call(
165 base_dir = os.path.dirname(origin) 184 ['npm', 'run-script', 'webpack', '--silent'],
166 modules = [] 185 cwd=os.path.dirname(__file__),
167 186 env={
168 for input_filename in input_files: 187 'EXTENSION_PATH': base_extension_path,
169 module_name = os.path.splitext(os.path.basename(input_filename))[0] 188 'ENTRY_POINTS': ' '.join(entry_files),
170 prefix = os.path.basename(os.path.dirname(input_filename)) 189 'OUTPUT_PATH': temp_dir,
171 if prefix != 'lib': 190 'BUNDLE_NAME': bundle_file,
172 module_name = '{}_{}'.format(prefix, module_name) 191 'RESOLVE_PATHS': resolve_paths,
173 with open(os.path.join(base_dir, input_filename), 'r') as file: 192 'INFO_PATH': info_module,
174 modules.append((module_name, file.read().decode('utf-8'))) 193 'PATH': os.environ['PATH']
175 files.pop(input_filename, None) 194 }
176 195 )
177 files[filename] = template.render( 196 for file_name in [bundle_file, bundle_file + '.map']:
178 args=current_args, 197 with open(os.path.join(temp_dir, file_name), 'r') as f:
179 basename=params['metadata'].get('general', 'basename'), 198 files[file_name] = f.read()
180 modules=modules, 199 finally:
181 type=params['type'], 200 shutil.rmtree(temp_dir)
182 version=params['metadata'].get('general', 'version')
183 ).encode('utf-8')
184 201
185 202
186 def toJson(data): 203 def toJson(data):
187 return json.dumps( 204 return json.dumps(
188 data, ensure_ascii=False, sort_keys=True, 205 data, ensure_ascii=False, sort_keys=True,
189 indent=2, separators=(',', ': ') 206 indent=2, separators=(',', ': ')
190 ).encode('utf-8') + '\n' 207 ).encode('utf-8') + '\n'
191 208
192 209
193 def import_string_webext(data, key, source): 210 def import_string_webext(data, key, source):
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 'metadata': metadata, 399 'metadata': metadata,
383 } 400 }
384 401
385 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] 402 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ]
386 files = Files(getPackageFiles(params), getIgnoredFiles(params), 403 files = Files(getPackageFiles(params), getIgnoredFiles(params),
387 process=lambda path, data: processFile(path, data, params)) 404 process=lambda path, data: processFile(path, data, params))
388 405
389 files.readMappedFiles(mapped) 406 files.readMappedFiles(mapped)
390 files.read(baseDir, skip=[opt for opt, _ in mapped]) 407 files.read(baseDir, skip=[opt for opt, _ in mapped])
391 408
392 if metadata.has_section('convert_js'): 409 if metadata.has_section('bundles'):
393 convertJS(params, files) 410 create_bundles(params, files)
394 411
395 if metadata.has_section('preprocess'): 412 if metadata.has_section('preprocess'):
396 files.preprocess( 413 files.preprocess(
397 [f for f, _ in metadata.items('preprocess')], 414 [f for f, _ in metadata.items('preprocess')],
398 {'needsExt': True} 415 {'needsExt': True}
399 ) 416 )
400 417
401 if metadata.has_section('import_locales'): 418 if metadata.has_section('import_locales'):
402 import_locales(params, files) 419 import_locales(params, files)
403 420
(...skipping 12 matching lines...) Expand all
416 params, 'testIndex.html.tmpl', ('general', 'testScripts') 433 params, 'testIndex.html.tmpl', ('general', 'testScripts')
417 ) 434 )
418 435
419 zipdata = files.zipToString() 436 zipdata = files.zipToString()
420 signature = None 437 signature = None
421 pubkey = None 438 pubkey = None
422 if keyFile != None: 439 if keyFile != None:
423 signature = signBinary(zipdata, keyFile) 440 signature = signBinary(zipdata, keyFile)
424 pubkey = getPublicKey(keyFile) 441 pubkey = getPublicKey(keyFile)
425 writePackage(outFile, pubkey, signature, zipdata) 442 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld