Skip to content

Commit d82f667

Browse files
committed
fix: script env
1 parent a528956 commit d82f667

File tree

5 files changed

+66
-13
lines changed

5 files changed

+66
-13
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: 19 additions & 10 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,26 @@ class RunnableScript {
9495
description: description,
9596
displayCmd: displayCmd,
9697
appendNewline: appendNewline,
98+
env: map['env'] as Map<String, String>? ?? {},
9799
);
98100
} catch (e) {
99-
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
101+
throw StateError(
102+
'Failed to parse script, arguments: $map, $fileSystem. Error: $e');
100103
}
101104
}
102105

103106
/// Runs the current script with the given extra arguments.
104-
Future<int> run(List<String> extraArgs) async {
107+
Future<int> run([List<String> extraArgs = const []]) async {
105108
final effectiveArgs = args + extraArgs;
106109
final config = await ScriptRunnerConfig.get(_fileSystem);
107110

108111
final scrContents = _getScriptContents(config, extraArgs: extraArgs);
109112
final scrPath = _getScriptPath();
113+
final scrFile = _fileSystem.file(scrPath);
110114

111-
await _fileSystem.file(scrPath).writeAsString(scrContents);
115+
await scrFile.writeAsString(scrContents);
112116

113-
if (config.shell.os != OS.windows) {
117+
if (config.shell.os != OS.windows && _fileSystem is! MemoryFileSystem) {
114118
final result = await io.Process.run("chmod", ["u+x", scrPath]);
115119
if (result.exitCode != 0) throw Exception(result.stderr);
116120
}
@@ -148,7 +152,7 @@ class RunnableScript {
148152
final result = await io.Process.start(
149153
config.shell.shell,
150154
[config.shell.shellExecFlag, scrPath],
151-
environment: {...?config.env, ...?env},
155+
environment: {...?config.env, ...env},
152156
workingDirectory: workingDir ?? config.workingDir,
153157
mode: io.ProcessStartMode.inheritStdio,
154158
includeParentEnvironment: true,
@@ -157,7 +161,8 @@ class RunnableScript {
157161
return exitCode;
158162
}
159163

160-
String _getScriptPath() => _fileSystem.path.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
164+
String _getScriptPath() => _fileSystem.path
165+
.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
161166

162167
String _getScriptContents(
163168
ScriptRunnerConfig config, {
@@ -177,8 +182,12 @@ class RunnableScript {
177182
].join('\n');
178183
case OS.linux:
179184
case OS.macos:
180-
return [...preloadScripts.map((e) => "[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"), script]
181-
.join('\n');
185+
return [
186+
...preloadScripts.map((e) =>
187+
"[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"),
188+
script
189+
].join('\n');
182190
}
183191
}
184192
}
193+

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: 40 additions & 1 deletion
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', () {
@@ -147,7 +184,8 @@ Future<void> _writeCustomConf(FileSystem fs, [String? contents]) async {
147184
final homeDir = fs.directory(Platform.environment['HOME']!);
148185
homeDir.create(recursive: true);
149186
fs.currentDirectory = homeDir;
150-
final pubFile = fs.file(path.join(fs.currentDirectory.path, 'script_runner.yaml'));
187+
final pubFile =
188+
fs.file(path.join(fs.currentDirectory.path, 'script_runner.yaml'));
151189
pubFile.create(recursive: true);
152190
print('writing custom conf to ${pubFile.path}');
153191
await pubFile.writeAsString(
@@ -181,3 +219,4 @@ Future<void> _writePubspec(FileSystem fs, [String? contents]) async {
181219
].join('\n'),
182220
);
183221
}
222+

0 commit comments

Comments
 (0)