refactor(build): 统一本地与 CI 构建入口及平台发布工具 (#9)
Gitea CI / Check code and tests (push) Successful in 7m38s
Gitea CI / Build Android APK (push) Successful in 6m20s
Gitea CI / Audit and package Windows x64 (push) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (push) Canceled after 0s
Gitea CI / Gitea CI passed (push) Canceled after 0s
Gitea CI / Build Windows x64 app (push) Canceled after 14m42s
Gitea CI / Check code and tests (push) Successful in 7m38s
Gitea CI / Build Android APK (push) Successful in 6m20s
Gitea CI / Audit and package Windows x64 (push) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (push) Canceled after 0s
Gitea CI / Gitea CI passed (push) Canceled after 0s
Gitea CI / Build Windows x64 app (push) Canceled after 14m42s
This commit was merged in pull request #9.
This commit is contained in:
1 parent
a13a54d5ec
commit
4a759d81a6
61 files changed
+1913
-388
No files matched your search
@@ -0,0 +1,195 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
Map<String, String> parseLocalEnvironment(String source) {
|
||||
final values = <String, String>{};
|
||||
final lines = const LineSplitter().convert(
|
||||
source.replaceFirst(RegExp(r'^\uFEFF'), ''),
|
||||
);
|
||||
final assignment = RegExp(
|
||||
r'^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$',
|
||||
);
|
||||
for (var index = 0; index < lines.length; index++) {
|
||||
final line = lines[index].trim();
|
||||
if (line.isEmpty || line.startsWith('#')) continue;
|
||||
final match = assignment.firstMatch(line);
|
||||
if (match == null) {
|
||||
throw FormatException(
|
||||
'Invalid .env.local assignment on line ${index + 1}.',
|
||||
);
|
||||
}
|
||||
final key = match[1]!;
|
||||
if (values.containsKey(key)) {
|
||||
throw FormatException('Duplicate .env.local key on line ${index + 1}.');
|
||||
}
|
||||
var value = match[2]!;
|
||||
if (value.startsWith('"') || value.startsWith("'")) {
|
||||
final end = value.indexOf(value[0], 1);
|
||||
if (end == -1 ||
|
||||
(value.substring(end + 1).trim().isNotEmpty &&
|
||||
!value.substring(end + 1).trim().startsWith('#'))) {
|
||||
throw FormatException(
|
||||
'Invalid .env.local quotes on line ${index + 1}.',
|
||||
);
|
||||
}
|
||||
value = value.substring(1, end);
|
||||
} else {
|
||||
value = value.split(RegExp(r'\s+#')).first.trimRight();
|
||||
}
|
||||
values[key] = value;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
bool isContinuousIntegration(Map<String, String> environment) => [
|
||||
'CI',
|
||||
'GITEA_ACTIONS',
|
||||
'GITHUB_ACTIONS',
|
||||
].any((key) => ['true', '1', 'yes'].contains(environment[key]?.toLowerCase()));
|
||||
|
||||
Map<String, String> mergeLocalEnvironment(
|
||||
Map<String, String> inherited,
|
||||
Map<String, String> local, {
|
||||
required bool windows,
|
||||
}) {
|
||||
final result = Map<String, String>.of(inherited);
|
||||
final existing = inherited.keys
|
||||
.map((key) => windows ? key.toUpperCase() : key)
|
||||
.toSet();
|
||||
for (final entry in local.entries) {
|
||||
final key = windows ? entry.key.toUpperCase() : entry.key;
|
||||
if (existing.add(key)) result[entry.key] = entry.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Map<String, String> loadLocalEnvironment(
|
||||
Directory root,
|
||||
Map<String, String> inherited, {
|
||||
required bool windows,
|
||||
}) {
|
||||
// Check the parent environment before opening any local file in CI.
|
||||
if (isContinuousIntegration(inherited)) return Map.of(inherited);
|
||||
final file = File.fromUri(root.uri.resolve('.env.local'));
|
||||
return mergeLocalEnvironment(
|
||||
inherited,
|
||||
file.existsSync() ? parseLocalEnvironment(file.readAsStringSync()) : {},
|
||||
windows: windows,
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> runLocalFlutter(
|
||||
List<String> arguments, {
|
||||
required Directory root,
|
||||
required Map<String, String> inherited,
|
||||
}) async {
|
||||
return runProjectTool('flutter', arguments, root: root, inherited: inherited);
|
||||
}
|
||||
|
||||
Directory projectSdkBin(Directory root, {bool allowCiSdk = false}) {
|
||||
final bin = allowCiSdk && isContinuousIntegration(Platform.environment)
|
||||
? File(Platform.resolvedExecutable).parent.parent.parent.parent
|
||||
: Directory.fromUri(root.uri.resolve('.fvm/flutter_sdk/bin/'));
|
||||
if (!File.fromUri(
|
||||
bin.uri.resolve(Platform.isWindows ? 'flutter.bat' : 'flutter'),
|
||||
).existsSync() ||
|
||||
!File.fromUri(
|
||||
bin.uri.resolve(
|
||||
'cache/dart-sdk/bin/${Platform.isWindows ? 'dart.exe' : 'dart'}',
|
||||
),
|
||||
).existsSync()) {
|
||||
throw StateError(
|
||||
'Project Flutter SDK is missing. Run fvm use first; CI requires a complete Flutter SDK.',
|
||||
);
|
||||
}
|
||||
return bin;
|
||||
}
|
||||
|
||||
Map<String, String> projectEnvironment(
|
||||
Directory root,
|
||||
Map<String, String> inherited,
|
||||
Directory sdkBin,
|
||||
) {
|
||||
final environment = loadLocalEnvironment(
|
||||
root,
|
||||
inherited,
|
||||
windows: Platform.isWindows,
|
||||
);
|
||||
final pathKey = environment.keys
|
||||
.where(
|
||||
(key) =>
|
||||
Platform.isWindows ? key.toUpperCase() == 'PATH' : key == 'PATH',
|
||||
)
|
||||
.firstOrNull;
|
||||
final path = pathKey == null ? '' : environment.remove(pathKey)!;
|
||||
environment['PATH'] = '${sdkBin.path}${Platform.isWindows ? ';' : ':'}$path';
|
||||
return environment;
|
||||
}
|
||||
|
||||
Future<int> runProjectTool(
|
||||
String tool,
|
||||
List<String> arguments, {
|
||||
required Directory root,
|
||||
required Map<String, String> inherited,
|
||||
Directory? workingDirectory,
|
||||
bool allowCiSdk = false,
|
||||
}) async {
|
||||
final bin = projectSdkBin(root, allowCiSdk: allowCiSdk);
|
||||
final environment = projectEnvironment(root, inherited, bin);
|
||||
final String executable;
|
||||
switch (tool) {
|
||||
case 'flutter':
|
||||
executable = File.fromUri(
|
||||
bin.uri.resolve(Platform.isWindows ? 'flutter.bat' : 'flutter'),
|
||||
).path;
|
||||
case 'dart':
|
||||
executable = File.fromUri(
|
||||
bin.uri.resolve(
|
||||
'cache/dart-sdk/bin/${Platform.isWindows ? 'dart.exe' : 'dart'}',
|
||||
),
|
||||
).path;
|
||||
case 'exec':
|
||||
if (arguments.isEmpty) {
|
||||
throw const FormatException('exec requires a command.');
|
||||
}
|
||||
executable = arguments.first;
|
||||
arguments = arguments.sublist(1);
|
||||
default:
|
||||
throw const FormatException('Expected flutter, dart or exec.');
|
||||
}
|
||||
var command = executable;
|
||||
var commandArguments = arguments;
|
||||
if (Platform.isWindows &&
|
||||
(executable.toLowerCase().endsWith('.bat') ||
|
||||
executable.toLowerCase().endsWith('.cmd'))) {
|
||||
// cmd expands this variable once. Keeping the quoted command in the
|
||||
// environment avoids expanding literal %VAR% in Flutter arguments.
|
||||
environment['CUTDEX_LOCAL_FLUTTER_COMMAND'] = [
|
||||
executable,
|
||||
...arguments,
|
||||
].map(_quoteWindowsBatchArgument).join(' ');
|
||||
command = 'cmd.exe';
|
||||
commandArguments = ['/d', '/v:off', '/c', '%CUTDEX_LOCAL_FLUTTER_COMMAND%'];
|
||||
}
|
||||
final process = await Process.start(
|
||||
command,
|
||||
commandArguments,
|
||||
workingDirectory: (workingDirectory ?? root).path,
|
||||
environment: environment,
|
||||
includeParentEnvironment: false,
|
||||
mode: ProcessStartMode.inheritStdio,
|
||||
);
|
||||
return process.exitCode;
|
||||
}
|
||||
|
||||
String _quoteWindowsBatchArgument(String argument) {
|
||||
if (argument.contains('\n') || argument.contains('\r')) {
|
||||
throw const FormatException('Flutter arguments cannot contain newlines.');
|
||||
}
|
||||
// Always quote, including shell metacharacters and empty arguments. Inside
|
||||
// quotes Windows' C argument parser accepts doubled quotes as literal quotes.
|
||||
final escaped = argument
|
||||
.replaceAllMapped(RegExp(r'(\\*)"'), (match) => '${match[1]! * 2}""')
|
||||
.replaceAllMapped(RegExp(r'(\\+)$'), (match) => match[1]! * 2);
|
||||
return '"$escaped"';
|
||||
}
|
||||
Reference in new issue
Block a user