Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add using python directive #1492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ case object ScalaPreprocessor extends Preprocessor {
UsingPlatformDirectiveHandler,
UsingPublishContextualDirectiveHandler,
UsingPublishDirectiveHandler,
UsingPythonDirectiveHandler,
UsingRepositoryDirectiveHandler,
UsingResourcesDirectiveHandler,
UsingScalaJsOptionsDirectiveHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package scala.build.preprocessing.directives
import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, MaybeScalaVersion, PostBuildOptions, ScalaOptions}

case object UsingPythonDirectiveHandler extends UsingDirectiveHandler {
def name = "Python"

def description = "Enable Python support"

def usage = "//> using python"

override def usageMd = "`//> using python"

override def examples = Seq(
"//> using python"
)

def keys = Seq("python")

override def isRestricted = true

def handleValues(
scopedDirective: ScopedDirective,
logger: Logger
): Either[BuildException, ProcessedUsingDirective] = {
val options = BuildOptions(
notForBloopOptions = PostBuildOptions(
python = Some(true)
)
)
Right(ProcessedDirective(Some(options), Nil))
}

override def getSupportedTypes(key: String) =
Set()
override protected def getValueNumberBounds(key: String): UsingDirectiveValueNumberBounds =
UsingDirectiveValueNumberBounds(0, 0)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2455,24 +2455,42 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
if (actualScalaVersion.startsWith("2.13.")) ""
else "import me.shadaj.scalapy.py" + System.lineSeparator()

test("scalapy") {
def scalapyTest(useDirective: Boolean): Unit = {
val maybeDirective =
if (useDirective)
"""//> using python
|""".stripMargin
else
""
val maybeCliArg =
if (useDirective) Nil
else Seq("--python")
val inputs = TestInputs(
os.rel / "helloscalapy.sc" ->
s"""$maybeScalapyPrefix
|import py.SeqConverters
|val len = py.Dynamic.global.len(List(0, 2, 3).toPythonProxy)
|println(s"Length is $$len")
|""".stripMargin
os.rel / "helloscalapy.sc" -> {
maybeDirective +
s"""$maybeScalapyPrefix
|import py.SeqConverters
|val len = py.Dynamic.global.len(List(0, 2, 3).toPythonProxy)
|println(s"Length is $$len")
|""".stripMargin
}
)

inputs.fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", extraOptions, ".", "--python").call(cwd = root)
val res = os.proc(TestUtil.cli, "run", extraOptions, ".", maybeCliArg).call(cwd = root)
val output = res.out.trim()
val expectedOutput = "Length is 3"
expect(output == expectedOutput)
}
}

test("scalapy from CLI") {
scalapyTest(useDirective = false)
}
test("scalapy via directive") {
scalapyTest(useDirective = true)
}

if (actualScalaVersion.startsWith("2.12."))
test("verify that Scala version 2.12.x < 2.12.4 is respected and compiles correctly") {
TestInputs(os.rel / "s.sc" -> "println(util.Properties.versionNumberString)").fromRoot {
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 @@ -180,6 +180,15 @@ Set contextual parameters for publishing

`//> using publish.secretKey "env:PUBLISH_SECRET_KEY"`

### Python

Enable Python support

`//> using python

#### Examples
`//> using python`

### Repository

Add a repository for dependency resolution
Expand Down