@@ -42,12 +42,26 @@ import org.scalajs.logging._
42
42
*
43
43
* @param logger The logger to use in the run. A [[JSEnv ]] is not required to
44
44
* log anything.
45
+ *
46
+ * @param additionalEnv Additional environment variables for this run.
47
+ *
48
+ * How these are retrieved in the JS code run inside the [[JSEnv ]] is
49
+ * completely up to the implementation, including whether:
50
+ * - they are implemented with system environment variables,
51
+ * - they share the same namespace than the system environment variables.
52
+ *
53
+ * However, in any case, the variables in [[additionalEnv ]] take precedence
54
+ * over any (explicitly or implicitly) ambiant environment vars.
55
+ *
56
+ * This is an optional feature; but [[JSEnv ]]s are required to support an
57
+ * empty [[additionalEnv ]].
45
58
*/
46
59
final class RunConfig private (
47
60
val onOutputStream : Option [RunConfig .OnOutputStream ],
48
61
val inheritOutput : Boolean ,
49
62
val inheritError : Boolean ,
50
63
val logger : Logger ,
64
+ val additionalEnv : Map [String , String ],
51
65
/** An option that will never be supported by anything because it is not exposed.
52
66
*
53
67
* This is used to test that [[JSEnv ]]s properly validate their configuration.
@@ -62,6 +76,7 @@ final class RunConfig private (
62
76
inheritOutput = true ,
63
77
inheritError = true ,
64
78
logger = NullLogger ,
79
+ additionalEnv = Map .empty,
65
80
eternallyUnsupportedOption = false )
66
81
}
67
82
@@ -77,6 +92,12 @@ final class RunConfig private (
77
92
def withLogger (logger : Logger ): RunConfig =
78
93
copy(logger = logger)
79
94
95
+ def withAdditionalEnv (additionalEnv : Map [String , String ]): RunConfig =
96
+ copy(additionalEnv = additionalEnv)
97
+
98
+ def withAdditionalEnv (name : String , value : String ): RunConfig =
99
+ copy(additionalEnv = additionalEnv + (name -> value))
100
+
80
101
private [jsenv] def withEternallyUnsupportedOption (
81
102
eternallyUnsupportedOption : Boolean ): RunConfig =
82
103
copy(eternallyUnsupportedOption = eternallyUnsupportedOption)
@@ -85,10 +106,11 @@ final class RunConfig private (
85
106
inheritOutput : Boolean = inheritOutput,
86
107
inheritError : Boolean = inheritError,
87
108
logger : Logger = logger,
109
+ additionalEnv : Map [String , String ] = additionalEnv,
88
110
eternallyUnsupportedOption : Boolean = eternallyUnsupportedOption
89
111
): RunConfig = {
90
112
new RunConfig (onOutputStream, inheritOutput, inheritError, logger,
91
- eternallyUnsupportedOption)
113
+ additionalEnv, eternallyUnsupportedOption)
92
114
}
93
115
94
116
/** Validates constraints on the config itself. */
@@ -119,9 +141,10 @@ final object RunConfig {
119
141
*/
120
142
final class Validator private (
121
143
inheritIO : Boolean ,
122
- onOutputStream : Boolean
144
+ onOutputStream : Boolean ,
145
+ additionalEnv : Boolean
123
146
) {
124
- private def this () = this (false , false )
147
+ private def this () = this (false , false , false )
125
148
126
149
/** The caller supports [[RunConfig#inheritOutput ]] and
127
150
* [[RunConfig#inheritError ]].
@@ -131,6 +154,9 @@ final object RunConfig {
131
154
/** The caller supports [[RunConfig#onOutputStream ]]. */
132
155
def supportsOnOutputStream (): Validator = copy(onOutputStream = true )
133
156
157
+ /** The caller supports [[RunConfig#additionalEnv ]]. */
158
+ def supportsAdditionalEnv (): Validator = copy(additionalEnv = true )
159
+
134
160
/** Validates that `config` is valid and only sets supported options.
135
161
*
136
162
* @throws java.lang.IllegalArgumentException if there are unsupported options.
@@ -146,13 +172,19 @@ final object RunConfig {
146
172
if (! onOutputStream && config.onOutputStream.isDefined)
147
173
fail(" onOutputStream is not supported." )
148
174
175
+ if (! additionalEnv && config.additionalEnv.nonEmpty)
176
+ fail(" additionalEnv is not supported." )
177
+
149
178
if (config.eternallyUnsupportedOption)
150
179
fail(" eternallyUnsupportedOption is not supported." )
151
180
}
152
181
153
- private def copy (inheritIO : Boolean = inheritIO,
154
- onOutputStream : Boolean = onOutputStream) = {
155
- new Validator (inheritIO, onOutputStream)
182
+ private def copy (
183
+ inheritIO : Boolean = inheritIO,
184
+ onOutputStream : Boolean = onOutputStream,
185
+ additionalEnv : Boolean = additionalEnv
186
+ ) = {
187
+ new Validator (inheritIO, onOutputStream, additionalEnv)
156
188
}
157
189
}
158
190
0 commit comments