-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgenerate.dart
192 lines (163 loc) · 5.81 KB
/
generate.dart
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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:io';
import 'package:markdown/markdown.dart';
import 'package:path/path.dart';
import 'package:pub_semver/pub_semver.dart';
import 'common/generate_common.dart';
import 'dart/generate_dart_client.dart';
import 'dart/generate_dart_common.dart';
import 'dart/generate_dart_interface.dart';
import 'java/generate_java.dart' as java show Api, api, JavaGenerator;
final bool _stampPubspecVersion = false;
/// Parse the 'service.md' into a model and generate both Dart and Java
/// libraries.
Future<void> main(List<String> args) async {
final codeGeneratorDir = dirname(Platform.script.toFilePath());
// Parse service.md into a model.
final file = File(
normalize(join(codeGeneratorDir, '../../../runtime/vm/service/service.md')),
);
final document = Document();
final buf = StringBuffer(file.readAsStringSync());
final nodes = document.parseLines(buf.toString().split('\n'));
print('Parsed ${file.path}.');
print('Service protocol version ${ApiParseUtil.parseVersionString(nodes)}.');
// Generate code from the model.
print('');
await _generateDartClient(codeGeneratorDir, nodes);
await _generateDartInterface(codeGeneratorDir, nodes);
await _generateJava(codeGeneratorDir, nodes);
}
Future<void> _generateDartClient(
String codeGeneratorDir, List<Node> nodes) async {
final outputFilePath = await _generateDartCommon(
api: VmServiceApi(),
nodes: nodes,
codeGeneratorDir: codeGeneratorDir,
packageName: 'vm_service',
interfaceName: 'VmService',
);
print('Wrote Dart client to $outputFilePath.');
}
Future<void> _generateDartInterface(
String codeGeneratorDir, List<Node> nodes) async {
final outputFilePath = await _generateDartCommon(
api: VmServiceInterfaceApi(),
nodes: nodes,
codeGeneratorDir: codeGeneratorDir,
packageName: 'vm_service_interface',
interfaceName: 'VmServiceInterface',
);
print('Wrote Dart interface to $outputFilePath.');
}
Future<String> _generateDartCommon({
required Api api,
required List<Node> nodes,
required String codeGeneratorDir,
required String packageName,
required String interfaceName,
}) async {
final outDirPath = normalize(
join(
codeGeneratorDir,
'../..',
packageName,
'lib/src',
),
);
final outDir = Directory(outDirPath);
if (!outDir.existsSync()) {
outDir.createSync(recursive: true);
}
final outputFile = File(
join(
outDirPath,
'$packageName.dart',
),
);
final generator = DartGenerator(interfaceName: interfaceName);
// Generate the code.
api.parse(nodes);
api.generate(generator);
outputFile.writeAsStringSync(generator.toString());
// Clean up the code.
await _runDartFormat(outDirPath);
if (_stampPubspecVersion) {
// Update the pubspec file.
Version version = ApiParseUtil.parseVersionSemVer(nodes);
_stampPubspec(version);
// Validate that the changelog contains an entry for the current version.
_checkUpdateChangelog(version);
}
return outputFile.path;
}
Future<void> _runDartFormat(String outDirPath) async {
ProcessResult result = Process.runSync('dart', ['format', outDirPath]);
if (result.exitCode != 0) {
print('dart format: ${result.stdout}\n${result.stderr}');
throw result.exitCode;
}
}
Future<void> _generateJava(String codeGeneratorDir, List<Node> nodes) async {
var srcDirPath = normalize(join(codeGeneratorDir, '..', 'java', 'src'));
var generator = java.JavaGenerator(srcDirPath);
final scriptPath = Platform.script.toFilePath();
final kSdk = '/sdk/';
final scriptLocation =
scriptPath.substring(scriptPath.indexOf(kSdk) + kSdk.length);
java.api = java.Api(scriptLocation);
java.api.parse(nodes);
java.api.generate(generator);
// We generate files into the java/src/ folder; ensure the generated files
// aren't committed to git (but manually maintained files in the same
// directory are).
List<String> generatedPaths = generator.allWrittenFiles
.map((path) => relative(path, from: 'java'))
.toList();
generatedPaths.sort();
File gitignoreFile = File(join(codeGeneratorDir, '..', 'java', '.gitignore'));
gitignoreFile.writeAsStringSync('''
# This is a generated file.
${generatedPaths.join('\n')}
''');
// Generate a version file.
Version version = ApiParseUtil.parseVersionSemVer(nodes);
File file = File(join('java', 'version.properties'));
file.writeAsStringSync('version=${version.major}.${version.minor}\n');
print('Wrote Java to $srcDirPath.');
}
// Push the major and minor versions into the pubspec.
void _stampPubspec(Version version) {
final String pattern = 'version: ';
File file = File('pubspec.yaml');
String text = file.readAsStringSync();
bool found = false;
text = text.split('\n').map((line) {
if (line.startsWith(pattern)) {
found = true;
Version v = Version.parse(line.substring(pattern.length));
String? pre = v.preRelease.isEmpty ? null : v.preRelease.join('-');
String? build = v.build.isEmpty ? null : v.build.join('+');
v = Version(version.major, version.minor, v.patch,
pre: pre, build: build);
return '$pattern${v.toString()}';
} else {
return line;
}
}).join('\n');
if (!found) throw '`$pattern` not found';
file.writeAsStringSync(text);
}
void _checkUpdateChangelog(Version version) {
// Look for `## major.minor`.
String check = '## ${version.major}.${version.minor}';
File file = File('CHANGELOG.md');
String text = file.readAsStringSync();
bool containsReleaseNotes =
text.split('\n').any((line) => line.startsWith(check));
if (!containsReleaseNotes) {
throw '`$check` not found in the CHANGELOG.md file';
}
}