OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:path/path.dart'; | 7 import 'package:path/path.dart'; |
8 | 8 |
9 /** | 9 /** |
10 * Type of functions used to compute the contents of a set of generated files. | 10 * Type of functions used to compute the contents of a set of generated files. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 } else { | 77 } else { |
78 print('All generated files up to date.'); | 78 print('All generated files up to date.'); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 /** | 82 /** |
83 * Regenerate all of the [targets]. [pkgPath] is the path to the current | 83 * Regenerate all of the [targets]. [pkgPath] is the path to the current |
84 * package. | 84 * package. |
85 */ | 85 */ |
86 static void generateAll(String pkgPath, Iterable<GeneratedContent> targets) { | 86 static void generateAll(String pkgPath, Iterable<GeneratedContent> targets) { |
87 print("Generating..."); | |
Jacob
2017/03/21 19:41:07
did you mean to leave this print in?
devoncarew
2017/03/21 22:50:58
Yup, this (and the other one) are here to let the
| |
87 for (GeneratedContent target in targets) { | 88 for (GeneratedContent target in targets) { |
88 target.generate(pkgPath); | 89 target.generate(pkgPath); |
89 } | 90 } |
90 } | 91 } |
91 } | 92 } |
92 | 93 |
93 /** | 94 /** |
94 * Class representing a single output directory (either generated code or | 95 * Class representing a single output directory (either generated code or |
95 * generated HTML). No other content should exist in the directory. | 96 * generated HTML). No other content should exist in the directory. |
96 */ | 97 */ |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 // Error caught while trying to delete the directory, this can happen if | 158 // Error caught while trying to delete the directory, this can happen if |
158 // it didn't yet exist. | 159 // it didn't yet exist. |
159 } | 160 } |
160 // re-create the empty directory | 161 // re-create the empty directory |
161 outputDirectory.createSync(recursive: true); | 162 outputDirectory.createSync(recursive: true); |
162 | 163 |
163 // generate all of the files in the directory | 164 // generate all of the files in the directory |
164 Map<String, FileContentsComputer> map = directoryContentsComputer(pkgPath); | 165 Map<String, FileContentsComputer> map = directoryContentsComputer(pkgPath); |
165 map.forEach((String file, FileContentsComputer fileContentsComputer) { | 166 map.forEach((String file, FileContentsComputer fileContentsComputer) { |
166 File outputFile = new File(posix.join(outputDirectory.path, file)); | 167 File outputFile = new File(posix.join(outputDirectory.path, file)); |
168 print(' ${outputFile.path}'); | |
167 outputFile.writeAsStringSync(fileContentsComputer(pkgPath)); | 169 outputFile.writeAsStringSync(fileContentsComputer(pkgPath)); |
168 }); | 170 }); |
169 } | 171 } |
170 | 172 |
171 @override | 173 @override |
172 Directory output(String pkgPath) => | 174 Directory output(String pkgPath) => |
173 new Directory(join(pkgPath, joinAll(posix.split(outputDirPath)))); | 175 new Directory(join(pkgPath, joinAll(posix.split(outputDirPath)))); |
174 } | 176 } |
175 | 177 |
176 /** | 178 /** |
177 * Class representing a single output file (either generated code or generated | 179 * Class representing a single output file (either generated code or generated |
178 * HTML). | 180 * HTML). |
179 */ | 181 */ |
180 class GeneratedFile extends GeneratedContent { | 182 class GeneratedFile extends GeneratedContent { |
181 /** | 183 /** |
182 * The output file to which generated output should be written, relative to | 184 * The output file to which generated output should be written, relative to |
183 * the "tool/spec" directory. This filename uses the posix path separator | 185 * the "tool/spec" directory. This filename uses the posix path separator |
184 * ('/') regardless of the OS. | 186 * ('/') regardless of the OS. |
185 */ | 187 */ |
186 final String outputPath; | 188 final String outputPath; |
187 | 189 |
188 /** | 190 /** |
189 * Callback function which computes the file. | 191 * Callback function which computes the file. |
190 */ | 192 */ |
191 final FileContentsComputer computeContents; | 193 final FileContentsComputer computeContents; |
192 | 194 |
193 GeneratedFile(this.outputPath, this.computeContents); | 195 GeneratedFile(this.outputPath, this.computeContents); |
194 | 196 |
197 bool get isDartFile => outputPath.endsWith('.dart'); | |
198 | |
195 @override | 199 @override |
196 bool check(String pkgPath) { | 200 bool check(String pkgPath) { |
197 File outputFile = output(pkgPath); | 201 File outputFile = output(pkgPath); |
198 String expectedContents = computeContents(pkgPath); | 202 String expectedContents = computeContents(pkgPath); |
203 if (isDartFile) { | |
204 expectedContents = DartFormat.formatText(expectedContents); | |
205 } | |
199 try { | 206 try { |
200 String actualContents = outputFile.readAsStringSync(); | 207 String actualContents = outputFile.readAsStringSync(); |
201 // Normalize Windows line endings to Unix line endings so that the | 208 // Normalize Windows line endings to Unix line endings so that the |
202 // comparison doesn't fail on Windows. | 209 // comparison doesn't fail on Windows. |
203 actualContents = actualContents.replaceAll('\r\n', '\n'); | 210 actualContents = actualContents.replaceAll('\r\n', '\n'); |
204 return expectedContents == actualContents; | 211 return expectedContents == actualContents; |
205 } catch (e) { | 212 } catch (e) { |
206 // There was a problem reading the file (most likely because it didn't | 213 // There was a problem reading the file (most likely because it didn't |
207 // exist). Treat that the same as if the file doesn't have the expected | 214 // exist). Treat that the same as if the file doesn't have the expected |
208 // contents. | 215 // contents. |
209 return false; | 216 return false; |
210 } | 217 } |
211 } | 218 } |
212 | 219 |
213 @override | 220 @override |
214 void generate(String pkgPath) { | 221 void generate(String pkgPath) { |
215 output(pkgPath).writeAsStringSync(computeContents(pkgPath)); | 222 File outputFile = output(pkgPath); |
223 print(' ${outputFile.path}'); | |
224 outputFile.writeAsStringSync(computeContents(pkgPath)); | |
225 if (isDartFile) { | |
226 DartFormat.formatFile(outputFile); | |
227 } | |
216 } | 228 } |
217 | 229 |
218 @override | 230 @override |
219 File output(String pkgPath) => | 231 File output(String pkgPath) => |
220 new File(join(pkgPath, joinAll(posix.split(outputPath)))); | 232 new File(join(pkgPath, joinAll(posix.split(outputPath)))); |
221 } | 233 } |
234 | |
235 /** | |
236 * A utility class for invoking dartfmt. | |
237 */ | |
238 class DartFormat { | |
239 static String get _dartfmtPath { | |
240 String binName = Platform.isWindows ? 'dartfmt.bat' : 'dartfmt'; | |
241 return join(dirname(Platform.resolvedExecutable), binName); | |
242 } | |
243 | |
244 static void formatFile(File file) { | |
245 ProcessResult result = Process.runSync(_dartfmtPath, ['-w', file.path]); | |
246 if (result.exitCode != 0) throw result.stderr; | |
247 } | |
248 | |
249 static String formatText(String text) { | |
250 File file = new File(join(Directory.systemTemp.path, 'gen.dart')); | |
251 file.writeAsStringSync(text); | |
252 ProcessResult result = Process.runSync(_dartfmtPath, ['-w', file.path]); | |
253 if (result.exitCode != 0) throw result.stderr; | |
254 return file.readAsStringSync(); | |
255 } | |
256 } | |
OLD | NEW |