fix: script env

This commit is contained in:
2024-04-09 00:40:10 +03:00
parent a52895643a
commit 62a3c403a2
5 changed files with 53 additions and 8 deletions

View File

@@ -1,3 +1,8 @@
## 0.7.4
- Fix: Script ENV not being passed properly
- Update dependencies
## 0.7.3
- Fix: Exit with code

View File

@@ -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,
);

View File

@@ -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;
@@ -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.
@@ -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,
@@ -94,6 +95,7 @@ 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');
@@ -101,16 +103,17 @@ class RunnableScript {
}
/// 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);
}
@@ -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,

View File

@@ -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

View File

@@ -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', () {