diff options
author | Nikolay Tatarinov <5min4eq.unity@gmail.com> | 2017-11-30 13:57:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-30 13:57:52 +0300 |
commit | b1df0b98612957b98aacabd5dbe38c98e748c660 (patch) | |
tree | 2404ae11cc3a2aae2674acb73a6a36522c0076e2 /scalaplugin | |
parent | 54f2af0a974e28b81026f503eff420fad2869d2f (diff) | |
parent | be7383f6440831d5961bf5ea56f10ec905174bfc (diff) | |
download | mill-b1df0b98612957b98aacabd5dbe38c98e748c660.tar.gz mill-b1df0b98612957b98aacabd5dbe38c98e748c660.tar.bz2 mill-b1df0b98612957b98aacabd5dbe38c98e748c660.zip |
Merge pull request #35 from lihaoyi/mainClass
Main class
Diffstat (limited to 'scalaplugin')
-rw-r--r-- | scalaplugin/src/main/scala/mill/scalaplugin/ScalaModule.scala | 10 | ||||
-rw-r--r-- | scalaplugin/src/test/scala/mill/scalaplugin/HelloWorldTests.scala | 53 |
2 files changed, 56 insertions, 7 deletions
diff --git a/scalaplugin/src/main/scala/mill/scalaplugin/ScalaModule.scala b/scalaplugin/src/main/scala/mill/scalaplugin/ScalaModule.scala index 6b67b708..c9cf4a49 100644 --- a/scalaplugin/src/main/scala/mill/scalaplugin/ScalaModule.scala +++ b/scalaplugin/src/main/scala/mill/scalaplugin/ScalaModule.scala @@ -224,6 +224,7 @@ trait ScalaModule extends Module with TaskModule{ outer => override def projectDeps = Seq(outer) } def scalaVersion: T[String] + def mainClass: T[Option[String]] = None def scalaBinaryVersion = T{ scalaVersion().split('.').dropRight(1).mkString(".") } def ivyDeps = T{ Seq[Dep]() } @@ -349,11 +350,16 @@ trait ScalaModule extends Module with TaskModule{ outer => def classpath = T{ Seq(resources(), compile()) } def jar = T{ val dest = T.ctx().dest - createJar(dest, Seq(resources(), compile()).map(_.path).filter(exists)) + createJar(dest, Seq(resources(), compile()).map(_.path).filter(exists), mainClass()) PathRef(dest) } - def run(mainClass: String) = T.command{ + def run() = T.command{ + val main = mainClass().getOrElse(throw new RuntimeException("No mainClass provided!")) + subprocess(main, runDepClasspath().map(_.path) :+ compile().path) + } + + def runMain(mainClass: String) = T.command{ subprocess(mainClass, runDepClasspath().map(_.path) :+ compile().path) } diff --git a/scalaplugin/src/test/scala/mill/scalaplugin/HelloWorldTests.scala b/scalaplugin/src/test/scala/mill/scalaplugin/HelloWorldTests.scala index 03b9b285..c2f5a7b8 100644 --- a/scalaplugin/src/test/scala/mill/scalaplugin/HelloWorldTests.scala +++ b/scalaplugin/src/test/scala/mill/scalaplugin/HelloWorldTests.scala @@ -17,6 +17,10 @@ trait HelloWorldModule extends ScalaModule { object HelloWorld extends HelloWorldModule +object HelloWorldWithMain extends HelloWorldModule { + override def mainClass = Some("Main") +} + object HelloWorldWarnUnused extends HelloWorldModule { override def scalacOptions = T(Seq("-Ywarn-unused")) } @@ -36,6 +40,7 @@ object HelloWorldTests extends TestSuite { TestEvaluator.eval(mapping, outputPath)(t) val helloWorldMapping = Discovered.mapping(HelloWorld) + val helloWorldWithMainMapping = Discovered.mapping(HelloWorldWithMain) def tests: Tests = Tests { prepareWorkspace() @@ -142,10 +147,10 @@ object HelloWorldTests extends TestSuite { assert(err.isInstanceOf[CompileFailed]) } } - 'run - { + 'runMain - { 'runMainObject - { val Right((_, evalCount)) = - eval(HelloWorld.run("Main"), helloWorldMapping) + eval(HelloWorld.runMain("Main"), helloWorldMapping) assert(evalCount > 0) @@ -157,7 +162,7 @@ object HelloWorldTests extends TestSuite { } 'notRunInvalidMainObject - { val Left(Result.Exception(err)) = - eval(HelloWorld.run("Invalid"), helloWorldMapping) + eval(HelloWorld.runMain("Invalid"), helloWorldMapping) assert( err.isInstanceOf[InteractiveShelloutException] @@ -167,13 +172,35 @@ object HelloWorldTests extends TestSuite { write.append(mainObject, "val x: ") val Left(Result.Exception(err)) = - eval(HelloWorld.run("Main"), helloWorldMapping) + eval(HelloWorld.runMain("Main"), helloWorldMapping) assert( err.isInstanceOf[CompileFailed] ) } } + 'run - { + 'runIfMainClassProvided - { + val Right((_, evalCount)) = + eval(HelloWorldWithMain.run(), helloWorldWithMainMapping) + + assert(evalCount > 0) + + val runResult = workspacePath / "hello-mill" + assert( + exists(runResult), + read(runResult) == "hello rockjam, your age is: 25" + ) + } + 'notRunWithoutMainClass - { + val Left(Result.Exception(err)) = + eval(HelloWorld.run(), helloWorldMapping) + + assert( + err.isInstanceOf[RuntimeException] + ) + } + } 'jar - { 'nonEmpty - { val Right((result, evalCount)) = @@ -200,7 +227,23 @@ object HelloWorldTests extends TestSuite { jarFiles.forall(expectedFiles.contains) ) } - // TODO: check that we can `java -jar` produced jar + 'runJar - { + val Right((result, evalCount)) = + eval(HelloWorldWithMain.jar, helloWorldWithMainMapping) + + assert( + exists(result.path), + evalCount > 0 + ) + + %("scala", result.path) + + val runResult = workspacePath / "hello-mill" + assert( + exists(runResult), + read(runResult) == "hello rockjam, your age is: 25" + ) + } } } |