Skip to content

Commit

Permalink
add @main entrypoint support for Scala 3
Browse files Browse the repository at this point in the history
mainargs 0.2.3 [added support for scala3](com-lihaoyi/mainargs#18) 🎉

To test (manually):

test.sc:
```scala
@main def main(name: String) = {
  println(s"Hello, $name!")
}
```

```bash
mill -i "amm[3.2.0].assembly" && out/amm/3.2.0/assembly.dest/out.jar test.sc --name Michael
mill -i "amm[2.13.9].assembly" && out/amm/2.13.9/assembly.dest/out.jar test.sc --name Michael
```
  • Loading branch information
mpollmeier committed Oct 7, 2022
1 parent 1c7b3c3 commit e4f3ddf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
7 changes: 1 addition & 6 deletions amm/src/main/scala/ammonite/main/Scripts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ object Scripts {
scriptArgs: Seq[String] = Nil) = {
interp.watch(path)
val (pkg, wrapper) = Util.pathToPackageWrapper(Seq(), path relativeTo wd)


val genRoutesCode =
// Entrypoints are not supported in Scala 3 for now (its macros are Scala 2-only)
if (interp.compilerBuilder.scalaVersion.startsWith("3.")) "null"
else "mainargs.ParserForMethods[$routesOuter.type]($routesOuter)"
val genRoutesCode = "mainargs.ParserForMethods[$routesOuter.type]($routesOuter)"

for{
scriptTxt <- try Res.Success(Util.normalizeNewlines(os.read(path))) catch{
Expand Down
25 changes: 17 additions & 8 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ object Deps {
val jlineReader = ivy"org.jline:jline-reader:3.14.1"
val jlineTerminal = ivy"org.jline:jline-terminal:3.14.1"
val jsch = ivy"com.jcraft:jsch:0.1.54"
val mainargs = ivy"com.lihaoyi::mainargs:0.2.2"
val mainargs = ivy"com.lihaoyi::mainargs:0.3.0"
val osLib = ivy"com.lihaoyi::os-lib:0.8.0"
val pprint = ivy"com.lihaoyi::pprint:0.7.3"
val requests = ivy"com.lihaoyi::requests:0.7.0"
Expand Down Expand Up @@ -180,9 +180,17 @@ trait AmmInternalModule extends CrossSbtModule with Bloop.Module{
acyclicOptions ++ tastyReaderOptions
}
def compileIvyDeps = T {
if (isScala2()) Agg(Deps.acyclic)
else Agg[Dep]()
Agg(Deps.mainargs) ++ (
if (isScala2()) Agg(Deps.acyclic)
else Agg[Dep]()
)
}
def runIvyDeps =
if (scala3Versions.contains(crossScalaVersion))
Agg(ivy"com.lihaoyi:mainargs_3:${Deps.mainargs.dep.version}")
else
Agg(Deps.mainargs)

def scalacPluginIvyDeps = T {
if (isScala2()) Agg(Deps.acyclic)
else Agg[Dep]()
Expand Down Expand Up @@ -378,8 +386,7 @@ object amm extends Cross[MainModule](fullCrossScalaVersions:_*){
def crossFullScalaVersion = true
def ivyDeps = Agg(
Deps.upickle,
Deps.requests,
withDottyCompat(Deps.mainargs, scalaVersion())
Deps.requests
)
}

Expand Down Expand Up @@ -485,9 +492,7 @@ object amm extends Cross[MainModule](fullCrossScalaVersions:_*){
def crossFullScalaVersion = true
def dependencyResourceFileName = "amm-dependencies.txt"
def moduleDeps = Seq(amm.util(), interp.api())
def ivyDeps = Agg(
withDottyCompat(Deps.mainargs, scalaVersion())
)
def compileIvyDeps = Agg(Deps.mainargs)

def generatedSources = T{
Seq(PathRef(generateConstantsFile(buildVersion, bspVersion = bspVersion)))
Expand Down Expand Up @@ -561,6 +566,10 @@ object amm extends Cross[MainModule](fullCrossScalaVersions:_*){
def mainClass = Some("ammonite.AmmoniteMain")
def moduleDeps = Seq(amm())
}

def compileIvyDeps = Agg(
Deps.mainargs
)
}

trait PatchScala3Library extends JavaModule {
Expand Down

0 comments on commit e4f3ddf

Please sign in to comment.