From 62a3c403a2160e5db0fb56c7f3afa267a76a8952 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 9 Apr 2024 00:40:10 +0300 Subject: [PATCH] fix: script env --- CHANGELOG.md | 5 +++++ lib/src/config.dart | 2 +- lib/src/runnable_script.dart | 15 +++++++++------ pubspec.yaml | 2 +- test/config_test.dart | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b4cb99..49a0e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.7.4 + +- Fix: Script ENV not being passed properly +- Update dependencies + ## 0.7.3 - Fix: Exit with code diff --git a/lib/src/config.dart b/lib/src/config.dart index f0a06ed..0132798 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -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, ); diff --git a/lib/src/runnable_script.dart b/lib/src/runnable_script.dart index 0ef94cd..58dfc77 100644 --- a/lib/src/runnable_script.dart +++ b/lib/src/runnable_script.dart @@ -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? env; + final Map 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? ?? {}, ); } 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 run(List extraArgs) async { + Future run([List 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, diff --git a/pubspec.yaml b/pubspec.yaml index 18132b6..6d2f8f8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/config_test.dart b/test/config_test.dart index 18cd484..e555649 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -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', () {