@@ -2,6 +2,7 @@ import 'dart:io' as io;
2
2
3
3
import 'package:file/file.dart' ;
4
4
import 'package:file/local.dart' ;
5
+ import 'package:file/memory.dart' ;
5
6
import 'package:script_runner/src/config.dart' ;
6
7
// ignore: no_leading_underscores_for_library_prefixes
7
8
import 'package:script_runner/src/utils.dart' as _utils;
@@ -30,7 +31,7 @@ class RunnableScript {
30
31
31
32
/// The environment variables to run the script in.
32
33
/// This map is appended to the one given in the config.
33
- final Map <String , String >? env;
34
+ final Map <String , String > env;
34
35
35
36
/// Other scripts in the config which are runnable by this script.
36
37
/// The script loader pre-loads these as temporary aliases to allow combined scripts to be run.
@@ -57,7 +58,7 @@ class RunnableScript {
57
58
required this .args,
58
59
this .description,
59
60
this .workingDir,
60
- this .env,
61
+ this .env = const {} ,
61
62
FileSystem ? fileSystem,
62
63
this .displayCmd = false ,
63
64
this .appendNewline = false ,
@@ -94,23 +95,26 @@ class RunnableScript {
94
95
description: description,
95
96
displayCmd: displayCmd,
96
97
appendNewline: appendNewline,
98
+ env: map['env' ] as Map <String , String >? ?? {},
97
99
);
98
100
} catch (e) {
99
- throw StateError ('Failed to parse script, arguments: $map , $fileSystem . Error: $e ' );
101
+ throw StateError (
102
+ 'Failed to parse script, arguments: $map , $fileSystem . Error: $e ' );
100
103
}
101
104
}
102
105
103
106
/// Runs the current script with the given extra arguments.
104
- Future <int > run (List <String > extraArgs) async {
107
+ Future <int > run ([ List <String > extraArgs = const []] ) async {
105
108
final effectiveArgs = args + extraArgs;
106
109
final config = await ScriptRunnerConfig .get (_fileSystem);
107
110
108
111
final scrContents = _getScriptContents (config, extraArgs: extraArgs);
109
112
final scrPath = _getScriptPath ();
113
+ final scrFile = _fileSystem.file (scrPath);
110
114
111
- await _fileSystem. file (scrPath) .writeAsString (scrContents);
115
+ await scrFile .writeAsString (scrContents);
112
116
113
- if (config.shell.os != OS .windows) {
117
+ if (config.shell.os != OS .windows && _fileSystem is ! MemoryFileSystem ) {
114
118
final result = await io.Process .run ("chmod" , ["u+x" , scrPath]);
115
119
if (result.exitCode != 0 ) throw Exception (result.stderr);
116
120
}
@@ -148,7 +152,7 @@ class RunnableScript {
148
152
final result = await io.Process .start (
149
153
config.shell.shell,
150
154
[config.shell.shellExecFlag, scrPath],
151
- environment: {...? config.env, ...? env},
155
+ environment: {...? config.env, ...env},
152
156
workingDirectory: workingDir ?? config.workingDir,
153
157
mode: io.ProcessStartMode .inheritStdio,
154
158
includeParentEnvironment: true ,
@@ -157,7 +161,8 @@ class RunnableScript {
157
161
return exitCode;
158
162
}
159
163
160
- String _getScriptPath () => _fileSystem.path.join (_fileSystem.systemTempDirectory.path, 'script_runner_$name .sh' );
164
+ String _getScriptPath () => _fileSystem.path
165
+ .join (_fileSystem.systemTempDirectory.path, 'script_runner_$name .sh' );
161
166
162
167
String _getScriptContents (
163
168
ScriptRunnerConfig config, {
@@ -177,8 +182,12 @@ class RunnableScript {
177
182
].join ('\n ' );
178
183
case OS .linux:
179
184
case OS .macos:
180
- return [...preloadScripts.map ((e) => "[[ ! \$ (which ${e .name }) ]] && alias ${e .name }='scr ${e .name }'" ), script]
181
- .join ('\n ' );
185
+ return [
186
+ ...preloadScripts.map ((e) =>
187
+ "[[ ! \$ (which ${e .name }) ]] && alias ${e .name }='scr ${e .name }'" ),
188
+ script
189
+ ].join ('\n ' );
182
190
}
183
191
}
184
192
}
193
+
0 commit comments