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

Side by Side Diff: pkg/polymer/lib/build_helper.dart

Issue 23898009: Switch polymer's build.dart to use the new linter. This CL does the following (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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * Common logic to make it easy to run the polymer linter and deploy tool.
7 *
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
10 * from the command line, but that can also invoked automatically by the Dart
11 * Editor whenever a file in your project changes or when selecting some menu
12 * options, such as 'Reanalyze Sources'.
13 *
14 * To work correctly, place the `build.dart` in the root of your project (where
15 * pubspec.yaml lives). The file must be named exactly `build.dart`.
16 *
17 * It's quite likely that in the near future `build.dart` will be replaced with
18 * something else. For example, `pub deploy` will deal with deploying
19 * applications automatically, and the Dart Editor might provide other
20 * mechanisms to hook linters.
21 *
22 * The following examples illustrate common ways to write a `build.dart` file.
23 *
24 * **Example 1**: Uses build.dart to run the linter tool.
25 *
26 * import 'dart:io';
27 * import 'package:polymer/build_helper.dart';
28 *
29 * main() {
30 * lint();
31 * }
32 *
33 * **Example 2**: Runs the linter and creates a deployable version of the app
34 * every time.
35 *
36 * import 'dart:io';
37 * import 'package:polymer/build_helper.dart';
38 *
39 * main() {
40 * lint().then(() => deploy());
41 * }
42 *
43 * **Example 3**: Runs the linter, but conditionally does the deploy step. See
44 * [parseOptions] for a description of options parsed automatically by this
45 * helper library.
46 *
47 * import 'dart:io';
48 * import 'package:polymer/build_helper.dart';
49 *
50 * main() {
51 * var options = parseOptions();
52 * lint().then(() {
53 * if (options.forceDeploy) deploy();
54 * });
55 * }
56 *
57 * **Example 4**: Like the previous example, but indicates to the linter and
58 * deploy tool which files are actually used as entrypoint files. See the
59 * documentation of [lint] and [deploy] below for more details.
60 *
61 * import 'dart:io';
62 * import 'package:polymer/build_helper.dart';
63 *
64 * main() {
Jennifer Messerly 2013/09/10 04:16:51 this looks like the most common pattern. let's com
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Cool idea, added it for now. I think once pub depl
65 * var options = parseOptions();
66 * lint(entrypoints: ['web/index.html']).then(() {
67 * if (options.forceDeploy) deploy(entrypoints: ['web/index.html']);
68 * });
69 * }
70 */
71 library build_helper;
Jennifer Messerly 2013/09/10 04:16:51 library polymer.build_helper; actually -- instead
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Agree, Note I uploaded 2 patch sets. I did all the
72
73 import 'dart:async';
74 import 'dart:io';
75 import 'package:args/args.dart';
76
77 import 'package:polymer/src/barback_helper.dart';
Jennifer Messerly 2013/09/10 04:16:51 this should be 'src/barback_helper.dart' for consi
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 oops, done.
78 import 'package:polymer/src/linter.dart';
79 import 'package:polymer/src/transform.dart';
80
81
82 /**
83 * Runs the polymer linter on any relevant file in your package,
84 * such as any .html file under 'lib/', 'asset/', and 'web/'.
85 *
86 * The [entrypoints] list contains files under web/ that should be treated as
87 * entrypoints. Each entry on this list is a relative path from the package root
88 * (for example 'web/index.html'). If emtpy, all files under 'web/' are treated
89 * as possible entrypoints.
90 *
91 * Options are read from the command line arguments, but you can override them
92 * passing the [options] argument.
93 *
94 * Internally, the linter needs to know the name of the [currentPackage] and
95 * the location where to find the code for any package it depends on
96 * ([packageDirs]). This is inferred automatically, but can be overriden if
97 * those arguments are provided.
98 */
99 Future lint({List<String> entrypoints, CommandLineOptions options,
100 String currentPackage, Map<String, String> packageDirs}) {
101 if (options == null) options = _options;
102 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec();
103 var linterOptions = new TransformOptions(currentPackage, entrypoints);
104 var linter = options.machineFormat ? new Linter(linterOptions, jsonFormatter)
105 : new Linter(linterOptions);
Jennifer Messerly 2013/09/10 04:16:51 slight improvement: var formatter = options.machi
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Done.
106 return runBarback(new BarbackOptions([[linter]], null,
107 currentPackage: currentPackage, packageDirs: packageDirs)).then((assets) {
108 for (var asset in assets) {
Jennifer Messerly 2013/09/10 04:16:51 how stable is this order? wondering if we should w
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 good point. added sorting below.
109 var id = asset.id;
110 if (id.package == currentPackage && id.path.endsWith('.messages')) {
111 asset.readAsString().then((content) {
112 if (!content.isEmpty) print(content);
Jennifer Messerly 2013/09/10 04:16:51 i'm not sure we want to print from an async callba
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Good point. did so below too.
113 });
114 }
115 }
116 });
117 }
118
119 /**
120 * Runs the polymer deploy step.
Jennifer Messerly 2013/09/10 04:16:51 "Creates a directory suitable for deploying a Poly
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Done.
121 *
122 * The [entrypoints] list contains files under web/ that should be treated as
Jennifer Messerly 2013/09/10 04:16:51 must they be under "web/"?
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Good question. For now that was the convention I w
123 * entrypoints. Each entry on this list is a relative path from the package root
124 * (for example 'web/index.html'). If emtpy, all files under 'web/' are treated
Jennifer Messerly 2013/09/10 04:16:51 very minor quibble: i'm not sure about having 0 wr
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Sorry, that is how it's implemented (null = max),
125 * as possible entrypoints.
126 *
127 * Options are read from the command line arguments, but you can override them
128 * passing the [options] list.
129 *
130 * Internally, the deploy script needs to know the name of the [currentPackage]
Jennifer Messerly 2013/09/10 04:16:51 trivial edit: I think you can remove "internally"
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Done, but used "step" instead of "function".
131 * and the location where to find the code for any package it depends on
132 * ([packageDirs]). This is inferred automatically, but can be overriden if
133 * those arguments are provided.
134 *
135 * Note: we expect this function to disappear in the future when `pub deploy`
Jennifer Messerly 2013/09/10 04:16:51 **Note**: this function will be replaced in the fu
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 sgtm
136 * becomes feature complete.
137 */
138 Future deploy({List<String> entrypoints, CommandLineOptions options,
139 String currentPackage, Map<String, String> packageDirs,
140 bool forceDeploy}) {
141 if (options == null) options = _options;
142 if (currentPackage == null) currentPackage = readCurrentPackageFromPubspec();
143 var barbackOptions = new BarbackOptions(
144 createDeployPhases(new TransformOptions(currentPackage, entrypoints)),
145 options.outDir, currentPackage: currentPackage,
146 packageDirs: packageDirs);
147 return runBarback(barbackOptions)
148 .then((_) => print('Done! All files written to "${options.outDir}"'));
149 }
150
151
152 /**
153 * Options that may be used either in build.dart or by the linter and deploy
154 * tools.
155 */
156 class CommandLineOptions {
157 /** Files marked as changed. */
158 List<String> changedFiles;
Jennifer Messerly 2013/09/10 04:16:51 final fields?
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Done.
159
160 /** Files marked as removed. */
161 List<String> removedFiles;
162
163 /** Whether to clean intermediate artifacts, if any. */
164 bool clean;
165
166 /** Whether to do a full build (as if all files have changed). */
167 bool full;
168
169 /** Whether to print results using a machine parseable format. */
170 bool machineFormat;
171
172 /** Whether the force deploy option was passed in the command line. */
173 bool forceDeploy;
174
175 /** Location where to generate output files. */
176 String outDir;
177
178 CommandLineOptions(this.changedFiles, this.removedFiles, this.clean,
179 this.full, this.machineFormat, this.forceDeploy, this.outDir);
180 }
181
182 /** Options parsed directly from the command line arguments. */
183 CommandLineOptions _options = parseOptions();
184
185 /**
186 * Parse command-line arguments and return a [CommandLineOptions] object. The
187 * following flags are parsed by this method.
188 *
189 * * `--changed file-path`: notify of a file change.
190 * * `--removed file-path`: notify that a file was removed.
191 * * `--clean`: remove temporary artifacts (if any)
192 * * `--full`: build everything, similar to marking every file as changed
193 * * `--machine`: produce output that can be parsed by tools, such as the Dart
194 * Editor.
195 * * `--deploy`: force deploy.
196 * * `--help`: print documentation for each option and exit.
197 *
198 * Currently not all the flags are used by [lint] or [deploy] above, but they
199 * are available so they can be used from your `build.dart`. For instance, see
200 * the top-level library documentation for an example that uses the force-deploy
201 * option to conditionally call [deploy].
202 *
203 * If this documentation becomes out of date, the best way to discover which
204 * flags are supported is to invoke this function from your build.dart, and run
205 * it with the `--help` command-line flag.
206 */
207 CommandLineOptions parseOptions([List<String> args]) {
208 var parser = new ArgParser()
209 ..addOption('changed', help: 'the file has changed since the last build',
210 allowMultiple: true)
211 ..addOption('removed', help: 'the file was removed since the last build',
212 allowMultiple: true)
213 ..addFlag('clean', negatable: false,
214 help: 'currently a noop, may be used in the future to remove any build'
Jennifer Messerly 2013/09/10 04:16:51 long line in help? consider adding \n
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 reworded it.
215 ' artifacts')
216 ..addFlag('full', negatable: false, help: 'perform a full build')
217 ..addFlag('machine', negatable: false,
218 help: 'produce warnings in a machine parseable format')
219 ..addFlag('deploy', negatable: false,
220 help: 'if using the deploy() function, whether to force deploying')
221 ..addOption('out', abbr: 'o', help: 'Directory where to generate files.',
Jennifer Messerly 2013/09/10 04:16:51 nit: noticed this one was capitalized and has a pe
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 good catch. I normally like lowercase + no period,
222 defaultsTo: 'out')
223 ..addFlag('help', abbr: 'h',
224 negatable: false, help: 'displays this help and exit');
225 var results = parser.parse(args == null ? new Options().arguments : args);
226 if (results['help']) {
227 print('A build script that invokes the polymer linter and deploy tools.');
228 print('Usage: dart build.dart [options]');
229 print('\nThese are valid options expected by build.dart:');
230 print(parser.getUsage());
231 exit(0);
232 }
233 return new CommandLineOptions(results['changed'], results['removed'],
Jennifer Messerly 2013/09/10 04:16:51 trivial: this might be a good case for a shortish
Siggi Cherem (dart-lang) 2013/09/11 01:45:26 Done.
234 results['clean'], results['full'], results['machine'], results['deploy'],
235 results['out']);
236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698