feat: improve stdio pass through

This commit is contained in:
2023-10-07 03:27:26 +03:00
parent d9b7a04815
commit 43a2cda24a
6 changed files with 51 additions and 24 deletions

View File

@@ -1,3 +1,7 @@
## 0.3.2
- Improve I/O pass-through to commands
## 0.3.1
- Add `appendNewLine` option (default: `false`)

View File

@@ -10,3 +10,12 @@ script_runner:
cmd: flutter build ipa
- name: build:all
cmd: build:apk && build:ipa
- name: push:apk
suppress_header_output: true
cmd: |-
name=$(dart run btool get packageName)
version=$(dart run btool get packageVersion)
source="$(pwd)/build/app/outputs/flutter-apk/app-release.apk"
target="/sdcard/Download/$name-$version.apk"
echo "adb push $source $target"
adb push $source $target

View File

@@ -10,3 +10,12 @@ scripts:
cmd: flutter build ipa
- name: build:all
cmd: build:apk && build:ipa
- name: push:apk
suppress_header_output: true
cmd: |-
name=$(dart run btool get packageName)
version=$(dart run btool get packageVersion)
source="$(pwd)/build/app/outputs/flutter-apk/app-release.apk"
target="/sdcard/Download/$name-$version.apk"
echo "adb push $source $target"
adb push $source $target

View File

@@ -2,15 +2,14 @@ library script_runner;
import 'dart:io';
import 'dart:math' as math;
import 'package:script_runner/src/runnable_script.dart';
import 'package:script_runner/src/utils.dart';
import 'package:yaml/yaml.dart';
// ignore: no_leading_underscores_for_library_prefixes
import 'utils.dart' as _utils;
import 'package:yaml/yaml.dart' as yaml;
import 'package:path/path.dart' as path;
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart' as yaml;
import 'runnable_script.dart';
import 'utils.dart' as utils;
/// The configuration for a script runner. See each field's documentation for more information.
class ScriptRunnerConfig {
@@ -163,7 +162,7 @@ class ScriptRunnerConfig {
);
print('');
for (final scr in scripts) {
final lines = _utils.chunks(
final lines = utils.chunks(
scr.description ?? '\$ ${[scr.cmd, ...scr.args].join(' ')}',
80 - padLen,
);
@@ -179,7 +178,7 @@ class ScriptRunnerConfig {
FileSystem fs, String startDir) async {
var dir = fs.directory(startDir);
String sourceFile;
YamlMap? source;
yaml.YamlMap? source;
bool rootSearched = false;
while (!rootSearched) {
if (dir.parent.path == dir.path) {
@@ -292,7 +291,7 @@ class ScriptRunnerShellConfig {
case OS.linux:
case OS.macos:
try {
final envShell = firstNonNull([
final envShell = utils.firstNonNull([
Platform.environment['SHELL'],
Platform.environment['TERM'],
]);
@@ -310,3 +309,4 @@ enum OS {
linux,
// other
}

View File

@@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io' as io;
import 'package:file/file.dart';
@@ -146,29 +145,30 @@ class RunnableScript {
}
if (exitCode != 0) {
final e = io.ProcessException(
cmd, args, 'Process exited with error code: $exitCode', exitCode);
cmd,
args,
'Process exited with error code: $exitCode',
exitCode,
);
throw e;
}
} catch (e) {
rethrow;
} finally {
await _fileSystem.file(scrPath).delete();
}
}
Future<int> _runShellScriptFile(ScriptRunnerConfig config, scrPath) async {
Future<int> _runShellScriptFile(
ScriptRunnerConfig config,
String scrPath,
) async {
final result = await io.Process.start(
config.shell.shell,
[config.shell.shellExecFlag, scrPath],
environment: {...?config.env, ...?env},
workingDirectory: workingDir ?? config.workingDir,
mode: io.ProcessStartMode.inheritStdio,
includeParentEnvironment: true,
);
result.stdout.listen((event) {
io.stdout.write(Utf8Decoder().convert(event));
});
result.stderr.listen((event) {
io.stdout.write(Utf8Decoder().convert(event));
});
final exitCode = await result.exitCode;
return exitCode;
}
@@ -176,8 +176,10 @@ class RunnableScript {
String _getScriptPath() => _fileSystem.path
.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
String _getScriptContents(ScriptRunnerConfig config,
{List<String> extraArgs = const []}) {
String _getScriptContents(
ScriptRunnerConfig config, {
List<String> extraArgs = const [],
}) {
final script = "$cmd ${(args + extraArgs).map(_utils.wrap).join(' ')}";
switch (config.shell.os) {
case OS.windows:
@@ -189,9 +191,11 @@ class RunnableScript {
case OS.linux:
case OS.macos:
return [
...preloadScripts.map((e) => "alias ${e.name}='scr ${e.name}'"),
...preloadScripts.map((e) =>
"[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"),
script
].join('\n');
}
}
}

View File

@@ -31,6 +31,7 @@ script_runner:
- name: 'version:set'
cmd: dart run btool set packageVersion
suppress_header_output: true
- format: dart format .
# Examples
- name: echo1