@@ -13,7 +13,7 @@ import scala.util.matching.Regex
13
13
val SnippetBlock = """ *(`{2}`+)[^ ]+ title=([\w\d.\-/_]+) *""" .r
14
14
val CompileBlock = """ *(`{2}`+) *(\w+) +(compile|fail) *(?:title=([\w\d.\-/_]+))? *(power)? *""" .r
15
15
def compileBlockEnds (backticks : String ) = s """ * $backticks * """ .r
16
- val BashCommand = """ *```bash *(fail|run-fail)? *""" .r
16
+ val BashCommand = """ *```bash *(fail|run-fail)? *(clean)? * """ .r
17
17
val CheckBlock = """ *\<\!-- Expected(-regex)?: *""" .r
18
18
val CheckBlockEnd = """ *\--> *""" .r
19
19
val Clear = """ *<!--+ *clear *-+-> *""" .r
@@ -35,8 +35,12 @@ enum Commands:
35
35
case Check (patterns, regex, _) =>
36
36
val kind = if regex then " regexes" else " patterns"
37
37
s " last output matches $kind: ${patterns.map(p => s " ' $p' " ).mkString(" , " )}"
38
- case Run (cmd, shouldFail, _) =>
39
- val prefix = if shouldFail then " [failure expected] " else " "
38
+ case Run (cmd, shouldFail, shouldClean, _) =>
39
+ val prefix = shouldFail -> shouldClean match
40
+ case (true , true ) => " [failure expected, clean]"
41
+ case (true , false ) => " [failure expected]"
42
+ case (false , true ) => " [clean]"
43
+ case _ => " "
40
44
cmd.mkString(prefix, " " , " " )
41
45
case Write (name, _, _) =>
42
46
name
@@ -53,7 +57,7 @@ enum Commands:
53
57
shouldFail : Boolean ,
54
58
power : Boolean
55
59
)
56
- case Run (scriptLines : Seq [String ], shouldFail : Boolean , context : Context )
60
+ case Run (scriptLines : Seq [String ], shouldFail : Boolean , shouldClean : Boolean , context : Context )
57
61
case Check (patterns : Seq [String ], regex : Boolean , context : Context )
58
62
case Clear (context : Context )
59
63
@@ -100,8 +104,8 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
100
104
compileBlockEnds(backticks)
101
105
)
102
106
103
- case BashCommand (failGroup) :: tail =>
104
- parseMultiline(tail, Commands .Run (_, failGroup != null , context))
107
+ case BashCommand (failGroup, clean ) :: tail =>
108
+ parseMultiline(tail, Commands .Run (_, failGroup != null , clean != null , context))
105
109
106
110
case CheckBlock (regexOpt) :: tail =>
107
111
val isRegex = regexOpt == " -regex"
@@ -187,15 +191,50 @@ def checkFile(file: os.Path, options: Options): Unit =
187
191
val binDir = {
188
192
val binDir0 = out / " .scala-cli"
189
193
os.makeDir.all(binDir0)
190
- val escapedCommand = options.scalaCliCommand
191
- .map(arg => " \" " + arg.replace(" \" " , " \\\" " ) + " \" " )
192
- .mkString(" " )
193
- val helperScript =
194
- s """ #!/usr/bin/env bash
195
- |exec $escapedCommand " $$ @"
196
- | """ .stripMargin
197
- os.write(binDir0 / " scala-cli" , helperScript)
198
- os.perms.set(binDir0 / " scala-cli" , " rwxr-xr-x" )
194
+
195
+ def createHelperScript (command : Seq [String ], scriptName : String ): Unit = {
196
+ val escapedCommand = command
197
+ .map(arg => " \" " + arg.replace(" \" " , " \\\" " ) + " \" " )
198
+ .mkString(" " )
199
+ val scriptCode =
200
+ s """ #!/usr/bin/env bash
201
+ |exec $escapedCommand " $$ @"
202
+ | """ .stripMargin
203
+ os.write(binDir0 / scriptName, scriptCode)
204
+ os.perms.set(binDir0 / scriptName, " rwxr-xr-x" )
205
+ }
206
+ createHelperScript(options.scalaCliCommand, " scala-cli" )
207
+ createHelperScript(options.scalaCliCommand, " scala" )
208
+ val coursierCliDep =
209
+ s " ${Constants .coursierOrg}: ${Constants .coursierCliModule}: ${Constants .coursierCliVersion}"
210
+ createHelperScript(
211
+ options.scalaCliCommand ++ Seq (
212
+ " run" ,
213
+ " --dep" ,
214
+ coursierCliDep,
215
+ " --" ,
216
+ " launch" ,
217
+ s " scala: ${Constants .defaultScalaVersion}" ,
218
+ " -M" ,
219
+ " dotty.tools.MainGenericRunner" ,
220
+ " --"
221
+ ),
222
+ " scala_legacy"
223
+ )
224
+ createHelperScript(
225
+ options.scalaCliCommand ++ Seq (
226
+ " run" ,
227
+ " --dep" ,
228
+ coursierCliDep,
229
+ " --" ,
230
+ " launch" ,
231
+ s " scala: ${Constants .defaultScalaVersion}" ,
232
+ " -M" ,
233
+ " dotty.tools.dotc.Main" ,
234
+ " --"
235
+ ),
236
+ " scalac"
237
+ )
199
238
binDir0
200
239
}
201
240
val extraEnv = {
@@ -239,7 +278,15 @@ def checkFile(file: os.Path, options: Options): Unit =
239
278
res.exitCode
240
279
241
280
cmd match
242
- case Commands .Run (cmds, shouldFail, _) =>
281
+ case Commands .Run (cmds, shouldFail, shouldClean, _) =>
282
+ if shouldClean then
283
+ os.list(out)
284
+ .filterNot(_ == binDir)
285
+ .filterNot(_.last.endsWith(" .scala" ))
286
+ .filterNot(_.last.endsWith(" .sc" ))
287
+ .filterNot(_.last.endsWith(" .java" ))
288
+ .filterNot(_.last.endsWith(" .md" ))
289
+ .foreach(os.remove.all)
243
290
val script = out / " .scala-build" / " run.sh"
244
291
os.write.over(script, mkBashScript(cmds), createFolders = true )
245
292
os.perms.set(script, " rwxr-xr-x" )
0 commit comments