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

#23 Compatibility with sbt 0.13.x #29

Merged
merged 5 commits into from
Apr 28, 2019
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
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ An example of multi-project build included Eta projects is provided in the [exam

1. Create a standard Scala/SBT project using your favorite method.

2. Initialize new Eta project one of the following ways:
2. Describe Eta project in `build.sbt` using plugin's DSL.

1. Describe Eta project using plugins DSL (preferred method)

2. Automatically with sbt command:

* Run `eta-init` command in `sbt`, e.g. `sbt eta-init`.

4. Write Scala/Eta code to perform your task.
3. Write Scala/Eta code to perform your task.

5. Start up `sbt` and run the `compile` and/or `run` task.
4. Start up `sbt` and run the `compile` and/or `run` task.

## Configuration

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val root = (project in file(".")).
settings(
inThisBuild(Seq(
version := "0.3.0",
organization := "com.typelead",
organization := "com.typelead"
)),
name := "sbt-eta",
sbtPlugin := true,
Expand Down
2 changes: 1 addition & 1 deletion example-multi/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val hio = (project in file("hio"))
.settings(
name := "hio",
version := "0.1.0-SNAPSHOT",
exposedModules in EtaLib += "Hello.Mod",
modules in EtaLib += exposed("Hello.Mod"),
libraryDependencies in EtaLib ++= Seq(
eta("text", "1.2.3.0"),
eta("bytestring", "0.10.8.2")
Expand Down
2 changes: 1 addition & 1 deletion example-multi/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.8
sbt.version=0.13.18
3 changes: 2 additions & 1 deletion example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ lazy val root = (project in file(".")).
version := "0.1.0-SNAPSHOT"
)),
name := "example",
exposedModules in EtaLib += "Example.Transform",
modules in EtaLib += exposed("Example.Transform"),
libraryDependencies in EtaLib ++= Seq(
eta("aeson"),
eta("lens-aeson"),
eta("lens"),
eta("text"),
"com.google.guava" % "guava" % "25.0-jre"
),
resolvers in EtaLib ++= Seq(Resolver.jcenterRepo, Resolver.sonatypeRepo("public")),
gitDependencies in EtaLib ++= Seq(
git("eta-spark-core", "https://github.com/Jyothsnasrinivas/eta-spark-core", branch("master"))
),
Expand Down
38 changes: 38 additions & 0 deletions src/main/scala-sbt-0.13/sbt/SbtUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sbt

import java.lang.ProcessBuilder.Redirect
import java.lang.{ProcessBuilder => JProcessBuilder}
import java.io.OutputStream

import scala.util.{Failure, Success, Try}

object SbtUtils {

def download(url: URL, file: File): Unit = IO.download(url, file)

def executeTrapExit(f: => Unit, log: Logger): Try[Unit] = {
Run.executeTrapExit(f, log).map(msg => Failure(new MessageOnlyException(msg))).getOrElse(Success(()))
}

def execInherited(command: Seq[String], cwd: File): Int = {
val jpb = new JProcessBuilder(command: _ *)
jpb.directory(cwd)
jpb.redirectInput(Redirect.INHERIT)
Process(jpb).run(SbtUtils.terminalIO).exitValue()
}

def runInTerminal(cmd: => Unit, log: Logger): Try[Unit] = {
JLine.usingTerminal { t =>
t.init()
executeTrapExit(cmd, log)
}
}

private def terminalIO: ProcessIO = BasicIO.standard(SbtUtils.inTerminal, _ => true)

private def inTerminal: OutputStream => Unit = { out =>
try { BasicIO.transferFully(JLine.createReader().getInput, out) }
catch { case _: InterruptedException => () }
}

}
44 changes: 44 additions & 0 deletions src/main/scala-sbt-1.0/sbt/SbtUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sbt

import java.lang.ProcessBuilder.Redirect
import java.lang.{ProcessBuilder => JProcessBuilder}
import java.io.OutputStream

import sbt.internal.util.JLine
import sbt.io.Using

import scala.sys.process.{BasicIO, ProcessIO, Process}
import scala.util.Try

object SbtUtils {

def download(url: URL, file: File): Unit = {
Using.urlInputStream(url) { input =>
IO.transfer(input, file)
}
}

def executeTrapExit(f: => Unit, log: Logger): Try[Unit] = Run.executeTrapExit(f, log)

def execInherited(command: Seq[String], cwd: File): Int = {
val jpb = new JProcessBuilder(command: _ *)
jpb.directory(cwd)
jpb.redirectInput(Redirect.INHERIT)
Process(jpb).run(SbtUtils.terminalIO).exitValue()
}

def runInTerminal(cmd: => Unit, log: Logger): Try[Unit] = {
JLine.usingTerminal { t =>
t.init()
executeTrapExit(cmd, log)
}
}

private def terminalIO: ProcessIO = BasicIO.standard(SbtUtils.inTerminal)

private def inTerminal: OutputStream => Unit = { out =>
try { BasicIO.transferFully(JLine.createReader().getInput, out) }
catch { case _: InterruptedException => () }
}

}
47 changes: 38 additions & 9 deletions src/main/scala/com/typelead/Cabal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final case class Cabal(projectName: String,

def buildDependencies: Seq[String] = artifacts.flatMap(_.buildDependencies).distinct
def mavenDependencies: Seq[String] = artifacts.flatMap(_.mavenDependencies).distinct
def mavenRepositories: Seq[String] = artifacts.flatMap(_.mavenRepositories).distinct
def gitDependencies: Seq[GitDependency] = distinctBy(artifacts.flatMap(_.gitDependencies))(_.packageName)

def hasLibrary : Boolean = projectLibrary.nonEmpty
Expand All @@ -64,16 +65,18 @@ final case class Cabal(projectName: String,
def getTmpCabal(etaPackages: Seq[EtaPackage]): Cabal = {
val allBuildDependencies = (buildDependencies ++ etaPackages.flatMap(_.cabal.buildDependencies)).distinct
val allMavenDependencies = (mavenDependencies ++ etaPackages.flatMap(_.cabal.mavenDependencies)).distinct
val allMavenRepositories = (mavenRepositories ++ etaPackages.flatMap(_.cabal.mavenRepositories)).distinct
val allGitDependencies = distinctBy(gitDependencies ++ etaPackages.flatMap(_.cabal.gitDependencies))(_.packageName)
Cabal(
projectName = projectName,
projectVersion = projectVersion,
projectLibrary = Some(Library(
name = projectName,
sourceDirectories = Nil,
exposedModules = Nil,
modules = Nil,
buildDependencies = allBuildDependencies,
mavenDependencies = allMavenDependencies,
mavenRepositories = allMavenRepositories,
gitDependencies = allGitDependencies,
cppOptions = Nil,
ghcOptions = Nil,
Expand Down Expand Up @@ -106,6 +109,12 @@ object Cabal {
testSuites = Nil
)

sealed trait Module {
def name: String
}
final case class ExposedModule(override val name: String) extends Module
final case class OtherModule(override val name: String) extends Module

object TestSuiteTypes extends Enumeration {
val exitcode: Value = Value("exitcode-stdio-1.0")
val detailed: Value = Value("detailed-0.9")
Expand All @@ -115,9 +124,10 @@ object Cabal {
def name: String
def depsPackage: String
def sourceDirectories: Seq[String]
def exposedModules: Seq[String]
def modules: Seq[Module]
def buildDependencies: Seq[String]
def mavenDependencies: Seq[String]
def mavenRepositories: Seq[String]
def gitDependencies: Seq[GitDependency]
def hsMain: Option[String]
def cppOptions: Seq[String]
Expand All @@ -127,14 +137,17 @@ object Cabal {
def extensions: Seq[String]
def language: String

def exposedModules: Seq[Module]
def otherModules: Seq[Module]
def addLibrary(artifact: Option[Library]): A
}

final case class Library(override val name: String,
override val sourceDirectories: Seq[String],
override val exposedModules: Seq[String],
override val modules: Seq[Module],
override val buildDependencies: Seq[String],
override val mavenDependencies: Seq[String],
override val mavenRepositories: Seq[String],
override val gitDependencies: Seq[GitDependency],
override val cppOptions: Seq[String],
override val ghcOptions: Seq[String],
Expand All @@ -145,15 +158,23 @@ object Cabal {

override val depsPackage: String = "lib:" + name
override val hsMain: Option[String] = None
val exposedModules: Seq[Module] = modules.collect {
case exposed: ExposedModule => exposed
}
val otherModules: Seq[Module] = modules.collect {
case other: OtherModule => other
}

override def addLibrary(artifact: Option[Library]): Library = this

}

final case class Executable(override val name: String,
override val sourceDirectories: Seq[String],
override val modules: Seq[Module],
override val buildDependencies: Seq[String],
override val mavenDependencies: Seq[String],
override val mavenRepositories: Seq[String],
override val gitDependencies: Seq[GitDependency],
override val hsMain: Option[String],
override val cppOptions: Seq[String],
Expand All @@ -164,16 +185,19 @@ object Cabal {
override val language: String) extends Artifact[Executable] {

override val depsPackage: String = "exe:" + name
override val exposedModules: Seq[String] = Nil
override val exposedModules: Seq[Module] = Nil
override val otherModules: Seq[Module] = modules

override def addLibrary(artifact: Option[Library]): Executable = this.copy(buildDependencies = buildDependencies ++ artifact.map(_.name).toList)

}

final case class TestSuite(override val name: String,
override val sourceDirectories: Seq[String],
override val modules: Seq[Module],
override val buildDependencies: Seq[String],
override val mavenDependencies: Seq[String],
override val mavenRepositories: Seq[String],
override val gitDependencies: Seq[GitDependency],
override val hsMain: Option[String],
override val cppOptions: Seq[String],
Expand All @@ -185,7 +209,8 @@ object Cabal {
testSuiteType: TestSuiteTypes.Value) extends Artifact[TestSuite] {

override val depsPackage: String = "test:" + name
override val exposedModules: Seq[String] = Nil
override val exposedModules: Seq[Module] = Nil
override val otherModules: Seq[Module] = modules

override def addLibrary(artifact: Option[Library]): TestSuite = this.copy(buildDependencies = buildDependencies ++ artifact.map(_.name).toList)

Expand All @@ -195,9 +220,9 @@ object Cabal {

type Filter = Artifact[_] => Boolean

def lib(name: String) : Library = Library (name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, "Haskell2010")
def exe(name: String) : Executable = Executable(name, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil, Nil, Nil, "Haskell2010")
def test(name: String): TestSuite = TestSuite (name, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil, Nil, Nil, "Haskell2010", TestSuiteTypes.exitcode)
def lib(name: String) : Library = Library (name, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil, "Haskell2010")
def exe(name: String) : Executable = Executable(name, Nil, Nil, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil, Nil, Nil, "Haskell2010")
def test(name: String): TestSuite = TestSuite (name, Nil, Nil, Nil, Nil, Nil, Nil, None, Nil, Nil, Nil, Nil, Nil, "Haskell2010", TestSuiteTypes.exitcode)

val all: Filter = _ => true
val library: Filter = {
Expand Down Expand Up @@ -285,12 +310,16 @@ object Cabal {
artifact.hsMain.map(m => getMainTag(artifact) + m).toList ++
writeLines(artifact.sourceDirectories,
" hs-source-dirs: ", " , ") ++
writeLines(artifact.exposedModules ,
writeLines(artifact.exposedModules.map(_.name),
" exposed-modules: ", " , ") ++
writeLines(artifact.otherModules.map(_.name),
" other-modules: ", " , ") ++
writeLines(artifact.buildDependencies ++ artifact.gitDependencies.map(_.packageName) ++ etaPackages.map(_.name).distinct,
" build-depends: ", " , ") ++
writeLines(artifact.mavenDependencies,
" maven-depends: ", " , ") ++
writeLines(artifact.mavenRepositories.filter(_ => artifact.mavenDependencies.nonEmpty),
" maven-repos: ", " , ") ++
writeLines(Some(artifact.cppOptions).filter(_.nonEmpty).map(_.mkString(" ")),
" cpp-options: ", " , ") ++
writeLines(Some(artifact.ghcOptions).filter(_.nonEmpty).map(_.mkString(" ")),
Expand Down
Loading