| 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 /// Definitions used to run the polymer linter and deploy tools without using | |
| 6 /// pub serve or pub deploy. | |
| 7 library polymer.src.build.runner; | |
| 8 | |
| 9 import 'dart:async'; | |
| 10 import 'dart:convert'; | |
| 11 import 'dart:io'; | |
| 12 | |
| 13 import 'package:barback/barback.dart'; | |
| 14 import 'package:path/path.dart' as path; | |
| 15 import 'package:stack_trace/stack_trace.dart'; | |
| 16 import 'package:yaml/yaml.dart'; | |
| 17 | |
| 18 | |
| 19 /// Collects different parameters needed to configure and run barback. | |
| 20 class BarbackOptions { | |
| 21 /// Phases of transformers to run for the current package. | |
| 22 /// Use packagePhases to specify phases for other packages. | |
| 23 final List<List<Transformer>> phases; | |
| 24 | |
| 25 /// Package to treat as the current package in barback. | |
| 26 final String currentPackage; | |
| 27 | |
| 28 /// Directory root for the current package. | |
| 29 final String packageHome; | |
| 30 | |
| 31 /// Mapping between package names and the path in the file system where | |
| 32 /// to find the sources of such package. | |
| 33 final Map<String, String> packageDirs; | |
| 34 | |
| 35 /// Whether to run transformers on the test folder. | |
| 36 final bool transformTests; | |
| 37 | |
| 38 /// Directory where to generate code, if any. | |
| 39 final String outDir; | |
| 40 | |
| 41 /// Disregard files that match these filters when copying in non | |
| 42 /// transformed files | |
| 43 List<String> fileFilter; | |
| 44 | |
| 45 /// Whether to print error messages using a json-format that tools, such as | |
| 46 /// the Dart Editor, can process. | |
| 47 final bool machineFormat; | |
| 48 | |
| 49 /// Whether to follow symlinks when listing directories. By default this is | |
| 50 /// false because directories have symlinks for the packages directory created | |
| 51 /// by pub, but it can be turned on for custom uses of this library. | |
| 52 final bool followLinks; | |
| 53 | |
| 54 /// Phases of transformers to apply to packages other than the current | |
| 55 /// package, keyed by the package name. | |
| 56 final Map<String, List<List<Transformer>>> packagePhases; | |
| 57 | |
| 58 BarbackOptions(this.phases, this.outDir, {currentPackage, String packageHome, | |
| 59 packageDirs, this.transformTests: false, this.machineFormat: false, | |
| 60 this.followLinks: false, | |
| 61 this.packagePhases: const {}, | |
| 62 this.fileFilter: const []}) | |
| 63 : currentPackage = (currentPackage != null | |
| 64 ? currentPackage : readCurrentPackageFromPubspec()), | |
| 65 packageHome = packageHome, | |
| 66 packageDirs = (packageDirs != null | |
| 67 ? packageDirs : readPackageDirsFromPub(packageHome, currentPackage)); | |
| 68 | |
| 69 } | |
| 70 | |
| 71 /// Creates a barback system as specified by [options] and runs it. Returns a | |
| 72 /// future that contains the list of assets generated after barback runs to | |
| 73 /// completion. | |
| 74 Future<AssetSet> runBarback(BarbackOptions options) { | |
| 75 var barback = new Barback(new _PackageProvider(options.packageDirs)); | |
| 76 _initBarback(barback, options); | |
| 77 _attachListeners(barback, options); | |
| 78 if (options.outDir == null) return barback.getAllAssets(); | |
| 79 return _emitAllFiles(barback, options); | |
| 80 } | |
| 81 | |
| 82 /// Extract the current package from the pubspec.yaml file. | |
| 83 String readCurrentPackageFromPubspec([String dir]) { | |
| 84 var pubspec = new File( | |
| 85 dir == null ? 'pubspec.yaml' : path.join(dir, 'pubspec.yaml')); | |
| 86 if (!pubspec.existsSync()) { | |
| 87 print('error: pubspec.yaml file not found, please run this script from ' | |
| 88 'your package root directory.'); | |
| 89 return null; | |
| 90 } | |
| 91 return loadYaml(pubspec.readAsStringSync())['name']; | |
| 92 } | |
| 93 | |
| 94 /// Extract a mapping between package names and the path in the file system | |
| 95 /// which has the source of the package. This map will contain an entry for the | |
| 96 /// current package and everything it depends on (extracted via `pub | |
| 97 /// list-package-dirs`). | |
| 98 Map<String, String> readPackageDirsFromPub( | |
| 99 [String packageHome, String currentPackage]) { | |
| 100 var cachedDir = Directory.current; | |
| 101 if (packageHome != null) { | |
| 102 Directory.current = new Directory(packageHome); | |
| 103 } else { | |
| 104 packageHome = cachedDir.path; | |
| 105 } | |
| 106 | |
| 107 var dartExec = Platform.executable; | |
| 108 // If dartExec == dart, then dart and pub are in standard PATH. | |
| 109 var sdkDir = dartExec == 'dart' ? '' : path.dirname(dartExec); | |
| 110 var pub = path.join(sdkDir, Platform.isWindows ? 'pub.bat' : 'pub'); | |
| 111 var result = Process.runSync(pub, ['list-package-dirs']); | |
| 112 if (result.exitCode != 0) { | |
| 113 print("unexpected error invoking 'pub':"); | |
| 114 print(result.stdout); | |
| 115 print(result.stderr); | |
| 116 exit(result.exitCode); | |
| 117 } | |
| 118 var map = JSON.decode(result.stdout)["packages"]; | |
| 119 map.forEach((k, v) { map[k] = path.absolute(packageHome, path.dirname(v)); }); | |
| 120 | |
| 121 if (currentPackage == null) { | |
| 122 currentPackage = readCurrentPackageFromPubspec(packageHome); | |
| 123 } | |
| 124 map[currentPackage] = packageHome; | |
| 125 | |
| 126 Directory.current = cachedDir; | |
| 127 return map; | |
| 128 } | |
| 129 | |
| 130 bool shouldSkip(List<String> filters, String path) { | |
| 131 return filters.any((filter) => path.contains(filter)); | |
| 132 } | |
| 133 | |
| 134 /// Return the relative path of each file under [subDir] in [package]. | |
| 135 Iterable<String> _listPackageDir(String package, String subDir, | |
| 136 BarbackOptions options) { | |
| 137 var packageDir = options.packageDirs[package]; | |
| 138 if (packageDir == null) return const []; | |
| 139 var dir = new Directory(path.join(packageDir, subDir)); | |
| 140 if (!dir.existsSync()) return const []; | |
| 141 return dir.listSync(recursive: true, followLinks: options.followLinks) | |
| 142 .where((f) => f is File) | |
| 143 .where((f) => !shouldSkip(options.fileFilter, f.path)) | |
| 144 .map((f) => path.relative(f.path, from: packageDir)); | |
| 145 } | |
| 146 | |
| 147 /// A simple provider that reads files directly from the pub cache. | |
| 148 class _PackageProvider implements PackageProvider { | |
| 149 Map<String, String> packageDirs; | |
| 150 Iterable<String> get packages => packageDirs.keys; | |
| 151 | |
| 152 _PackageProvider(this.packageDirs); | |
| 153 | |
| 154 Future<Asset> getAsset(AssetId id) => new Future.value( | |
| 155 new Asset.fromPath(id, path.join(packageDirs[id.package], | |
| 156 _toSystemPath(id.path)))); | |
| 157 } | |
| 158 | |
| 159 /// Convert asset paths to system paths (Assets always use the posix style). | |
| 160 String _toSystemPath(String assetPath) { | |
| 161 if (path.Style.platform != path.Style.windows) return assetPath; | |
| 162 return path.joinAll(path.posix.split(assetPath)); | |
| 163 } | |
| 164 | |
| 165 /// Tell barback which transformers to use and which assets to process. | |
| 166 void _initBarback(Barback barback, BarbackOptions options) { | |
| 167 var assets = []; | |
| 168 void addAssets(String package, String subDir) { | |
| 169 for (var filepath in _listPackageDir(package, subDir, options)) { | |
| 170 assets.add(new AssetId(package, filepath)); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 for (var package in options.packageDirs.keys) { | |
| 175 // Notify barback to process anything under 'lib' and 'asset'. | |
| 176 addAssets(package, 'lib'); | |
| 177 addAssets(package, 'asset'); | |
| 178 | |
| 179 if (options.packagePhases.containsKey(package)) { | |
| 180 barback.updateTransformers(package, options.packagePhases[package]); | |
| 181 } | |
| 182 } | |
| 183 barback.updateTransformers(options.currentPackage, options.phases); | |
| 184 | |
| 185 // In case of the current package, include also 'web'. | |
| 186 addAssets(options.currentPackage, 'web'); | |
| 187 if (options.transformTests) addAssets(options.currentPackage, 'test'); | |
| 188 | |
| 189 // Add the sources after the transformers so all transformers are present | |
| 190 // when barback starts processing the assets. | |
| 191 barback.updateSources(assets); | |
| 192 } | |
| 193 | |
| 194 /// Attach error listeners on [barback] so we can report errors. | |
| 195 void _attachListeners(Barback barback, BarbackOptions options) { | |
| 196 // Listen for errors and results | |
| 197 barback.errors.listen((e) { | |
| 198 var trace = null; | |
| 199 if (e is Error) trace = e.stackTrace; | |
| 200 if (trace != null) { | |
| 201 print(Trace.format(trace)); | |
| 202 } | |
| 203 print('error running barback: $e'); | |
| 204 exit(1); | |
| 205 }); | |
| 206 | |
| 207 barback.results.listen((result) { | |
| 208 if (!result.succeeded) { | |
| 209 print("build failed with errors: ${result.errors}"); | |
| 210 exit(1); | |
| 211 } | |
| 212 }); | |
| 213 | |
| 214 barback.log.listen((entry) { | |
| 215 if (options.machineFormat) { | |
| 216 print(_jsonFormatter(entry)); | |
| 217 } else { | |
| 218 print(_consoleFormatter(entry)); | |
| 219 } | |
| 220 }); | |
| 221 } | |
| 222 | |
| 223 /// Emits all outputs of [barback] and copies files that we didn't process (like | |
| 224 /// dependent package's libraries). | |
| 225 Future _emitAllFiles(Barback barback, BarbackOptions options) { | |
| 226 return barback.getAllAssets().then((assets) { | |
| 227 // Delete existing output folder before we generate anything | |
| 228 var dir = new Directory(options.outDir); | |
| 229 if (dir.existsSync()) dir.deleteSync(recursive: true); | |
| 230 return _emitPackagesDir(options) | |
| 231 .then((_) => _emitTransformedFiles(assets, options)) | |
| 232 .then((_) => _addPackagesSymlinks(assets, options)) | |
| 233 .then((_) => assets); | |
| 234 }); | |
| 235 } | |
| 236 | |
| 237 Future _emitTransformedFiles(AssetSet assets, BarbackOptions options) { | |
| 238 // Copy all the assets we transformed | |
| 239 var futures = []; | |
| 240 var currentPackage = options.currentPackage; | |
| 241 var transformTests = options.transformTests; | |
| 242 var outPackages = path.join(options.outDir, 'packages'); | |
| 243 | |
| 244 return Future.forEach(assets, (asset) { | |
| 245 var id = asset.id; | |
| 246 var dir = _firstDir(id.path); | |
| 247 if (dir == null) return null; | |
| 248 | |
| 249 var filepath; | |
| 250 if (dir == 'lib') { | |
| 251 // Put lib files directly under the packages folder (e.g. 'lib/foo.dart' | |
| 252 // will be emitted at out/packages/package_name/foo.dart). | |
| 253 filepath = path.join(outPackages, id.package, | |
| 254 _toSystemPath(id.path.substring(4))); | |
| 255 } else if (id.package == currentPackage && | |
| 256 (dir == 'web' || (transformTests && dir == 'test'))) { | |
| 257 filepath = path.join(options.outDir, _toSystemPath(id.path)); | |
| 258 } else { | |
| 259 // TODO(sigmund): do something about other assets? | |
| 260 return null; | |
| 261 } | |
| 262 | |
| 263 return _writeAsset(filepath, asset); | |
| 264 }); | |
| 265 } | |
| 266 | |
| 267 /// Adds a package symlink from each directory under `out/web/foo/` to | |
| 268 /// `out/packages`. | |
| 269 void _addPackagesSymlinks(AssetSet assets, BarbackOptions options) { | |
| 270 var outPackages = path.join(options.outDir, 'packages'); | |
| 271 var currentPackage = options.currentPackage; | |
| 272 for (var asset in assets) { | |
| 273 var id = asset.id; | |
| 274 if (id.package != currentPackage) continue; | |
| 275 var firstDir = _firstDir(id.path); | |
| 276 if (firstDir == null) continue; | |
| 277 | |
| 278 if (firstDir == 'web' || (options.transformTests && firstDir == 'test')) { | |
| 279 var dir = path.join(options.outDir, path.dirname(_toSystemPath(id.path))); | |
| 280 var linkPath = path.join(dir, 'packages'); | |
| 281 var link = new Link(linkPath); | |
| 282 if (!link.existsSync()) { | |
| 283 var targetPath = Platform.operatingSystem == 'windows' | |
| 284 ? path.normalize(path.absolute(outPackages)) | |
| 285 : path.normalize(path.relative(outPackages, from: dir)); | |
| 286 link.createSync(targetPath); | |
| 287 } | |
| 288 } | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 /// Emits a 'packages' directory directly under `out/packages` with the contents | |
| 293 /// of every file that was not transformed by barback. | |
| 294 Future _emitPackagesDir(BarbackOptions options) { | |
| 295 var outPackages = path.join(options.outDir, 'packages'); | |
| 296 _ensureDir(outPackages); | |
| 297 | |
| 298 // Copy all the files we didn't process | |
| 299 var dirs = options.packageDirs; | |
| 300 return Future.forEach(dirs.keys, (package) { | |
| 301 return Future.forEach(_listPackageDir(package, 'lib', options), (relpath) { | |
| 302 var inpath = path.join(dirs[package], relpath); | |
| 303 var outpath = path.join(outPackages, package, relpath.substring(4)); | |
| 304 return _copyFile(inpath, outpath); | |
| 305 }); | |
| 306 }); | |
| 307 } | |
| 308 | |
| 309 /// Ensure [dirpath] exists. | |
| 310 void _ensureDir(String dirpath) { | |
| 311 new Directory(dirpath).createSync(recursive: true); | |
| 312 } | |
| 313 | |
| 314 /// Returns the first directory name on a url-style path, or null if there are | |
| 315 /// no slashes. | |
| 316 String _firstDir(String url) { | |
| 317 var firstSlash = url.indexOf('/'); | |
| 318 if (firstSlash == -1) return null; | |
| 319 return url.substring(0, firstSlash); | |
| 320 } | |
| 321 | |
| 322 /// Copy a file from [inpath] to [outpath]. | |
| 323 Future _copyFile(String inpath, String outpath) { | |
| 324 _ensureDir(path.dirname(outpath)); | |
| 325 return new File(inpath).openRead().pipe(new File(outpath).openWrite()); | |
| 326 } | |
| 327 | |
| 328 /// Write contents of an [asset] into a file at [filepath]. | |
| 329 Future _writeAsset(String filepath, Asset asset) { | |
| 330 _ensureDir(path.dirname(filepath)); | |
| 331 return asset.read().pipe(new File(filepath).openWrite()); | |
| 332 } | |
| 333 | |
| 334 String _kindFromEntry(LogEntry entry) { | |
| 335 var level = entry.level; | |
| 336 return level == LogLevel.ERROR ? 'error' | |
| 337 : (level == LogLevel.WARNING ? 'warning' : 'info'); | |
| 338 } | |
| 339 | |
| 340 /// Formatter that generates messages using a format that can be parsed | |
| 341 /// by tools, such as the Dart Editor, for reporting error messages. | |
| 342 String _jsonFormatter(LogEntry entry) { | |
| 343 var kind = _kindFromEntry(entry); | |
| 344 var span = entry.span; | |
| 345 return JSON.encode((span == null) | |
| 346 ? [{'method': kind, 'params': {'message': entry.message}}] | |
| 347 : [{'method': kind, | |
| 348 'params': { | |
| 349 'file': span.sourceUrl.toString(), | |
| 350 'message': entry.message, | |
| 351 'line': span.start.line + 1, | |
| 352 'charStart': span.start.offset, | |
| 353 'charEnd': span.end.offset, | |
| 354 }}]); | |
| 355 } | |
| 356 | |
| 357 /// Formatter that generates messages that are easy to read on the console (used | |
| 358 /// by default). | |
| 359 String _consoleFormatter(LogEntry entry) { | |
| 360 var kind = _kindFromEntry(entry); | |
| 361 var useColors = stdioType(stdout) == StdioType.TERMINAL; | |
| 362 var levelColor = (kind == 'error') ? _RED_COLOR : _MAGENTA_COLOR; | |
| 363 var output = new StringBuffer(); | |
| 364 if (useColors) output.write(levelColor); | |
| 365 output..write(kind)..write(' '); | |
| 366 if (useColors) output.write(_NO_COLOR); | |
| 367 if (entry.span == null) { | |
| 368 output.write(entry.message); | |
| 369 } else { | |
| 370 output.write(entry.span.message(entry.message, | |
| 371 color: useColors ? levelColor : null)); | |
| 372 } | |
| 373 return output.toString(); | |
| 374 } | |
| 375 | |
| 376 const String _RED_COLOR = '\u001b[31m'; | |
| 377 const String _MAGENTA_COLOR = '\u001b[35m'; | |
| 378 const String _NO_COLOR = '\u001b[0m'; | |
| OLD | NEW |