-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathreport.dart
More file actions
137 lines (118 loc) · 3.8 KB
/
Copy pathreport.dart
File metadata and controls
137 lines (118 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert' show jsonEncode;
import 'package:yaml/yaml.dart';
import 'generated/benchmark.pb.dart' as pb;
pb.Response? findUpdatedResponse(pb.Report? beforeRep, pb.Report afterRep) {
if (beforeRep == null) return afterRep.responses[0];
assert(beforeRep.responses.length == afterRep.responses.length);
for (var i = 0; i < afterRep.responses.length; i++) {
var before = beforeRep.responses[i];
var after = afterRep.responses[i];
assert(before.request.id == after.request.id);
assert(before.request.params == after.request.params);
if (before.samples.length != after.samples.length) {
return after;
}
}
return null; // not found
}
/// Returns a partially filled in pb.Platform.
///
/// It only includes the values we can determine without dart:html or dart:io.
pb.Platform createPlatform() {
return pb.Platform()
..dartVM = _isDartVM
..checkedMode = _implicitChecksEnabled;
}
bool get _isDartVM => !identical(1, 1.0);
/// Returns `false` if running via dart2js and `--omit-implicit-checks` is set
final bool _implicitChecksEnabled = () {
// ignore: unused_local_variable
var x = true;
try {
// Trigger an exception if we're in checked mode.
x = '' as dynamic;
return false;
} catch (e) {
return true;
}
}();
/// Given the contents of the pubspec.yaml and pubspec.lock files,
/// create a pb.Packages object.
pb.Packages createPackages(String pubspecYaml, String pubspecLock) {
var out = pb.Packages();
for (var line in pubspecYaml.split('\n')) {
line = line.trim();
if (line.startsWith('version:')) {
out.version = line.substring('version:'.length).trim();
}
}
out.packages.addAll(_parseLockFile(pubspecLock));
return out;
}
/// Quick and dirty lock file parser.
/// - ignores indentation
/// - assumes one top-level key "packages:"
/// - assumes all other lines without a value except 'description'
/// start a new package
/// - only allows known keys within a package
Iterable<pb.PackageVersion> _parseLockFile(String contents) sync* {
var yaml = loadYaml(contents) as YamlMap;
var packages = yaml['packages'] as YamlMap;
for (var entry in packages.entries) {
var map = entry.value as YamlMap;
var pkgVersion = pb.PackageVersion()
..name = entry.key as String
..source = map['source']
..version = map['version'];
var path = (map['description'] as YamlMap)['path'];
if (path != null) {
pkgVersion.path = path;
}
yield pkgVersion;
}
}
/// Encodes a report as nicely-formatted JSON.
String encodeReport(pb.Report report) {
var json = report.writeToJsonMap();
var out = StringBuffer();
_stringifyMap(out, json, '');
out.writeln();
return out.toString();
}
String _stringifyMap(StringBuffer out, Map json, String indent) {
var childIndent = '$indent ';
out.writeln('{');
var first = true;
for (var key in json.keys) {
if (!first) out.writeln(',');
first = false;
var value = json[key];
out.write(childIndent);
out.write(jsonEncode(key));
out.write(': ');
if (value is List) {
_stringifyList(out, value, childIndent);
} else if (value is Map && indent.length < 4) {
_stringifyMap(out, value, childIndent);
} else {
out.write(jsonEncode(value));
}
}
out.write('\n$indent}');
return out.toString();
}
void _stringifyList(StringBuffer out, List json, String indent) {
var childIndent = '$indent ';
out.write('[\n');
var first = true;
for (var item in json) {
if (!first) out.write(',\n');
first = false;
out.write(childIndent);
out.write(jsonEncode(item));
}
out.write('\n$indent]');
}