@@ -81,6 +81,9 @@ case class Settings(
81
81
def withSave : Settings =
82
82
this .copy(save = true )
83
83
84
+ def noSave : Settings =
85
+ this .copy(save = false )
86
+
84
87
def withModeShouldBePossibleRun : Settings =
85
88
this .copy(modeShouldBePossibleRun = true )
86
89
@@ -144,6 +147,13 @@ object MainGenericRunner {
144
147
process(remainingArgs, settings)
145
148
case (o @ colorOption(_* )) :: tail =>
146
149
process(tail, settings.withScalaArgs(o))
150
+ case " -e" :: expression :: tail =>
151
+ val tempScript = writeFile(s " @main def main(args: String *): Unit = \n ${expression}" )
152
+ settings
153
+ .withExecuteMode(ExecuteMode .Script )
154
+ .withTargetScript(tempScript)
155
+ .withScriptArgs(tail* )
156
+ .noSave // useless
147
157
case arg :: tail =>
148
158
val line = Try (Source .fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
149
159
lazy val hasScalaHashbang = { val s = line.getOrElse(" " ) ; s.startsWith(" #!" ) && s.contains(" scala" ) }
@@ -156,6 +166,7 @@ object MainGenericRunner {
156
166
val newSettings = if arg.startsWith(" -" ) then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
157
167
process(tail, newSettings.withResidualArgs(arg))
158
168
169
+
159
170
def main (args : Array [String ]): Unit =
160
171
val scalaOpts = envOrNone(" SCALA_OPTS" ).toArray.flatMap(_.split(" " )).filter(_.nonEmpty)
161
172
val allArgs = scalaOpts ++ args
@@ -244,4 +255,11 @@ object MainGenericRunner {
244
255
e.foreach(_.printStackTrace())
245
256
! isFailure
246
257
}
258
+ def writeFile (contents : String ): String = {
259
+ val file = Files .createTempFile(" exec" , " .scala" )
260
+ file.toFile.deleteOnExit()
261
+ Files .write(file, contents.getBytes)
262
+ file.toString.replace('\\ ' , '/' )
263
+ }
264
+
247
265
}
0 commit comments