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 * Definitions used to run the polymer linter and deploy tools without using | 6 * Definitions used to run the polymer linter and deploy tools without using |
| 7 * pub serve or pub deploy. | 7 * pub serve or pub deploy. |
| 8 */ | 8 */ |
| 9 library polymer.src.build.runner; | 9 library polymer.src.build.runner; |
| 10 | 10 |
| 11 import 'dart:async'; | 11 import 'dart:async'; |
| 12 import 'dart:convert'; | 12 import 'dart:convert'; |
| 13 import 'dart:io'; | 13 import 'dart:io'; |
| 14 | 14 |
| 15 import 'package:barback/barback.dart'; | 15 import 'package:barback/barback.dart'; |
| 16 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
| 17 import 'package:stack_trace/stack_trace.dart'; | 17 import 'package:stack_trace/stack_trace.dart'; |
| 18 import 'package:yaml/yaml.dart'; | 18 import 'package:yaml/yaml.dart'; |
| 19 | 19 |
| 20 | 20 |
| 21 /** Collects different parameters needed to configure and run barback. */ | 21 /** Collects different parameters needed to configure and run barback. */ |
| 22 class BarbackOptions { | 22 class BarbackOptions { |
| 23 /** Phases of transformers to run. */ | 23 /** Phases of transformers to run for the current package. |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
nit: fix the multiline style of this comment (extr
blois
2014/02/12 02:39:27
Done.
| |
| 24 * Use packagePhases to specify phases for other packages. | |
| 25 */ | |
| 24 final List<List<Transformer>> phases; | 26 final List<List<Transformer>> phases; |
| 25 | 27 |
| 26 /** Package to treat as the current package in barback. */ | 28 /** Package to treat as the current package in barback. */ |
| 27 final String currentPackage; | 29 final String currentPackage; |
| 28 | 30 |
| 31 /** Directory root for the current package. */ | |
| 32 final String packageHome; | |
| 33 | |
| 29 /** | 34 /** |
| 30 * Mapping between package names and the path in the file system where | 35 * Mapping between package names and the path in the file system where |
| 31 * to find the sources of such package. | 36 * to find the sources of such package. |
| 32 */ | 37 */ |
| 33 final Map<String, String> packageDirs; | 38 final Map<String, String> packageDirs; |
| 34 | 39 |
| 35 /** Whether to run transformers on the test folder. */ | 40 /** Whether to run transformers on the test folder. */ |
| 36 final bool transformTests; | 41 final bool transformTests; |
| 37 | 42 |
| 38 /** Whether to apply transformers on polymer dependencies. */ | |
| 39 final bool transformPolymerDependencies; | |
| 40 | |
| 41 /** Directory where to generate code, if any. */ | 43 /** Directory where to generate code, if any. */ |
| 42 final String outDir; | 44 final String outDir; |
| 43 | 45 |
| 44 /** | 46 /** |
| 45 * Whether to print error messages using a json-format that tools, such as the | 47 * Whether to print error messages using a json-format that tools, such as the |
| 46 * Dart Editor, can process. | 48 * Dart Editor, can process. |
| 47 */ | 49 */ |
| 48 final bool machineFormat; | 50 final bool machineFormat; |
| 49 | 51 |
| 50 /** | 52 /** |
| 51 * Whether to follow symlinks when listing directories. By default this is | 53 * Whether to follow symlinks when listing directories. By default this is |
| 52 * false because directories have symlinks for the packages directory created | 54 * false because directories have symlinks for the packages directory created |
| 53 * by pub, but it can be turned on for custom uses of this library. | 55 * by pub, but it can be turned on for custom uses of this library. |
| 54 */ | 56 */ |
| 55 final bool followLinks; | 57 final bool followLinks; |
| 56 | 58 |
| 57 BarbackOptions(this.phases, this.outDir, {currentPackage, packageDirs, | 59 /** Phases of transformers to apply to packages other than the current |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
same here (style should be:
/**
* Phases ...
)
blois
2014/02/12 02:39:27
Done.
| |
| 58 this.transformTests: false, this.transformPolymerDependencies: false, | 60 * package, keyed by the package name. |
| 59 this.machineFormat: false, this.followLinks: false}) | 61 */ |
| 62 final Map<String, List<List<Transformer>>> packagePhases; | |
| 63 | |
| 64 BarbackOptions(this.phases, this.outDir, {currentPackage, packageHome, | |
| 65 packageDirs, this.transformTests: false, this.machineFormat: false, | |
| 66 this.followLinks: false, packagePhases}) | |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
lets add a type for packagePhases here
blois
2014/02/12 02:39:27
Done.
| |
| 60 : currentPackage = (currentPackage != null | 67 : currentPackage = (currentPackage != null |
| 61 ? currentPackage : readCurrentPackageFromPubspec()), | 68 ? currentPackage : readCurrentPackageFromPubspec()), |
| 62 packageDirs = (packageDirs != null | 69 packageDirs = (packageDirs != null |
| 63 ? packageDirs : _readPackageDirsFromPub(currentPackage)); | 70 ? packageDirs : readPackageDirsFromPub(packageHome)), |
| 71 packageHome = (packageHome != null | |
| 72 ? packageHome : Directory.current.path), | |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
you seem to already default to Directory.current i
blois
2014/02/12 02:39:27
Done.
| |
| 73 packagePhases = (packagePhases != null ? packagePhases : {}); | |
| 64 | 74 |
| 65 } | 75 } |
| 66 | 76 |
| 67 /** | 77 /** |
| 68 * Creates a barback system as specified by [options] and runs it. Returns a | 78 * Creates a barback system as specified by [options] and runs it. Returns a |
| 69 * future that contains the list of assets generated after barback runs to | 79 * future that contains the list of assets generated after barback runs to |
| 70 * completion. | 80 * completion. |
| 71 */ | 81 */ |
| 72 Future<AssetSet> runBarback(BarbackOptions options) { | 82 Future<AssetSet> runBarback(BarbackOptions options) { |
| 73 var barback = new Barback(new _PolymerPackageProvider(options.packageDirs)); | 83 var barback = new Barback(new _PackageProvider(options.packageDirs)); |
| 74 _initBarback(barback, options); | 84 _initBarback(barback, options); |
| 75 _attachListeners(barback, options); | 85 _attachListeners(barback, options); |
| 76 if (options.outDir == null) return barback.getAllAssets(); | 86 if (options.outDir == null) return barback.getAllAssets(); |
| 77 return _emitAllFiles(barback, options); | 87 return _emitAllFiles(barback, options); |
| 78 } | 88 } |
| 79 | 89 |
| 80 /** Extract the current package from the pubspec.yaml file. */ | 90 /** Extract the current package from the pubspec.yaml file. */ |
| 81 String readCurrentPackageFromPubspec([String dir]) { | 91 String readCurrentPackageFromPubspec([String dir]) { |
| 82 var pubspec = new File( | 92 var pubspec = new File( |
| 83 dir == null ? 'pubspec.yaml' : path.join(dir, 'pubspec.yaml')); | 93 dir == null ? 'pubspec.yaml' : path.join(dir, 'pubspec.yaml')); |
| 84 if (!pubspec.existsSync()) { | 94 if (!pubspec.existsSync()) { |
| 85 print('error: pubspec.yaml file not found, please run this script from ' | 95 print('error: pubspec.yaml file not found, please run this script from ' |
| 86 'your package root directory.'); | 96 'your package root directory.'); |
| 87 return null; | 97 return null; |
| 88 } | 98 } |
| 89 return loadYaml(pubspec.readAsStringSync())['name']; | 99 return loadYaml(pubspec.readAsStringSync())['name']; |
| 90 } | 100 } |
| 91 | 101 |
| 92 /** | 102 /** |
| 93 * Extract a mapping between package names and the path in the file system where | 103 * Extract a mapping between package names and the path in the file system where |
| 94 * to find the sources of such package. This map will contain an entry for the | 104 * to find the sources of such package. This map will contain an entry for the |
| 95 * current package and everything it depends on (extracted via `pub | 105 * current package and everything it depends on (extracted via `pub |
| 96 * list-package-dirs`). | 106 * list-package-dirs`). |
| 97 */ | 107 */ |
| 98 Map<String, String> _readPackageDirsFromPub(String currentPackage) { | 108 Map<String, String> readPackageDirsFromPub([String packageHome]) { |
| 109 var cachedDir = Directory.current; | |
| 110 if (packageHome != null) { | |
| 111 Directory.current = new Directory(packageHome); | |
| 112 } | |
| 113 | |
| 99 var dartExec = Platform.executable; | 114 var dartExec = Platform.executable; |
| 100 // If dartExec == dart, then dart and pub are in standard PATH. | 115 // If dartExec == dart, then dart and pub are in standard PATH. |
| 101 var sdkDir = dartExec == 'dart' ? '' : path.dirname(dartExec); | 116 var sdkDir = dartExec == 'dart' ? '' : path.dirname(dartExec); |
| 102 var pub = path.join(sdkDir, Platform.isWindows ? 'pub.bat' : 'pub'); | 117 var pub = path.join(sdkDir, Platform.isWindows ? 'pub.bat' : 'pub'); |
| 103 var result = Process.runSync(pub, ['list-package-dirs']); | 118 var result = Process.runSync(pub, ['list-package-dirs']); |
| 104 if (result.exitCode != 0) { | 119 if (result.exitCode != 0) { |
| 105 print("unexpected error invoking 'pub':"); | 120 print("unexpected error invoking 'pub':"); |
| 106 print(result.stdout); | 121 print(result.stdout); |
| 107 print(result.stderr); | 122 print(result.stderr); |
| 108 exit(result.exitCode); | 123 exit(result.exitCode); |
| 109 } | 124 } |
| 110 var map = JSON.decode(result.stdout)["packages"]; | 125 var map = JSON.decode(result.stdout)["packages"]; |
| 111 map.forEach((k, v) { map[k] = path.dirname(v); }); | 126 map.forEach((k, v) { map[k] = path.absolute(packageHome, path.dirname(v)); }); |
| 112 map[currentPackage] = '.'; | 127 map[readCurrentPackageFromPubspec(packageHome)] = packageHome; |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
seems that we are computing the current-package tw
blois
2014/02/12 02:39:27
Updated to only read if currentPackage is null.
| |
| 128 | |
| 129 Directory.current = cachedDir; | |
| 113 return map; | 130 return map; |
| 114 } | 131 } |
| 115 | 132 |
| 116 /** Internal packages used by polymer. */ | |
| 117 // TODO(sigmund): consider computing this list by recursively parsing | |
| 118 // pubspec.yaml files in the `Options.packageDirs`. | |
| 119 final Set<String> _polymerPackageDependencies = [ | |
| 120 'analyzer', 'args', 'barback', 'browser', 'custom_element', 'html5lib', | |
| 121 'html_import', 'js', 'logging', 'mutation_observer', 'observe', 'path' | |
| 122 'polymer_expressions', 'serialization', 'shadow_dom', 'source_maps', | |
| 123 'stack_trace', 'template_binding', 'unittest', 'unmodifiable_collection', | |
| 124 'yaml'].toSet(); | |
| 125 | |
| 126 /** Return the relative path of each file under [subDir] in [package]. */ | 133 /** Return the relative path of each file under [subDir] in [package]. */ |
| 127 Iterable<String> _listPackageDir(String package, String subDir, | 134 Iterable<String> _listPackageDir(String package, String subDir, |
| 128 BarbackOptions options) { | 135 BarbackOptions options) { |
| 129 var packageDir = options.packageDirs[package]; | 136 var packageDir = options.packageDirs[package]; |
| 130 if (packageDir == null) return const []; | 137 if (packageDir == null) return const []; |
| 131 var dir = new Directory(path.join(packageDir, subDir)); | 138 var dir = new Directory(path.join(packageDir, subDir)); |
| 132 if (!dir.existsSync()) return const []; | 139 if (!dir.existsSync()) return const []; |
| 133 return dir.listSync(recursive: true, followLinks: options.followLinks) | 140 return dir.listSync(recursive: true, followLinks: options.followLinks) |
| 134 .where((f) => f is File) | 141 .where((f) => f is File) |
| 135 .map((f) => path.relative(f.path, from: packageDir)); | 142 .map((f) => path.relative(f.path, from: packageDir)); |
| 136 } | 143 } |
| 137 | 144 |
| 138 /** A simple provider that reads files directly from the pub cache. */ | 145 /** A simple provider that reads files directly from the pub cache. */ |
| 139 class _PolymerPackageProvider implements PackageProvider { | 146 class _PackageProvider implements PackageProvider { |
| 140 Map<String, String> packageDirs; | 147 Map<String, String> packageDirs; |
| 141 Iterable<String> get packages => packageDirs.keys; | 148 Iterable<String> get packages => packageDirs.keys; |
| 142 | 149 |
| 143 _PolymerPackageProvider(this.packageDirs); | 150 _PackageProvider(this.packageDirs); |
| 144 | 151 |
| 145 Future<Asset> getAsset(AssetId id) => new Future.value( | 152 Future<Asset> getAsset(AssetId id) => new Future.value( |
| 146 new Asset.fromPath(id, path.join(packageDirs[id.package], | 153 new Asset.fromPath(id, path.join(packageDirs[id.package], |
| 147 _toSystemPath(id.path)))); | 154 _toSystemPath(id.path)))); |
| 148 } | 155 } |
| 149 | 156 |
| 150 /** Convert asset paths to system paths (Assets always use the posix style). */ | 157 /** Convert asset paths to system paths (Assets always use the posix style). */ |
| 151 String _toSystemPath(String assetPath) { | 158 String _toSystemPath(String assetPath) { |
| 152 if (path.Style.platform != path.Style.windows) return assetPath; | 159 if (path.Style.platform != path.Style.windows) return assetPath; |
| 153 return path.joinAll(path.posix.split(assetPath)); | 160 return path.joinAll(path.posix.split(assetPath)); |
| 154 } | 161 } |
| 155 | 162 |
| 156 /** Tell barback which transformers to use and which assets to process. */ | 163 /** Tell barback which transformers to use and which assets to process. */ |
| 157 void _initBarback(Barback barback, BarbackOptions options) { | 164 void _initBarback(Barback barback, BarbackOptions options) { |
| 158 var assets = []; | 165 var assets = []; |
| 159 void addAssets(String package, String subDir) { | 166 void addAssets(String package, String subDir) { |
| 160 for (var filepath in _listPackageDir(package, subDir, options)) { | 167 for (var filepath in _listPackageDir(package, subDir, options)) { |
| 161 assets.add(new AssetId(package, filepath)); | 168 assets.add(new AssetId(package, filepath)); |
| 162 } | 169 } |
| 163 } | 170 } |
| 164 | 171 |
| 165 for (var package in options.packageDirs.keys) { | 172 for (var package in options.packageDirs.keys) { |
| 166 // There is nothing to do in the polymer package dependencies. | |
| 167 // However: in Polymer package *itself*, we need to replace Observable | |
| 168 // with ChangeNotifier. | |
| 169 if (!options.transformPolymerDependencies && | |
| 170 _polymerPackageDependencies.contains(package)) continue; | |
| 171 barback.updateTransformers(package, options.phases); | |
| 172 | |
| 173 // Notify barback to process anything under 'lib' and 'asset'. | 173 // Notify barback to process anything under 'lib' and 'asset'. |
| 174 addAssets(package, 'lib'); | 174 addAssets(package, 'lib'); |
| 175 addAssets(package, 'asset'); | 175 addAssets(package, 'asset'); |
| 176 | |
| 177 if (options.packagePhases.containsKey(package)) { | |
| 178 barback.updateTransformers(package, options.packagePhases[package]); | |
| 179 } | |
| 176 } | 180 } |
| 181 barback.updateTransformers(options.currentPackage, options.phases); | |
| 177 | 182 |
| 178 // In case of the current package, include also 'web'. | 183 // In case of the current package, include also 'web'. |
| 179 addAssets(options.currentPackage, 'web'); | 184 addAssets(options.currentPackage, 'web'); |
| 180 if (options.transformTests) addAssets(options.currentPackage, 'test'); | 185 if (options.transformTests) addAssets(options.currentPackage, 'test'); |
| 181 | 186 |
| 182 barback.updateSources(assets); | 187 barback.updateSources(assets); |
|
Siggi Cherem (dart-lang)
2014/02/11 23:15:20
would you mind adding a small comment here saying
blois
2014/02/12 02:39:27
Done.
| |
| 183 } | 188 } |
| 184 | 189 |
| 185 /** Attach error listeners on [barback] so we can report errors. */ | 190 /** Attach error listeners on [barback] so we can report errors. */ |
| 186 void _attachListeners(Barback barback, BarbackOptions options) { | 191 void _attachListeners(Barback barback, BarbackOptions options) { |
| 187 // Listen for errors and results | 192 // Listen for errors and results |
| 188 barback.errors.listen((e) { | 193 barback.errors.listen((e) { |
| 189 var trace = null; | 194 var trace = null; |
| 190 if (e is Error) trace = e.stackTrace; | 195 if (e is Error) trace = e.stackTrace; |
| 191 if (trace != null) { | 196 if (trace != null) { |
| 192 print(Trace.format(trace)); | 197 print(Trace.format(trace)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 206 if (options.machineFormat) { | 211 if (options.machineFormat) { |
| 207 print(_jsonFormatter(entry)); | 212 print(_jsonFormatter(entry)); |
| 208 } else { | 213 } else { |
| 209 print(_consoleFormatter(entry)); | 214 print(_consoleFormatter(entry)); |
| 210 } | 215 } |
| 211 }); | 216 }); |
| 212 } | 217 } |
| 213 | 218 |
| 214 /** | 219 /** |
| 215 * Emits all outputs of [barback] and copies files that we didn't process (like | 220 * Emits all outputs of [barback] and copies files that we didn't process (like |
| 216 * polymer's libraries). | 221 * dependent package's libraries). |
| 217 */ | 222 */ |
| 218 Future _emitAllFiles(Barback barback, BarbackOptions options) { | 223 Future _emitAllFiles(Barback barback, BarbackOptions options) { |
| 219 return barback.getAllAssets().then((assets) { | 224 return barback.getAllAssets().then((assets) { |
| 220 // Delete existing output folder before we generate anything | 225 // Delete existing output folder before we generate anything |
| 221 var dir = new Directory(options.outDir); | 226 var dir = new Directory(options.outDir); |
| 222 if (dir.existsSync()) dir.deleteSync(recursive: true); | 227 if (dir.existsSync()) dir.deleteSync(recursive: true); |
| 223 return _emitPackagesDir(options) | 228 return _emitPackagesDir(options) |
| 224 .then((_) => _emitTransformedFiles(assets, options)) | 229 .then((_) => _emitTransformedFiles(assets, options)) |
| 225 .then((_) => _addPackagesSymlinks(assets, options)) | 230 .then((_) => _addPackagesSymlinks(assets, options)) |
| 226 .then((_) => assets); | 231 .then((_) => assets); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 } | 287 } |
| 283 } | 288 } |
| 284 } | 289 } |
| 285 } | 290 } |
| 286 | 291 |
| 287 /** | 292 /** |
| 288 * Emits a 'packages' directory directly under `out/packages` with the contents | 293 * Emits a 'packages' directory directly under `out/packages` with the contents |
| 289 * of every file that was not transformed by barback. | 294 * of every file that was not transformed by barback. |
| 290 */ | 295 */ |
| 291 Future _emitPackagesDir(BarbackOptions options) { | 296 Future _emitPackagesDir(BarbackOptions options) { |
| 292 if (options.transformPolymerDependencies) return new Future.value(null); | |
| 293 var outPackages = path.join(options.outDir, 'packages'); | 297 var outPackages = path.join(options.outDir, 'packages'); |
| 294 _ensureDir(outPackages); | 298 _ensureDir(outPackages); |
| 295 | 299 |
| 296 // Copy all the files we didn't process | 300 // Copy all the files we didn't process |
| 297 var dirs = options.packageDirs; | 301 var dirs = options.packageDirs; |
| 298 | 302 |
| 299 return Future.forEach(_polymerPackageDependencies, (package) { | 303 return Future.forEach(dirs.keys, (package) { |
| 300 return Future.forEach(_listPackageDir(package, 'lib', options), (relpath) { | 304 return Future.forEach(_listPackageDir(package, 'lib', options), (relpath) { |
| 301 var inpath = path.join(dirs[package], relpath); | 305 var inpath = path.join(dirs[package], relpath); |
| 302 var outpath = path.join(outPackages, package, relpath.substring(4)); | 306 var outpath = path.join(outPackages, package, relpath.substring(4)); |
| 303 return _copyFile(inpath, outpath); | 307 return _copyFile(inpath, outpath); |
| 304 }); | 308 }); |
| 305 }); | 309 }); |
| 306 } | 310 } |
| 307 | 311 |
| 308 /** Ensure [dirpath] exists. */ | 312 /** Ensure [dirpath] exists. */ |
| 309 void _ensureDir(String dirpath) { | 313 void _ensureDir(String dirpath) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 output.write(entry.span.getLocationMessage(entry.message, | 379 output.write(entry.span.getLocationMessage(entry.message, |
| 376 useColors: useColors, | 380 useColors: useColors, |
| 377 color: levelColor)); | 381 color: levelColor)); |
| 378 } | 382 } |
| 379 return output.toString(); | 383 return output.toString(); |
| 380 } | 384 } |
| 381 | 385 |
| 382 const String _RED_COLOR = '\u001b[31m'; | 386 const String _RED_COLOR = '\u001b[31m'; |
| 383 const String _MAGENTA_COLOR = '\u001b[35m'; | 387 const String _MAGENTA_COLOR = '\u001b[35m'; |
| 384 const String _NO_COLOR = '\u001b[0m'; | 388 const String _NO_COLOR = '\u001b[0m'; |
| OLD | NEW |