Skip to content

Commit 05eb57b

Browse files
stereotype441Commit Bot
authored andcommitted
Migration: add assemble_resources.dart script.
This script does the same job as `generate_resources.dart`, but it doesn't search the filesystem for the input files to use, nor invoke the compiler to create the `migration.js` file. Instead, it simply accepts a list of input files on the command line, and outputs their contents in an appropriate form to be stored in `resources.g.dart`. This script will allow us to integrate more cleanly with the internal build system. Change-Id: I4b5445d1f5fd7eccbb5fb36f6ee4fa97d2cea87e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254421 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent a547cfc commit 05eb57b

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2022, 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+
// This script provides an alternative way of creating the
6+
// lib/src/front_end/resources/resources.g.dart file, based on a set of provided
7+
// files that has already been suitably compiled.
8+
9+
import 'dart:io';
10+
11+
import 'package:args/args.dart';
12+
13+
import 'generate_resources.dart';
14+
15+
main(List<String> args) {
16+
var argParser = ArgParser()
17+
..addOption('output', abbr: 'o', help: 'Output to FILE', valueHelp: 'FILE')
18+
..addFlag('help', abbr: 'h', negatable: false, help: 'Show help');
19+
var parsedArgs = argParser.parse(args);
20+
if (parsedArgs['help'] as bool) {
21+
print('Usage: dart assemble_resources.dart INPUT_FILES');
22+
print('');
23+
print(argParser.usage);
24+
exit(1);
25+
}
26+
var content =
27+
generateResourceFile([for (var arg in parsedArgs.rest) File(arg)]);
28+
var output = parsedArgs['output'] as String?;
29+
if (output == null) {
30+
stdout.write(content);
31+
} else {
32+
File(output).writeAsStringSync(content);
33+
}
34+
}

pkg/nnbd_migration/tool/codegen/generate_resources.dart

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ Future<void> compileWebFrontEnd(
110110
}
111111

112112
void createResourcesGDart() {
113-
var content =
114-
generateResourceFile(sortDir(resourceDir.listSync()).where((entity) {
115-
var name = path.basename(entity.path);
116-
return entity is File && resourceTypes.contains(path.extension(name));
117-
}).cast<File>());
113+
var content = generateResourceFile(
114+
sortDir(resourceDir.listSync()).where((entity) {
115+
var name = path.basename(entity.path);
116+
return entity is File && resourceTypes.contains(path.extension(name));
117+
}).cast<File>(),
118+
sourcesMd5: _computeSourcesMd5());
118119

119120
// write the content
120121
resourcesFile.writeAsStringSync(content);
@@ -149,7 +150,7 @@ To re-generate lib/src/front_end/resources/resources.g.dart, run:
149150
exit(1);
150151
}
151152

152-
String generateResourceFile(Iterable<File> resources) {
153+
String generateResourceFile(Iterable<File> resources, {String? sourcesMd5}) {
153154
var filePath = path.relative(Platform.script.toFilePath());
154155
var buf = StringBuffer('''
155156
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
@@ -196,17 +197,10 @@ String _decode(String data) {
196197

197198
buf.writeln();
198199
buf.writeln('String? _$name;');
199-
if (name == path.basename(javascriptOutput.path).replaceAll('.', '_')) {
200+
if (sourcesMd5 != null &&
201+
name == path.basename(javascriptOutput.path).replaceAll('.', '_')) {
200202
// Write out the crc for the dart code.
201-
var sourceCode = StringBuffer();
202-
// collect the dart source code
203-
for (var entity in sortDir(dartSources.parent.listSync())) {
204-
if (entity.path.endsWith('.dart')) {
205-
sourceCode.write((entity as File).readAsStringSync());
206-
}
207-
}
208-
buf.writeln(
209-
"// migration_dart md5 is '${md5String(sourceCode.toString())}'");
203+
buf.writeln("// migration_dart md5 is '$sourcesMd5'");
210204
} else {
211205
// highlight_css md5 is 'fb012626bafd286510d32da815dae448'
212206
buf.writeln("// $name md5 is '${md5String(source)}'");
@@ -272,18 +266,24 @@ void verifyResourcesGDartGenerated({
272266
}
273267

274268
// verify the compiled dart code
269+
String hash = _computeSourcesMd5();
270+
if (hash != resourceHashes['migration_dart']) {
271+
failVerification('Compiled javascript not up to date in resources.g.dart');
272+
}
273+
274+
print('Generated resources up to date.');
275+
}
276+
277+
String _computeSourcesMd5() {
275278
var sourceCode = StringBuffer();
279+
// collect the dart source code
276280
for (var entity in sortDir(dartSources.parent.listSync())) {
277281
if (entity.path.endsWith('.dart')) {
278282
sourceCode.write((entity as File).readAsStringSync());
279283
}
280284
}
281-
var hash = md5String(sourceCode.toString());
282-
if (hash != resourceHashes['migration_dart']) {
283-
failVerification('Compiled javascript not up to date in resources.g.dart');
284-
}
285-
286-
print('Generated resources up to date.');
285+
var sourcesMd5 = md5String(sourceCode.toString());
286+
return sourcesMd5;
287287
}
288288

289289
typedef VerificationFunction = void Function(String);

0 commit comments

Comments
 (0)