Skip to content

Commit 62a3c40

Browse files
committed
fix: script env
1 parent a528956 commit 62a3c40

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.7.4
2+
3+
- Fix: Script ENV not being passed properly
4+
- Update dependencies
5+
16
## 0.7.3
27

38
- Fix: Exit with code

lib/src/config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ScriptRunnerConfig {
9090
scripts: _parseScriptsList(source['scripts'], fileSystem: fs),
9191
env: env,
9292
workingDir: source['cwd'],
93-
fileSystem: fileSystem,
93+
fileSystem: fs,
9494
lineLength: source['line_length'] ?? 80,
9595
configSource: configSource,
9696
);

lib/src/runnable_script.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io' as io;
22

33
import 'package:file/file.dart';
44
import 'package:file/local.dart';
5+
import 'package:file/memory.dart';
56
import 'package:script_runner/src/config.dart';
67
// ignore: no_leading_underscores_for_library_prefixes
78
import 'package:script_runner/src/utils.dart' as _utils;
@@ -30,7 +31,7 @@ class RunnableScript {
3031

3132
/// The environment variables to run the script in.
3233
/// This map is appended to the one given in the config.
33-
final Map<String, String>? env;
34+
final Map<String, String> env;
3435

3536
/// Other scripts in the config which are runnable by this script.
3637
/// The script loader pre-loads these as temporary aliases to allow combined scripts to be run.
@@ -57,7 +58,7 @@ class RunnableScript {
5758
required this.args,
5859
this.description,
5960
this.workingDir,
60-
this.env,
61+
this.env = const {},
6162
FileSystem? fileSystem,
6263
this.displayCmd = false,
6364
this.appendNewline = false,
@@ -94,23 +95,25 @@ class RunnableScript {
9495
description: description,
9596
displayCmd: displayCmd,
9697
appendNewline: appendNewline,
98+
env: map['env'] as Map<String, String>? ?? {},
9799
);
98100
} catch (e) {
99101
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
100102
}
101103
}
102104

103105
/// Runs the current script with the given extra arguments.
104-
Future<int> run(List<String> extraArgs) async {
106+
Future<int> run([List<String> extraArgs = const []]) async {
105107
final effectiveArgs = args + extraArgs;
106108
final config = await ScriptRunnerConfig.get(_fileSystem);
107109

108110
final scrContents = _getScriptContents(config, extraArgs: extraArgs);
109111
final scrPath = _getScriptPath();
112+
final scrFile = _fileSystem.file(scrPath);
110113

111-
await _fileSystem.file(scrPath).writeAsString(scrContents);
114+
await scrFile.writeAsString(scrContents);
112115

113-
if (config.shell.os != OS.windows) {
116+
if (config.shell.os != OS.windows && _fileSystem is! MemoryFileSystem) {
114117
final result = await io.Process.run("chmod", ["u+x", scrPath]);
115118
if (result.exitCode != 0) throw Exception(result.stderr);
116119
}
@@ -148,7 +151,7 @@ class RunnableScript {
148151
final result = await io.Process.start(
149152
config.shell.shell,
150153
[config.shell.shellExecFlag, scrPath],
151-
environment: {...?config.env, ...?env},
154+
environment: {...?config.env, ...env},
152155
workingDirectory: workingDir ?? config.workingDir,
153156
mode: io.ProcessStartMode.inheritStdio,
154157
includeParentEnvironment: true,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: script_runner
22
description: Run all your project-related scripts in a portable, simple config.
3-
version: 0.7.3
3+
version: 0.7.4
44
homepage: https://casraf.dev/projects/dart-script-runner
55
repository: https://github.com/chenasraf/dart_script_runner
66
license: MIT

test/config_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@ void main() {
5757
expect(testScr.args, []);
5858
});
5959
});
60+
61+
group('env injection', () {
62+
fs = MemoryFileSystem();
63+
final fooFile = fs.file('/tmp/foo.txt');
64+
65+
setUp(() async {
66+
fs = MemoryFileSystem();
67+
await _writePubspec(
68+
fs,
69+
[
70+
'script_runner:',
71+
' shell:',
72+
' linux: /bin/zsh',
73+
' macos: /bin/bash',
74+
' windows: powershell.exe',
75+
' scripts:',
76+
' - name: test',
77+
' cwd: .',
78+
' cmd: "echo \\"\$FOO\\" > ${fooFile.path}"',
79+
' env:',
80+
' FOO: bar',
81+
].join('\n'),
82+
);
83+
});
84+
85+
test('works', () async {
86+
final conf = await ScriptRunnerConfig.get(fs);
87+
final testScr = conf.scriptsMap['test']!;
88+
expect(testScr.name, 'test');
89+
expect(testScr.cmd, 'echo "\$FOO" > ${fooFile.path}');
90+
expect(testScr.args, []);
91+
expect(testScr.env, {'FOO': 'bar'});
92+
// await testScr.run();
93+
// final contents = await fooFile.readAsString();
94+
// expect(contents, 'bar\n');
95+
});
96+
});
6097
});
6198

6299
group('ScriptRunnerShellConfig', () {

0 commit comments

Comments
 (0)