Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 * Common logic to make it easy to run the polymer linter and deploy tool. | 6 * Common logic to make it easy to run the polymer linter and deploy tool. |
| 7 * | 7 * |
| 8 * The functions in this library are designed to make it easier to create | 8 * The functions in this library are designed to make it easier to create |
| 9 * `build.dart` files. A `build.dart` file is a Dart script that can be invoked | 9 * `build.dart` files. A `build.dart` file is a Dart script that can be invoked |
| 10 * from the command line, but that can also invoked automatically by the Dart | 10 * from the command line, but that can also invoked automatically by the Dart |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 * lint().then((_) => deploy()); | 42 * lint().then((_) => deploy()); |
| 43 * } | 43 * } |
| 44 * | 44 * |
| 45 * **Example 3**: Runs the linter, but conditionally does the deploy step. See | 45 * **Example 3**: Runs the linter, but conditionally does the deploy step. See |
| 46 * [parseOptions] for a description of options parsed automatically by this | 46 * [parseOptions] for a description of options parsed automatically by this |
| 47 * helper library. | 47 * helper library. |
| 48 * | 48 * |
| 49 * import 'dart:io'; | 49 * import 'dart:io'; |
| 50 * import 'package:polymer/builder.dart'; | 50 * import 'package:polymer/builder.dart'; |
| 51 * | 51 * |
| 52 * main() { | 52 * main() { |
|
Siggi Cherem (dart-lang)
2013/10/30 17:43:33
we need to update some of these examples too.
ad
| |
| 53 * var options = parseOptions(); | 53 * var options = parseOptions(); |
| 54 * lint().then((_) { | 54 * lint().then((_) { |
| 55 * if (options.forceDeploy) deploy(); | 55 * if (options.forceDeploy) deploy(); |
| 56 * }); | 56 * }); |
| 57 * } | 57 * } |
| 58 * | 58 * |
| 59 * **Example 4**: Same as above, but uses [build] (which internally calls [lint] | 59 * **Example 4**: Same as above, but uses [build] (which internally calls [lint] |
| 60 * and optionally calls [deploy]). | 60 * and optionally calls [deploy]). |
| 61 * | 61 * |
| 62 * import 'dart:io'; | 62 * import 'dart:io'; |
| 63 * import 'package:polymer/builder.dart'; | 63 * import 'package:polymer/builder.dart'; |
| 64 * | 64 * |
| 65 * main() { | 65 * main() { |
| 66 * build(); | 66 * build(); |
|
Siggi Cherem (dart-lang)
2013/10/30 17:43:33
Here we need to make 'options:' explicit too:
mai
| |
| 67 * } | 67 * } |
| 68 * | 68 * |
| 69 * **Example 5**: Like the previous example, but indicates to the linter and | 69 * **Example 5**: Like the previous example, but indicates to the linter and |
| 70 * deploy tool which files are actually used as entry point files. See the | 70 * deploy tool which files are actually used as entry point files. See the |
| 71 * documentation of [build] below for more details. | 71 * documentation of [build] below for more details. |
| 72 * | 72 * |
| 73 * import 'dart:io'; | 73 * import 'dart:io'; |
| 74 * import 'package:polymer/builder.dart'; | 74 * import 'package:polymer/builder.dart'; |
| 75 * | 75 * |
| 76 * main() { | 76 * main() { |
| 77 * build(entryPoints: ['web/index.html']); | 77 * build(entryPoints: ['web/index.html']); |
|
Siggi Cherem (dart-lang)
2013/10/30 17:43:33
here too
| |
| 78 * } | 78 * } |
| 79 */ | 79 */ |
| 80 library polymer.builder; | 80 library polymer.builder; |
| 81 | 81 |
| 82 import 'dart:async'; | 82 import 'dart:async'; |
| 83 import 'dart:io'; | 83 import 'dart:io'; |
| 84 | 84 |
| 85 import 'package:args/args.dart'; | 85 import 'package:args/args.dart'; |
| 86 | 86 |
| 87 import 'src/build/linter.dart'; | 87 import 'src/build/linter.dart'; |
| 88 import 'src/build/runner.dart'; | 88 import 'src/build/runner.dart'; |
| 89 import 'src/build/common.dart'; | 89 import 'src/build/common.dart'; |
| 90 | 90 |
| 91 import 'transformer.dart'; | 91 import 'transformer.dart'; |
| 92 | 92 |
| 93 | 93 |
| 94 /** | 94 /** |
| 95 * Runs the polymer linter on any relevant file in your package, such as any | 95 * Runs the polymer linter on any relevant file in your package, such as any |
| 96 * .html file under 'lib/', 'asset/', and 'web/'. And, if requested, creates a | 96 * .html file under 'lib/', 'asset/', and 'web/'. And, if requested, creates a |
| 97 * directory suitable for deploying a Polymer application to a server. | 97 * directory suitable for deploying a Polymer application to a server. |
| 98 * | 98 * |
| 99 * The [entryPoints] list contains files under web/ that should be treated as | 99 * The [entryPoints] list contains files under web/ that should be treated as |
| 100 * entry points. Each entry on this list is a relative path from the package | 100 * entry points. Each entry on this list is a relative path from the package |
| 101 * root (for example 'web/index.html'). If null, all files under 'web/' are | 101 * root (for example 'web/index.html'). If null, all files under 'web/' are |
| 102 * treated as possible entry points. | 102 * treated as possible entry points. |
| 103 * | 103 * |
| 104 * Options are read from the command line arguments, but you can override them | 104 * Options must be passed by |
| 105 * passing the [options] argument. The deploy operation is run only when the | 105 * passing the [options] argument. The deploy operation is run only when the |
| 106 * command-line argument `--deploy` is present, or equivalently when | 106 * command-line argument `--deploy` is present, or equivalently when |
| 107 * `options.forceDeploy` is true. | 107 * `options.forceDeploy` is true. |
| 108 * | 108 * |
| 109 * The linter and deploy steps needs to know the name of the [currentPackage] | 109 * The linter and deploy steps needs to know the name of the [currentPackage] |
| 110 * and the location where to find the code for any package it depends on | 110 * and the location where to find the code for any package it depends on |
| 111 * ([packageDirs]). This is inferred automatically, but can be overriden if | 111 * ([packageDirs]). This is inferred automatically, but can be overriden if |
| 112 * those arguments are provided. | 112 * those arguments are provided. |
| 113 */ | 113 */ |
| 114 Future build({List<String> entryPoints, CommandLineOptions options, | 114 Future build({List<String> entryPoints, CommandLineOptions options, |
| 115 String currentPackage, Map<String, String> packageDirs}) { | 115 String currentPackage, Map<String, String> packageDirs}) { |
| 116 if (options == null) options = _options; | 116 if (options == null) { |
| 117 // The dart:io Options class has been removed, and command-line | |
| 118 // arguments are only passed to main, as main(List<String> arguments). | |
| 119 throw new UnsupportedError( | |
| 120 "polymer builder tools must pass options to build()"); | |
| 121 } | |
| 117 return lint(entryPoints: entryPoints, options: options, | 122 return lint(entryPoints: entryPoints, options: options, |
| 118 currentPackage: currentPackage, packageDirs: packageDirs).then((res) { | 123 currentPackage: currentPackage, packageDirs: packageDirs).then((res) { |
| 119 if (options.forceDeploy) { | 124 if (options.forceDeploy) { |
| 120 return deploy(entryPoints: entryPoints, options: options, | 125 return deploy(entryPoints: entryPoints, options: options, |
| 121 currentPackage: currentPackage, packageDirs: packageDirs); | 126 currentPackage: currentPackage, packageDirs: packageDirs); |
| 122 } | 127 } |
| 123 }); | 128 }); |
| 124 } | 129 } |
| 125 | 130 |
| 126 | 131 |
| 127 /** | 132 /** |
| 128 * Runs the polymer linter on any relevant file in your package, | 133 * Runs the polymer linter on any relevant file in your package, |
| 129 * such as any .html file under 'lib/', 'asset/', and 'web/'. | 134 * such as any .html file under 'lib/', 'asset/', and 'web/'. |
| 130 * | 135 * |
| 131 * The [entryPoints] list contains files under web/ that should be treated as | 136 * The [entryPoints] list contains files under web/ that should be treated as |
| 132 * entry points. Each entry on this list is a relative path from the package | 137 * entry points. Each entry on this list is a relative path from the package |
| 133 * root (for example 'web/index.html'). If null, all files under 'web/' are | 138 * root (for example 'web/index.html'). If null, all files under 'web/' are |
| 134 * treated as possible entry points. | 139 * treated as possible entry points. |
| 135 * | 140 * |
| 136 * Options are read from the command line arguments, but you can override them | 141 * Options must be passed by passing the [options] argument. |
| 137 * passing the [options] argument. | |
| 138 * | 142 * |
| 139 * The linter needs to know the name of the [currentPackage] and the location | 143 * The linter needs to know the name of the [currentPackage] and the location |
| 140 * where to find the code for any package it depends on ([packageDirs]). This is | 144 * where to find the code for any package it depends on ([packageDirs]). This is |
| 141 * inferred automatically, but can be overriden if those arguments are provided. | 145 * inferred automatically, but can be overriden if those arguments are provided. |
| 142 */ | 146 */ |
| 143 Future lint({List<String> entryPoints, CommandLineOptions options, | 147 Future lint({List<String> entryPoints, CommandLineOptions options, |
| 144 String currentPackage, Map<String, String> packageDirs}) { | 148 String currentPackage, Map<String, String> packageDirs}) { |
| 145 if (options == null) options = _options; | 149 if (options == null) { |
| 150 // The dart:io Options class has been removed, and command-line | |
| 151 // arguments are only passed to main, as main(List<String> arguments). | |
| 152 throw new UnsupportedError( | |
| 153 "polymer builder tools must pass options to lint()"); | |
| 154 } | |
| 146 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec(); | 155 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec(); |
| 147 var linterOptions = new TransformOptions(entryPoints: entryPoints); | 156 var linterOptions = new TransformOptions(entryPoints: entryPoints); |
| 148 var formatter = options.machineFormat ? jsonFormatter : consoleFormatter; | 157 var formatter = options.machineFormat ? jsonFormatter : consoleFormatter; |
| 149 var linter = new Linter(linterOptions, formatter); | 158 var linter = new Linter(linterOptions, formatter); |
| 150 return runBarback(new BarbackOptions([[linter]], null, | 159 return runBarback(new BarbackOptions([[linter]], null, |
| 151 currentPackage: currentPackage, packageDirs: packageDirs)).then((assets) { | 160 currentPackage: currentPackage, packageDirs: packageDirs)).then((assets) { |
| 152 var messages = {}; | 161 var messages = {}; |
| 153 var futures = []; | 162 var futures = []; |
| 154 for (var asset in assets) { | 163 for (var asset in assets) { |
| 155 var id = asset.id; | 164 var id = asset.id; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 181 * Creates a directory suitable for deploying a Polymer application to a server. | 190 * Creates a directory suitable for deploying a Polymer application to a server. |
| 182 * | 191 * |
| 183 * **Note**: this function will be replaced in the future by the `pub deploy` | 192 * **Note**: this function will be replaced in the future by the `pub deploy` |
| 184 * command. | 193 * command. |
| 185 * | 194 * |
| 186 * The [entryPoints] list contains files under web/ that should be treated as | 195 * The [entryPoints] list contains files under web/ that should be treated as |
| 187 * entry points. Each entry on this list is a relative path from the package | 196 * entry points. Each entry on this list is a relative path from the package |
| 188 * root (for example 'web/index.html'). If null, all files under 'web/' are | 197 * root (for example 'web/index.html'). If null, all files under 'web/' are |
| 189 * treated as possible entry points. | 198 * treated as possible entry points. |
| 190 * | 199 * |
| 191 * Options are read from the command line arguments, but you can override them | 200 * Options must be passed by passing the [options] list. |
| 192 * passing the [options] list. | |
| 193 * | 201 * |
| 194 * The deploy step needs to know the name of the [currentPackage] and the | 202 * The deploy step needs to know the name of the [currentPackage] and the |
| 195 * location where to find the code for any package it depends on | 203 * location where to find the code for any package it depends on |
| 196 * ([packageDirs]). This is inferred automatically, but can be overriden if | 204 * ([packageDirs]). This is inferred automatically, but can be overriden if |
| 197 * those arguments are provided. | 205 * those arguments are provided. |
| 198 */ | 206 */ |
| 199 Future deploy({List<String> entryPoints, CommandLineOptions options, | 207 Future deploy({List<String> entryPoints, CommandLineOptions options, |
| 200 String currentPackage, Map<String, String> packageDirs}) { | 208 String currentPackage, Map<String, String> packageDirs}) { |
| 201 if (options == null) options = _options; | 209 if (options == null) { |
| 210 // The dart:io Options class has been removed, and command-line | |
| 211 // arguments are only passed to main, as main(List<String> arguments). | |
| 212 throw new UnsupportedError( | |
| 213 "polymer builder tools must pass options to lint()"); | |
| 214 } | |
| 202 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec(); | 215 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec(); |
| 203 | 216 |
| 204 var transformOptions = new TransformOptions( | 217 var transformOptions = new TransformOptions( |
| 205 entryPoints: entryPoints, | 218 entryPoints: entryPoints, |
| 206 directlyIncludeJS: options.directlyIncludeJS, | 219 directlyIncludeJS: options.directlyIncludeJS, |
| 207 contentSecurityPolicy: options.contentSecurityPolicy); | 220 contentSecurityPolicy: options.contentSecurityPolicy); |
| 208 | 221 |
| 209 var barbackOptions = new BarbackOptions( | 222 var barbackOptions = new BarbackOptions( |
| 210 new PolymerTransformerGroup(transformOptions).phases, | 223 new PolymerTransformerGroup(transformOptions).phases, |
| 211 options.outDir, currentPackage: currentPackage, | 224 options.outDir, currentPackage: currentPackage, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 * True to include the JS script tag directly, without the | 261 * True to include the JS script tag directly, without the |
| 249 * "packages/browser/dart.js" trampoline. | 262 * "packages/browser/dart.js" trampoline. |
| 250 */ | 263 */ |
| 251 final bool directlyIncludeJS; | 264 final bool directlyIncludeJS; |
| 252 | 265 |
| 253 CommandLineOptions(this.changedFiles, this.removedFiles, this.clean, | 266 CommandLineOptions(this.changedFiles, this.removedFiles, this.clean, |
| 254 this.full, this.machineFormat, this.forceDeploy, this.outDir, | 267 this.full, this.machineFormat, this.forceDeploy, this.outDir, |
| 255 this.directlyIncludeJS, this.contentSecurityPolicy); | 268 this.directlyIncludeJS, this.contentSecurityPolicy); |
| 256 } | 269 } |
| 257 | 270 |
| 258 /** Options parsed directly from the command line arguments. */ | |
| 259 CommandLineOptions _options = parseOptions(); | |
| 260 | |
| 261 /** | 271 /** |
| 262 * Parse command-line arguments and return a [CommandLineOptions] object. The | 272 * Parse command-line arguments and return a [CommandLineOptions] object. The |
| 263 * following flags are parsed by this method. | 273 * following flags are parsed by this method. |
| 264 * | 274 * |
| 265 * * `--changed file-path`: notify of a file change. | 275 * * `--changed file-path`: notify of a file change. |
| 266 * * `--removed file-path`: notify that a file was removed. | 276 * * `--removed file-path`: notify that a file was removed. |
| 267 * * `--clean`: remove temporary artifacts (if any) | 277 * * `--clean`: remove temporary artifacts (if any) |
| 268 * * `--full`: build everything, similar to marking every file as changed | 278 * * `--full`: build everything, similar to marking every file as changed |
| 269 * * `--machine`: produce output that can be parsed by tools, such as the Dart | 279 * * `--machine`: produce output that can be parsed by tools, such as the Dart |
| 270 * Editor. | 280 * Editor. |
| 271 * * `--deploy`: force deploy. | 281 * * `--deploy`: force deploy. |
| 272 * * `--no-js`: deploy replaces *.dart scripts with *.dart.js. You can turn | 282 * * `--no-js`: deploy replaces *.dart scripts with *.dart.js. You can turn |
| 273 * this feature off with --no-js, which leaves "packages/browser/dart.js". | 283 * this feature off with --no-js, which leaves "packages/browser/dart.js". |
| 274 * * `--csp`: replaces *.dart with *.dart.precompiled.js to comply with | 284 * * `--csp`: replaces *.dart with *.dart.precompiled.js to comply with |
| 275 * Content Security Policy restrictions. | 285 * Content Security Policy restrictions. |
| 276 * * `--help`: print documentation for each option and exit. | 286 * * `--help`: print documentation for each option and exit. |
| 277 * | 287 * |
| 278 * Currently not all the flags are used by [lint] or [deploy] above, but they | 288 * Currently not all the flags are used by [lint] or [deploy] above, but they |
| 279 * are available so they can be used from your `build.dart`. For instance, see | 289 * are available so they can be used from your `build.dart`. For instance, see |
| 280 * the top-level library documentation for an example that uses the force-deploy | 290 * the top-level library documentation for an example that uses the force-deploy |
| 281 * option to conditionally call [deploy]. | 291 * option to conditionally call [deploy]. |
| 282 * | 292 * |
| 283 * If this documentation becomes out of date, the best way to discover which | 293 * If this documentation becomes out of date, the best way to discover which |
| 284 * flags are supported is to invoke this function from your build.dart, and run | 294 * flags are supported is to invoke this function from your build.dart, and run |
| 285 * it with the `--help` command-line flag. | 295 * it with the `--help` command-line flag. |
| 286 */ | 296 */ |
| 287 CommandLineOptions parseOptions([List<String> args]) { | 297 CommandLineOptions parseOptions([List<String> args]) { |
| 298 if (args == null) { | |
| 299 throw new UnsupportedError( | |
| 300 "polymer builder tools must pass options from main(List<String> args)"); | |
| 301 } | |
| 288 var parser = new ArgParser() | 302 var parser = new ArgParser() |
| 289 ..addOption('changed', help: 'The file has changed since the last build.', | 303 ..addOption('changed', help: 'The file has changed since the last build.', |
| 290 allowMultiple: true) | 304 allowMultiple: true) |
| 291 ..addOption('removed', help: 'The file was removed since the last build.', | 305 ..addOption('removed', help: 'The file was removed since the last build.', |
| 292 allowMultiple: true) | 306 allowMultiple: true) |
| 293 ..addFlag('clean', negatable: false, | 307 ..addFlag('clean', negatable: false, |
| 294 help: 'Remove any build artifacts (if any).') | 308 help: 'Remove any build artifacts (if any).') |
| 295 ..addFlag('full', negatable: false, help: 'perform a full build') | 309 ..addFlag('full', negatable: false, help: 'perform a full build') |
| 296 ..addFlag('machine', negatable: false, | 310 ..addFlag('machine', negatable: false, |
| 297 help: 'Produce warnings in a machine parseable format.') | 311 help: 'Produce warnings in a machine parseable format.') |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 310 negatable: false, help: 'Displays this help and exit.'); | 324 negatable: false, help: 'Displays this help and exit.'); |
| 311 | 325 |
| 312 showUsage() { | 326 showUsage() { |
| 313 print('Usage: dart build.dart [options]'); | 327 print('Usage: dart build.dart [options]'); |
| 314 print('\nThese are valid options expected by build.dart:'); | 328 print('\nThese are valid options expected by build.dart:'); |
| 315 print(parser.getUsage()); | 329 print(parser.getUsage()); |
| 316 } | 330 } |
| 317 | 331 |
| 318 var res; | 332 var res; |
| 319 try { | 333 try { |
| 320 res = parser.parse(args == null ? new Options().arguments : args); | 334 res = parser.parse(args); |
| 321 } on FormatException catch (e) { | 335 } on FormatException catch (e) { |
| 322 print(e.message); | 336 print(e.message); |
| 323 showUsage(); | 337 showUsage(); |
| 324 exit(1); | 338 exit(1); |
| 325 } | 339 } |
| 326 if (res['help']) { | 340 if (res['help']) { |
| 327 print('A build script that invokes the polymer linter and deploy tools.'); | 341 print('A build script that invokes the polymer linter and deploy tools.'); |
| 328 showUsage(); | 342 showUsage(); |
| 329 exit(0); | 343 exit(0); |
| 330 } | 344 } |
| 331 return new CommandLineOptions(res['changed'], res['removed'], res['clean'], | 345 return new CommandLineOptions(res['changed'], res['removed'], res['clean'], |
| 332 res['full'], res['machine'], res['deploy'], res['out'], res['js'], | 346 res['full'], res['machine'], res['deploy'], res['out'], res['js'], |
| 333 res['csp']); | 347 res['csp']); |
| 334 } | 348 } |
| OLD | NEW |