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

Add scala native support! #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
89 changes: 58 additions & 31 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ inThisBuild(
Resolver.sonatypeRepo("releases"),
Resolver.sonatypeRepo("snapshots")
),
libraryDependencies ++= List(
munit.value % Test,
scalacheck % Test,
scalametaTestkit % Test
),
testFrameworks += new TestFramework("munit.Framework")
)
)
Expand All @@ -64,7 +59,7 @@ commands += Command.command("ci-test") { s =>
s
}

lazy val dynamic = project
lazy val dynamic = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-dynamic"))
.settings(
moduleName := "scalafmt-dynamic",
Expand All @@ -76,14 +71,24 @@ lazy val dynamic = project
"io.get-coursier" % "interface" % "0.0.17",
"com.typesafe" % "config" % "1.4.1",
munit.value % Test,
scalametaTestkit % Test
scalametaTestkit.value % Test
),
scalacOptions ++= scalacJvmOptions.value
)
.nativeSettings(
libraryDependencies ++= List(
// "org.ekrich" %%% "sconfig" % "x.y.z",
"org.ekrich" %%% "sjavatime" % "1.1.5"
),
nativeLinkStubs := true
)
.dependsOn(interfaces)
.enablePlugins(BuildInfoPlugin)

lazy val interfaces = project
lazy val dynamicJVM = dynamic.jvm
lazy val dynamicNative = dynamic.native

lazy val interfaces = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-interfaces"))
.settings(
moduleName := "scalafmt-interfaces",
Expand All @@ -100,14 +105,16 @@ lazy val interfaces = project
}
)

lazy val core = crossProject(JVMPlatform)
lazy val interfacesJVM = interfaces.jvm
lazy val interfacesNative = interfaces.native

lazy val core = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-core"))
.settings(
moduleName := "scalafmt-core",
buildInfoSettings,
scalacOptions ++= scalacJvmOptions.value,
libraryDependencies ++= Seq(
metaconfig.value,
scalameta.value,
// scala-reflect is an undeclared dependency of fansi, see #1252.
// Scalafmt itself does not require scala-reflect.
Expand All @@ -128,21 +135,23 @@ lazy val core = crossProject(JVMPlatform)
}
}
)
// .jsSettings(
// libraryDependencies ++= List(
// metaconfigHocon.value,
// scalatest.value % Test // must be here for coreJS/test to run anything
// )
// )
.nativeSettings(
libraryDependencies ++= List(
metaconfigSconfig.value
),
nativeLinkStubs := true
)
.jvmSettings(
Test / run / fork := true,
libraryDependencies ++= List(
metaconfigTypesafe.value
metaconfigTypesafe.value,
metaconfig.value
)
)
.enablePlugins(BuildInfoPlugin)

lazy val coreJVM = core.jvm
// lazy val coreJS = core.js
lazy val coreNative = core.native

import sbtassembly.AssemblyPlugin.defaultUniversalScript

Expand All @@ -160,7 +169,7 @@ val scalacJvmOptions = Def.setting {
}
}

lazy val cli = project
lazy val cli = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-cli"))
.settings(
moduleName := "scalafmt-cli",
Expand All @@ -175,9 +184,8 @@ lazy val cli = project
oldStrategy(x)
},
libraryDependencies ++= Seq(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"com.github.scopt" %% "scopt" % "4.0.1",
// "com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"com.github.scopt" %%% "scopt" % "4.0.1",
// undeclared transitive dependency of coursier-small
"org.scala-lang.modules" %% "scala-xml" % "1.3.0"
),
Expand All @@ -196,36 +204,55 @@ lazy val cli = project
.toSeq
}
)
.dependsOn(coreJVM, dynamic)
.jvmSettings {
libraryDependencies += "com.martiansoftware" % "nailgun-server" % "0.9.1",
}
.nativeSettings {
nativeLinkStubs := true
}
.dependsOn(core, dynamic)
.enablePlugins(GraalVMNativeImagePlugin)

lazy val tests = project
lazy val cliJVM = cli.jvm
lazy val cliNative = cli.native

lazy val tests = crossProject(JVMPlatform, NativePlatform)
.in(file("scalafmt-tests"))
.settings(
publish / skip := true,
libraryDependencies ++= Seq(
// Test dependencies
"com.lihaoyi" %% "scalatags" % "0.9.4",
scalametaTestkit,
"com.lihaoyi" %%% "scalatags" % "0.9.4",
scalametaTestkit.value,
munit.value
),
scalacOptions ++= scalacJvmOptions.value,
javaOptions += "-Dfile.encoding=UTF8",
buildInfoPackage := "org.scalafmt.tests",
buildInfoKeys := Seq[BuildInfoKey](
"resourceDirectory" -> (Test / resourceDirectory).value
"resourceDirectory" -> (baseDirectory.value / ".." / "shared" / "src" / "test" / "resources")
)
)
.enablePlugins(BuildInfoPlugin)
.dependsOn(coreJVM, dynamic, cli)
.dependsOn(core, dynamic, cli)
.nativeSettings(
nativeConfig ~= {
_.withMode(scalanative.build.Mode.debug)
.withLinkStubs(true)
}
).jvmSettings(
javaOptions += "-Dfile.encoding=UTF8"
)

lazy val testsJVM = tests.jvm
lazy val testsNative = tests.native

lazy val benchmarks = project
.in(file("scalafmt-benchmarks"))
.settings(
publish / skip := true,
moduleName := "scalafmt-benchmarks",
libraryDependencies ++= Seq(
scalametaTestkit
scalametaTestkit.value
),
run / javaOptions ++= Seq(
"-Djava.net.preferIPv4Stack=true",
Expand Down Expand Up @@ -254,7 +281,7 @@ lazy val docs = project
publish / skip := true,
mdoc := (Compile / run).evaluated
)
.dependsOn(cli, dynamic)
.dependsOn(cliJVM, dynamicJVM)
.enablePlugins(DocusaurusPlugin)

val V = "\\d+\\.\\d+\\.\\d+"
Expand Down
5 changes: 3 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._

object Dependencies {
val metaconfigV = "0.9.13"
val scalametaV = "4.4.19"
val scalametaV = "4.4.22"
val scalacheckV = "1.15.4"
val coursier = "1.0.3"
val munitV = "0.7.26"
Expand All @@ -18,13 +18,14 @@ object Dependencies {
)
}

val scalametaTestkit = "org.scalameta" %% "testkit" % scalametaV
val scalametaTestkit = Def.setting("org.scalameta" %%% "testkit" % scalametaV)

val scalacheck = "org.scalacheck" %% "scalacheck" % scalacheckV
val munit = Def.setting("org.scalameta" %%% "munit" % munitV)
val scalameta = Def.setting("org.scalameta" %%% "scalameta" % scalametaV excludeAll scalapb.value)

val metaconfig = Def.setting("com.geirsson" %%% "metaconfig-core" % metaconfigV)
val metaconfigSconfig = Def.setting("com.geirsson" %%% "metaconfig-sconfig" % metaconfigV)
val metaconfigTypesafe = Def.setting("com.geirsson" %%% "metaconfig-typesafe-config" % metaconfigV)
val metaconfigHocon = Def.setting("com.geirsson" %%% "metaconfig-hocon" % metaconfigV)

Expand Down
3 changes: 3 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.1")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.9.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.8.1")

addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.0.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0")
31 changes: 31 additions & 0 deletions scalafmt-cli/jvm/src/main/scala/org/scalafmt/cli/CliUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.scalafmt.cli

import com.martiansoftware.nailgun.NGContext
import org.scalafmt.util.AbsoluteFile

trait CliUtils { this: Cli =>

def nailMain(nGContext: NGContext): Unit = {
val workingDirectory =
AbsoluteFile.fromPath(nGContext.getWorkingDirectory).getOrElse {
throw new IllegalStateException(
s"Expected absolute path, " +
s"obtained nGContext.getWorkingDirectory = ${nGContext.getWorkingDirectory}"
)
}
val exit = mainWithOptions(
nGContext.getArgs,
CliOptions.default.copy(
common = CliOptions.default.common.copy(
cwd = workingDirectory,
out = nGContext.out,
in = nGContext.in,
err = nGContext.err
)
)
)
nGContext.exit(exit.code)
}

protected val isNative: Boolean = false
}
12 changes: 12 additions & 0 deletions scalafmt-cli/jvm/src/main/scala/org/scalafmt/cli/TermUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalafmt.cli

import java.sql.Timestamp

trait TermUtils {

private val format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
protected def formatTimestamp(ts: Long): String =
format.format(new Timestamp(ts))

def noConsole = System.console() == null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.scalafmt.cli


trait CliUtils {
protected val isNative: Boolean = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.scalafmt.cli

import java.time.Instant

trait TermUtils {

// TODO print according to format
// private val format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
protected def formatTimestamp(ts: Long): String ={
Instant.ofEpochMilli(ts).toString

}
def noConsole = true
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.scalafmt.cli

import com.martiansoftware.nailgun.NGContext
import java.nio.file.{Files, Paths}

import org.scalafmt.Versions
Expand All @@ -9,39 +8,18 @@ import org.scalafmt.util.AbsoluteFile
import scala.io.Source
import scala.util.control.NoStackTrace

object Cli {
def nailMain(nGContext: NGContext): Unit = {
val workingDirectory =
AbsoluteFile.fromPath(nGContext.getWorkingDirectory).getOrElse {
throw new IllegalStateException(
s"Expected absolute path, " +
s"obtained nGContext.getWorkingDirectory = ${nGContext.getWorkingDirectory}"
)
}
val exit = mainWithOptions(
nGContext.getArgs,
CliOptions.default.copy(
common = CliOptions.default.common.copy(
cwd = workingDirectory,
out = nGContext.out,
in = nGContext.in,
err = nGContext.err
)
)
)
nGContext.exit(exit.code)
}
class Cli extends CliUtils {

private def throwIfError(exit: ExitCode): Unit = {
if (exit != ExitCode.Ok) {
throw new RuntimeException(exit.toString) with NoStackTrace
}
}

def main(args: Array[String]): Unit = {
val exit = mainWithOptions(args, CliOptions.default)
sys.exit(exit.code)
}
// def main(args: Array[String]): Unit = {
// val exit = mainWithOptions(args, CliOptions.default)
// sys.exit(exit.code)
// }

def exceptionThrowingMain(args: Array[String]): Unit = {
val exit = mainWithOptions(args, CliOptions.default)
Expand Down Expand Up @@ -99,7 +77,7 @@ object Cli {
// - `scalafmt-core` if the specified `version` setting match with build version
// (or if the `version` is not specified).
options.getVersionIfDifferent match {
case Some(v) if isNativeImage =>
case Some(v) if isNativeImage || isNative =>
Left(
s"""error: invalid Scalafmt version.
|
Expand Down Expand Up @@ -156,3 +134,19 @@ object Cli {
}
}
}

object Cli extends Cli {

def main(args: Array[String]): Unit = {
val workingDirectory = Paths.get(".").toAbsolutePath
val options = CliOptions.default.copy(
common = CommonOptions(
in = System.in,
out = System.out,
err = System.err,
cwd = AbsoluteFile.fromPath(workingDirectory).get
)
)
mainWithOptions(args, options)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ object CliArgParser {

val scoptParser: OptionParser[CliOptions] =
new scopt.OptionParser[CliOptions]("scalafmt") {
override def showUsageOnError: Option[Boolean] = Some(false)

override def showUsageOnError: Option[Boolean] = Some(false)
private def printAndExit(
includeUsage: Boolean
)(ignore: Unit, c: CliOptions): CliOptions = {
Expand Down