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 d82f667
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 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
29 changes: 19 additions & 10 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,26 @@ 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');
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 +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,
Expand All @@ -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, {
Expand All @@ -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');
}
}
}

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
41 changes: 40 additions & 1 deletion 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 Expand Up @@ -147,7 +184,8 @@ Future<void> _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(
Expand Down Expand Up @@ -181,3 +219,4 @@ Future<void> _writePubspec(FileSystem fs, [String? contents]) async {
].join('\n'),
);
}

0 comments on commit d82f667

Please sign in to comment.