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