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

Feature parity with the Gradle plugin by #23 #24

Merged
merged 1 commit into from
Apr 14, 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
45 changes: 39 additions & 6 deletions src/main/scala/com/typelead/Etlas.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.typelead

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

import sbt.Keys._
import sbt._
import Keys._

import scala.collection.mutable.ArrayBuffer
import scala.sys.process.{Process, ProcessLogger}
import scala.util.Try

object Etlas {

Expand All @@ -29,13 +33,11 @@ object Etlas {

val logger = new ProcessLogger {
override def out(s: => String): Unit = {
lineBuffer += s
if (filterLog(s)) {
log.info(s)
}
if (saveOutput) lineBuffer += s
if (filterLog(s)) log.info(s)
}
override def err(s: => String): Unit = {
lineBuffer += s
if (saveOutput) lineBuffer += s
log.error(s)
}
override def buffer[T](s: => T): T = s
Expand All @@ -56,6 +58,25 @@ object Etlas {

}

private def fork(args: Seq[String], cwd: File, log: sbt.Logger): Unit = {

val logCmd = getParam("etlas.logger.cmd.level") match {
case Some("INFO") => Logger(log).info _
case _ => Logger(log).debug _
}
logCmd(s"Running `etlas ${args.mkString(" ")} in '$cwd'`...")

val jpb = new JProcessBuilder(("etlas" +: args).toArray: _ *)
jpb.directory(cwd)
jpb.redirectInput(Redirect.INHERIT)
val exitCode = Process(jpb).run(SbtUtils.terminalIO).exitValue()

if (exitCode != 0) {
sys.error("\n\n[etlas] Exit Failure " ++ exitCode.toString)
}

}

// Commands

private def withBuildDir(args: Seq[String], dist: File): Seq[String] = {
Expand Down Expand Up @@ -123,6 +144,18 @@ object Etlas {
etlas(Seq("install", "--dependencies-only"), cwd, log)
}

def repl(cwd: File, dist: File, log: sbt.Logger): Try[Unit] = {
def console0(): Unit = {
log.info("Starting Eta interpreter...")
fork(withBuildDir(Seq("repl"), dist), cwd, log)
}
Run.executeTrapExit(console0(), log).recover {
case _: InterruptedException =>
log.info("Eta REPL was interrupted.")
()
}
}

def run(cwd: File, dist: File, log: Logger): Unit = {
etlas(withBuildDir(Seq("run"), dist), cwd, log)
()
Expand Down
29 changes: 17 additions & 12 deletions src/main/scala/com/typelead/SbtEta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ object SbtEta extends AutoPlugin {
},

libraryDependencies := {
val deps = libraryDependencies.value

//Etlas.install(etaPackageDir.value, Logger(sLog.value))

deps ++
libraryDependencies.value ++
Etlas.getLibraryDependencies(etaPackageDir.value, Logger(sLog.value), Artifact.not(Artifact.testSuite)) ++
Etlas.getLibraryDependencies(etaPackageDir.value, Logger(sLog.value), Artifact.testSuite).map(_ % Test)
},
Expand All @@ -68,17 +64,15 @@ object SbtEta extends AutoPlugin {
(libraryDependencies in Compile).value
(etaCompile in Compile).value

val cp = (unmanagedJars in Compile).value

cp ++ Etlas.getFullClasspath(etaPackageDir.value, etaTarget.value, Logger(streams.value), Artifact.not(Artifact.testSuite))
(unmanagedJars in Compile).value ++
Etlas.getFullClasspath(etaPackageDir.value, etaTarget.value, Logger(streams.value), Artifact.not(Artifact.testSuite))
},
unmanagedJars in Test := {
(libraryDependencies in Compile).value
(etaCompile in Test).value

val cp = (unmanagedJars in Test).value

cp ++ Etlas.getFullClasspath(etaPackageDir.value, etaTarget.value, Logger(streams.value), Artifact.testSuite)
(unmanagedJars in Test).value ++
Etlas.getFullClasspath(etaPackageDir.value, etaTarget.value, Logger(streams.value), Artifact.testSuite)
},

compile in Test := {
Expand All @@ -99,7 +93,7 @@ object SbtEta extends AutoPlugin {

watchSources ++= ((etaSource in Compile).value ** "*").get,

commands += etaInitCommand
commands ++= Seq(etaInitCommand, etaReplCommand)
)

override def projectSettings: Seq[Def.Setting[_]] = baseEtaSettings
Expand Down Expand Up @@ -127,4 +121,15 @@ object SbtEta extends AutoPlugin {
}
}

private def etaReplCommand: Command = Command.command("eta-repl") { state =>
val extracted = Project.extract(state)
Etlas.repl(
extracted.get(etaPackageDir),
extracted.get(etaTarget),
extracted.get(sLog)
).get
println()
state
}

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

import java.io.OutputStream

import sbt.internal.util.JLine

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

object SbtUtils {

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

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

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

}