Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 * Library for taking a JSON file and putting the comments located within into | |
| 7 * the HTML files the comments are associated with. | |
| 8 * | |
| 9 * The format of the JSON file is: | |
| 10 * { | |
| 11 * "$filename": | |
| 12 * { | |
| 13 * "$lineInHtml": | |
|
sra1
2012/11/26 20:36:57
If you can change the format to
{
"$filename":
Andrei Mouravski
2012/11/26 23:24:11
Noted. I'm not too concerned about upgrading at th
| |
| 14 * [ | |
| 15 * "lines of comment", | |
| 16 * "here" | |
| 17 * ] | |
| 18 * }, | |
| 19 * ... | |
| 20 * } | |
| 21 */ | |
| 22 library jsonToHtml; | |
| 23 | |
| 24 import 'dart:json'; | |
| 25 import 'dart:io'; | |
| 26 | |
| 27 | |
| 28 /// True if any errors were triggered through the conversion. | |
| 29 bool _anyErrors = false; | |
| 30 | |
| 31 | |
| 32 /** | |
| 33 * Take comments from [jsonPath] and apply them to all the files found in | |
| 34 * [htmlPath]. This will overwrite the files in htmlPath. | |
| 35 */ | |
| 36 Future<bool> convert(Path htmlPath, Path jsonPath) { | |
| 37 final completer = new Completer(); | |
| 38 | |
| 39 final jsonFile = new File.fromPath(jsonPath); | |
| 40 final htmlDir = new Directory.fromPath(htmlPath); | |
| 41 | |
| 42 if (!jsonFile.existsSync()) { | |
| 43 print("ERROR: No JSON file found at: ${jsonPath}"); | |
| 44 _anyErrors = true; | |
| 45 completer.complete(false); | |
| 46 } else if (!htmlDir.existsSync()) { | |
| 47 print("ERROR: No HTML directory found at: ${htmlPath}"); | |
| 48 _anyErrors = true; | |
| 49 completer.complete(false); | |
| 50 } | |
| 51 | |
| 52 | |
| 53 var fileJson = {}; | |
| 54 var jsonRead = Strings.join(jsonFile.readAsLinesSync(), '\n'); | |
| 55 | |
| 56 if (jsonRead == '') { | |
| 57 print('WARNING: no data read from ${jsonPath.filename}'); | |
| 58 _anyErrors = true; | |
| 59 completer.complete(false); | |
| 60 } else { | |
| 61 fileJson = JSON.parse(jsonRead) as Map<String, Map<String, List<String>>>; | |
| 62 } | |
| 63 | |
| 64 // Find html files. (lister) | |
| 65 | |
| 66 final lister = htmlDir.list(recursive: false); | |
| 67 | |
| 68 lister.onFile = (String path) { | |
| 69 final name = new Path.fromNative(path).filename; | |
| 70 | |
| 71 // Ignore private classes. | |
| 72 if (name.startsWith('_')) return; | |
| 73 | |
| 74 // Ignore non-dart files. | |
| 75 if (!name.endsWith('.dart')) return; | |
| 76 | |
| 77 File file = new File(path); | |
| 78 | |
| 79 // TODO(amouravski): Handle missing file. | |
| 80 if (!file.existsSync()) { | |
| 81 print('ERROR: cannot find file: $path'); | |
| 82 _anyErrors = true; | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 if (!fileJson.containsKey(name)) { | |
| 87 print('WARNING: file found that is not in JSON: $path'); | |
| 88 _anyErrors = true; | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 var comments = fileJson[name]; | |
| 93 | |
| 94 _convertFile(file, comments); | |
| 95 | |
| 96 fileJson.remove(name); | |
| 97 }; | |
| 98 | |
| 99 lister.onDone = (_) { | |
| 100 | |
| 101 fileJson.forEach((key, _) { | |
| 102 print('WARNING: the following filename was found in the JSON but not in ' | |
| 103 '${htmlDir.path}:\n"$key"'); | |
| 104 _anyErrors = true; | |
| 105 }); | |
| 106 | |
| 107 completer.complete(_anyErrors); | |
| 108 }; | |
| 109 | |
| 110 return completer.future; | |
| 111 } | |
| 112 | |
| 113 | |
| 114 /** | |
| 115 * Inserts the comments from JSON into a single file. | |
| 116 */ | |
| 117 void _convertFile(File file, Map<String,List<String>> comments) { | |
| 118 var fileLines = file.readAsLinesSync(); | |
| 119 | |
| 120 var unusedComments = {}; | |
| 121 | |
| 122 comments.forEach((key, comments) { | |
| 123 var index = fileLines.indexOf(key); | |
| 124 // If the key is found in any line past the first one. | |
| 125 if (index > 0 && fileLines[index - 1].trim().startsWith('///') && | |
| 126 fileLines[index - 1].contains('@docsEditable')) { | |
| 127 | |
| 128 // Add comments. | |
| 129 fileLines.insertRange(index - 1, comments.length); | |
| 130 fileLines.setRange(index - 1, comments.length, comments); | |
| 131 } else { | |
| 132 unusedComments.putIfAbsent(key, () => comments); | |
| 133 } | |
| 134 }); | |
| 135 | |
| 136 unusedComments.forEach((String key, _) { | |
| 137 print('WARNING: the following key was found in the JSON but not in ' | |
| 138 '${new Path(file.fullPathSync()).filename}:\n"$key"'); | |
| 139 _anyErrors = true; | |
| 140 }); | |
| 141 | |
| 142 var outputStream = file.openOutputStream(); | |
| 143 outputStream.writeString(Strings.join(fileLines, '\n')); | |
| 144 | |
| 145 outputStream.onNoPendingWrites = () { | |
| 146 outputStream.close(); | |
| 147 }; | |
| 148 } | |
| OLD | NEW |