Skip to content

Commit 450a761

Browse files
authored
[hooks_runner] Add the manifest generator to the ci checks (#2491)
1 parent 83b6b84 commit 450a761

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

pkgs/hooks_runner/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies:
2525
yaml: ^3.1.3
2626

2727
dev_dependencies:
28+
args: any
2829
custom_lint: ^0.7.5
2930
dart_flutter_team_lints: ^3.5.2
3031
data_assets: any # Used in tests.

pkgs/hooks_runner/test_data/manifest_generator.dart

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,48 @@
44

55
import 'dart:io';
66

7-
void main(List<String> args) async {
8-
final testDataDirectory = Directory.fromUri(Platform.script.resolve('.'));
9-
updateManifest(testDataDirectory, allowPartialProjects: false);
7+
import 'package:args/args.dart';
8+
import 'package:native_test_helpers/native_test_helpers.dart';
9+
10+
void main(List<String> args) {
11+
final stopwatch = Stopwatch()..start();
12+
final parser = ArgParser()
13+
..addFlag(
14+
'set-exit-if-changed',
15+
negatable: false,
16+
help: 'Return a non-zero exit code if any files were changed.',
17+
);
18+
final argResults = parser.parse(args);
19+
final setExitIfChanged = argResults['set-exit-if-changed'] as bool;
20+
21+
final counts = Counts();
22+
23+
updateManifests(counts);
24+
25+
stopwatch.stop();
26+
final duration = stopwatch.elapsedMilliseconds / 1000.0;
27+
print(
28+
'Generated ${counts.generated} files (${counts.changed} changed) in '
29+
'${duration.toStringAsFixed(2)} seconds.',
30+
);
31+
if (setExitIfChanged && counts.changed > 0) {
32+
exit(1);
33+
}
34+
}
35+
36+
class Counts {
37+
int generated = 0;
38+
int changed = 0;
39+
}
40+
41+
void updateManifests(Counts counts) async {
42+
final packageUri = findPackageRoot('hooks_runner');
43+
final testDataUri = packageUri.resolve('test_data/');
44+
final testDataDirectory = Directory.fromUri(testDataUri);
45+
updateManifest(testDataDirectory, counts, allowPartialProjects: false);
1046
final all = testDataDirectory.listSync(recursive: true);
1147
all.whereType<Directory>().forEach(
12-
(e) => updateManifest(e, allowPartialProjects: true),
48+
(e) => updateManifest(e, counts, allowPartialProjects: true),
1349
);
1450
}
1551

@@ -34,13 +70,17 @@ const partialProjects = [
3470
'simple_link_change_asset',
3571
];
3672

37-
void updateManifest(Directory directory, {required bool allowPartialProjects}) {
73+
void updateManifest(
74+
Directory directory,
75+
Counts counts, {
76+
required bool allowPartialProjects,
77+
}) {
3878
final manifestFile = File.fromUri(directory.uri.resolve('manifest.yaml'));
3979
if (!manifestFile.existsSync()) {
4080
return;
4181
}
4282
final all = directory.listSync(recursive: true);
43-
final dirPath = directory.uri.toFilePath();
83+
final dirPath = directory.uri.toFilePath(windows: false);
4484
final files =
4585
all
4686
.whereType<File>()
@@ -52,15 +92,32 @@ void updateManifest(Directory directory, {required bool allowPartialProjects}) {
5292
'$partialProject/pubspec.yaml',
5393
],
5494
]) {
55-
if (f.path.contains(denyString)) return false;
95+
if (f.uri.toFilePath(windows: false).contains(denyString)) {
96+
return false;
97+
}
5698
}
5799

58100
return true;
59101
})
60-
.map((e) => e.path.replaceFirst(dirPath, ''))
102+
.map(
103+
(e) => e.uri.toFilePath(windows: false).replaceFirst(dirPath, ''),
104+
)
61105
.toList()
62106
..sort();
63-
manifestFile.writeAsStringSync(header + files.map((e) => '- $e\n').join());
107+
108+
var oldContent = '';
109+
if (manifestFile.existsSync()) {
110+
oldContent = manifestFile.readAsStringSync();
111+
}
112+
final newContent = header + files.map((e) => '- $e\n').join();
113+
final newContentNormalized = newContent.replaceAll('\r\n', '\n');
114+
final oldContentNormalized = oldContent.replaceAll('\r\n', '\n');
115+
if (newContentNormalized != oldContentNormalized) {
116+
manifestFile.writeAsStringSync(newContent);
117+
print('Generated ${manifestFile.uri} (content changed)');
118+
counts.changed++;
119+
}
120+
counts.generated++;
64121
}
65122

66123
const header = '''

tool/ci.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void main(List<String> arguments) async {
5555

5656
if (argResults['generate'] as bool) {
5757
const generators = [
58+
'pkgs/hooks_runner/test_data/manifest_generator.dart',
5859
'pkgs/hooks/tool/generate_schemas.dart',
5960
'pkgs/hooks/tool/generate_syntax.dart',
6061
'pkgs/hooks/tool/normalize.dart',

0 commit comments

Comments
 (0)