fix add to app workspace troubles

pull/2701/head
Eric Windmill 1 month ago
parent 716d87844b
commit 87b3799832

@ -5,9 +5,9 @@ environment:
sdk: ^3.9.0-0 sdk: ^3.9.0-0
workspace: workspace:
- add_to_app/android_view/flutter_module_using_plugin - add_to_app/android_view/flutter_module_using_plugin_android_view
- add_to_app/books/flutter_module_books - add_to_app/books/flutter_module_books
- add_to_app/fullscreen/flutter_module - add_to_app/fullscreen/flutter_module_fullscreen
- add_to_app/multiple_flutters/multiple_flutters_module - add_to_app/multiple_flutters/multiple_flutters_module
- add_to_app/plugin/flutter_module_using_plugin - add_to_app/plugin/flutter_module_using_plugin
- add_to_app/prebuilt_module/flutter_module - add_to_app/prebuilt_module/flutter_module

@ -9,6 +9,7 @@ Future<void> main() async {
final pubspecYaml = loadYaml(pubspecContent); final pubspecYaml = loadYaml(pubspecContent);
final workspace = pubspecYaml['workspace'] as YamlList?; final workspace = pubspecYaml['workspace'] as YamlList?;
await _runCommand('flutter', ['pub', 'get'], workingDirectory: rootDir.path);
if (workspace == null) { if (workspace == null) {
print('No workspace found in pubspec.yaml'); print('No workspace found in pubspec.yaml');
exit(1); exit(1);
@ -19,16 +20,20 @@ Future<void> main() async {
for (final package in packages) { for (final package in packages) {
final packagePath = path.join(rootDir.path, package); final packagePath = path.join(rootDir.path, package);
print('== Testing \'$package\' =='); print('== Testing \'$package\' ==');
await _runCommand('dart', [
await _runCommand('flutter', ['pub', 'get'], workingDirectory: packagePath); 'analyze',
await _runCommand('dart', ['analyze', '--fatal-infos', '--fatal-warnings'], '--fatal-infos',
workingDirectory: packagePath); '--fatal-warnings',
await _runCommand('dart', ['format', '--output', 'none', '.'], ], workingDirectory: packagePath);
workingDirectory: packagePath); await _runCommand('dart', [
'format',
'--output',
'none',
'.',
], workingDirectory: packagePath);
if (await Directory(path.join(packagePath, 'test')).exists()) { if (await Directory(path.join(packagePath, 'test')).exists()) {
final packagePubspecFile = final packagePubspecFile = File(path.join(packagePath, 'pubspec.yaml'));
File(path.join(packagePath, 'pubspec.yaml'));
final packagePubspecContent = await packagePubspecFile.readAsString(); final packagePubspecContent = await packagePubspecFile.readAsString();
if (packagePubspecContent.contains('flutter:')) { if (packagePubspecContent.contains('flutter:')) {
await _runCommand('flutter', ['test'], workingDirectory: packagePath); await _runCommand('flutter', ['test'], workingDirectory: packagePath);
@ -39,13 +44,22 @@ Future<void> main() async {
} }
} }
Future<void> _runCommand(String executable, List<String> arguments, Future<void> _runCommand(
{required String workingDirectory}) async { String executable,
final process = await Process.start(executable, arguments, List<String> arguments, {
workingDirectory: workingDirectory, mode: ProcessStartMode.inheritStdio); required String workingDirectory,
}) async {
final process = await Process.start(
executable,
arguments,
workingDirectory: workingDirectory,
mode: ProcessStartMode.inheritStdio,
);
final exitCode = await process.exitCode; final exitCode = await process.exitCode;
if (exitCode != 0) { if (exitCode != 0) {
print('Command "$executable ${arguments.join(' ')}" failed with exit code $exitCode in $workingDirectory'); print(
'Command "$executable ${arguments.join(' ')}" failed with exit code $exitCode in $workingDirectory',
);
exit(exitCode); exit(exitCode);
} }
} }

@ -19,19 +19,23 @@ class ReleaseScriptRunner {
Future<void> run(List<String> arguments) async { Future<void> run(List<String> arguments) async {
final parser = ArgParser() final parser = ArgParser()
..addFlag('dry-run', ..addFlag(
negatable: false, 'dry-run',
help: negatable: false,
'Prints the commands that would be executed, but does not run them.'); help:
'Prints the commands that would be executed, but does not run them.',
);
final argResults = parser.parse(arguments); final argResults = parser.parse(arguments);
_isDryRun = argResults['dry-run'] as bool; _isDryRun = argResults['dry-run'] as bool;
_setupLogging(); _setupLogging();
log( log(
styleBold styleBold.wrap(
.wrap('Flutter Monorepo Update Script Started: ${DateTime.now()}'), 'Flutter Monorepo Update Script Started: ${DateTime.now()}',
stdout); ),
stdout,
);
log(blue.wrap('Log file: ${_logFile.path}')!, stdout); log(blue.wrap('Log file: ${_logFile.path}')!, stdout);
if (_isDryRun) { if (_isDryRun) {
@ -52,15 +56,19 @@ class ReleaseScriptRunner {
final packages = await _getWorkspacePackages(); final packages = await _getWorkspacePackages();
if (packages.isEmpty) { if (packages.isEmpty) {
log(yellow.wrap('No packages found in the root pubspec.yaml workspace.'), log(
stdout); yellow.wrap('No packages found in the root pubspec.yaml workspace.'),
stdout,
);
exit(0); exit(0);
} }
log( log(
blue.wrap( blue.wrap(
'Found ${packages.length} Flutter project(s): ${packages.join(', ')}'), 'Found ${packages.length} Flutter project(s): ${packages.join(', ')}',
stdout); ),
stdout,
);
final failedProjects = <String>[]; final failedProjects = <String>[];
for (final packagePath in packages) { for (final packagePath in packages) {
@ -73,9 +81,11 @@ class ReleaseScriptRunner {
_printSummary(packages.length, failedProjects); _printSummary(packages.length, failedProjects);
log( log(
styleBold.wrap( styleBold.wrap(
'\nFlutter Monorepo Update Script Completed: ${DateTime.now()}'), '\nFlutter Monorepo Update Script Completed: ${DateTime.now()}',
stdout); ),
stdout,
);
if (failedProjects.isNotEmpty) { if (failedProjects.isNotEmpty) {
exit(1); exit(1);
@ -167,22 +177,28 @@ class ReleaseScriptRunner {
log(blue.wrap('Updating SDK constraints to use Dart $dartVersion'), stdout); log(blue.wrap('Updating SDK constraints to use Dart $dartVersion'), stdout);
if (!_isDryRun) { if (!_isDryRun) {
if (!await _updateSdkConstraints(projectPath, dartVersion)) { if (!await _updateSdkConstraints(projectPath, dartVersion)) {
log(red.wrap('Failed to update SDK constraints for $projectName'), log(
stderr); red.wrap('Failed to update SDK constraints for $projectName'),
stderr,
);
return false; return false;
} }
} }
final commands = [ final commands = [
Command('dart analyze', 'Running dart analyze...', 'dart', Command('dart analyze', 'Running dart analyze...', 'dart', [
['analyze', '--fatal-infos', '--fatal-warnings']), 'analyze',
'--fatal-infos',
'--fatal-warnings',
]),
Command('dart format', 'Running dart format...', 'dart', ['format', '.']), Command('dart format', 'Running dart format...', 'dart', ['format', '.']),
]; ];
final testDir = Directory(p.join(projectPath, 'test')); final testDir = Directory(p.join(projectPath, 'test'));
if (projectName != 'material_3_demo' && testDir.existsSync()) { if (projectName != 'material_3_demo' && testDir.existsSync()) {
commands.add( commands.add(
Command('flutter test', 'Running tests...', 'flutter', ['test'])); Command('flutter test', 'Running tests...', 'flutter', ['test']),
);
} }
for (final command in commands) { for (final command in commands) {
@ -229,7 +245,9 @@ class ReleaseScriptRunner {
} }
Future<bool> _updateSdkConstraints( Future<bool> _updateSdkConstraints(
String projectDir, String versionString) async { String projectDir,
String versionString,
) async {
final pubspecFile = File(p.join(projectDir, 'pubspec.yaml')); final pubspecFile = File(p.join(projectDir, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) { if (!pubspecFile.existsSync()) {
log(red.wrap('pubspec.yaml not found in $projectDir'), stderr); log(red.wrap('pubspec.yaml not found in $projectDir'), stderr);
@ -245,44 +263,61 @@ class ReleaseScriptRunner {
await pubspecFile.writeAsString(editor.toString()); await pubspecFile.writeAsString(editor.toString());
log( log(
blue.wrap( blue.wrap(
'Updated Dart SDK constraint in $projectDir to: $newConstraint'), 'Updated Dart SDK constraint in $projectDir to: $newConstraint',
stdout); ),
stdout,
);
return true; return true;
} catch (e) { } catch (e) {
log(red.wrap('Failed to update SDK constraint in $projectDir: $e'), log(
stderr); red.wrap('Failed to update SDK constraint in $projectDir: $e'),
stderr,
);
return false; return false;
} }
} }
Future<(bool, String)> _runCommand(String executable, List<String> arguments, Future<(bool, String)> _runCommand(
{String? workingDirectory}) async { String executable,
List<String> arguments, {
String? workingDirectory,
}) async {
final commandString = '$executable ${arguments.join(' ')}'; final commandString = '$executable ${arguments.join(' ')}';
if (_isDryRun) { if (_isDryRun) {
log( log(
yellow.wrap( yellow.wrap(
' [DRY RUN] Would execute: `$commandString` in `${workingDirectory ?? '.'}`'), ' [DRY RUN] Would execute: `$commandString` in `${workingDirectory ?? '.'}`',
stdout); ),
stdout,
);
return (true, ''); return (true, '');
} }
final process = await Process.start(executable, arguments, final process = await Process.start(
workingDirectory: workingDirectory, runInShell: true); executable,
arguments,
workingDirectory: workingDirectory,
runInShell: true,
);
StringBuffer output = StringBuffer(''); StringBuffer output = StringBuffer('');
final stdoutFuture = final stdoutFuture = process.stdout
process.stdout.transform(SystemEncoding().decoder).forEach((line) { .transform(SystemEncoding().decoder)
log(line, stdout); .forEach((line) {
if (!_isOnlyWhitespace(line)) output.writeln('${line.trim().padLeft(2)}'); log(line, stdout);
}); if (!_isOnlyWhitespace(line))
output.writeln('${line.trim().padLeft(2)}');
final stderrFuture = });
process.stderr.transform(SystemEncoding().decoder).forEach((line) {
log(red.wrap(line), stderr); final stderrFuture = process.stderr
if (!_isOnlyWhitespace(line)) output.writeln('${line.trim().padLeft(2)}'); .transform(SystemEncoding().decoder)
}); .forEach((line) {
log(red.wrap(line), stderr);
if (!_isOnlyWhitespace(line))
output.writeln('${line.trim().padLeft(2)}');
});
await Future.wait([stdoutFuture, stderrFuture]); await Future.wait([stdoutFuture, stderrFuture]);

Loading…
Cancel
Save