Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Temporary deploy command used to create a version of the app that can be | 6 * Temporary deploy command used to create a version of the app that can be |
| 7 * compiled with dart2js and deployed. This library should go away once `pub | 7 * compiled with dart2js and deployed. This library should go away once `pub |
| 8 * deploy` can be configured to run barback transformers. | 8 * deploy` can be configured to run barback transformers. |
| 9 * | 9 * |
| 10 * From an application package you can run this program by calling dart with a | 10 * From an application package you can run this program by calling dart with a |
| 11 * 'package:' url to this file: | 11 * 'package:' url to this file: |
| 12 * | 12 * |
| 13 * dart package:polymer/deploy.dart | 13 * dart package:polymer/deploy.dart |
| 14 */ | 14 */ |
| 15 library polymer.deploy; | 15 library polymer.deploy; |
| 16 | 16 |
| 17 import 'dart:async'; | 17 import 'dart:async'; |
| 18 import 'dart:convert'; | 18 import 'dart:convert'; |
| 19 import 'dart:io'; | 19 import 'dart:io'; |
| 20 | 20 |
| 21 import 'package:barback/barback.dart'; | 21 import 'package:barback/barback.dart'; |
| 22 import 'package:path/path.dart' as path; | 22 import 'package:path/path.dart' as path; |
| 23 import 'package:polymer/src/transform.dart' show phases; | 23 import 'package:polymer/src/transform.dart' show phases; |
| 24 import 'package:stack_trace/stack_trace.dart'; | 24 import 'package:stack_trace/stack_trace.dart'; |
| 25 import 'package:yaml/yaml.dart'; | 25 import 'package:yaml/yaml.dart'; |
| 26 import 'package:args/args.dart'; | 26 import 'package:args/args.dart'; |
| 27 | 27 |
| 28 main() { | 28 main() { |
| 29 var args = _parseArgs(new Options().arguments); | 29 var args = _parseArgs(new Options().arguments); |
|
kustermann
2013/09/04 12:02:23
I haven't looked at all of this, but I strongly ho
| |
| 30 if (args == null) return; | 30 if (args == null) return; |
| 31 | |
| 32 var test = args['test']; | |
| 33 if (test != null) { | |
| 34 _initForTest(test); | |
| 35 } | |
| 36 | |
| 31 print('polymer/deploy.dart: creating a deploy target for "$_currentPackage"'); | 37 print('polymer/deploy.dart: creating a deploy target for "$_currentPackage"'); |
| 32 var outDir = args['out']; | 38 var outDir = args['out']; |
| 33 _run(args['webdir'], outDir).then( | 39 _run(outDir, test != null).then( |
| 34 (_) => print('Done! All files written to "$outDir"')); | 40 (_) => print('Done! All files written to "$outDir"')); |
| 35 } | 41 } |
| 36 | 42 |
| 43 // TODO(jmesserly): the current deploy/barback architecture is very unfriendly | |
| 44 // to deploying a single test. We need to fix it somehow but it isn't clear yet. | |
| 45 void _initForTest(String testFile) { | |
| 46 var testDir = path.normalize(path.dirname(testFile)); | |
| 47 | |
| 48 // A test must be allowed to import things in the package. | |
| 49 // So we must find its package root, given the entry point. We can do this | |
| 50 // by walking up to find pubspec.yaml. | |
| 51 var pubspecDir = _findDirWithFile(path.absolute(testDir), 'pubspec.yaml'); | |
| 52 if (pubspecDir == null) { | |
| 53 print('error: pubspec.yaml file not found, please run this script from ' | |
| 54 'your package root directory or a subdirectory.'); | |
| 55 exit(1); | |
| 56 } | |
| 57 | |
| 58 _currentPackage = '_test'; | |
| 59 _packageDirs = {'_test' : pubspecDir}; | |
| 60 } | |
| 61 | |
| 62 String _findDirWithFile(String dir, String filename) { | |
| 63 while (!new File(path.join(dir, filename)).existsSync()) { | |
| 64 var parentDir = path.dirname(dir); | |
| 65 // If we reached root and failed to find it, bail. | |
| 66 if (parentDir == dir) return null; | |
| 67 dir = parentDir; | |
| 68 } | |
| 69 return dir; | |
| 70 } | |
| 71 | |
| 37 /** | 72 /** |
| 38 * API exposed for testing purposes. Runs this deploy command but prentend that | 73 * API exposed for testing purposes. Runs this deploy command but prentend that |
| 39 * the sources under [webDir] belong to package 'test'. | 74 * the sources under [webDir] belong to package 'test'. |
| 40 */ | 75 */ |
| 41 Future runForTest(String webDir, String outDir) { | 76 Future runForTest(String webDir, String outDir) { |
| 42 _currentPackage = 'test'; | 77 _currentPackage = 'test'; |
| 43 | 78 |
| 44 // associate package dirs with their location in the repo: | 79 // associate package dirs with their location in the repo: |
| 45 _packageDirs = {'test' : '.'}; | 80 _packageDirs = {'test' : '.'}; |
| 46 addPackages(String dir) { | 81 _addPackages('..'); |
| 47 for (var packageDir in new Directory(dir).listSync().map((d) => d.path)) { | 82 _addPackages('../third_party'); |
| 48 _packageDirs[path.basename(packageDir)] = packageDir; | 83 _addPackages('../../third_party/pkg'); |
| 49 } | |
| 50 } | |
| 51 addPackages('..'); | |
| 52 addPackages('../third_party'); | |
| 53 addPackages('../../third_party/pkg'); | |
| 54 return _run(webDir, outDir); | 84 return _run(webDir, outDir); |
| 55 } | 85 } |
| 56 | 86 |
| 57 Future _run(String webDir, String outDir) { | 87 _addPackages(String dir) { |
| 88 for (var packageDir in new Directory(dir).listSync().map((d) => d.path)) { | |
| 89 _packageDirs[path.basename(packageDir)] = packageDir; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 Future _run(String outDir, bool includeTests) { | |
| 58 var barback = new Barback(new _PolymerDeployProvider()); | 94 var barback = new Barback(new _PolymerDeployProvider()); |
| 59 _initializeBarback(barback, webDir); | 95 _initializeBarback(barback, includeTests); |
| 60 _attachListeners(barback); | 96 _attachListeners(barback); |
| 61 return _emitAllFiles(barback, webDir, outDir); | 97 return _emitAllFiles(barback, 'web', outDir).then( |
| 98 (_) => includeTests ? _emitAllFiles(barback, 'test', outDir) : null); | |
| 62 } | 99 } |
| 63 | 100 |
| 64 /** Tell barback which transformers to use and which assets to process. */ | 101 /** Tell barback which transformers to use and which assets to process. */ |
| 65 void _initializeBarback(Barback barback, String webDir) { | 102 void _initializeBarback(Barback barback, bool includeTests) { |
| 66 var assets = []; | 103 var assets = []; |
| 104 void addAssets(String package, String subDir) { | |
| 105 for (var filepath in _listDir(package, subDir)) { | |
| 106 assets.add(new AssetId(package, filepath)); | |
| 107 } | |
| 108 } | |
| 109 | |
| 67 for (var package in _packageDirs.keys) { | 110 for (var package in _packageDirs.keys) { |
| 68 // Do not process packages like 'polymer' where there is nothing to do. | 111 // Do not process packages like 'polymer' where there is nothing to do. |
| 69 if (_ignoredPackages.contains(package)) continue; | 112 if (_ignoredPackages.contains(package)) continue; |
| 70 barback.updateTransformers(package, phases); | 113 barback.updateTransformers(package, phases); |
| 71 | 114 |
| 72 // notify barback to process anything under 'lib' and 'asset' | 115 // notify barback to process anything under 'lib' and 'asset' |
| 73 for (var filepath in _listDir(package, 'lib')) { | 116 addAssets(package, 'lib'); |
| 74 assets.add(new AssetId(package, filepath)); | 117 addAssets(package, 'asset'); |
| 75 } | |
| 76 | |
| 77 for (var filepath in _listDir(package, 'asset')) { | |
| 78 assets.add(new AssetId(package, filepath)); | |
| 79 } | |
| 80 } | 118 } |
| 81 | 119 |
| 82 // In case of the current package, include also 'web'. | 120 // In case of the current package, include also 'web'. |
| 83 for (var filepath in _listDir(_currentPackage, webDir)) { | 121 addAssets(_currentPackage, 'web'); |
| 84 assets.add(new AssetId(_currentPackage, filepath)); | 122 if (includeTests) addAssets(_currentPackage, 'test'); |
| 85 } | 123 |
| 86 barback.updateSources(assets); | 124 barback.updateSources(assets); |
| 87 } | 125 } |
| 88 | 126 |
| 89 /** Return the relative path of each file under [subDir] in a [package]. */ | 127 /** Return the relative path of each file under [subDir] in a [package]. */ |
| 90 Iterable<String> _listDir(String package, String subDir) { | 128 Iterable<String> _listDir(String package, String subDir) { |
| 91 var packageDir = _packageDirs[package]; | 129 var packageDir = _packageDirs[package]; |
| 92 if (packageDir == null) return const []; | 130 if (packageDir == null) return const []; |
| 93 var dir = new Directory(path.join(packageDir, subDir)); | 131 var dir = new Directory(path.join(packageDir, subDir)); |
| 94 if (!dir.existsSync()) return const []; | 132 if (!dir.existsSync()) return const []; |
| 95 return dir.listSync(recursive: true, followLinks: false) | 133 return dir.listSync(recursive: true, followLinks: false) |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 (const [ 'analyzer_experimental', 'args', 'barback', 'browser', 'csslib', | 261 (const [ 'analyzer_experimental', 'args', 'barback', 'browser', 'csslib', |
| 224 'custom_element', 'fancy_syntax', 'html5lib', 'html_import', 'js', | 262 'custom_element', 'fancy_syntax', 'html5lib', 'html_import', 'js', |
| 225 'logging', 'mdv', 'meta', 'mutation_observer', 'observe', 'path', | 263 'logging', 'mdv', 'meta', 'mutation_observer', 'observe', 'path', |
| 226 'polymer', 'polymer_expressions', 'serialization', 'shadow_dom', | 264 'polymer', 'polymer_expressions', 'serialization', 'shadow_dom', |
| 227 'source_maps', 'stack_trace', 'unittest', | 265 'source_maps', 'stack_trace', 'unittest', |
| 228 'unmodifiable_collection', 'yaml' | 266 'unmodifiable_collection', 'yaml' |
| 229 ]).toSet(); | 267 ]).toSet(); |
| 230 | 268 |
| 231 ArgResults _parseArgs(arguments) { | 269 ArgResults _parseArgs(arguments) { |
| 232 var parser = new ArgParser() | 270 var parser = new ArgParser() |
| 233 ..addFlag('help', abbr: 'h', help: 'Displays this help message', | 271 ..addFlag('help', abbr: 'h', help: 'Displays this help message.', |
| 234 defaultsTo: false, negatable: false) | 272 defaultsTo: false, negatable: false) |
| 235 ..addOption('webdir', help: 'Directory containing the application', | 273 ..addOption('out', abbr: 'o', help: 'Directory where to generated files.', |
| 236 defaultsTo: 'web') | 274 defaultsTo: 'out') |
| 237 ..addOption('out', abbr: 'o', help: 'Directory where to generated files', | 275 ..addOption('test', help: 'Deploy the test at the given path.\n' |
| 238 defaultsTo: 'out'); | 276 'Note: currently this will deploy all tests in its directory,\n' |
| 277 'but it will eventually deploy only the specified test.'); | |
| 239 try { | 278 try { |
| 240 var results = parser.parse(arguments); | 279 var results = parser.parse(arguments); |
| 241 if (results['help']) { | 280 if (results['help']) { |
| 242 _showUsage(parser); | 281 _showUsage(parser); |
| 243 return null; | 282 return null; |
| 244 } | 283 } |
| 245 return results; | 284 return results; |
| 246 } on FormatException catch (e) { | 285 } on FormatException catch (e) { |
| 247 print(e.message); | 286 print(e.message); |
| 248 _showUsage(parser); | 287 _showUsage(parser); |
| 249 return null; | 288 return null; |
| 250 } | 289 } |
| 251 } | 290 } |
| 252 | 291 |
| 253 _showUsage(parser) { | 292 _showUsage(parser) { |
| 254 print('Usage: dart package:polymer/deploy.dart [options]'); | 293 print('Usage: dart package:polymer/deploy.dart [options]'); |
| 255 print(parser.getUsage()); | 294 print(parser.getUsage()); |
| 256 } | 295 } |
| OLD | NEW |