| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Definitions used to run the polymer linter and deploy tools without using | |
| 7 * pub serve or pub deploy. | |
| 8 */ | |
| 9 library polymer.src.barback_runner; | |
| 10 | |
| 11 import 'dart:async'; | |
| 12 import 'dart:convert'; | |
| 13 import 'dart:io'; | |
| 14 | |
| 15 import 'package:args/args.dart'; | |
| 16 import 'package:barback/barback.dart'; | |
| 17 import 'package:path/path.dart' as path; | |
| 18 import 'package:stack_trace/stack_trace.dart'; | |
| 19 import 'package:yaml/yaml.dart'; | |
| 20 | |
| 21 | |
| 22 /** Collects different parameters needed to configure and run barback. */ | |
| 23 class BarbackOptions { | |
| 24 /** Phases of transformers to run. */ | |
| 25 final List<List<Transformer>> phases; | |
| 26 | |
| 27 /** Package to treat as the current package in barback. */ | |
| 28 final String currentPackage; | |
| 29 | |
| 30 /** | |
| 31 * Mapping between package names and the path in the file system where | |
| 32 * to find the sources of such package. | |
| 33 */ | |
| 34 final Map<String, String> packageDirs; | |
| 35 | |
| 36 /** Whether to run transformers on the test folder. */ | |
| 37 final bool transformTests; | |
| 38 | |
| 39 /** Whether to apply transformers on polymer dependencies. */ | |
| 40 final bool transformPolymerDependencies; | |
| 41 | |
| 42 /** Directory where to generate code, if any. */ | |
| 43 final String outDir; | |
| 44 | |
| 45 BarbackOptions(this.phases, this.outDir, {currentPackage, packageDirs, | |
| 46 this.transformTests: false, this.transformPolymerDependencies: false}) | |
| 47 : currentPackage = (currentPackage != null | |
| 48 ? currentPackage : readCurrentPackageFromPubspec()), | |
| 49 packageDirs = (packageDirs != null | |
| 50 ? packageDirs : _readPackageDirsFromPub(currentPackage)); | |
| 51 | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Creates a barback system as specified by [options] and runs it. Returns a | |
| 56 * future that contains the list of assets generated after barback runs to | |
| 57 * completion. | |
| 58 */ | |
| 59 Future<AssetSet> runBarback(BarbackOptions options) { | |
| 60 var barback = new Barback(new _PolymerPackageProvider(options.packageDirs)); | |
| 61 _initBarback(barback, options); | |
| 62 _attachListeners(barback); | |
| 63 if (options.outDir == null) return barback.getAllAssets(); | |
| 64 return _emitAllFiles(barback, options); | |
| 65 } | |
| 66 | |
| 67 /** Extract the current package from the pubspec.yaml file. */ | |
| 68 String readCurrentPackageFromPubspec() { | |
| 69 var pubspec = new File('pubspec.yaml'); | |
| 70 if (!pubspec.existsSync()) { | |
| 71 print('error: pubspec.yaml file not found, please run this script from ' | |
| 72 'your package root directory.'); | |
| 73 return null; | |
| 74 } | |
| 75 return loadYaml(pubspec.readAsStringSync())['name']; | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Extract a mapping between package names and the path in the file system where | |
| 80 * to find the sources of such package. This map will contain an entry for the | |
| 81 * current package and everything it depends on (extracted via `pub | |
| 82 * list-pacakge-dirs`). | |
| 83 */ | |
| 84 Map<String, String> _readPackageDirsFromPub(String currentPackage) { | |
| 85 var dartExec = new Options().executable; | |
| 86 // If dartExec == dart, then dart and pub are in standard PATH. | |
| 87 var sdkDir = dartExec == 'dart' ? '' : path.dirname(dartExec); | |
| 88 var pub = path.join(sdkDir, Platform.isWindows ? 'pub.bat' : 'pub'); | |
| 89 var result = Process.runSync(pub, ['list-package-dirs']); | |
| 90 if (result.exitCode != 0) { | |
| 91 print("unexpected error invoking 'pub':"); | |
| 92 print(result.stdout); | |
| 93 print(result.stderr); | |
| 94 exit(result.exitCode); | |
| 95 } | |
| 96 var map = JSON.decode(result.stdout)["packages"]; | |
| 97 map.forEach((k, v) { map[k] = path.dirname(v); }); | |
| 98 map[currentPackage] = '.'; | |
| 99 return map; | |
| 100 } | |
| 101 | |
| 102 /** Internal packages used by polymer. */ | |
| 103 // TODO(sigmund): consider computing this list by recursively parsing | |
| 104 // pubspec.yaml files in the `Options.packageDirs`. | |
| 105 final Set<String> _polymerPackageDependencies = [ | |
| 106 'analyzer_experimental', 'args', 'barback', 'browser', 'csslib', | |
| 107 'custom_element', 'fancy_syntax', 'html5lib', 'html_import', 'js', | |
| 108 'logging', 'mdv', 'meta', 'mutation_observer', 'observe', 'path', 'polymer', | |
| 109 'polymer_expressions', 'serialization', 'shadow_dom', 'source_maps', | |
| 110 'stack_trace', 'unittest', 'unmodifiable_collection', 'yaml'].toSet(); | |
| 111 | |
| 112 /** Return the relative path of each file under [subDir] in [package]. */ | |
| 113 Iterable<String> _listPackageDir(String package, String subDir, | |
| 114 BarbackOptions options) { | |
| 115 var packageDir = options.packageDirs[package]; | |
| 116 if (packageDir == null) return const []; | |
| 117 var dir = new Directory(path.join(packageDir, subDir)); | |
| 118 if (!dir.existsSync()) return const []; | |
| 119 return dir.listSync(recursive: true, followLinks: false) | |
| 120 .where((f) => f is File) | |
| 121 .map((f) => path.relative(f.path, from: packageDir)); | |
| 122 } | |
| 123 | |
| 124 /** A simple provider that reads files directly from the pub cache. */ | |
| 125 class _PolymerPackageProvider implements PackageProvider { | |
| 126 Map<String, String> packageDirs; | |
| 127 Iterable<String> get packages => packageDirs.keys; | |
| 128 | |
| 129 _PolymerPackageProvider(this.packageDirs); | |
| 130 | |
| 131 Future<Asset> getAsset(AssetId id) => new Future.value( | |
| 132 new Asset.fromPath(id, path.join(packageDirs[id.package], | |
| 133 _toSystemPath(id.path)))); | |
| 134 } | |
| 135 | |
| 136 /** Convert asset paths to system paths (Assets always use the posix style). */ | |
| 137 String _toSystemPath(String assetPath) { | |
| 138 if (path.Style.platform != path.Style.windows) return assetPath; | |
| 139 return path.joinAll(path.posix.split(assetPath)); | |
| 140 } | |
| 141 | |
| 142 /** Tell barback which transformers to use and which assets to process. */ | |
| 143 void _initBarback(Barback barback, BarbackOptions options) { | |
| 144 var assets = []; | |
| 145 void addAssets(String package, String subDir) { | |
| 146 for (var filepath in _listPackageDir(package, subDir, options)) { | |
| 147 assets.add(new AssetId(package, filepath)); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 for (var package in options.packageDirs.keys) { | |
| 152 // There is nothing to do in the 'polymer' package and its dependencies. | |
| 153 if (!options.transformPolymerDependencies && | |
| 154 _polymerPackageDependencies.contains(package)) continue; | |
| 155 barback.updateTransformers(package, options.phases); | |
| 156 | |
| 157 // Notify barback to process anything under 'lib' and 'asset'. | |
| 158 addAssets(package, 'lib'); | |
| 159 addAssets(package, 'asset'); | |
| 160 } | |
| 161 | |
| 162 // In case of the current package, include also 'web'. | |
| 163 addAssets(options.currentPackage, 'web'); | |
| 164 if (options.transformTests) addAssets(options.currentPackage, 'test'); | |
| 165 | |
| 166 barback.updateSources(assets); | |
| 167 } | |
| 168 | |
| 169 /** Attach error listeners on [barback] so we can report errors. */ | |
| 170 void _attachListeners(Barback barback) { | |
| 171 // Listen for errors and results | |
| 172 barback.errors.listen((e) { | |
| 173 var trace = getAttachedStackTrace(e); | |
| 174 if (trace != null) { | |
| 175 print(Trace.format(trace)); | |
| 176 } | |
| 177 print('error running barback: $e'); | |
| 178 exit(1); | |
| 179 }); | |
| 180 | |
| 181 barback.results.listen((result) { | |
| 182 if (!result.succeeded) { | |
| 183 print("build failed with errors: ${result.errors}"); | |
| 184 exit(1); | |
| 185 } | |
| 186 }); | |
| 187 } | |
| 188 | |
| 189 /** | |
| 190 * Emits all outputs of [barback] and copies files that we didn't process (like | |
| 191 * polymer's libraries). | |
| 192 */ | |
| 193 Future _emitAllFiles(Barback barback, BarbackOptions options) { | |
| 194 return barback.getAllAssets().then((assets) { | |
| 195 // Delete existing output folder before we generate anything | |
| 196 var dir = new Directory(options.outDir); | |
| 197 if (dir.existsSync()) dir.deleteSync(recursive: true); | |
| 198 return _emitPackagesDir(options) | |
| 199 .then((_) => _emitTransformedFiles(assets, options)) | |
| 200 .then((_) => _addPackagesSymlinks(assets, options)) | |
| 201 .then((_) => assets); | |
| 202 }); | |
| 203 } | |
| 204 | |
| 205 Future _emitTransformedFiles(AssetSet assets, BarbackOptions options) { | |
| 206 // Copy all the assets we transformed | |
| 207 var futures = []; | |
| 208 var currentPackage = options.currentPackage; | |
| 209 var transformTests = options.transformTests; | |
| 210 var outPackages = path.join(options.outDir, 'packages'); | |
| 211 for (var asset in assets) { | |
| 212 var id = asset.id; | |
| 213 var dir = _firstDir(id.path); | |
| 214 if (dir == null) continue; | |
| 215 | |
| 216 var filepath; | |
| 217 if (dir == 'lib') { | |
| 218 // Put lib files directly under the packages folder (e.g. 'lib/foo.dart' | |
| 219 // will be emitted at out/packages/package_name/foo.dart). | |
| 220 filepath = path.join(outPackages, id.package, | |
| 221 _toSystemPath(id.path.substring(4))); | |
| 222 } else if (id.package == currentPackage && | |
| 223 (dir == 'web' || (transformTests && dir == 'test'))) { | |
| 224 filepath = path.join(options.outDir, _toSystemPath(id.path)); | |
| 225 } else { | |
| 226 // TODO(sigmund): do something about other assets? | |
| 227 continue; | |
| 228 } | |
| 229 | |
| 230 futures.add(_writeAsset(filepath, asset)); | |
| 231 } | |
| 232 return Future.wait(futures); | |
| 233 } | |
| 234 | |
| 235 /** | |
| 236 * Adds a package symlink from each directory under `out/web/foo/` to | |
| 237 * `out/packages`. | |
| 238 */ | |
| 239 Future _addPackagesSymlinks(AssetSet assets, BarbackOptions options) { | |
| 240 var outPackages = path.join(options.outDir, 'packages'); | |
| 241 var currentPackage = options.currentPackage; | |
| 242 for (var asset in assets) { | |
| 243 var id = asset.id; | |
| 244 if (id.package != currentPackage) continue; | |
| 245 var firstDir = _firstDir(id.path); | |
| 246 if (firstDir == null) continue; | |
| 247 | |
| 248 if (firstDir == 'web' || (options.transformTests && firstDir == 'test')) { | |
| 249 var dir = path.join(options.outDir, path.dirname(_toSystemPath(id.path))); | |
| 250 var linkPath = path.join(dir, 'packages'); | |
| 251 var link = new Link(linkPath); | |
| 252 if (!link.existsSync()) { | |
| 253 var targetPath = Platform.operatingSystem == 'windows' | |
| 254 ? path.normalize(path.absolute(outPackages)) | |
| 255 : path.normalize(path.relative(outPackages, from: dir)); | |
| 256 link.createSync(targetPath); | |
| 257 } | |
| 258 } | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * Emits a 'packages' directory directly under `out/packages` with the contents | |
| 264 * of every file that was not transformed by barback. | |
| 265 */ | |
| 266 Future _emitPackagesDir(BarbackOptions options) { | |
| 267 if (options.transformPolymerDependencies) return new Future.value(null); | |
| 268 var outPackages = path.join(options.outDir, 'packages'); | |
| 269 _ensureDir(outPackages); | |
| 270 | |
| 271 // Copy all the files we didn't process | |
| 272 var futures = []; | |
| 273 var dirs = options.packageDirs; | |
| 274 for (var package in _polymerPackageDependencies) { | |
| 275 for (var relpath in _listPackageDir(package, 'lib', options)) { | |
| 276 var inpath = path.join(dirs[package], relpath); | |
| 277 var outpath = path.join(outPackages, package, relpath.substring(4)); | |
| 278 futures.add(_copyFile(inpath, outpath)); | |
| 279 } | |
| 280 } | |
| 281 return Future.wait(futures); | |
| 282 } | |
| 283 | |
| 284 /** Ensure [dirpath] exists. */ | |
| 285 void _ensureDir(String dirpath) { | |
| 286 new Directory(dirpath).createSync(recursive: true); | |
| 287 } | |
| 288 | |
| 289 /** | |
| 290 * Returns the first directory name on a url-style path, or null if there are no | |
| 291 * slashes. | |
| 292 */ | |
| 293 String _firstDir(String url) { | |
| 294 var firstSlash = url.indexOf('/'); | |
| 295 if (firstSlash == -1) return null; | |
| 296 return url.substring(0, firstSlash); | |
| 297 } | |
| 298 | |
| 299 /** Copy a file from [inpath] to [outpath]. */ | |
| 300 Future _copyFile(String inpath, String outpath) { | |
| 301 _ensureDir(path.dirname(outpath)); | |
| 302 var writer = new File(outpath).openWrite(); | |
| 303 return writer.addStream(new File(inpath).openRead()) | |
| 304 .then((_) => writer.close()); | |
| 305 } | |
| 306 | |
| 307 /** Write contents of an [asset] into a file at [filepath]. */ | |
| 308 Future _writeAsset(String filepath, Asset asset) { | |
| 309 _ensureDir(path.dirname(filepath)); | |
| 310 var writer = new File(filepath).openWrite(); | |
| 311 return writer.addStream(asset.read()).then((_) => writer.close()); | |
| 312 } | |
| OLD | NEW |