@@ -6,6 +6,8 @@ library protoc.benchmark.report;
66
77import 'dart:convert' show jsonEncode;
88
9+ import 'package:yaml/yaml.dart' ;
10+
911import 'generated/benchmark.pb.dart' as pb;
1012
1113pb.Response findUpdatedResponse (pb.Report beforeRep, pb.Report afterRep) {
@@ -67,75 +69,23 @@ pb.Packages createPackages(String pubspecYaml, String pubspecLock) {
6769/// - assumes all other lines without a value except 'description'
6870/// start a new package
6971/// - only allows known keys within a package
70- List <pb.PackageVersion > _parseLockFile (String contents) {
71- var out = < pb.PackageVersion > [];
72-
73- bool inPackages = false ;
74- bool inDescription = false ;
75- pb.PackageVersion pv = null ;
76- var lineNumber = 0 ;
77- for (var line in contents.split ("\n " )) {
78- lineNumber++ ;
79- line = line.trim (); // ignore indentation
80- if (line == "" || line.startsWith ("#" )) continue ;
81-
82- // find key and value
83- var colon = line.indexOf (":" );
84- if (colon == - 1 ) {
85- throw "can't parse pubspec.lock at line $lineNumber " ;
86- }
87- var key = line.substring (0 , colon).trim ();
88- var value = line.substring (colon + 1 ).trim ();
89- if (value.length >= 2 && value.startsWith ('"' ) && value.endsWith ('"' )) {
90- value = value.substring (1 , value.length - 1 );
91- }
92-
93- if (! inPackages) {
94- if (key == "packages" && value == "" ) {
95- inPackages = true ;
96- continue ;
97- }
98- throw "can't parse pubspec.lock at line $lineNumber " ;
99- }
100-
101- if (value == "" ) {
102- if (key == "description" ) {
103- inDescription = true ;
104- continue ;
105- }
106- if (pv != null ) out.add (pv);
107- pv = new pb.PackageVersion ()..name = key;
108- continue ;
109- }
110-
111- if (pv == null ) {
112- throw "can't parse pubspec.lock at line $lineNumber - no value for $key " ;
113- }
114-
115- if (inDescription && (key == "name" || key == "url" )) continue ;
116- inDescription = false ;
117-
118- switch (key) {
119- case "description" :
120- break ;
121- case "source" :
122- pv.source = value;
123- break ;
124- case "version" :
125- pv.version = value;
126- break ;
127- case "path" :
128- pv.path = value;
129- break ;
130- case "relative" :
131- break ; // ignore
132- case "sdk" :
133- break ; // ignore
134- default :
135- throw "can't parse pubspec.lock at line $lineNumber - unknown key $key " ;
72+ Iterable <pb.PackageVersion > _parseLockFile (String contents) sync * {
73+ var yaml = loadYaml (contents) as YamlMap ;
74+ var packages = yaml['packages' ] as YamlMap ;
75+
76+ for (var entry in packages.entries) {
77+ var map = entry.value as YamlMap ;
78+ var pkgVersion = new pb.PackageVersion ()
79+ ..name = entry.key as String
80+ ..source = map['source' ]
81+ ..version = map['version' ];
82+
83+ var path = (map['description' ] as YamlMap )['path' ];
84+ if (path != null ) {
85+ pkgVersion.path = path;
13686 }
87+ yield pkgVersion;
13788 }
138- return out;
13989}
14090
14191/// Encodes a report as nicely-formatted JSON.
0 commit comments