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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 | 123 |
124 /** A simple provider that reads files directly from the pub cache. */ | 124 /** A simple provider that reads files directly from the pub cache. */ |
125 class _PolymerPackageProvider implements PackageProvider { | 125 class _PolymerPackageProvider implements PackageProvider { |
126 Map<String, String> packageDirs; | 126 Map<String, String> packageDirs; |
127 Iterable<String> get packages => packageDirs.keys; | 127 Iterable<String> get packages => packageDirs.keys; |
128 | 128 |
129 _PolymerPackageProvider(this.packageDirs); | 129 _PolymerPackageProvider(this.packageDirs); |
130 | 130 |
131 Future<Asset> getAsset(AssetId id) => new Future.value( | 131 Future<Asset> getAsset(AssetId id) => new Future.value( |
132 new Asset.fromPath(id, path.join(packageDirs[id.package], | 132 new Asset.fromPath(id, path.join(packageDirs[id.package], |
133 // Assets always use the posix style paths | 133 _toSystemPath(id.path)))); |
134 path.joinAll(path.posix.split(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)); | |
135 } | 140 } |
136 | 141 |
137 /** Tell barback which transformers to use and which assets to process. */ | 142 /** Tell barback which transformers to use and which assets to process. */ |
138 void _initBarback(Barback barback, BarbackOptions options) { | 143 void _initBarback(Barback barback, BarbackOptions options) { |
139 var assets = []; | 144 var assets = []; |
140 void addAssets(String package, String subDir) { | 145 void addAssets(String package, String subDir) { |
141 for (var filepath in _listPackageDir(package, subDir, options)) { | 146 for (var filepath in _listPackageDir(package, subDir, options)) { |
142 assets.add(new AssetId(package, filepath)); | 147 assets.add(new AssetId(package, filepath)); |
143 } | 148 } |
144 } | 149 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 } | 185 } |
181 }); | 186 }); |
182 } | 187 } |
183 | 188 |
184 /** | 189 /** |
185 * Emits all outputs of [barback] and copies files that we didn't process (like | 190 * Emits all outputs of [barback] and copies files that we didn't process (like |
186 * polymer's libraries). | 191 * polymer's libraries). |
187 */ | 192 */ |
188 Future _emitAllFiles(Barback barback, BarbackOptions options) { | 193 Future _emitAllFiles(Barback barback, BarbackOptions options) { |
189 return barback.getAllAssets().then((assets) { | 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); | |
Siggi Cherem (dart-lang)
2013/09/12 22:18:05
this other change is also unnecessary, but I think
| |
190 return _emitPackagesDir(options) | 198 return _emitPackagesDir(options) |
191 .then((_) => _emitTransformedFiles(assets, options)) | 199 .then((_) => _emitTransformedFiles(assets, options)) |
192 .then((_) => _addPackagesSymlinks(assets, options)) | 200 .then((_) => _addPackagesSymlinks(assets, options)) |
193 .then((_) => assets); | 201 .then((_) => assets); |
194 }); | 202 }); |
195 } | 203 } |
196 | 204 |
197 Future _emitTransformedFiles(AssetSet assets, BarbackOptions options) { | 205 Future _emitTransformedFiles(AssetSet assets, BarbackOptions options) { |
198 // Copy all the assets we transformed | 206 // Copy all the assets we transformed |
199 var futures = []; | 207 var futures = []; |
200 var currentPackage = options.currentPackage; | 208 var currentPackage = options.currentPackage; |
201 var transformTests = options.transformTests; | 209 var transformTests = options.transformTests; |
202 var outPackages = path.join(options.outDir, 'packages'); | 210 var outPackages = path.join(options.outDir, 'packages'); |
203 for (var asset in assets) { | 211 for (var asset in assets) { |
204 var id = asset.id; | 212 var id = asset.id; |
205 var dir = _firstDir(id.path); | 213 var dir = _firstDir(id.path); |
206 if (dir == null) continue; | 214 if (dir == null) continue; |
207 | 215 |
208 var filepath; | 216 var filepath; |
209 if (dir == 'lib') { | 217 if (dir == 'lib') { |
210 // Put lib files directly under the packages folder (e.g. 'lib/foo.dart' | 218 // Put lib files directly under the packages folder (e.g. 'lib/foo.dart' |
211 // will be emitted at out/packages/package_name/foo.dart). | 219 // will be emitted at out/packages/package_name/foo.dart). |
212 filepath = path.join(outPackages, id.package, id.path.substring(4)); | 220 filepath = path.join(outPackages, id.package, |
221 _toSystemPath(id.path.substring(4))); | |
213 } else if (id.package == currentPackage && | 222 } else if (id.package == currentPackage && |
214 (dir == 'web' || (transformTests && dir == 'test'))) { | 223 (dir == 'web' || (transformTests && dir == 'test'))) { |
215 filepath = path.join(options.outDir, id.path); | 224 filepath = path.join(options.outDir, _toSystemPath(id.path)); |
216 } else { | 225 } else { |
217 // TODO(sigmund): do something about other assets? | 226 // TODO(sigmund): do something about other assets? |
218 continue; | 227 continue; |
219 } | 228 } |
220 | 229 |
221 futures.add(_writeAsset(filepath, asset)); | 230 futures.add(_writeAsset(filepath, asset)); |
222 } | 231 } |
223 return Future.wait(futures); | 232 return Future.wait(futures); |
224 } | 233 } |
225 | 234 |
226 /** | 235 /** |
227 * Adds a package symlink from each directory under `out/web/foo/` to | 236 * Adds a package symlink from each directory under `out/web/foo/` to |
228 * `out/packages`. | 237 * `out/packages`. |
229 */ | 238 */ |
230 Future _addPackagesSymlinks(AssetSet assets, BarbackOptions options) { | 239 Future _addPackagesSymlinks(AssetSet assets, BarbackOptions options) { |
231 var outPackages = path.join(options.outDir, 'packages'); | 240 var outPackages = path.join(options.outDir, 'packages'); |
232 var currentPackage = options.currentPackage; | 241 var currentPackage = options.currentPackage; |
233 for (var asset in assets) { | 242 for (var asset in assets) { |
234 var id = asset.id; | 243 var id = asset.id; |
235 if (id.package != currentPackage) continue; | 244 if (id.package != currentPackage) continue; |
236 var firstDir = _firstDir(id.path); | 245 var firstDir = _firstDir(id.path); |
237 if (firstDir == null) continue; | 246 if (firstDir == null) continue; |
238 | 247 |
239 if (firstDir == 'web' || (options.transformTests && firstDir == 'test')) { | 248 if (firstDir == 'web' || (options.transformTests && firstDir == 'test')) { |
240 var dir = path.join(options.outDir, path.dirname(id.path)); | 249 var dir = path.join(options.outDir, path.dirname(_toSystemPath(id.path))); |
241 var linkPath = path.join(dir, 'packages'); | 250 var linkPath = path.join(dir, 'packages'); |
242 _deleteIfPresent(linkPath); | 251 var link = new Link(linkPath); |
243 var targetPath = Platform.operatingSystem == 'windows' | 252 if (!link.existsSync()) { |
244 ? path.normalize(path.absolute(outPackages)) | 253 var targetPath = Platform.operatingSystem == 'windows' |
245 : path.normalize(path.relative(outPackages, from: dir)); | 254 ? path.normalize(path.absolute(outPackages)) |
246 new Link(linkPath).createSync(targetPath); | 255 : path.normalize(path.relative(outPackages, from: dir)); |
256 link.createSync(targetPath); | |
257 } | |
247 } | 258 } |
248 } | 259 } |
249 } | 260 } |
250 | 261 |
251 /** | 262 /** |
252 * Emits a 'packages' directory directly under `out/packages` with the contents | 263 * Emits a 'packages' directory directly under `out/packages` with the contents |
253 * of every file that was not transformed by barback. | 264 * of every file that was not transformed by barback. |
254 */ | 265 */ |
255 Future _emitPackagesDir(BarbackOptions options) { | 266 Future _emitPackagesDir(BarbackOptions options) { |
256 // Ensure we don't have a packages symlink in our output folder (could happen | 267 if (options.transformPolymerDependencies) return new Future.value(null); |
257 // when people are using nested packages). | |
258 var outPackages = path.join(options.outDir, 'packages'); | 268 var outPackages = path.join(options.outDir, 'packages'); |
259 _deleteIfPresent(outPackages); | 269 _ensureDir(outPackages); |
260 | |
261 if (options.transformPolymerDependencies) return new Future.value(null); | |
262 | 270 |
263 // Copy all the files we didn't process | 271 // Copy all the files we didn't process |
264 var futures = []; | 272 var futures = []; |
265 var dirs = options.packageDirs; | 273 var dirs = options.packageDirs; |
266 for (var package in _polymerPackageDependencies) { | 274 for (var package in _polymerPackageDependencies) { |
267 for (var relpath in _listPackageDir(package, 'lib', options)) { | 275 for (var relpath in _listPackageDir(package, 'lib', options)) { |
268 var inpath = path.join(dirs[package], relpath); | 276 var inpath = path.join(dirs[package], relpath); |
269 var outpath = path.join(outPackages, package, relpath.substring(4)); | 277 var outpath = path.join(outPackages, package, relpath.substring(4)); |
270 futures.add(_copyFile(inpath, outpath)); | 278 futures.add(_copyFile(inpath, outpath)); |
271 } | 279 } |
272 } | 280 } |
273 return Future.wait(futures); | 281 return Future.wait(futures); |
274 } | 282 } |
275 | 283 |
276 /** Ensure [dirpath] exists. */ | 284 /** Ensure [dirpath] exists. */ |
277 void _ensureDir(String dirpath) { | 285 void _ensureDir(String dirpath) { |
278 new Directory(dirpath).createSync(recursive: true); | 286 new Directory(dirpath).createSync(recursive: true); |
279 } | 287 } |
280 | 288 |
281 /** Deletes [packagesPath] if it's a packages symlink, file, or folder. */ | |
282 void _deleteIfPresent(String packagesPath) { | |
283 try { | |
284 var link = new Link(packagesPath); | |
285 if (link.existsSync()) { | |
286 link.deleteSync(); | |
287 return; | |
288 } | |
289 | |
290 var dir = new Directory(packagesPath); | |
291 if (dir.existsSync()) { | |
292 dir.deleteSync(recursive: true); | |
293 return; | |
294 } | |
295 | |
296 var file = new File(packagesPath); | |
297 if (file.existsSync()) { | |
298 file.deleteSync(); | |
299 } | |
300 } catch (e) { | |
301 print('Error while deleting $pacakgesPath: $e'); | |
302 } | |
303 } | |
304 | |
305 /** | 289 /** |
306 * Returns the first directory name on a url-style path, or null if there are no | 290 * Returns the first directory name on a url-style path, or null if there are no |
307 * slashes. | 291 * slashes. |
308 */ | 292 */ |
309 String _firstDir(String url) { | 293 String _firstDir(String url) { |
310 var firstSlash = url.indexOf('/'); | 294 var firstSlash = url.indexOf('/'); |
311 if (firstSlash == -1) return null; | 295 if (firstSlash == -1) return null; |
312 return url.substring(0, firstSlash); | 296 return url.substring(0, firstSlash); |
313 } | 297 } |
314 | 298 |
315 /** Copy a file from [inpath] to [outpath]. */ | 299 /** Copy a file from [inpath] to [outpath]. */ |
316 Future _copyFile(String inpath, String outpath) { | 300 Future _copyFile(String inpath, String outpath) { |
317 _ensureDir(path.dirname(outpath)); | 301 _ensureDir(path.dirname(outpath)); |
318 var writer = new File(outpath).openWrite(); | 302 var writer = new File(outpath).openWrite(); |
319 return writer.addStream(new File(inpath).openRead()) | 303 return writer.addStream(new File(inpath).openRead()) |
320 .then((_) => writer.close()); | 304 .then((_) => writer.close()); |
321 } | 305 } |
322 | 306 |
323 /** Write contents of an [asset] into a file at [filepath]. */ | 307 /** Write contents of an [asset] into a file at [filepath]. */ |
324 Future _writeAsset(String filepath, Asset asset) { | 308 Future _writeAsset(String filepath, Asset asset) { |
325 _ensureDir(path.dirname(filepath)); | 309 _ensureDir(path.dirname(filepath)); |
326 var writer = new File(filepath).openWrite(); | 310 var writer = new File(filepath).openWrite(); |
327 return writer.addStream(asset.read()).then((_) => writer.close()); | 311 return writer.addStream(asset.read()).then((_) => writer.close()); |
328 } | 312 } |
OLD | NEW |