OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 protoc.benchmark.report; | 5 library protoc.benchmark.report; |
6 | 6 |
7 import 'dart:convert' show JSON; | 7 import 'dart:convert' show JSON; |
8 | 8 |
9 import 'generated/benchmark.pb.dart' as pb; | 9 import 'generated/benchmark.pb.dart' as pb; |
10 | 10 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 } | 59 } |
60 | 60 |
61 out.packages.addAll(_parseLockFile(pubspecLock)); | 61 out.packages.addAll(_parseLockFile(pubspecLock)); |
62 | 62 |
63 return out; | 63 return out; |
64 } | 64 } |
65 | 65 |
66 /// Quick and dirty lock file parser. | 66 /// Quick and dirty lock file parser. |
67 /// - ignores indentation | 67 /// - ignores indentation |
68 /// - assumes one top-level key "packages:" | 68 /// - assumes one top-level key "packages:" |
69 /// - assumes all other lines without a value start a new package | 69 /// - assumes all other lines without a value except 'description' |
70 /// - only allows three known keys within a package | 70 /// start a new package |
| 71 /// - only allows known keys within a package |
71 List<pb.PackageVersion> _parseLockFile(String contents) { | 72 List<pb.PackageVersion> _parseLockFile(String contents) { |
72 var out = <pb.PackageVersion>[]; | 73 var out = <pb.PackageVersion>[]; |
73 | 74 |
74 bool inPackages = false; | 75 bool inPackages = false; |
| 76 bool inDescription = false; |
75 pb.PackageVersion pv = null; | 77 pb.PackageVersion pv = null; |
76 var lineNumber = 0; | 78 var lineNumber = 0; |
77 for (var line in contents.split("\n")) { | 79 for (var line in contents.split("\n")) { |
78 lineNumber++; | 80 lineNumber++; |
79 line = line.trim(); // ignore indentation | 81 line = line.trim(); // ignore indentation |
80 if (line == "" || line.startsWith("#")) continue; | 82 if (line == "" || line.startsWith("#")) continue; |
81 | 83 |
82 // find key and value | 84 // find key and value |
83 var colon = line.indexOf(":"); | 85 var colon = line.indexOf(":"); |
84 if (colon == -1) { | 86 if (colon == -1) { |
85 throw "can't parse pubspec.lock at line $lineNumber"; | 87 throw "can't parse pubspec.lock at line $lineNumber"; |
86 } | 88 } |
87 var key = line.substring(0, colon).trim(); | 89 var key = line.substring(0, colon).trim(); |
88 var value = line.substring(colon + 1).trim(); | 90 var value = line.substring(colon + 1).trim(); |
89 if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) { | 91 if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) { |
90 value = value.substring(1, value.length - 1); | 92 value = value.substring(1, value.length - 1); |
91 } | 93 } |
92 | 94 |
93 if (!inPackages) { | 95 if (!inPackages) { |
94 if (key == "packages" && value == "") { | 96 if (key == "packages" && value == "") { |
95 inPackages = true; | 97 inPackages = true; |
96 continue; | 98 continue; |
97 } | 99 } |
98 throw "can't parse pubspec.lock at line $lineNumber"; | 100 throw "can't parse pubspec.lock at line $lineNumber"; |
99 } | 101 } |
100 | 102 |
101 if (value == "") { | 103 if (value == "") { |
| 104 if (key == "description") { |
| 105 inDescription = true; |
| 106 continue; |
| 107 } |
102 if (pv != null) out.add(pv); | 108 if (pv != null) out.add(pv); |
103 pv = new pb.PackageVersion()..name = key; | 109 pv = new pb.PackageVersion()..name = key; |
104 continue; | 110 continue; |
105 } | 111 } |
106 | 112 |
107 if (pv == null) { | 113 if (pv == null) { |
108 throw "can't parse pubspec.lock at line $lineNumber"; | 114 throw "can't parse pubspec.lock at line $lineNumber - no value for $key"; |
109 } | 115 } |
110 | 116 |
| 117 if (inDescription && (key == "name" || key == "url")) continue; |
| 118 inDescription = false; |
| 119 |
111 switch (key) { | 120 switch (key) { |
112 case "description": | 121 case "description": |
113 break; | 122 break; |
114 case "source": | 123 case "source": |
115 pv.source = value; | 124 pv.source = value; |
116 break; | 125 break; |
117 case "version": | 126 case "version": |
118 pv.version = value; | 127 pv.version = value; |
119 break; | 128 break; |
120 case "path": | 129 case "path": |
121 pv.path = value; | 130 pv.path = value; |
122 break; | 131 break; |
123 case "relative": | 132 case "relative": |
124 break; // ignore | 133 break; // ignore |
| 134 case "sdk": |
| 135 break; // ignore |
125 default: | 136 default: |
126 throw "can't parse pubspec.lock at line $lineNumber"; | 137 throw "can't parse pubspec.lock at line $lineNumber - unknown key $key"; |
127 } | 138 } |
128 } | 139 } |
129 return out; | 140 return out; |
130 } | 141 } |
131 | 142 |
132 /// Encodes a report as nicely-formatted JSON. | 143 /// Encodes a report as nicely-formatted JSON. |
133 String encodeReport(pb.Report report) { | 144 String encodeReport(pb.Report report) { |
134 var json = report.writeToJsonMap(); | 145 var json = report.writeToJsonMap(); |
135 var out = new StringBuffer(); | 146 var out = new StringBuffer(); |
136 _stringifyMap(out, json, ""); | 147 _stringifyMap(out, json, ""); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 out.write("[\n"); | 179 out.write("[\n"); |
169 bool first = true; | 180 bool first = true; |
170 for (var item in json) { | 181 for (var item in json) { |
171 if (!first) out.write(",\n"); | 182 if (!first) out.write(",\n"); |
172 first = false; | 183 first = false; |
173 out.write(childIndent); | 184 out.write(childIndent); |
174 out.write(JSON.encode(item)); | 185 out.write(JSON.encode(item)); |
175 } | 186 } |
176 out.write("\n$indent]"); | 187 out.write("\n$indent]"); |
177 } | 188 } |
OLD | NEW |