Skip to content

Commit

Permalink
Add 'objectWrapper' directive
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed May 23, 2023
1 parent c01b670 commit 68541d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object DirectivesPreprocessingUtils {
directives.JavaHome.handler,
directives.Jvm.handler,
directives.MainClass.handler,
directives.ObjectWrapper.handler,
directives.Packaging.handler,
directives.Platform.handler,
directives.Plugin.handler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scala.build.preprocessing.directives

import scala.build.directives.*
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, ScriptOptions}
import scala.cli.commands.SpecificationLevel

@DirectiveExamples("//> using objectWrapper")
@DirectiveUsage("//> using objectWrapper", "`//> using objectWrapper")
@DirectiveDescription("Set the default code wrapper for scripts to object wrapper")
@DirectiveLevel(SpecificationLevel.RESTRICTED)
final case class ObjectWrapper(
@DirectiveName("object.wrapper")
objectWrapper: Boolean = false
) extends HasBuildOptions {
def buildOptions: Either[BuildException, BuildOptions] =
val options = BuildOptions(scriptOptions =
ScriptOptions(forceObjectWrapper = Some(true))
)
Right(options)
}

object ObjectWrapper {
val handler: DirectiveHandler[ObjectWrapper] = DirectiveHandler.derive
}
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
|""".stripMargin,
os.rel / "script_with_directive.sc" ->
"""//> using dep "com.lihaoyi::os-lib:0.9.1"
|//> using objectWrapper
|//> using object.wrapper
|@main def main(args: String*): Unit = println("Hello")
|""".stripMargin
)
Expand All @@ -546,6 +546,15 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
expect(outputNormalized.contains(
"[warn] Annotation @main in .sc scripts is not supported, it will be ignored, use .scala format instead"
))

val directiveRes = os.proc(TestUtil.cli, "--power", "script_with_directive.sc")
.call(cwd = root, mergeErrIntoOut = true, stdout = os.Pipe)

val directiveOutputNormalized: String = normalizeConsoleOutput(directiveRes.out.text())

expect(directiveOutputNormalized.contains(
"[warn] Annotation @main in .sc scripts is not supported, it will be ignored, use .scala format instead"
))
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ Specify default main class
#### Examples
`//> using mainClass helloWorld`

### ObjectWrapper

Set the default code wrapper for scripts to object wrapper

`//> using objectWrapper

#### Examples
`//> using objectWrapper`

### Packaging

Set parameters for packaging
Expand Down

0 comments on commit 68541d5

Please sign in to comment.