| 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.barback_runner; | 9 library polymer.src.barback_runner; |
| 10 | 10 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 exit(1); | 179 exit(1); |
| 180 } | 180 } |
| 181 }); | 181 }); |
| 182 } | 182 } |
| 183 | 183 |
| 184 /** | 184 /** |
| 185 * Emits all outputs of [barback] and copies files that we didn't process (like | 185 * Emits all outputs of [barback] and copies files that we didn't process (like |
| 186 * polymer's libraries). | 186 * polymer's libraries). |
| 187 */ | 187 */ |
| 188 Future _emitAllFiles(Barback barback, BarbackOptions options) { | 188 Future _emitAllFiles(Barback barback, BarbackOptions options) { |
| 189 return _emitFiles(barback, options, 'web').then((res) { | 189 return barback.getAllAssets().then((assets) { |
| 190 if (options.transformTests) return _emitFiles(barback, options, 'test'); | 190 return _emitPackagesDir(options) |
| 191 return res; | 191 .then((_) => _emitTransformedFiles(assets, options)) |
| 192 .then((_) => _addPackagesSymlinks(assets, options)) |
| 193 .then((_) => assets); |
| 192 }); | 194 }); |
| 193 } | 195 } |
| 194 | 196 |
| 195 Future _emitFiles(Barback barback, BarbackOptions options, String emitSubDir) { | 197 Future _emitTransformedFiles(AssetSet assets, BarbackOptions options) { |
| 196 return barback.getAllAssets().then((assets) { | 198 // Copy all the assets we transformed |
| 197 // Copy all the assets we transformed | 199 var futures = []; |
| 198 var futures = []; | 200 var currentPackage = options.currentPackage; |
| 199 for (var asset in assets) { | 201 var transformTests = options.transformTests; |
| 200 var id = asset.id; | 202 var outPackages = path.join(options.outDir, 'packages'); |
| 201 var filepath; | 203 for (var asset in assets) { |
| 202 if (id.package == options.currentPackage && | 204 var id = asset.id; |
| 203 id.path.startsWith('$emitSubDir/')) { | 205 var dir = _firstDir(id.path); |
| 204 filepath = path.join(options.outDir, id.path); | 206 if (dir == null) continue; |
| 205 } else if (id.path.startsWith('lib/')) { | |
| 206 filepath = path.join(options.outDir, emitSubDir, 'packages', id.package, | |
| 207 id.path.substring(4)); | |
| 208 } else { | |
| 209 // TODO(sigmund): do something about other assets? | |
| 210 continue; | |
| 211 } | |
| 212 | 207 |
| 213 _ensureDir(path.dirname(filepath)); | 208 var filepath; |
| 214 var writer = new File(filepath).openWrite(); | 209 if (dir == 'lib') { |
| 215 futures.add(writer.addStream(asset.read()).then((_) => writer.close())); | 210 // Put lib files directly under the packages folder (e.g. 'lib/foo.dart' |
| 211 // will be emitted at out/packages/package_name/foo.dart). |
| 212 filepath = path.join(outPackages, id.package, id.path.substring(4)); |
| 213 } else if (id.package == currentPackage && |
| 214 (dir == 'web' || (transformTests && dir == 'test'))) { |
| 215 filepath = path.join(options.outDir, id.path); |
| 216 } else { |
| 217 // TODO(sigmund): do something about other assets? |
| 218 continue; |
| 216 } | 219 } |
| 217 return Future.wait(futures).then((_) { | |
| 218 // Copy also all the files we didn't process | |
| 219 var futures = []; | |
| 220 for (var package in _polymerPackageDependencies) { | |
| 221 for (var relpath in _listPackageDir(package, 'lib', options)) { | |
| 222 var inpath = path.join(options.packageDirs[package], relpath); | |
| 223 var outpath = path.join(options.outDir, emitSubDir, | |
| 224 'packages', package, relpath.substring(4)); | |
| 225 _ensureDir(path.dirname(outpath)); | |
| 226 | 220 |
| 227 var writer = new File(outpath).openWrite(); | 221 futures.add(_writeAsset(filepath, asset)); |
| 228 futures.add(writer.addStream(new File(inpath).openRead()) | 222 } |
| 229 .then((_) => writer.close())); | 223 return Future.wait(futures); |
| 230 } | 224 } |
| 231 } | 225 |
| 232 return Future.wait(futures); | 226 /** |
| 233 }).then((_) => assets); | 227 * Adds a package symlink from each directory under `out/web/foo/` to |
| 234 }); | 228 * `out/packages`. |
| 229 */ |
| 230 Future _addPackagesSymlinks(AssetSet assets, BarbackOptions options) { |
| 231 var outPackages = path.join(options.outDir, 'packages'); |
| 232 var currentPackage = options.currentPackage; |
| 233 for (var asset in assets) { |
| 234 var id = asset.id; |
| 235 if (id.package != currentPackage) continue; |
| 236 var firstDir = _firstDir(id.path); |
| 237 if (firstDir == null) continue; |
| 238 |
| 239 if (firstDir == 'web' || (options.transformTests && firstDir == 'test')) { |
| 240 var dir = path.join(options.outDir, path.dirname(id.path)); |
| 241 var linkPath = path.join(dir, 'packages'); |
| 242 var targetPath = path.relative(outPackages, from: dir); |
| 243 _deleteIfPresent(linkPath); |
| 244 new Link(linkPath).createSync(targetPath); |
| 245 } |
| 246 } |
| 247 } |
| 248 |
| 249 /** |
| 250 * Emits a 'packages' directory directly under `out/packages` with the contents |
| 251 * of every file that was not transformed by barback. |
| 252 */ |
| 253 Future _emitPackagesDir(BarbackOptions options) { |
| 254 // Ensure we don't have a packages symlink in our output folder (could happen |
| 255 // when people are using nested packages). |
| 256 var outPackages = path.join(options.outDir, 'packages'); |
| 257 _deleteIfPresent(outPackages); |
| 258 |
| 259 if (options.transformPolymerDependencies) return new Future.value(null); |
| 260 |
| 261 // Copy all the files we didn't process |
| 262 var futures = []; |
| 263 var dirs = options.packageDirs; |
| 264 for (var package in _polymerPackageDependencies) { |
| 265 for (var relpath in _listPackageDir(package, 'lib', options)) { |
| 266 var inpath = path.join(dirs[package], relpath); |
| 267 var outpath = path.join(outPackages, package, relpath.substring(4)); |
| 268 futures.add(_copyFile(inpath, outpath)); |
| 269 } |
| 270 } |
| 271 return Future.wait(futures); |
| 235 } | 272 } |
| 236 | 273 |
| 237 /** Ensure [dirpath] exists. */ | 274 /** Ensure [dirpath] exists. */ |
| 238 void _ensureDir(var dirpath) { | 275 void _ensureDir(String dirpath) { |
| 239 new Directory(dirpath).createSync(recursive: true); | 276 new Directory(dirpath).createSync(recursive: true); |
| 240 } | 277 } |
| 278 |
| 279 /** Deletes [packagesPath] if it's a packages symlink, file, or folder. */ |
| 280 void _deleteIfPresent(String packagesPath) { |
| 281 try { |
| 282 var link = new Link(packagesPath); |
| 283 if (link.existsSync()) { |
| 284 link.deleteSync(); |
| 285 return; |
| 286 } |
| 287 |
| 288 var dir = new Directory(packagesPath); |
| 289 if (dir.existsSync()) { |
| 290 dir.deleteSync(recursive: true); |
| 291 return; |
| 292 } |
| 293 |
| 294 var file = new File(packagesPath); |
| 295 if (file.existsSync()) { |
| 296 file.deleteSync(); |
| 297 } |
| 298 } catch (e) { |
| 299 print('Error while deleting $pacakgesPath: $e'); |
| 300 } |
| 301 } |
| 302 |
| 303 /** |
| 304 * Returns the first directory name on a url-style path, or null if there are no |
| 305 * slashes. |
| 306 */ |
| 307 String _firstDir(String url) { |
| 308 var firstSlash = url.indexOf('/'); |
| 309 if (firstSlash == -1) return null; |
| 310 return url.substring(0, firstSlash); |
| 311 } |
| 312 |
| 313 /** Copy a file from [inpath] to [outpath]. */ |
| 314 Future _copyFile(String inpath, String outpath) { |
| 315 _ensureDir(path.dirname(outpath)); |
| 316 var writer = new File(outpath).openWrite(); |
| 317 return writer.addStream(new File(inpath).openRead()) |
| 318 .then((_) => writer.close()); |
| 319 } |
| 320 |
| 321 /** Write contents of an [asset] into a file at [filepath]. */ |
| 322 Future _writeAsset(String filepath, Asset asset) { |
| 323 _ensureDir(path.dirname(filepath)); |
| 324 var writer = new File(filepath).openWrite(); |
| 325 return writer.addStream(asset.read()).then((_) => writer.close()); |
| 326 } |
| OLD | NEW |