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 library trydart.source_update; | 5 library trydart.source_update; |
6 | 6 |
7 /// Returns [updates] expanded to full compilation units/source files. | 7 /// Returns [updates] expanded to full compilation units/source files. |
8 /// | 8 /// |
9 /// [updates] is a convenient way to write updates/patches to a single source | 9 /// [updates] is a convenient way to write updates/patches to a single source |
10 /// file without repeating common parts. | 10 /// file without repeating common parts. |
(...skipping 21 matching lines...) Expand all Loading... |
32 } | 32 } |
33 } else { | 33 } else { |
34 for (StringBuffer buffer in result) { | 34 for (StringBuffer buffer in result) { |
35 buffer.write(chunk); | 35 buffer.write(chunk); |
36 } | 36 } |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 return result.map((e) => '$e').toList(); | 40 return result.map((e) => '$e').toList(); |
41 } | 41 } |
| 42 |
| 43 /// Returns [files] split into mulitple named files. The keys in the returned |
| 44 /// map are filenames, the values are the files' content. |
| 45 /// |
| 46 /// Names are indicated by a line on the form "==> filename <==". Spaces are |
| 47 /// significant. For example, given this input: |
| 48 /// |
| 49 /// ==> file1.dart <== |
| 50 /// First line of file 1. |
| 51 /// Second line of file 1. |
| 52 /// Third line of file 1. |
| 53 /// ==> empty.dart <== |
| 54 /// ==> file2.dart <== |
| 55 /// First line of file 2. |
| 56 /// Second line of file 2. |
| 57 /// Third line of file 2. |
| 58 /// |
| 59 /// This function would return: |
| 60 /// |
| 61 /// { |
| 62 /// "file1.dart": """ |
| 63 /// First line of file 1. |
| 64 /// Second line of file 1. |
| 65 /// Third line of file 1. |
| 66 /// """, |
| 67 /// |
| 68 /// "empty.dart":"", |
| 69 /// |
| 70 /// "file2.dart":""" |
| 71 /// First line of file 2. |
| 72 /// Second line of file 2. |
| 73 /// Third line of file 2. |
| 74 /// """ |
| 75 /// } |
| 76 Map<String, String> splitFiles(String files) { |
| 77 Map<String, String> result = <String, String>{}; |
| 78 String currentName; |
| 79 List<String> content; |
| 80 void finishFile() { |
| 81 if (currentName != null) { |
| 82 if (result.containsKey(currentName)) { |
| 83 throw new ArgumentError("Duplicated filename $currentName in $files"); |
| 84 } |
| 85 result[currentName] = content.join(''); |
| 86 } |
| 87 content = null; |
| 88 } |
| 89 void processDirective(String line) { |
| 90 finishFile(); |
| 91 if (line.length < 8 || !line.endsWith(" <==\n")) { |
| 92 throw new ArgumentError( |
| 93 "Malformed line: expected '==> ... <==', but got: '$line'"); |
| 94 } |
| 95 currentName = line.substring(4, line.length - 5); |
| 96 content = <String>[]; |
| 97 } |
| 98 for (String line in splitLines(files)) { |
| 99 if (line.startsWith("==>")) { |
| 100 processDirective(line); |
| 101 } else { |
| 102 content.add(line); |
| 103 } |
| 104 } |
| 105 finishFile(); |
| 106 return result; |
| 107 } |
| 108 |
| 109 /// Split [text] into lines preserving trailing newlines (unlike |
| 110 /// String.split("\n"). Also, if [text] is empty, return an empty list (unlike |
| 111 /// String.split("\n")). |
| 112 List<String> splitLines(String text) { |
| 113 return text.split(new RegExp('^', multiLine: true)); |
| 114 } |
OLD | NEW |