Chromium Code Reviews| Index: pkg/analysis_server/tool/spec/codegen_tools.dart |
| diff --git a/pkg/analysis_server/tool/spec/codegen_tools.dart b/pkg/analysis_server/tool/spec/codegen_tools.dart |
| index 3b4d64d6f07f9fd9257246c4296f55ae02db32e7..9462ab7e7a8da208231ae48acb66e8f5c2585780 100644 |
| --- a/pkg/analysis_server/tool/spec/codegen_tools.dart |
| +++ b/pkg/analysis_server/tool/spec/codegen_tools.dart |
| @@ -31,6 +31,13 @@ String camelJoin(List<String> parts, {bool capitalize: false}) { |
| return upcasedParts.join(); |
| } |
| +/** |
| + * Capitalize and return the passed String. |
| + */ |
| +String capitalize(String string) { |
| + return string[0].toUpperCase() + string.substring(1); |
| +} |
| + |
| final RegExp trailingWhitespaceRegExp = new RegExp(r' +$', multiLine: true); |
| /** |
| @@ -76,16 +83,14 @@ class CodeGenerator { |
| /** |
| * Execute [callback], using [additionalIndent] to indent any code it outputs. |
| */ |
| - void indentBy(String additionalIndent, void callback()) => indentSpecial( |
| - additionalIndent, additionalIndent, callback); |
| + void indentBy(String additionalIndent, void callback()) => indentSpecial(additionalIndent, additionalIndent, callback); |
| /** |
| * Execute [callback], using [additionalIndent] to indent any code it outputs. |
| * The first line of output is indented by [firstAdditionalIndent] instead of |
| * [additionalIndent]. |
| */ |
| - void indentSpecial(String firstAdditionalIndent, String additionalIndent, void |
| - callback()) { |
| + void indentSpecial(String firstAdditionalIndent, String additionalIndent, void callback()) { |
| String oldNextIndent = _state.nextIndent; |
| String oldIndent = _state.indent; |
| try { |
| @@ -119,7 +124,7 @@ class CodeGenerator { |
| void outputHeader({bool javaStyle: false}) { |
| String header; |
| - if(javaStyle) { |
| + if (javaStyle) { |
| header = ''' |
| /* |
| * Copyright (c) 2014, the Dart project authors. |
| @@ -137,8 +142,7 @@ class CodeGenerator { |
| * This file has been automatically generated. Please do not edit it manually. |
| * To regenerate the file, use the script "pkg/analysis_server/spec/generate_files". |
| */'''; |
| - } |
| - else { |
| + } else { |
| header = ''' |
| // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| @@ -297,13 +301,20 @@ class _HtmlCodeGeneratorState { |
| /** |
| * Type of functions used to compute the contents of generated files. |
| */ |
| -typedef String ContentsComputer(); |
| +typedef String FileContentsComputer(); |
| + |
| +typedef Map<String, FileContentsComputer> DirectoryContentsComputer(); |
| + |
| +abstract class GeneratedContent { |
| + bool check(); |
| + void generate(); |
| +} |
| /** |
| * Class representing a single output file (either generated code or generated |
| * HTML). |
| */ |
| -class GeneratedFile { |
| +class GeneratedFile extends GeneratedContent { |
| /** |
| * The output file to which generated output should be written, relative to |
| * the "tool/spec" directory. This filename uses the posix path separator |
| @@ -314,7 +325,7 @@ class GeneratedFile { |
| /** |
| * Callback function which computes the file. |
| */ |
| - final ContentsComputer computeContents; |
| + final FileContentsComputer computeContents; |
| GeneratedFile(this.outputPath, this.computeContents); |
| @@ -327,11 +338,12 @@ class GeneratedFile { |
| * Check whether the file has the correct contents, and return true if it |
| * does. |
| */ |
| + @override |
| bool check() { |
| String expectedContents = computeContents(); |
| try { |
| return expectedContents == outputFile.readAsStringSync(); |
| - } catch(e) { |
| + } catch (e) { |
| // There was a problem reading the file (most likely because it didn't |
| // exist). Treat that the same as if the file doesn't have the expected |
| // contents. |
| @@ -348,3 +360,44 @@ class GeneratedFile { |
| outputFile.writeAsStringSync(computeContents()); |
| } |
| } |
| + |
| +class GeneratedDirectory extends GeneratedContent { |
| + |
| + final String outputDirPath; |
| + final DirectoryContentsComputer directoryContentsComputer; |
| + GeneratedDirectory(this.outputDirPath, this.directoryContentsComputer); |
| + |
| + /** |
| + * Get a Directory object representing the output directory. |
| + */ |
| + Directory get outputDir => new Directory(joinAll(posix.split(outputDirPath))); |
| + |
| + @override |
| + bool check() { |
|
Paul Berry
2014/08/13 21:39:07
We should also list the directory and make sure it
jwren
2014/08/13 22:29:16
Acknowledged. Meant to add a TODO, adding now.
|
| + Map<String, FileContentsComputer> map = directoryContentsComputer(); |
| + map.forEach((String file, FileContentsComputer fileContentsComputer) { |
| + String expectedContents = fileContentsComputer(); |
| + File outputFile = new File(joinAll(posix.split(outputDirPath + file))); |
| + try { |
| + if (expectedContents != outputFile.readAsStringSync()) { |
| + return false; |
| + } |
| + } catch (e) { |
| + // There was a problem reading the file (most likely because it didn't |
| + // exist). Treat that the same as if the file doesn't have the expected |
| + // contents. |
| + return false; |
| + } |
| + }); |
| + return true; |
| + } |
| + |
| + @override |
| + void generate() { |
|
Paul Berry
2014/08/13 21:39:07
We should also delete any files that are in the di
jwren
2014/08/13 22:29:16
Acknowledged. Meant to add a TODO, adding now.
|
| + Map<String, FileContentsComputer> map = directoryContentsComputer(); |
| + map.forEach((String file, FileContentsComputer fileContentsComputer) { |
| + File outputFile = new File(joinAll(posix.split(outputDirPath + file))); |
| + outputFile.writeAsStringSync(fileContentsComputer()); |
| + }); |
| + } |
| +} |