| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 * Tools for code generation. | 6 * Tools for code generation. |
| 7 */ | 7 */ |
| 8 library codegen.tools; | 8 library codegen.tools; |
| 9 | 9 |
| 10 import 'dart:io'; |
| 11 |
| 10 import 'package:html5lib/dom.dart' as dom; | 12 import 'package:html5lib/dom.dart' as dom; |
| 13 import 'package:path/path.dart'; |
| 11 | 14 |
| 12 import 'text_formatter.dart'; | 15 import 'text_formatter.dart'; |
| 13 import 'html_tools.dart'; | 16 import 'html_tools.dart'; |
| 14 | 17 |
| 15 /** | 18 /** |
| 16 * Join the given strings using camelCase. If [capitalize] is true, the first | 19 * Join the given strings using camelCase. If [capitalize] is true, the first |
| 17 * part will be capitalized as well. | 20 * part will be capitalized as well. |
| 18 */ | 21 */ |
| 19 String camelJoin(List<String> parts, {bool capitalize: false}) { | 22 String camelJoin(List<String> parts, {bool capitalize: false}) { |
| 20 List<String> upcasedParts = <String>[]; | 23 List<String> upcasedParts = <String>[]; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 if (lines.last.isEmpty) { | 286 if (lines.last.isEmpty) { |
| 284 lines.removeLast(); | 287 lines.removeLast(); |
| 285 buffer.add(new dom.Text(lines.join('\n$indent') + '\n')); | 288 buffer.add(new dom.Text(lines.join('\n$indent') + '\n')); |
| 286 indentNeeded = true; | 289 indentNeeded = true; |
| 287 } else { | 290 } else { |
| 288 buffer.add(new dom.Text(lines.join('\n$indent'))); | 291 buffer.add(new dom.Text(lines.join('\n$indent'))); |
| 289 indentNeeded = false; | 292 indentNeeded = false; |
| 290 } | 293 } |
| 291 } | 294 } |
| 292 } | 295 } |
| 296 |
| 297 /** |
| 298 * Type of functions used to compute the contents of generated files. |
| 299 */ |
| 300 typedef String ContentsComputer(); |
| 301 |
| 302 /** |
| 303 * Class representing a single output file (either generated code or generated |
| 304 * HTML). |
| 305 */ |
| 306 class GeneratedFile { |
| 307 /** |
| 308 * The output file to which generated output should be written, relative to |
| 309 * the "tool/spec" directory. This filename uses the posix path separator |
| 310 * ('/') regardless of the OS. |
| 311 */ |
| 312 final String outputPath; |
| 313 |
| 314 /** |
| 315 * Callback function which computes the file. |
| 316 */ |
| 317 final ContentsComputer computeContents; |
| 318 |
| 319 GeneratedFile(this.outputPath, this.computeContents); |
| 320 |
| 321 /** |
| 322 * Get a File object representing the output file. |
| 323 */ |
| 324 File get outputFile => new File(joinAll(posix.split(outputPath))); |
| 325 |
| 326 /** |
| 327 * Check whether the file has the correct contents, and return true if it |
| 328 * does. |
| 329 */ |
| 330 bool check() { |
| 331 String expectedContents = computeContents(); |
| 332 try { |
| 333 return expectedContents == outputFile.readAsStringSync(); |
| 334 } catch(e) { |
| 335 // There was a problem reading the file (most likely because it didn't |
| 336 // exist). Treat that the same as if the file doesn't have the expected |
| 337 // contents. |
| 338 return false; |
| 339 } |
| 340 } |
| 341 |
| 342 /** |
| 343 * Replace the file with the correct contents. [spec] is the "tool/spec" |
| 344 * directory. If [spec] is unspecified, it is assumed to be the directory |
| 345 * containing Platform.executable. |
| 346 */ |
| 347 void generate() { |
| 348 outputFile.writeAsStringSync(computeContents()); |
| 349 } |
| 350 } |
| OLD | NEW |