Skip to content

Commit

Permalink
Breaking: Make dart/flutter async (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Jul 16, 2024
1 parent c6e8f97 commit 0b123eb
Show file tree
Hide file tree
Showing 29 changed files with 179 additions and 108 deletions.
3 changes: 2 additions & 1 deletion sidekick/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ analyzer:

linter:
rules:
avoid_print: false
avoid_print: false
unawaited_futures: true
3 changes: 1 addition & 2 deletions sidekick/test/plugins_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:dcli/dcli.dart';
import 'package:dcli_core/dcli_core.dart';
import 'package:recase/recase.dart';
import 'package:sidekick_core/sidekick_core.dart';
import 'package:sidekick_core/src/commands/plugins/create_plugin_command.dart';
Expand Down Expand Up @@ -55,7 +54,7 @@ void main() {

'git init'.start(workingDirectory: pluginDir.path);
'git add .'.start(workingDirectory: pluginDir.path);
withEnvironment(
await withEnvironment(
() async =>
'git commit -m "initial"'.start(workingDirectory: pluginDir.path),
// without this, `git commit` crashes on CI
Expand Down
2 changes: 1 addition & 1 deletion sidekick/test/recompile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {
printOnFailure(stdout);
final stderr = await updateProcess.stderrStream().join('\n');
printOnFailure(stderr);
updateProcess.shouldExit(0);
await updateProcess.shouldExit(0);
expect(stderr, contains('Installing dashi command line application'));
expect(stderr, contains('Getting dependencies'));
expect(stderr, contains('Bundling assets'));
Expand Down
1 change: 1 addition & 0 deletions sidekick_core/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ include: package:lint/analysis_options_package.yaml
linter:
rules:
avoid_print: false
unawaited_futures: true
# public_member_api_docs: true
4 changes: 3 additions & 1 deletion sidekick_core/lib/sidekick_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export 'package:sidekick_core/src/directory_ext.dart';
export 'package:sidekick_core/src/file_util.dart';
export 'package:sidekick_core/src/flutter.dart';
export 'package:sidekick_core/src/forward_command.dart';
export 'package:sidekick_core/src/process.dart' show ProcessCompletion;
export 'package:sidekick_core/src/repository.dart';
export 'package:sidekick_core/src/sdk_initializer.dart';
export 'package:sidekick_core/src/sidekick_context.dart' show SidekickContext;
export 'package:sidekick_core/src/sidekick_package.dart';
export 'package:sidekick_core/src/template/sidekick_package.template.dart';
Expand All @@ -45,7 +47,7 @@ export 'package:sidekick_core/src/template/sidekick_package.template.dart';
/// This is used by the update command to determine if your sidekick cli
/// requires an update
// DO NOT MANUALLY EDIT THIS VERSION, instead run `sk bump-version sidekick_core`
final Version version = Version.parse('3.0.0-preview.0');
final Version version = Version.parse('2.1.2');

/// Initializes sidekick, call this at the very start of your CLI program
///
Expand Down
4 changes: 3 additions & 1 deletion sidekick_core/lib/src/commands/analyze_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class DartAnalyzeCommand extends ForwardCommand {
@override
Future<void> run() async {
// running in root of project, includes all packages
exitCode = dart(
// TODO run for each package separately, with different SDK versions
final completion = await dart(
['analyze', ...argResults!.arguments],
workingDirectory: SidekickContext.projectRoot,
nothrow: true,
);
exitCode = completion.exitCode ?? 1;
}
}
3 changes: 2 additions & 1 deletion sidekick_core/lib/src/commands/dart_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DartCommand extends ForwardCommand {

@override
Future<void> run() async {
exitCode = dart(argResults!.arguments, nothrow: true);
final completion = await dart(argResults!.arguments, nothrow: true);
exitCode = completion.exitCode ?? 1;
}
}
10 changes: 6 additions & 4 deletions sidekick_core/lib/src/commands/deps_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DepsCommand extends Command {
}
_warnIfNotInProject();
// only get deps for selected package
_getDependencies(package);
await _getDependencies(package);
return;
}

Expand All @@ -89,7 +89,7 @@ class DepsCommand extends Command {

for (final package in allPackages.whereNot(excluded.contains)) {
try {
_getDependencies(package);
await _getDependencies(package);
} catch (e, stack) {
print('Error while getting dependencies for ${package.name} '
'(${package.root.path})');
Expand All @@ -101,14 +101,16 @@ class DepsCommand extends Command {
printerr("\n\nErrors while getting dependencies:");
printerr(errorText);
exitCode = 1;
} else {
exitCode = 0;
}
}

void _getDependencies(DartPackage package) {
Future<void> _getDependencies(DartPackage package) async {
print(yellow('=== package ${package.name} ==='));
final packageDir = package.root;
final dartOrFlutter = package.isFlutterPackage ? flutter : dart;
dartOrFlutter(
await dartOrFlutter(
['pub', 'get'],
workingDirectory: packageDir,
throwOnError: () =>
Expand Down
3 changes: 2 additions & 1 deletion sidekick_core/lib/src/commands/flutter_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class FlutterCommand extends ForwardCommand {
@override
Future<void> run() async {
final args = argResults!.arguments;
exitCode = flutter(args, nothrow: true);
final completion = await flutter(args, nothrow: true);
exitCode = completion.exitCode ?? 1;
}
}
13 changes: 7 additions & 6 deletions sidekick_core/lib/src/commands/format_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class FormatCommand extends Command {
}
final int lineLength = getLineLength(package) ?? defaultLineLength;
final allDartFiles = package.root.findFilesToFormat(globExcludes);
_format(
await _format(
name: "package:${package.name}",
lineLength: lineLength,
files: allDartFiles,
Expand All @@ -125,15 +125,15 @@ class FormatCommand extends Command {
for (final file in filesInPackage) {
allFiles.remove(file);
}
_format(
await _format(
name: "package:${package.name}",
lineLength: resolvedLineLength,
files: filesInPackage,
verify: verify,
);
}
if (allFiles.isNotEmpty) {
_format(
await _format(
name: "Other",
lineLength: defaultLineLength,
files: allFiles,
Expand All @@ -154,12 +154,12 @@ class FormatCommand extends Command {
}
}

void _format({
Future<void> _format({
required String name,
required int lineLength,
required Iterable<File> files,
bool verify = false,
}) {
}) async {
if (verify) {
print("Verifying $name");
} else {
Expand All @@ -171,7 +171,7 @@ class FormatCommand extends Command {
}
final progress =
verify ? Progress.capture() : Progress.print(capture: true);
final exitCode = dart(
final completion = await dart(
[
'format',
'-l',
Expand All @@ -186,6 +186,7 @@ class FormatCommand extends Command {
// should only be printed when the change is actually written to the files (when verify is false)
progress: progress,
);
exitCode = completion.exitCode ?? 1;
if (exitCode != 0) {
foundFormatError = true;
unformattedFiles.addAll(
Expand Down
28 changes: 14 additions & 14 deletions sidekick_core/lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ import 'package:sidekick_core/sidekick_core.dart';
///
/// If [throwOnError] is given and the command returns a non-zero exit code,
/// the result of [throwOnError] will be thrown regardless of [nothrow]
int dart(
Future<ProcessCompletion> dart(
List<String> args, {
Directory? workingDirectory,
dcli.Progress? progress,
bool nothrow = false,
String Function()? throwOnError,
}) {
}) async {
Directory? sdk = dartSdk;
if (sdk == null) {
if (flutterSdk != null) {
final embeddedSdk = flutterSdk!.directory('bin/cache/dart-sdk');
if (!embeddedSdk.existsSync()) {
// Flutter SDK is not fully initialized, the Dart SDK not yet downloaded
// Execute flutter_tool to download the embedded dart runtime
flutter([], workingDirectory: workingDirectory, nothrow: true);
}
if (embeddedSdk.existsSync()) {
sdk = embeddedSdk;
}
if (sdk == null && flutterSdk != null) {
final embeddedSdk = flutterSdk!.directory('bin/cache/dart-sdk');
if (!embeddedSdk.existsSync()) {
// Flutter SDK is not fully initialized, the Dart SDK not yet downloaded
// Execute flutter_tool to download the embedded dart runtime
await flutter([], workingDirectory: workingDirectory, nothrow: true);
}
if (embeddedSdk.existsSync()) {
sdk = embeddedSdk;
}
}
if (sdk == null) {
throw DartSdkNotSetException();
}

await initializeSdkForPackage(workingDirectory);

final dart =
Platform.isWindows ? sdk.file('bin/dart.exe') : sdk.file('bin/dart');

Expand All @@ -55,7 +55,7 @@ int dart(
throw throwOnError();
}

return exitCode;
return ProcessCompletion(exitCode: exitCode);
}

/// The Dart SDK path is not set in [initializeSidekick] (param [dartSdk], neither is is the [flutterSdk])
Expand Down
53 changes: 6 additions & 47 deletions sidekick_core/lib/src/flutter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:dcli/dcli.dart' as dcli;
import 'package:sidekick_core/sidekick_core.dart';

Expand All @@ -9,26 +11,19 @@ import 'package:sidekick_core/sidekick_core.dart';
///
/// If [throwOnError] is given and the command returns a non-zero exit code,
/// the result of [throwOnError] will be thrown regardless of [nothrow]
int flutter(
Future<ProcessCompletion> flutter(
List<String> args, {
Directory? workingDirectory,
dcli.Progress? progress,
bool nothrow = false,
String Function()? throwOnError,
}) {
}) async {
final sdk = flutterSdk;
if (sdk == null) {
throw FlutterSdkNotSetException();
}

for (final initializer in _sdkInitializers) {
initializer(
FlutterInitializerContext(
sdk: sdk,
packagePath: workingDirectory,
),
);
}
await initializeSdkForPackage(workingDirectory);

int exitCode = -1;
try {
Expand All @@ -54,7 +49,7 @@ int flutter(
throw throwOnError();
}

return exitCode;
return ProcessCompletion(exitCode: exitCode);
}

/// The Flutter SDK path is not set in [initializeSidekick] (param [flutterSdk])
Expand Down Expand Up @@ -91,39 +86,3 @@ Directory? systemFlutterSdk() {

/// Returns the path to Flutter SDK of the `flutter` executable on `PATH`
String? systemFlutterSdkPath() => systemFlutterSdk()?.path;

/// Registers an initializer function that is called before executing the flutter command
/// to prepare the SDK, such as downloading it.
///
/// Also this function will be called multiple times, once for each usage of the [flutter] method
Removable addFlutterSdkInitializer(FlutterInitializer initializer) {
if (!_sdkInitializers.contains(initializer)) {
_sdkInitializers.add(initializer);
}
return () => _sdkInitializers.remove(initializer);
}

/// Can be called to remove a listener
typedef Removable = void Function();

/// Called by [flutter] before executing the flutter executable
typedef FlutterInitializer = Function(FlutterInitializerContext context);

/// Initializers that have to be executed before executing the flutter command
List<FlutterInitializer> _sdkInitializers = [];

/// Called by [flutter] before executing the flutter executable
class FlutterInitializerContext {
FlutterInitializerContext({
this.sdk,
this.packagePath,
});

/// The Flutter SDK directory, this directory is set by flutterSdkPath in [initializeSidekick]
/// Make sure the SDK will be initialized in this directory
/// You may want to use a symlink to the actual SDK directory
final Directory? sdk;

/// The package directory where the [flutter] and [dart] command is executed
final Directory? packagePath;
}
17 changes: 17 additions & 0 deletions sidekick_core/lib/src/process.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// All information about a process that has completed.
abstract interface class ProcessCompletion {
int? get exitCode;

factory ProcessCompletion({
required int exitCode,
}) = _ProcessResult;
}

class _ProcessResult implements ProcessCompletion {
@override
final int? exitCode;

_ProcessResult({
required this.exitCode,
});
}
Loading

0 comments on commit 0b123eb

Please sign in to comment.