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..f5de23d 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,23 +95,26 @@ 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'); + throw StateError( + 'Failed to parse script, arguments: $map, $fileSystem. Error: $e'); } } /// 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 +152,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, @@ -157,7 +161,8 @@ class RunnableScript { return exitCode; } - String _getScriptPath() => _fileSystem.path.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh'); + String _getScriptPath() => _fileSystem.path + .join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh'); String _getScriptContents( ScriptRunnerConfig config, { @@ -177,8 +182,12 @@ class RunnableScript { ].join('\n'); case OS.linux: case OS.macos: - return [...preloadScripts.map((e) => "[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"), script] - .join('\n'); + return [ + ...preloadScripts.map((e) => + "[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"), + script + ].join('\n'); } } } + 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..81b45e4 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', () { @@ -147,7 +184,8 @@ Future _writeCustomConf(FileSystem fs, [String? contents]) async { final homeDir = fs.directory(Platform.environment['HOME']!); homeDir.create(recursive: true); fs.currentDirectory = homeDir; - final pubFile = fs.file(path.join(fs.currentDirectory.path, 'script_runner.yaml')); + final pubFile = + fs.file(path.join(fs.currentDirectory.path, 'script_runner.yaml')); pubFile.create(recursive: true); print('writing custom conf to ${pubFile.path}'); await pubFile.writeAsString( @@ -181,3 +219,4 @@ Future _writePubspec(FileSystem fs, [String? contents]) async { ].join('\n'), ); } +