OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:collection'; | |
6 | |
7 import 'package:source_span/source_span.dart'; | |
8 import 'package:yaml/yaml.dart'; | |
9 | |
10 PSEntry _findEntry(YamlMap map, String key) { | |
11 PSEntry entry; | |
12 map.nodes.forEach((k, v) { | |
13 if (k is YamlScalar && key == k.toString()) { | |
14 entry = _processScalar(k, v); | |
15 } | |
16 }); | |
17 return entry; | |
18 } | |
19 | |
20 PSDependencyList _processDependencies(YamlScalar key, YamlNode v) { | |
21 if (v is! YamlMap) { | |
22 return null; | |
23 } | |
24 YamlMap depsMap = v; | |
25 | |
26 _PSDependencyList deps = new _PSDependencyList(new _PSNode(key)); | |
27 depsMap.nodes.forEach((k, v) => deps.add(new _PSDependency(k, v))); | |
28 return deps; | |
29 } | |
30 | |
31 PSGitRepo _processGitRepo(YamlScalar key, YamlNode v) { | |
32 if (v is! YamlMap) { | |
33 return null; | |
34 } | |
35 YamlMap hostMap = v; | |
36 // url: git://github.com/munificent/kittens.git | |
37 // ref: some-branch | |
38 _PSGitRepo repo = new _PSGitRepo(); | |
39 repo.token = new _PSNode(key); | |
40 repo.ref = _findEntry(hostMap, 'ref'); | |
41 repo.url = _findEntry(hostMap, 'url'); | |
42 return repo; | |
43 } | |
44 | |
45 PSHost _processHost(YamlScalar key, YamlNode v) { | |
46 if (v is! YamlMap) { | |
47 return null; | |
48 } | |
49 YamlMap hostMap = v; | |
50 // name: transmogrify | |
51 // url: http://your-package-server.com | |
52 _PSHost host = new _PSHost(); | |
53 host.token = new _PSNode(key); | |
54 host.name = _findEntry(hostMap, 'name'); | |
55 host.url = _findEntry(hostMap, 'url'); | |
56 return host; | |
57 } | |
58 | |
59 PSNodeList _processList(YamlScalar key, YamlNode v) { | |
60 if (v is! YamlList) { | |
61 return null; | |
62 } | |
63 YamlList nodeList = v; | |
64 | |
65 return new _PSNodeList( | |
66 new _PSNode(key), nodeList.nodes.map((n) => new _PSNode(n))); | |
67 } | |
68 | |
69 PSEntry _processScalar(YamlScalar key, YamlNode value) { | |
70 if (value is! YamlScalar) { | |
71 return null; | |
72 //WARN? | |
73 } | |
74 return new PSEntry(new _PSNode(key), new _PSNode(value)); | |
75 } | |
76 | |
77 abstract class PSDependency { | |
78 PSGitRepo get git; | |
79 PSHost get host; | |
80 PSNode get name; | |
81 PSEntry get version; | |
82 } | |
83 | |
84 abstract class PSDependencyList extends Object | |
85 with IterableMixin<PSDependency> {} | |
86 | |
87 class PSEntry { | |
88 final PSNode key; | |
89 final PSNode value; | |
90 PSEntry(this.key, this.value); | |
91 | |
92 @override | |
93 String toString() => '${key != null ? (key.toString() + ': ') : ''}$value'; | |
94 } | |
95 | |
96 abstract class PSGitRepo { | |
97 PSEntry get ref; | |
98 PSNode get token; | |
99 PSEntry get url; | |
100 } | |
101 | |
102 abstract class PSHost { | |
103 PSEntry get name; | |
104 PSNode get token; | |
105 PSEntry get url; | |
106 } | |
107 | |
108 abstract class PSNode { | |
109 SourceSpan get span; | |
110 String get text; | |
111 } | |
112 | |
113 abstract class PSNodeList extends Object with IterableMixin<PSNode> { | |
114 @override | |
115 Iterator<PSNode> get iterator; | |
116 PSNode get token; | |
117 } | |
118 | |
119 abstract class Pubspec { | |
120 factory Pubspec.parse(String source, {Uri sourceUrl}) => | |
121 new _Pubspec(source, sourceUrl: sourceUrl); | |
122 PSEntry get author; | |
123 PSNodeList get authors; | |
124 PSDependencyList get dependencies; | |
125 PSEntry get description; | |
126 PSDependencyList get devDependencies; | |
127 PSEntry get documentation; | |
128 PSEntry get homepage; | |
129 PSEntry get name; | |
130 PSEntry get version; | |
131 accept(PubspecVisitor visitor); | |
132 } | |
133 | |
134 abstract class PubspecVisitor<T> { | |
135 T visitPackageAuthor(PSEntry author) => null; | |
136 T visitPackageAuthors(PSNodeList authors) => null; | |
137 T visitPackageDependencies(PSDependencyList dependencies) => null; | |
138 T visitPackageDependency(PSDependency dependency) => null; | |
139 T visitPackageDescription(PSEntry description) => null; | |
140 T visitPackageDevDependencies(PSDependencyList dependencies) => null; | |
141 T visitPackageDevDependency(PSDependency dependency) => null; | |
142 T visitPackageDocumentation(PSEntry documentation) => null; | |
143 T visitPackageHomepage(PSEntry homepage) => null; | |
144 T visitPackageName(PSEntry name) => null; | |
145 T visitPackageVersion(PSEntry version) => null; | |
146 } | |
147 | |
148 class _PSDependency extends PSDependency { | |
149 @override | |
150 PSNode name; | |
151 @override | |
152 PSEntry version; | |
153 @override | |
154 PSHost host; | |
155 @override | |
156 PSGitRepo git; | |
157 | |
158 factory _PSDependency(dynamic k, YamlNode v) { | |
159 if (k is! YamlScalar) { | |
160 return null; | |
161 } | |
162 YamlScalar key = k; | |
163 | |
164 _PSDependency dep = new _PSDependency._(); | |
165 | |
166 dep.name = new _PSNode(key); | |
167 | |
168 if (v is YamlScalar) { | |
169 // Simple version | |
170 dep.version = new PSEntry(null, new _PSNode(v)); | |
171 } else if (v is YamlMap) { | |
172 // hosted: | |
173 // name: transmogrify | |
174 // url: http://your-package-server.com | |
175 // version: '>=0.4.0 <1.0.0' | |
176 YamlMap details = v; | |
177 details.nodes.forEach((k, v) { | |
178 if (k is! YamlScalar) { | |
179 return; | |
180 } | |
181 YamlScalar key = k; | |
182 switch (key.toString()) { | |
183 case 'version': | |
184 dep.version = _processScalar(key, v); | |
185 break; | |
186 case 'hosted': | |
187 dep.host = _processHost(key, v); | |
188 break; | |
189 case 'git': | |
190 dep.git = _processGitRepo(key, v); | |
191 break; | |
192 } | |
193 }); | |
194 } | |
195 return dep; | |
196 } | |
197 | |
198 _PSDependency._(); | |
199 | |
200 @override | |
201 String toString() { | |
202 var sb = new StringBuffer(); | |
203 if (name != null) { | |
204 sb.write('$name:'); | |
205 } | |
206 var versionInfo = ''; | |
207 if (version != null) { | |
208 if (version.key == null) { | |
209 versionInfo = ' $version'; | |
210 } else { | |
211 versionInfo = '\n $version'; | |
212 } | |
213 } | |
214 sb.writeln(versionInfo); | |
215 if (host != null) { | |
216 sb.writeln(host); | |
217 } | |
218 if (git != null) { | |
219 sb.writeln(git); | |
220 } | |
221 return sb.toString(); | |
222 } | |
223 } | |
224 | |
225 class _PSDependencyList extends PSDependencyList { | |
226 final dependencies = <PSDependency>[]; | |
227 final PSNode token; | |
228 | |
229 _PSDependencyList(this.token); | |
230 | |
231 @override | |
232 Iterator<PSDependency> get iterator => dependencies.iterator; | |
233 | |
234 add(PSDependency dependency) { | |
235 if (dependency != null) { | |
236 dependencies.add(dependency); | |
237 } | |
238 } | |
239 | |
240 @override | |
241 String toString() => '$token\n${dependencies.join(' ')}'; | |
242 } | |
243 | |
244 class _PSGitRepo implements PSGitRepo { | |
245 @override | |
246 PSNode token; | |
247 @override | |
248 PSEntry ref; | |
249 @override | |
250 PSEntry url; | |
251 @override | |
252 String toString() => ''' | |
253 $token: | |
254 $url | |
255 $ref'''; | |
256 } | |
257 | |
258 class _PSHost implements PSHost { | |
259 @override | |
260 PSNode token; | |
261 @override | |
262 PSEntry name; | |
263 @override | |
264 PSEntry url; | |
265 @override | |
266 String toString() => ''' | |
267 $token: | |
268 $name | |
269 $url'''; | |
270 } | |
271 | |
272 class _PSNode implements PSNode { | |
273 @override | |
274 final String text; | |
275 @override | |
276 final SourceSpan span; | |
277 | |
278 _PSNode(YamlNode node) | |
279 : text = node.value?.toString(), | |
280 span = node.span; | |
281 | |
282 @override | |
283 String toString() => '$text'; | |
284 } | |
285 | |
286 class _PSNodeList extends PSNodeList { | |
287 @override | |
288 final PSNode token; | |
289 final Iterable<PSNode> nodes; | |
290 | |
291 _PSNodeList(this.token, this.nodes); | |
292 | |
293 @override | |
294 Iterator<PSNode> get iterator => nodes.iterator; | |
295 | |
296 @override | |
297 String toString() => ''' | |
298 $token: | |
299 - ${nodes.join('\n - ')}'''; | |
300 } | |
301 | |
302 class _Pubspec implements Pubspec { | |
303 @override | |
304 PSEntry author; | |
305 @override | |
306 PSNodeList authors; | |
307 @override | |
308 PSEntry description; | |
309 @override | |
310 PSEntry documentation; | |
311 @override | |
312 PSEntry homepage; | |
313 @override | |
314 PSEntry name; | |
315 @override | |
316 PSEntry version; | |
317 @override | |
318 PSDependencyList dependencies; | |
319 @override | |
320 PSDependencyList devDependencies; | |
321 | |
322 _Pubspec(String src, {Uri sourceUrl}) { | |
323 try { | |
324 _parse(src, sourceUrl: sourceUrl); | |
325 } on Exception { | |
326 // ignore | |
327 } | |
328 } | |
329 | |
330 @override | |
331 void accept(PubspecVisitor visitor) { | |
332 if (author != null) { | |
333 visitor.visitPackageAuthor(author); | |
334 } | |
335 if (authors != null) { | |
336 visitor.visitPackageAuthors(authors); | |
337 } | |
338 if (description != null) { | |
339 visitor.visitPackageDescription(description); | |
340 } | |
341 if (documentation != null) { | |
342 visitor.visitPackageDocumentation(documentation); | |
343 } | |
344 if (homepage != null) { | |
345 visitor.visitPackageHomepage(homepage); | |
346 } | |
347 if (name != null) { | |
348 visitor.visitPackageName(name); | |
349 } | |
350 if (version != null) { | |
351 visitor.visitPackageVersion(version); | |
352 } | |
353 if (dependencies != null) { | |
354 visitor.visitPackageDependencies(dependencies); | |
355 dependencies.forEach((d) => visitor.visitPackageDependency(d)); | |
356 } | |
357 if (devDependencies != null) { | |
358 visitor.visitPackageDevDependencies(devDependencies); | |
359 devDependencies.forEach((d) => visitor.visitPackageDevDependency(d)); | |
360 } | |
361 } | |
362 | |
363 @override | |
364 String toString() { | |
365 var sb = new _StringBuilder(); | |
366 sb.writelin(name); | |
367 sb.writelin(version); | |
368 sb.writelin(author); | |
369 sb.writelin(authors); | |
370 sb.writelin(description); | |
371 sb.writelin(homepage); | |
372 sb.writelin(dependencies); | |
373 sb.writelin(devDependencies); | |
374 return sb.toString(); | |
375 } | |
376 | |
377 _parse(String src, {Uri sourceUrl}) { | |
378 var yaml = loadYamlNode(src, sourceUrl: sourceUrl); | |
379 if (yaml is! YamlMap) { | |
380 return; | |
381 } | |
382 YamlMap yamlMap = yaml; | |
383 yamlMap.nodes.forEach((k, v) { | |
384 if (k is! YamlScalar) { | |
385 return; | |
386 } | |
387 YamlScalar key = k; | |
388 switch (key.toString()) { | |
389 case 'author': | |
390 author = _processScalar(key, v); | |
391 break; | |
392 case 'authors': | |
393 authors = _processList(key, v); | |
394 break; | |
395 case 'homepage': | |
396 homepage = _processScalar(key, v); | |
397 break; | |
398 case 'name': | |
399 name = _processScalar(key, v); | |
400 break; | |
401 case 'description': | |
402 description = _processScalar(key, v); | |
403 break; | |
404 case 'documentation': | |
405 documentation = _processScalar(key, v); | |
406 break; | |
407 case 'dependencies': | |
408 dependencies = _processDependencies(key, v); | |
409 break; | |
410 case 'dev_dependencies': | |
411 devDependencies = _processDependencies(key, v); | |
412 break; | |
413 case 'version': | |
414 version = _processScalar(key, v); | |
415 break; | |
416 } | |
417 }); | |
418 } | |
419 } | |
420 | |
421 class _StringBuilder { | |
422 StringBuffer buffer = new StringBuffer(); | |
423 @override | |
424 String toString() => buffer.toString(); | |
425 writelin(Object value) { | |
426 if (value != null) { | |
427 buffer.writeln(value); | |
428 } | |
429 } | |
430 } | |
OLD | NEW |