Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: pkg/polymer/lib/src/barback_runner.dart

Issue 23876012: Fix polymer build steps so we don't trust existing packages symlinks or folders. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/polymer/test/run.sh » ('j') | pkg/polymer/test/run.sh » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
Siggi Cherem (dart-lang) 2013/09/11 23:54:58 this code became _emitTransformedFiles
Jennifer Messerly 2013/09/12 02:45:03 yeah, nice cleanup! I like how the main Future cha
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((_) {
Siggi Cherem (dart-lang) 2013/09/11 23:54:58 this code moved to _emitPackagesDir
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);
Siggi Cherem (dart-lang) 2013/09/11 23:54:58 alternatively, instead of deleting 'packages/' I c
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(var 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(var packagesPath) {
Jennifer Messerly 2013/09/12 02:45:03 nit: "var" doesn't add anything. you could type t
Siggi Cherem (dart-lang) 2013/09/12 16:53:14 thanks, I think I copied it from the other method,
281 var link = new Link(packagesPath);
282 if (link.existsSync()) {
Jennifer Messerly 2013/09/12 02:45:03 I dunno if it's worth worrying about, but every ti
Siggi Cherem (dart-lang) 2013/09/12 16:53:14 good point. I added the try+catch.
283 link.deleteSync();
284 return;
285 }
286
287 var dir = new Directory(packagesPath);
288 if (dir.existsSync()) {
289 dir.deleteSync(recursive: true);
290 return;
291 }
292
293 var file = new File(packagesPath);
Jennifer Messerly 2013/09/12 02:45:03 do you actually need to check for all 3 kinds of t
Siggi Cherem (dart-lang) 2013/09/12 16:53:14 I'm a little paranoid on whether this works in Win
Jennifer Messerly 2013/09/12 17:53:47 yeah, I wonder though. Pub can be a tricky place t
294 if (file.existsSync()) {
295 file.deleteSync();
296 }
297 }
298
299 /**
300 * Returns the first directory name on a url-style path, or null if there are no
301 * slashes.
302 */
303 String _firstDir(String url) {
304 var firstSlash = url.indexOf('/');
305 if (firstSlash == -1) return null;
306 return url.substring(0, firstSlash);
307 }
308
309 /** Copy a file from [inpath] to [outpath]. */
310 Future _copyFile(String inpath, String outpath) {
Jennifer Messerly 2013/09/12 02:45:03 this feels like a helper that should exist somewhe
Siggi Cherem (dart-lang) 2013/09/12 16:53:14 Yeah, I couldn't find it anywhere in the repo thou
311 _ensureDir(path.dirname(outpath));
312 var writer = new File(outpath).openWrite();
313 return writer.addStream(new File(inpath).openRead())
314 .then((_) => writer.close());
315 }
316
317 /** Write contents of an [asset] into a file at [filepath]. */
318 Future _writeAsset(String filepath, Asset asset) {
319 _ensureDir(path.dirname(filepath));
320 var writer = new File(filepath).openWrite();
321 return writer.addStream(asset.read()).then((_) => writer.close());
322 }
OLDNEW
« no previous file with comments | « no previous file | pkg/polymer/test/run.sh » ('j') | pkg/polymer/test/run.sh » ('J')

Powered by Google App Engine
This is Rietveld 408576698