-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathci_with_channel.dart
More file actions
137 lines (120 loc) · 3.86 KB
/
ci_with_channel.dart
File metadata and controls
137 lines (120 loc) · 3.86 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
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';
/// Do not use this with GitHub Actions. It is a convenience script for running
/// CI locally.
///
/// Usage: dart tool/ci_with_channel.dart -c stable,beta
Future<void> main(List<String> args) async {
final parser = ArgParser()
..addFlag(
'help',
abbr: 'h',
negatable: false,
help: 'Print this usage information.',
)
..addOption(
'channels',
abbr: 'c',
help:
'Comma-separated list of channels to run on (e.g., stable,beta,main)',
);
final results = parser.parse(args);
if (results['help'] as bool) {
print(parser.usage);
return;
}
final channelsArg = results['channels'] as String?;
List<String> channels;
if (channelsArg != null) {
channels = channelsArg.split(',').map((c) => c.trim()).toList();
} else {
// Default to current channel if none specified
channels = [];
}
final rootDir = Directory.current;
final originalChannel = await _getCurrentChannel();
try {
if (channels.isEmpty) {
print('Running on current channel: $originalChannel');
await _runCI(rootDir);
} else {
for (final channel in channels) {
print('\n=== Switching to channel: $channel ===');
await _runCommand('flutter', ['channel', channel]);
await _runCommand('flutter', ['doctor']);
await _runCI(rootDir);
}
}
} finally {
if (channels.isNotEmpty && originalChannel != null) {
print('\n=== Switching back to original channel: $originalChannel ===');
await _runCommand('flutter', ['channel', originalChannel]);
}
}
}
Future<String?> _getCurrentChannel() async {
final result = await Process.run('flutter', ['channel']);
if (result.exitCode != 0) {
return null;
}
final output = result.stdout as String;
final match = RegExp(r'\* (\w+)').firstMatch(output);
return match?.group(1);
}
Future<void> _runCI(Directory rootDir) async {
final pubspecFile = File(path.join(rootDir.path, 'pubspec.yaml'));
final pubspecContent = await pubspecFile.readAsString();
final pubspecYaml = loadYaml(pubspecContent);
final workspace = pubspecYaml['workspace'] as YamlList?;
if (workspace == null) {
print('No workspace found in pubspec.yaml');
exit(1);
}
// pub workspace, only run 'get' once
await _runCommand('flutter', ['pub', 'get'], workingDirectory: rootDir.path);
final packages = workspace.map((e) => e.toString()).toList();
for (final package in packages) {
final packagePath = path.join(rootDir.path, package);
print('== Testing \'$package\' ==');
await _runCommand('dart', [
'analyze',
'--fatal-infos',
'--fatal-warnings',
], workingDirectory: packagePath);
await _runCommand('dart', ['format', '.'], workingDirectory: packagePath);
if (await Directory(path.join(packagePath, 'test')).exists()) {
final packagePubspecFile = File(path.join(packagePath, 'pubspec.yaml'));
final packagePubspecContent = await packagePubspecFile.readAsString();
if (packagePubspecContent.contains('flutter:')) {
await _runCommand('flutter', [
'test',
'--no-pub',
], workingDirectory: packagePath);
} else {
await _runCommand('dart', ['test'], workingDirectory: packagePath);
}
}
}
}
Future<void> _runCommand(
String executable,
List<String> arguments, {
String? workingDirectory,
}) async {
final process = await Process.start(
executable,
arguments,
workingDirectory: workingDirectory,
runInShell: true,
mode: ProcessStartMode.inheritStdio,
);
final exitCode = await process.exitCode;
if (exitCode != 0) {
print(
'Command "$executable ${arguments.join(' ')}" failed with exit code $exitCode in $workingDirectory',
);
exit(exitCode);
}
}