Skip to content

Commit 43a2cda

Browse files
committed
feat: improve stdio pass through
1 parent d9b7a04 commit 43a2cda

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.2
2+
3+
- Improve I/O pass-through to commands
4+
15
## 0.3.1
26

37
- Add `appendNewLine` option (default: `false`)

example/pubspec.example.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ script_runner:
1010
cmd: flutter build ipa
1111
- name: build:all
1212
cmd: build:apk && build:ipa
13+
- name: push:apk
14+
suppress_header_output: true
15+
cmd: |-
16+
name=$(dart run btool get packageName)
17+
version=$(dart run btool get packageVersion)
18+
source="$(pwd)/build/app/outputs/flutter-apk/app-release.apk"
19+
target="/sdcard/Download/$name-$version.apk"
20+
echo "adb push $source $target"
21+
adb push $source $target

example/script_runner.example.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ scripts:
1010
cmd: flutter build ipa
1111
- name: build:all
1212
cmd: build:apk && build:ipa
13+
- name: push:apk
14+
suppress_header_output: true
15+
cmd: |-
16+
name=$(dart run btool get packageName)
17+
version=$(dart run btool get packageVersion)
18+
source="$(pwd)/build/app/outputs/flutter-apk/app-release.apk"
19+
target="/sdcard/Download/$name-$version.apk"
20+
echo "adb push $source $target"
21+
adb push $source $target

lib/src/config.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ library script_runner;
22

33
import 'dart:io';
44
import 'dart:math' as math;
5-
import 'package:script_runner/src/runnable_script.dart';
6-
import 'package:script_runner/src/utils.dart';
7-
import 'package:yaml/yaml.dart';
8-
// ignore: no_leading_underscores_for_library_prefixes
9-
import 'utils.dart' as _utils;
10-
import 'package:yaml/yaml.dart' as yaml;
11-
import 'package:path/path.dart' as path;
5+
126
import 'package:file/file.dart';
137
import 'package:file/local.dart';
8+
import 'package:path/path.dart' as path;
9+
import 'package:yaml/yaml.dart' as yaml;
10+
11+
import 'runnable_script.dart';
12+
import 'utils.dart' as utils;
1413

1514
/// The configuration for a script runner. See each field's documentation for more information.
1615
class ScriptRunnerConfig {
@@ -163,7 +162,7 @@ class ScriptRunnerConfig {
163162
);
164163
print('');
165164
for (final scr in scripts) {
166-
final lines = _utils.chunks(
165+
final lines = utils.chunks(
167166
scr.description ?? '\$ ${[scr.cmd, ...scr.args].join(' ')}',
168167
80 - padLen,
169168
);
@@ -179,7 +178,7 @@ class ScriptRunnerConfig {
179178
FileSystem fs, String startDir) async {
180179
var dir = fs.directory(startDir);
181180
String sourceFile;
182-
YamlMap? source;
181+
yaml.YamlMap? source;
183182
bool rootSearched = false;
184183
while (!rootSearched) {
185184
if (dir.parent.path == dir.path) {
@@ -292,7 +291,7 @@ class ScriptRunnerShellConfig {
292291
case OS.linux:
293292
case OS.macos:
294293
try {
295-
final envShell = firstNonNull([
294+
final envShell = utils.firstNonNull([
296295
Platform.environment['SHELL'],
297296
Platform.environment['TERM'],
298297
]);
@@ -310,3 +309,4 @@ enum OS {
310309
linux,
311310
// other
312311
}
312+

lib/src/runnable_script.dart

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dart:convert';
21
import 'dart:io' as io;
32

43
import 'package:file/file.dart';
@@ -146,38 +145,41 @@ class RunnableScript {
146145
}
147146
if (exitCode != 0) {
148147
final e = io.ProcessException(
149-
cmd, args, 'Process exited with error code: $exitCode', exitCode);
148+
cmd,
149+
args,
150+
'Process exited with error code: $exitCode',
151+
exitCode,
152+
);
150153
throw e;
151154
}
152-
} catch (e) {
153-
rethrow;
154155
} finally {
155156
await _fileSystem.file(scrPath).delete();
156157
}
157158
}
158159

159-
Future<int> _runShellScriptFile(ScriptRunnerConfig config, scrPath) async {
160+
Future<int> _runShellScriptFile(
161+
ScriptRunnerConfig config,
162+
String scrPath,
163+
) async {
160164
final result = await io.Process.start(
161165
config.shell.shell,
162166
[config.shell.shellExecFlag, scrPath],
163167
environment: {...?config.env, ...?env},
164168
workingDirectory: workingDir ?? config.workingDir,
169+
mode: io.ProcessStartMode.inheritStdio,
170+
includeParentEnvironment: true,
165171
);
166-
result.stdout.listen((event) {
167-
io.stdout.write(Utf8Decoder().convert(event));
168-
});
169-
result.stderr.listen((event) {
170-
io.stdout.write(Utf8Decoder().convert(event));
171-
});
172172
final exitCode = await result.exitCode;
173173
return exitCode;
174174
}
175175

176176
String _getScriptPath() => _fileSystem.path
177177
.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
178178

179-
String _getScriptContents(ScriptRunnerConfig config,
180-
{List<String> extraArgs = const []}) {
179+
String _getScriptContents(
180+
ScriptRunnerConfig config, {
181+
List<String> extraArgs = const [],
182+
}) {
181183
final script = "$cmd ${(args + extraArgs).map(_utils.wrap).join(' ')}";
182184
switch (config.shell.os) {
183185
case OS.windows:
@@ -189,9 +191,11 @@ class RunnableScript {
189191
case OS.linux:
190192
case OS.macos:
191193
return [
192-
...preloadScripts.map((e) => "alias ${e.name}='scr ${e.name}'"),
194+
...preloadScripts.map((e) =>
195+
"[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"),
193196
script
194197
].join('\n');
195198
}
196199
}
197200
}
201+

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ script_runner:
3131
- name: 'version:set'
3232
cmd: dart run btool set packageVersion
3333
suppress_header_output: true
34+
- format: dart format .
3435

3536
# Examples
3637
- name: echo1

0 commit comments

Comments
 (0)