Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BashCommand #168

Merged
merged 8 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sidekick_core/lib/sidekick_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export 'package:dcli/dcli.dart' hide run, start, startFromArgs, absolute;
export 'package:pub_semver/pub_semver.dart' show Version;
export 'package:sidekick_core/src/cli_util.dart';
export 'package:sidekick_core/src/commands/analyze_command.dart';
export 'package:sidekick_core/src/commands/bash_command.dart';
export 'package:sidekick_core/src/commands/dart_command.dart';
export 'package:sidekick_core/src/commands/deps_command.dart';
export 'package:sidekick_core/src/commands/flutter_command.dart';
Expand Down
14 changes: 13 additions & 1 deletion sidekick_core/lib/src/cli_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,33 @@ io.File tempExecutableScriptFile(String content, {Directory? tempDir}) {
}

/// Executes a script by first writing it as file and then running it as shell script
///
/// Use [args] to pass arguments to the script
///
/// Use [workingDirectory] to set the working directory of the script, default
/// to current working directory
///
/// When [terminal] is `true` (default: `false`) Stdio handles are inherited by
/// the child process. This allows stdin to read by the script
dcli.Progress writeAndRunShellScript(
String scriptContent, {
List<String> args = const [],
Directory? workingDirectory,
dcli.Progress? progress,
bool terminal = false,
}) {
final script = tempExecutableScriptFile(scriptContent);
final Progress scriptProgress =
progress ?? Progress(print, stderr: printerr, captureStderr: true);

try {
return dcli.start(
return dcli.startFromArgs(
script.absolute.path,
args,
workingDirectory:
workingDirectory?.absolute.path ?? entryWorkingDirectory.path,
progress: scriptProgress,
terminal: terminal,
);
} catch (e) {
print(
Expand Down
76 changes: 76 additions & 0 deletions sidekick_core/lib/src/commands/bash_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'dart:async';

import 'package:sidekick_core/sidekick_core.dart';

/// A Command that wraps a bash script
///
/// Easy way to convert an existing bash script into a command for sidekick
///
/// This makes it easy to handle paths within the bash script. You can define a
/// static [workingDirectory] and always know where the script is executed.
///
/// Usage as plain text
/// ```dart
/// runner
/// ..addCommand(
/// BashCommand(
/// name: 'test-bash-command',
/// description: 'Prints inputs of a sidekick BashCommand',
/// workingDirectory: runner.repository.root,
/// script: () => '''
/// echo "arguments: \$@"
/// echo "workingDirectory: \$(pwd)"
/// # Access paths from sidekick
/// ${systemFlutterSdkPath()}/bin/flutter --version
/// ''',
/// ),
/// );
/// ```
///
/// Or load your script from a file
/// ```dart
/// runner
/// ..addCommand(
/// BashCommand(
/// name: 'test-bash-command',
/// description: 'Prints inputs of a sidekick BashCommand',
/// workingDirectory: runner.repository.root,
/// script: () => runner.repository.root
/// .file('scripts/test-bash-command.sh')
/// .readAsString(),
/// ),
/// )
/// ```
class BashCommand extends ForwardCommand {
BashCommand({
passsy marked this conversation as resolved.
Show resolved Hide resolved
required this.script,
required this.description,
required this.name,
this.workingDirectory,
});

@override
final String name;

@override
final String description;

/// The script to be executed.
///
/// You may load this script from a file or generate it on the fly.
final FutureOr<String> Function() script;

/// The directory the bash script is running in.
final Directory? workingDirectory;

@override
Future<void> run() async {
final bashScript = await script();
writeAndRunShellScript(
bashScript,
args: argResults!.arguments,
workingDirectory: workingDirectory,
terminal: true,
);
}
}
2 changes: 1 addition & 1 deletion sidekick_core/lib/src/forward_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:args/command_runner.dart';

/// A [Command] which accepts all arguments and forwards everything to another cli app
///
/// Arguments to format are available via `argResults!.arguments`
/// Arguments are available via `argResults!.arguments`, not `argResults!.rest`
abstract class ForwardCommand extends Command {
ForwardCommand() {
// recreate the _argParser and change it to allowAnything
Expand Down
Empty file.