Skip to content

Commit

Permalink
fix: script env
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Apr 8, 2024
1 parent a528956 commit 62a3c40
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.7.4

- Fix: Script ENV not being passed properly
- Update dependencies

## 0.7.3

- Fix: Exit with code
Expand Down
2 changes: 1 addition & 1 deletion lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ScriptRunnerConfig {
scripts: _parseScriptsList(source['scripts'], fileSystem: fs),
env: env,
workingDir: source['cwd'],
fileSystem: fileSystem,
fileSystem: fs,
lineLength: source['line_length'] ?? 80,
configSource: configSource,
);
Expand Down
15 changes: 9 additions & 6 deletions lib/src/runnable_script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io' as io;

import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:file/memory.dart';
import 'package:script_runner/src/config.dart';
// ignore: no_leading_underscores_for_library_prefixes
import 'package:script_runner/src/utils.dart' as _utils;
Expand Down Expand Up @@ -30,7 +31,7 @@ class RunnableScript {

/// The environment variables to run the script in.
/// This map is appended to the one given in the config.
final Map<String, String>? env;
final Map<String, String> env;

/// Other scripts in the config which are runnable by this script.
/// The script loader pre-loads these as temporary aliases to allow combined scripts to be run.
Expand All @@ -57,7 +58,7 @@ class RunnableScript {
required this.args,
this.description,
this.workingDir,
this.env,
this.env = const {},
FileSystem? fileSystem,
this.displayCmd = false,
this.appendNewline = false,
Expand Down Expand Up @@ -94,23 +95,25 @@ class RunnableScript {
description: description,
displayCmd: displayCmd,
appendNewline: appendNewline,
env: map['env'] as Map<String, String>? ?? {},
);
} catch (e) {
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
}
}

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

final scrContents = _getScriptContents(config, extraArgs: extraArgs);
final scrPath = _getScriptPath();
final scrFile = _fileSystem.file(scrPath);

await _fileSystem.file(scrPath).writeAsString(scrContents);
await scrFile.writeAsString(scrContents);

if (config.shell.os != OS.windows) {
if (config.shell.os != OS.windows && _fileSystem is! MemoryFileSystem) {
final result = await io.Process.run("chmod", ["u+x", scrPath]);
if (result.exitCode != 0) throw Exception(result.stderr);
}
Expand Down Expand Up @@ -148,7 +151,7 @@ class RunnableScript {
final result = await io.Process.start(
config.shell.shell,
[config.shell.shellExecFlag, scrPath],
environment: {...?config.env, ...?env},
environment: {...?config.env, ...env},
workingDirectory: workingDir ?? config.workingDir,
mode: io.ProcessStartMode.inheritStdio,
includeParentEnvironment: true,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: script_runner
description: Run all your project-related scripts in a portable, simple config.
version: 0.7.3
version: 0.7.4
homepage: https://casraf.dev/projects/dart-script-runner
repository: https://github.com/chenasraf/dart_script_runner
license: MIT
Expand Down
37 changes: 37 additions & 0 deletions test/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ void main() {
expect(testScr.args, []);
});
});

group('env injection', () {
fs = MemoryFileSystem();
final fooFile = fs.file('/tmp/foo.txt');

setUp(() async {
fs = MemoryFileSystem();
await _writePubspec(
fs,
[
'script_runner:',
' shell:',
' linux: /bin/zsh',
' macos: /bin/bash',
' windows: powershell.exe',
' scripts:',
' - name: test',
' cwd: .',
' cmd: "echo \\"\$FOO\\" > ${fooFile.path}"',
' env:',
' FOO: bar',
].join('\n'),
);
});

test('works', () async {
final conf = await ScriptRunnerConfig.get(fs);
final testScr = conf.scriptsMap['test']!;
expect(testScr.name, 'test');
expect(testScr.cmd, 'echo "\$FOO" > ${fooFile.path}');
expect(testScr.args, []);
expect(testScr.env, {'FOO': 'bar'});
// await testScr.run();
// final contents = await fooFile.readAsString();
// expect(contents, 'bar\n');
});
});
});

group('ScriptRunnerShellConfig', () {
Expand Down

0 comments on commit 62a3c40

Please sign in to comment.