| 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:io'; | 18 import 'dart:io'; |
| 19 import 'dart:json' as json; | 19 import 'dart:json' as json; |
| 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(); | 29 var args = _parseArgs(new Options().arguments); |
| 30 if (args == null) return; | 30 if (args == null) return; |
| 31 print('polymer/deploy.dart: creating a deploy target for "$_currentPackage"'); | 31 print('polymer/deploy.dart: creating a deploy target for "$_currentPackage"'); |
| 32 var outDir = args['out']; |
| 33 _run(args['webdir'], outDir).then( |
| 34 (_) => print('Done! All files written to "$outDir"')); |
| 35 } |
| 36 |
| 37 /** |
| 38 * API exposed for testing purposes. Runs this deploy command but prentend that |
| 39 * the sources under [webDir] belong to package 'test'. |
| 40 */ |
| 41 Future runForTest(String webDir, String outDir) { |
| 42 _currentPackage = 'test'; |
| 43 return _run(webDir, outDir); |
| 44 } |
| 45 |
| 46 Future _run(String webDir, String outDir) { |
| 32 var barback = new Barback(new _PolymerDeployProvider()); | 47 var barback = new Barback(new _PolymerDeployProvider()); |
| 33 _initializeBarback(barback); | 48 _initializeBarback(barback, webDir); |
| 34 _attachListeners(barback); | 49 _attachListeners(barback); |
| 35 _emitAllFiles(barback, args['out']); | 50 return _emitAllFiles(barback, webDir, outDir); |
| 36 } | 51 } |
| 37 | 52 |
| 38 /** Tell barback which transformers to use and which assets to process. */ | 53 /** Tell barback which transformers to use and which assets to process. */ |
| 39 void _initializeBarback(Barback barback) { | 54 void _initializeBarback(Barback barback, String webDir) { |
| 40 var assets = []; | 55 var assets = []; |
| 41 for (var package in _packageDirs.keys) { | 56 for (var package in _packageDirs.keys) { |
| 42 // Do not process packages like 'polymer' where there is nothing to do. | 57 // Do not process packages like 'polymer' where there is nothing to do. |
| 43 if (_ignoredPackages.contains(package)) continue; | 58 if (_ignoredPackages.contains(package)) continue; |
| 44 barback.updateTransformers(package, phases); | 59 barback.updateTransformers(package, phases); |
| 45 | 60 |
| 46 // notify barback to process anything under 'lib' and 'asset' | 61 // notify barback to process anything under 'lib' and 'asset' |
| 47 for (var filepath in _listDir(package, 'lib')) { | 62 for (var filepath in _listDir(package, 'lib')) { |
| 48 assets.add(new AssetId(package, filepath)); | 63 assets.add(new AssetId(package, filepath)); |
| 49 } | 64 } |
| 50 | 65 |
| 51 for (var filepath in _listDir(package, 'asset')) { | 66 for (var filepath in _listDir(package, 'asset')) { |
| 52 assets.add(new AssetId(package, filepath)); | 67 assets.add(new AssetId(package, filepath)); |
| 53 } | 68 } |
| 54 } | 69 } |
| 55 | 70 |
| 56 // In case of the current package, include also 'web'. | 71 // In case of the current package, include also 'web'. |
| 57 for (var filepath in _listDir(_currentPackage, 'web')) { | 72 for (var filepath in _listDir(_currentPackage, webDir)) { |
| 58 assets.add(new AssetId(_currentPackage, filepath)); | 73 assets.add(new AssetId(_currentPackage, filepath)); |
| 59 } | 74 } |
| 60 barback.updateSources(assets); | 75 barback.updateSources(assets); |
| 61 } | 76 } |
| 62 | 77 |
| 63 /** Return the relative path of each file under [subDir] in a [package]. */ | 78 /** Return the relative path of each file under [subDir] in a [package]. */ |
| 64 Iterable<String> _listDir(String package, String subDir) { | 79 Iterable<String> _listDir(String package, String subDir) { |
| 65 var packageDir = _packageDirs[package]; | 80 var packageDir = _packageDirs[package]; |
| 66 if (packageDir == null) return const []; | 81 if (packageDir == null) return const []; |
| 67 var dir = new Directory(path.join(packageDir, subDir)); | 82 var dir = new Directory(path.join(packageDir, subDir)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 93 | 108 |
| 94 /** Ensure [dirpath] exists. */ | 109 /** Ensure [dirpath] exists. */ |
| 95 void _ensureDir(var dirpath) { | 110 void _ensureDir(var dirpath) { |
| 96 new Directory(dirpath).createSync(recursive: true); | 111 new Directory(dirpath).createSync(recursive: true); |
| 97 } | 112 } |
| 98 | 113 |
| 99 /** | 114 /** |
| 100 * Emits all outputs of [barback] and copies files that we didn't process (like | 115 * Emits all outputs of [barback] and copies files that we didn't process (like |
| 101 * polymer's libraries). | 116 * polymer's libraries). |
| 102 */ | 117 */ |
| 103 Future _emitAllFiles(Barback barback, String outDir) { | 118 Future _emitAllFiles(Barback barback, String webDir, String outDir) { |
| 104 return barback.getAllAssets().then((assets) { | 119 return barback.getAllAssets().then((assets) { |
| 105 // Copy all the assets we transformed | 120 // Copy all the assets we transformed |
| 106 var futures = []; | 121 var futures = []; |
| 107 for (var asset in assets) { | 122 for (var asset in assets) { |
| 108 var id = asset.id; | 123 var id = asset.id; |
| 109 var filepath; | 124 var filepath; |
| 110 if (id.package == _currentPackage && id.path.startsWith('web/')) { | 125 if (id.package == _currentPackage && id.path.startsWith('$webDir/')) { |
| 111 filepath = path.join(outDir, id.path); | 126 filepath = path.join(outDir, id.path); |
| 112 } else if (id.path.startsWith('lib/')) { | 127 } else if (id.path.startsWith('lib/')) { |
| 113 filepath = path.join(outDir, 'web', 'packages', id.package, | 128 filepath = path.join(outDir, webDir, 'packages', id.package, |
| 114 id.path.substring(4)); | 129 id.path.substring(4)); |
| 115 } else { | 130 } else { |
| 116 // TODO(sigmund): do something about other assets? | 131 // TODO(sigmund): do something about other assets? |
| 117 continue; | 132 continue; |
| 118 } | 133 } |
| 119 | 134 |
| 120 _ensureDir(path.dirname(filepath)); | 135 _ensureDir(path.dirname(filepath)); |
| 121 var writer = new File(filepath).openWrite(); | 136 var writer = new File(filepath).openWrite(); |
| 122 futures.add(writer.addStream(asset.read()).then((_) => writer.close())); | 137 futures.add(writer.addStream(asset.read()).then((_) => writer.close())); |
| 123 } | 138 } |
| 124 return Future.wait(futures); | 139 return Future.wait(futures); |
| 125 }).then((_) { | 140 }).then((_) { |
| 126 // Copy also all the files we didn't process | 141 // Copy also all the files we didn't process |
| 127 var futures = []; | 142 var futures = []; |
| 128 for (var package in _ignoredPackages) { | 143 for (var package in _ignoredPackages) { |
| 129 for (var relpath in _listDir(package, 'lib')) { | 144 for (var relpath in _listDir(package, 'lib')) { |
| 130 var inpath = path.join(_packageDirs[package], relpath); | 145 var inpath = path.join(_packageDirs[package], relpath); |
| 131 var outpath = path.join(outDir, 'web', 'packages', package, | 146 var outpath = path.join(outDir, webDir, 'packages', package, |
| 132 relpath.substring(4)); | 147 relpath.substring(4)); |
| 133 _ensureDir(path.dirname(outpath)); | 148 _ensureDir(path.dirname(outpath)); |
| 134 | 149 |
| 135 var writer = new File(outpath).openWrite(); | 150 var writer = new File(outpath).openWrite(); |
| 136 futures.add(writer.addStream(new File(inpath).openRead()) | 151 futures.add(writer.addStream(new File(inpath).openRead()) |
| 137 .then((_) => writer.close())); | 152 .then((_) => writer.close())); |
| 138 } | 153 } |
| 139 } | 154 } |
| 140 return Future.wait(futures) | 155 return Future.wait(futures); |
| 141 .then((_) => print('Done! All files written to "$outDir"')); | |
| 142 }); | 156 }); |
| 143 } | 157 } |
| 144 | 158 |
| 145 /** A simple provider that reads files directly from the pub cache. */ | 159 /** A simple provider that reads files directly from the pub cache. */ |
| 146 class _PolymerDeployProvider implements PackageProvider { | 160 class _PolymerDeployProvider implements PackageProvider { |
| 147 | 161 |
| 148 Iterable<String> get packages => _packageDirs.keys; | 162 Iterable<String> get packages => _packageDirs.keys; |
| 149 _PolymerDeployProvider(); | 163 _PolymerDeployProvider(); |
| 150 | 164 |
| 151 Future<Asset> getAsset(AssetId id) => | 165 Future<Asset> getAsset(AssetId id) => |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 map[_currentPackage] = '.'; | 201 map[_currentPackage] = '.'; |
| 188 return map; | 202 return map; |
| 189 }(); | 203 }(); |
| 190 | 204 |
| 191 /** | 205 /** |
| 192 * Internal packages used by polymer which we can copy directly to the output | 206 * Internal packages used by polymer which we can copy directly to the output |
| 193 * folder without having to process them with barback. | 207 * folder without having to process them with barback. |
| 194 */ | 208 */ |
| 195 // TODO(sigmund): consider computing this list by recursively parsing | 209 // TODO(sigmund): consider computing this list by recursively parsing |
| 196 // pubspec.yaml files in the [_packageDirs]. | 210 // pubspec.yaml files in the [_packageDirs]. |
| 197 Set<String> _ignoredPackages = | 211 final Set<String> _ignoredPackages = |
| 198 (const [ 'analyzer_experimental', 'args', 'barback', 'browser', 'csslib', | 212 (const [ 'analyzer_experimental', 'args', 'barback', 'browser', 'csslib', |
| 199 'custom_element', 'fancy_syntax', 'html5lib', 'html_import', 'js', | 213 'custom_element', 'fancy_syntax', 'html5lib', 'html_import', 'js', |
| 200 'logging', 'mdv', 'meta', 'mutation_observer', 'observe', 'path', | 214 'logging', 'mdv', 'meta', 'mutation_observer', 'observe', 'path', |
| 201 'polymer', 'polymer_expressions', 'serialization', 'shadow_dom', | 215 'polymer', 'polymer_expressions', 'serialization', 'shadow_dom', |
| 202 'source_maps', 'stack_trace', 'unittest', | 216 'source_maps', 'stack_trace', 'unittest', |
| 203 'unmodifiable_collection', 'yaml' | 217 'unmodifiable_collection', 'yaml' |
| 204 ]).toSet(); | 218 ]).toSet(); |
| 205 | 219 |
| 206 ArgResults _parseArgs() { | 220 ArgResults _parseArgs(arguments) { |
| 207 var parser = new ArgParser() | 221 var parser = new ArgParser() |
| 208 ..addFlag('help', abbr: 'h', help: 'Displays this help message', | 222 ..addFlag('help', abbr: 'h', help: 'Displays this help message', |
| 209 defaultsTo: false, negatable: false) | 223 defaultsTo: false, negatable: false) |
| 224 ..addOption('webdir', help: 'Directory containing the application', |
| 225 defaultsTo: 'web') |
| 210 ..addOption('out', abbr: 'o', help: 'Directory where to generated files', | 226 ..addOption('out', abbr: 'o', help: 'Directory where to generated files', |
| 211 defaultsTo: 'out'); | 227 defaultsTo: 'out'); |
| 212 try { | 228 try { |
| 213 var results = parser.parse(new Options().arguments); | 229 var results = parser.parse(arguments); |
| 214 if (results['help']) { | 230 if (results['help']) { |
| 215 _showUsage(parser); | 231 _showUsage(parser); |
| 216 return null; | 232 return null; |
| 217 } | 233 } |
| 218 return results; | 234 return results; |
| 219 } on FormatException catch (e) { | 235 } on FormatException catch (e) { |
| 220 print(e.message); | 236 print(e.message); |
| 221 _showUsage(parser); | 237 _showUsage(parser); |
| 222 return null; | 238 return null; |
| 223 } | 239 } |
| 224 } | 240 } |
| 225 | 241 |
| 226 _showUsage(parser) { | 242 _showUsage(parser) { |
| 227 print('Usage: dart package:polymer/deploy.dart [options]'); | 243 print('Usage: dart package:polymer/deploy.dart [options]'); |
| 228 print(parser.getUsage()); | 244 print(parser.getUsage()); |
| 229 } | 245 } |
| OLD | NEW |