OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env dart | |
2 | |
3 /// Converts block-style Doc comments in Dart code to line style. | |
4 library line_doc_comments; | |
5 import 'dart:io'; | |
6 | |
7 import '../pkg/path/lib/path.dart' as path; | |
8 | |
9 final oneLineBlock = new RegExp(r'^(\s*)/\*\*\s?(.*)\*/\s*$'); | |
10 final startBlock = new RegExp(r'^(\s*)/\*\*(.*)$'); | |
11 final blockLine = new RegExp(r'^\s*\*\s?(.*)$'); | |
12 final endBlock = new RegExp(r'^\s*\*/\s*$'); | |
13 | |
14 main() { | |
15 var args = new Options().arguments; | |
16 if (args.length != 1) { | |
17 print('Converts "/**"-style block doc comments in a directory '); | |
18 print('containing Dart code to "///"-style line doc comments.'); | |
19 print(''); | |
20 print('Usage: ./line_doc_coments.dart <dir>'); | |
nweiz
2012/12/19 20:56:07
Style nit: I'd ditch the "./" here.
Bob Nystrom
2012/12/20 02:04:15
Done.
| |
21 return; | |
22 } | |
23 | |
24 var dir = new Directory(args[0]); | |
25 var lister = dir.list(recursive: true); | |
26 lister.onFile = (file) { | |
27 if (path.extension(file) != '.dart') return; | |
28 fixFile(file); | |
29 }; | |
30 } | |
31 | |
32 void fixFile(String path) { | |
33 new File(path).readAsLines().transform(fixContents).chain((fixed) { | |
nweiz
2012/12/19 20:56:07
Maybe a little cleaner to assign the file to a loc
Bob Nystrom
2012/12/20 02:04:15
Done.
| |
34 return new File(path).writeAsString(fixed); | |
35 }).then((file) { | |
36 print(file.name); | |
37 }); | |
38 } | |
39 | |
40 String fixContents(List<String> lines) { | |
41 var buffer = new StringBuffer(); | |
42 var inBlock = false; | |
43 var indent; | |
44 for (var line in lines) { | |
45 if (inBlock) { | |
46 // See if it's the end of the comment. | |
47 var match = endBlock.firstMatch(line); | |
48 if (match != null) { | |
nweiz
2012/12/19 20:56:07
Slightly cleaner to do "if (endBlock.hasMatch(line
Bob Nystrom
2012/12/20 02:04:15
Done.
| |
49 inBlock = false; | |
50 | |
51 // Just a pointless line, delete it! | |
52 line = null; | |
53 } else { | |
54 match = blockLine.firstMatch(line); | |
55 line = '$indent/// ${match[1]}'; | |
nweiz
2012/12/19 20:56:07
This will leave trailing whitespace if `match[1]`
Bob Nystrom
2012/12/20 02:04:15
Done.
| |
56 } | |
57 } else { | |
58 // See if it's a one-line block comment like: /** Blah. */ | |
59 var match = oneLineBlock.firstMatch(line); | |
60 if (match != null) { | |
61 line = '${match[1]}/// ${match[2]}'; | |
62 } else { | |
63 // See if it's the start of a block doc comment. | |
64 match = startBlock.firstMatch(line); | |
65 if (match != null) { | |
66 inBlock = true; | |
67 indent = match[1]; | |
68 if (match[2] != '') { | |
69 // Got comment on /** line. | |
70 line = match[2]; | |
71 } else { | |
72 // Just a pointless line, delete it! | |
73 line = null; | |
74 } | |
75 } | |
76 } | |
77 } | |
78 | |
79 if (line != null) buffer.add('$line\n'); | |
80 } | |
81 | |
82 return buffer.toString(); | |
83 } | |
84 | |
85 match(RegExp regex, String string, callback(List<String> groups)) { | |
nweiz
2012/12/19 20:56:07
Unused function.
Bob Nystrom
2012/12/20 02:04:15
Done.
| |
86 var result = regex.firstMatch(string); | |
87 if (result != null) callback(result.groups); | |
88 return result != null; | |
89 } | |
OLD | NEW |