-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This enables things like mono_repo dart fix --apply, mono_repo dart analyze, mono_repo dart run build_runner build etc. I did bump the min SDK so I could use a switch expression, let me know if you feel strongly about supporting older SDKs. They can still use older versions so I think it's fine though. Closes #458
- Loading branch information
Showing
15 changed files
with
246 additions
and
163 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:args/args.dart'; | ||
import 'package:io/ansi.dart'; | ||
import 'package:path/path.dart' as p; | ||
|
||
import '../package_config.dart'; | ||
import '../root_config.dart'; | ||
import 'mono_repo_command.dart'; | ||
|
||
class DartCommand extends MonoRepoCommand { | ||
@override | ||
final ArgParser argParser = ArgParser.allowAnything(); | ||
|
||
@override | ||
String get name => 'dart'; | ||
|
||
@override | ||
String get description => | ||
'Runs the `dart` command with the provided arguments across all ' | ||
'packages.'; | ||
|
||
@override | ||
Future<void> run() => dart( | ||
rootConfig(), | ||
arguments, | ||
executableForPackage, | ||
); | ||
|
||
/// The arguments to pass to the executable. | ||
List<String> get arguments => argResults?.rest ?? const []; | ||
|
||
/// The executable to use for a given package. | ||
Executable executableForPackage(PackageConfig config) => Executable.dart; | ||
} | ||
|
||
Future<void> dart( | ||
RootConfig rootConfig, | ||
List<String> args, | ||
Executable Function(PackageConfig) executableForPackage, | ||
) async { | ||
final pkgDirs = rootConfig.map((pc) => pc.relativePath).toList(); | ||
|
||
print( | ||
lightBlue.wrap( | ||
'Running `dart ${args.join(' ')}` across ${pkgDirs.length} ' | ||
'package(s).', | ||
), | ||
); | ||
|
||
var successCount = 0; | ||
final failSet = <String>{}; | ||
|
||
for (var config in rootConfig) { | ||
final dir = config.relativePath; | ||
|
||
print(''); | ||
print( | ||
wrapWith( | ||
'`$dir`: Starting `${args.join(' ')}`', | ||
[styleBold, lightBlue], | ||
), | ||
); | ||
final workingDir = p.join(rootConfig.rootDirectory, dir); | ||
|
||
final proc = await Process.start( | ||
executableForPackage(config).path, | ||
args, | ||
mode: ProcessStartMode.inheritStdio, | ||
workingDirectory: workingDir, | ||
); | ||
|
||
final exit = await proc.exitCode; | ||
|
||
if (exit == 0) { | ||
successCount++; | ||
print(wrapWith('`$dir`: success!', [styleBold, green])); | ||
} else { | ||
failSet.add(dir); | ||
print(wrapWith('`$dir`: failed!', [styleBold, red])); | ||
} | ||
|
||
if (rootConfig.length > 1) { | ||
print(''); | ||
print('Successes: $successCount'); | ||
if (failSet.isNotEmpty) { | ||
print( | ||
'Failures: ${failSet.length}\n' | ||
'${failSet.map((e) => ' $e').join('\n')}', | ||
); | ||
} | ||
final remaining = rootConfig.length - (successCount + failSet.length); | ||
if (remaining > 0) { | ||
print('Remaining: $remaining'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// The path to the root directory of the SDK. | ||
final String _sdkDir = (() { | ||
// The Dart executable is in "/path/to/sdk/bin/dart", so two levels up is | ||
// "/path/to/sdk". | ||
final aboveExecutable = p.dirname(p.dirname(Platform.resolvedExecutable)); | ||
assert(FileSystemEntity.isFileSync(p.join(aboveExecutable, 'version'))); | ||
return aboveExecutable; | ||
})(); | ||
|
||
final String _dartPath = p.join(_sdkDir, 'bin', 'dart'); | ||
|
||
/// The "flutter[.bat]" command. | ||
final String _flutterPath = Platform.isWindows ? 'flutter.bat' : 'flutter'; | ||
|
||
enum Executable { | ||
dart, | ||
flutter, | ||
} | ||
|
||
extension ExecutablePath on Executable { | ||
String get path => switch (this) { | ||
Executable.dart => _dartPath, | ||
Executable.flutter => _flutterPath, | ||
}; | ||
} |
Oops, something went wrong.