Skip to content

Commit

Permalink
work on #103, add runSync and runExecutableArgumentsSync both to glob…
Browse files Browse the repository at this point in the history
…al space and Shell class
  • Loading branch information
alextekartik committed Jan 15, 2024
1 parent f9ee913 commit 8a61474
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 31 deletions.
17 changes: 17 additions & 0 deletions packages/process_run/example/demo_sync.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:io';

import 'package:process_run/process_run.dart';

void main() {
// Run the command
runExecutableArgumentsSync('echo', ['hello world']);

// Stream the out to stdout
runExecutableArgumentsSync('echo', ['hello world']);

// Calling dart
runExecutableArgumentsSync('dart', ['--version'], verbose: true);

// stream the output to stderr
runExecutableArgumentsSync('dart', ['--version'], stderr: stderr);
}
2 changes: 0 additions & 2 deletions packages/process_run/lib/process_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ export 'package:process_run/src/shell_utils_common.dart'
show argumentsToString, argumentToString;

export 'shell.dart';
export 'src/process_run.dart'
show runExecutableArguments, executableArgumentsToString;
export 'which.dart' show which, whichSync;
7 changes: 5 additions & 2 deletions packages/process_run/lib/shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ export 'src/process_cmd.dart'
/// Deprecated
ProcessCmd;
export 'src/process_run.dart'
show runExecutableArguments, executableArgumentsToString;
show
runExecutableArguments,
executableArgumentsToString,
runExecutableArgumentsSync;
export 'src/prompt.dart' show promptConfirm, promptTerminate, prompt;
export 'src/shell.dart' show run, Shell, ShellException;
export 'src/shell.dart' show run, runSync, Shell, ShellException;
export 'src/shell_environment.dart'
show
ShellEnvironment,
Expand Down
100 changes: 100 additions & 0 deletions packages/process_run/lib/src/process_run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,106 @@ Future<ProcessResult> runExecutableArguments(
return result;
}

///
/// if [commandVerbose] or [verbose] is true, display the command.
/// if [verbose] is true, stream stdout & stdin
///
/// Compared to the async version, it is not possible to kill the spawn process nor to
/// feed any input.
ProcessResult runExecutableArgumentsSync(
String executable, List<String> arguments,
{String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool? runInShell,
Encoding? stdoutEncoding = systemEncoding,
Encoding? stderrEncoding = systemEncoding,
StreamSink<List<int>>? stdout,
StreamSink<List<int>>? stderr,
bool? verbose,
bool? commandVerbose}) {
if (verbose == true) {
commandVerbose = true;
stdout ??= io.stdout;
stderr ??= io.stderr;
}

if (commandVerbose == true) {
utils.streamSinkWriteln(stdout ?? io.stdout,
'\$ ${executableArgumentsToString(executable, arguments)}',
encoding: stdoutEncoding);
}

// Build our environment
var shellEnvironment = ShellEnvironment.full(
environment: environment,
includeParentEnvironment: includeParentEnvironment);

// Default is the full command
var executableShortName = executable;

// Find executable if needed, i.e. if it is only a name
if (basename(executable) == executable) {
// Try to find it in path or use it as is
executable = utils.findExecutableSync(executable, shellEnvironment.paths) ??
executable;
} else {
// resolve locally
executable = utils.findExecutableSync(basename(executable), [
join(workingDirectory ?? Directory.current.path, dirname(executable))
]) ??
executable;
}

// Fix runInShell on windows (force run in shell for non-.exe)
runInShell = utils.fixRunInShell(runInShell, executable);

io.ProcessResult result;
try {
result = Process.runSync(
executable,
arguments,
environment: shellEnvironment,
includeParentEnvironment: false,
runInShell: runInShell,
workingDirectory: workingDirectory,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding,
);
} catch (e) {
if (verbose == true) {
io.stderr.writeln(e);
io.stderr.writeln(
'\$ ${executableArgumentsToString(executableShortName, arguments)}');
io.stderr.writeln(
'workingDirectory: ${workingDirectory ?? Directory.current.path}');
}
rethrow;
}

List<int> outputToIntList(dynamic data, Encoding? encoding) {
if (data is List<int>) {
return data;
} else if (data is String && encoding != null) {
return encoding.encode(data);
} else {
throw 'Unexpected data type: ${data.runtimeType}';
}
}

if (stdout != null) {
var out = outputToIntList(result.stdout, stdoutEncoding);
stdout.add(out);
}

if (stderr != null) {
var err = outputToIntList(result.stderr, stderrEncoding);
stderr.add(err);
}

return result;
}

/// Command runner. not exported
/// Execute a predefined ProcessCmd command
Expand Down
Loading

0 comments on commit 8a61474

Please sign in to comment.