@@ -10,7 +10,6 @@ import java.lang.reflect.Method
1010import java .net .URLClassLoader
1111import java .nio .file .{Files , Path , Paths }
1212import scala .language .unsafeNulls
13- import scala .util .control .NonFatal
1413import scala .util .{Failure , Try }
1514
1615/**
@@ -43,27 +42,34 @@ class ScriptingDriver(compilerArgs: Array[String],
4342 println(s " compiler arguments: ${compilerArgs.mkString(" ," )}" )
4443 }
4544
46- def compileAndRun (): Try [Unit ] = {
45+ def compileAndRun (): Try [Object ] = {
4746 assert(! executed, " scripting driver can only be used once, and this instance has already been used." )
4847 executed = true
4948 val inputFiles = (wrappedScript +: predefFiles).filter(Files .exists(_))
50- try {
51- new SimpleDriver (
52- linesBeforeRunBeforeCode = wrappingResult.linesBeforeRunBeforeCode,
53- linesBeforeScript = wrappingResult.linesBeforeScript
54- ) .compile(compilerArgs, inputFiles, verbose) { (ctx, outDir) =>
55- given Context = ctx
56- tempFiles += outDir
49+ val driver = new SimpleDriver (
50+ linesBeforeRunBeforeCode = wrappingResult.linesBeforeRunBeforeCode,
51+ linesBeforeScript = wrappingResult.linesBeforeScript
52+ )
53+ val result = driver .compile(compilerArgs, inputFiles, verbose) { (ctx, outDir) =>
54+ given Context = ctx
55+ tempFiles += outDir
5756
58- val inheritedClasspath = ctx.settings.classpath.value
59- val classpathEntries = ClassPath .expandPath(inheritedClasspath, expandStar = true ).map(Paths .get(_))
60- val mainMethod = lookupMainMethod(outDir, classpathEntries)
61- mainMethod.invoke(null , scriptArgs)
62- }
63- } catch {
64- case NonFatal (e) => Failure (e)
65- } finally {
66- tempFiles.result().foreach(deleteRecursively)
57+ val inheritedClasspath = ctx.settings.classpath.value
58+ val classpathEntries = ClassPath .expandPath(inheritedClasspath, expandStar = true ).map(Paths .get(_))
59+ val mainMethod = lookupMainMethod(outDir, classpathEntries)
60+ mainMethod.invoke(null , scriptArgs)
61+ }
62+ tempFiles.result().foreach(deleteRecursively)
63+
64+ result.recoverWith { case e =>
65+ val msgAddonMaybe = if (verbose) " " else " Re-run with `--verbose` for more details"
66+ Failure (CompilerError (
67+ s """ Error during compilation: ${e.getMessage}
68+ |Please check error output above!
69+ |For given input files: ${inputFiles.mkString(" , " )}
70+ | $msgAddonMaybe
71+ | """ .stripMargin
72+ ))
6773 }
6874 }
6975
0 commit comments