Skip to content
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
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 2.4.2
version = 2.5.1
style = defaultWithAlign
maxColumn = 100

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addCommandAlias("ci-test", "scalafmtCheck; scalafmtSbtCheck; test")
addCommandAlias("ci-test", "scalafmtCheckAll; scalafmtSbtCheck; test")
addCommandAlias("ci-docs", "github; project-docs/mdoc; headerCreateAll")

Universal / javaOptions += "-Dscala.classpath.closeZip=true"
Expand Down
6 changes: 3 additions & 3 deletions project/ProjectPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ object ProjectPlugin extends AutoPlugin {
"org.typelevel" %% "cats-effect" % V.catsEffect,
"io.circe" %% "circe-core" % V.circe,
"io.circe" %% "circe-generic" % V.circe,
"org.slf4j" % "slf4j-simple" % V.slf4j,
"org.slf4j" % "slf4j-simple" % V.slf4j,
"org.http4s" %% "http4s-dsl" % V.http4s,
"org.http4s" %% "http4s-blaze-server" % V.http4s,
"org.http4s" %% "http4s-circe" % V.http4s,
"io.get-coursier" %% "coursier" % V.coursier,
"io.get-coursier" %% "coursier-cache" % V.coursier,
"com.typesafe" % "config" % V.config,
"com.typesafe" % "config" % V.config,
"com.pauldijou" %% "jwt-core" % V.jwtCore,
"io.get-coursier" %% "coursier-cats-interop" % V.coursier,
"org.scalatest" %% "scalatest" % V.scalatest,
"org.scalatestplus" %% "scalacheck-1-14" % V.scalatestplusScheck,
"joda-time" % "joda-time" % V.jodaTime
"joda-time" % "joda-time" % V.jodaTime
)
)

Expand Down
24 changes: 12 additions & 12 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.5.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.3")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.4")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.1.5")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")
addSbtPlugin("com.alejandrohdezma" %% "sbt-github" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-github-header" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-github-mdoc" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-mdoc-toc" % "0.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.5.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.3")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.4")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.1.5")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")
addSbtPlugin("com.alejandrohdezma" %% "sbt-github" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-github-header" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-github-mdoc" % "0.8.0")
addSbtPlugin("com.alejandrohdezma" % "sbt-mdoc-toc" % "0.2")
24 changes: 11 additions & 13 deletions server/src/main/scala/org/scalaexercises/evaluator/auth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ object auth {

val SecretKeyPath = "eval.auth.secretKey"

val secretKey = if (config.hasPath(SecretKeyPath)) {
config.getString(SecretKeyPath)
} else {
throw new IllegalStateException(
"Missing -Deval.auth.secretKey=[YOUR_KEY_HERE] or env var [EVAL_SECRET_KEY] "
)
}
val secretKey =
if (config.hasPath(SecretKeyPath))
config.getString(SecretKeyPath)
else {
throw new IllegalStateException(
"Missing -Deval.auth.secretKey=[YOUR_KEY_HERE] or env var [EVAL_SECRET_KEY] "
)
}

def generateToken(value: String = "{}") =
Jwt.encode(value, secretKey, JwtAlgorithm.HS256)
Expand Down Expand Up @@ -69,22 +70,19 @@ object auth {
def apply[F[_]: Sync](service: HttpApp[F]): HttpApp[F] =
HttpRoutes
.of[F] {
case req if req.headers.nonEmpty => {
case req if req.headers.nonEmpty =>
req.headers.get(`X-Scala-Eval-Api-Token`) match {
case Some(header) =>
Jwt.decodeRaw(header.token, secretKey, Seq(JwtAlgorithm.HS256)) match {
case Success(tokenIdentity) => {
case Success(tokenIdentity) =>
logger.info(s"Auth success with identity : $tokenIdentity")
service(req)
}
case Failure(ex) => {
case Failure(ex) =>
logger.warn(s"Auth failed : $ex")
Sync[F].pure(Response(Status.Unauthorized))
}
}
case None => Sync[F].pure(Response(Status.Unauthorized))
}
}
case _ => Sync[F].pure(Response(Status.Unauthorized))
}
.orNotFound
Expand Down
84 changes: 39 additions & 45 deletions server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import scala.tools.nsc.{Global, Settings}
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)(
implicit F: ConcurrentEffect[F],
class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)(implicit
F: ConcurrentEffect[F],
T: Timer[F]
) {
type Remote = String
Expand Down Expand Up @@ -188,19 +188,17 @@ private class StringCompiler(
}
// the line number is not always available
val lineMessage =
try {
"line " + (pos.line - lineOffset)
} catch {
try "line " + (pos.line - lineOffset)
catch {
case _: Throwable => ""
}
messages += (severityName + lineMessage + ": " + message) ::
(if (pos.isDefined) {
pos.finalPosition.lineContent.stripLineEnd ::
(" " * (pos.column - 1) + "^") ::
Nil
} else {
Nil
})
} else
Nil)
}

override def reset = {
Expand All @@ -213,16 +211,13 @@ private class StringCompiler(

def reset() = {
targetDir match {
case None => {
case None =>
output.asInstanceOf[VirtualDirectory].clear()
}
case Some(t) => {
case Some(t) =>
output.foreach { abstractFile =>
if (abstractFile.file == null || abstractFile.file.getName.endsWith(".class")) {
if (abstractFile.file == null || abstractFile.file.getName.endsWith(".class"))
abstractFile.delete()
}
}
}
}
global.cleanup
cache.clear()
Expand Down Expand Up @@ -303,9 +298,8 @@ private class StringCompiler(
*/
class Eval(target: Option[File] = None, jars: List[File] = Nil) {
private lazy val compilerPath =
try {
classPathOfClass("scala.tools.nsc.Interpreter")
} catch {
try classPathOfClass("scala.tools.nsc.Interpreter")
catch {
case e: Throwable =>
throw new RuntimeException(
"Unable to load Scala interpreter from classpath (scala-compiler jar is missing?)",
Expand All @@ -314,9 +308,8 @@ class Eval(target: Option[File] = None, jars: List[File] = Nil) {
}

private lazy val libPath =
try {
classPathOfClass("scala.AnyVal")
} catch {
try classPathOfClass("scala.AnyVal")
catch {
case e: Throwable =>
throw new RuntimeException(
"Unable to load scala base object from classpath (scala-library jar is missing?)",
Expand Down Expand Up @@ -445,31 +438,32 @@ class ${className} extends (() => Any) with java.io.Serializable {
val classPath = getClassPath(this.getClass.getClassLoader)
val currentClassPath = classPath.head

def checkCurrentClassPath: List[String] = currentClassPath match {
case List(jarFile) if jarFile.endsWith(".jar") =>
val relativeRoot = new File(jarFile).getParentFile
val jarResource: Resource[IO, JarFile] =
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))

val nestedClassPath: IO[String] =
jarResource
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
.handleError {
case scala.util.control.NonFatal(e) =>
throw new CompilerException(List(List(e.getMessage)))
}

nestedClassPath.map {
case ncp if ncp eq null => Nil
case ncp =>
ncp
.split(" ")
.map(f => new File(relativeRoot, f).getAbsolutePath)
.toList

}.unsafeRunSync
case _ => Nil
}
def checkCurrentClassPath: List[String] =
currentClassPath match {
case List(jarFile) if jarFile.endsWith(".jar") =>
val relativeRoot = new File(jarFile).getParentFile
val jarResource: Resource[IO, JarFile] =
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))

val nestedClassPath: IO[String] =
jarResource
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
.handleError {
case scala.util.control.NonFatal(e) =>
throw new CompilerException(List(List(e.getMessage)))
}

nestedClassPath.map {
case ncp if ncp eq null => Nil
case ncp =>
ncp
.split(" ")
.map(f => new File(relativeRoot, f).getAbsolutePath)
.toList

}.unsafeRunSync
case _ => Nil
}

// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
Expand Down
22 changes: 12 additions & 10 deletions server/src/test/scala/org/scalaexercises/evaluator/helper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ object helper {

case object Scala213 extends ScalaVersion("2.13.1")

def toScalaVersion(v: String): ScalaVersion = v match {
case version if version.startsWith("2.13") => Scala213
case _ => throw new IllegalArgumentException(s"Unknown Scala Version $v")
}
def toScalaVersion(v: String): ScalaVersion =
v match {
case version if version.startsWith("2.13") => Scala213
case _ => throw new IllegalArgumentException(s"Unknown Scala Version $v")
}

val commonResolvers = List(
"https://oss.sonatype.org/content/repositories/snapshots",
"https://oss.sonatype.org/content/repositories/public",
"http://repo1.maven.org/maven2"
)

def scalaDependencies(scala: ScalaVersion): List[Dependency] = List(
Dependency("org.scala-lang", s"scala-library", s"${scala.version}"),
Dependency("org.scala-lang", s"scala-reflect", s"${scala.version}"),
Dependency("org.scala-lang", s"scala-compiler", s"${scala.version}"),
Dependency("org.scala-lang.modules", s"scala-xml_${scala.version.substring(0, 4)}", "1.2.0")
)
def scalaDependencies(scala: ScalaVersion): List[Dependency] =
List(
Dependency("org.scala-lang", s"scala-library", s"${scala.version}"),
Dependency("org.scala-lang", s"scala-reflect", s"${scala.version}"),
Dependency("org.scala-lang", s"scala-compiler", s"${scala.version}"),
Dependency("org.scala-lang.modules", s"scala-xml_${scala.version.substring(0, 4)}", "1.2.0")
)

def circeLibraryDependencies(scala: ScalaVersion): List[Dependency] = {
val sv = scala.version
Expand Down