Skip to content

Commit

Permalink
Merge pull request #218 from semenodm/scala-cross-compilation
Browse files Browse the repository at this point in the history
Scala cross compilation
  • Loading branch information
Baccata authored Dec 6, 2023
2 parents 99ef6c5 + a7e3f9a commit 1673e68
Show file tree
Hide file tree
Showing 35 changed files with 211 additions and 120 deletions.
122 changes: 88 additions & 34 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import mill.scalalib.api.ZincWorkerUtil

import scala.Ordering.Implicits._

val scalaVersions = List("2.13.12", "2.12.18")

trait BaseModule extends Module with HeaderModule {
def millSourcePath: Path = {
val originalRelativePath = super.millSourcePath.relativeTo(os.pwd)
Expand Down Expand Up @@ -105,8 +107,16 @@ trait BasePublishModule extends BaseModule with CiReleaseModule {
}
}

trait ScalaVersionModule extends ScalaModule with ScalafmtModule {
def scalaVersion = T.input("2.13.12")
trait Scala213VersionModule extends ScalaModule with ScalafmtModule {
override def scalaVersion = T.input("2.13.12")

def scalacOptions = T {
super.scalacOptions() ++ scalacOptionsFor(scalaVersion())
}
}

trait ScalaVersionModule extends CrossScalaModule with ScalafmtModule {
override def scalaVersion = T.input("2.13.12")

def scalacOptions = T {
super.scalacOptions() ++ scalacOptionsFor(scalaVersion())
Expand All @@ -120,7 +130,20 @@ trait BaseScalaNoPublishModule extends BaseModule with ScalaVersionModule {
)
}

trait BaseScala213NoPublishModule
extends BaseModule
with Scala213VersionModule {

override def scalacPluginIvyDeps = super.scalacPluginIvyDeps() ++ Agg(
ivy"org.typelevel:::kind-projector:0.13.2"
)
}

trait BaseScalaModule extends BaseScalaNoPublishModule with BasePublishModule
trait BaseScala213Module
extends BaseScala213NoPublishModule
with BasePublishModule

trait BaseScalaJSModule extends BaseScalaModule with ScalaJSModule {
def scalaJSVersion = "1.11.0"
def moduleKind = ModuleKind.CommonJSModule
Expand All @@ -138,14 +161,14 @@ trait BaseMunitTests extends TestModule.Munit {
)
}

object `json-schema` extends BaseScalaModule {
def moduleDeps = Seq(
openapi
)
object `json-schema` extends Cross[`json-schema-module`](scalaVersions)
trait `json-schema-module` extends BaseScalaModule {
def moduleDeps = Seq(openapi())

def ivyDeps = Agg(
Deps.circe.jawn,
Deps.everit.jsonSchema
Deps.everit.jsonSchema,
Deps.collectionsCompat
)

object tests extends this.ScalaTests with BaseMunitTests {
Expand All @@ -156,14 +179,16 @@ object `json-schema` extends BaseScalaModule {
}
}

object openapi extends BaseScalaModule {
object openapi extends Cross[OpenApiModule](scalaVersions)
trait OpenApiModule extends BaseScalaModule {
def ivyDeps = Deps.swagger.parser ++ Agg(
Deps.smithy.model,
Deps.smithy.build,
Deps.cats.mtl,
Deps.ciString,
Deps.slf4j,
Deps.alloy.core
Deps.alloy.core,
Deps.collectionsCompat
)

def moduleDeps = Seq(
Expand All @@ -172,12 +197,13 @@ object openapi extends BaseScalaModule {

object tests extends this.ScalaTests with BaseMunitTests {
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.smithy.build
Deps.smithy.build,
Deps.scalaJavaCompat
)
}
}

object cli extends BaseScalaModule with buildinfo.BuildInfo {
object cli extends BaseScala213Module with buildinfo.BuildInfo {
def ivyDeps = Agg(
Deps.decline,
Deps.coursier,
Expand All @@ -197,7 +223,13 @@ object cli extends BaseScalaModule with buildinfo.BuildInfo {
BuildInfo.Value("cliVersion", publishVersion().toString)
)

def moduleDeps = Seq(openapi, proto.core, `json-schema`, formatter.jvm)
def moduleDeps =
Seq(
openapi("2.13.12"),
proto.core("2.13.12"),
`json-schema`("2.13.12"),
formatter.jvm("2.13.12")
)

def runProtoAux = T.task { (inputs: List[Path], output: Path) =>
val inputArgs = inputs.flatMap { p =>
Expand All @@ -216,10 +248,13 @@ object cli extends BaseScalaModule with buildinfo.BuildInfo {

object formatter extends BaseModule { outer =>
val deps = Agg(
ivy"org.typelevel::cats-parse::1.0.0"
ivy"org.typelevel::cats-parse::1.0.0",
Deps.collectionsCompat
)

object jvm extends BaseScalaModule {
object jvm extends Cross[JvmModule](scalaVersions)

trait JvmModule extends BaseScalaModule {
override def ivyDeps = T { super.ivyDeps() ++ deps }
override def millSourcePath = outer.millSourcePath

Expand All @@ -230,8 +265,9 @@ object formatter extends BaseModule { outer =>
)
}

object `parser-test` extends BaseScalaNoPublishModule {
def moduleDeps = Seq(formatter.jvm)
object `parser-test` extends Cross[ParserTestModule](scalaVersions)
trait ParserTestModule extends BaseScalaNoPublishModule {
def moduleDeps = Seq(formatter.jvm())
override def millSourcePath = outer.millSourcePath / "parser-test"

def ivyDeps = Agg(
Expand All @@ -240,14 +276,15 @@ object formatter extends BaseModule { outer =>
)
}

object shaded extends BaseJavaModule {
object shaded extends ShadedModule
trait ShadedModule extends BaseJavaModule {
override def millSourcePath = outer.millSourcePath / "shaded"

override def localClasspath: T[Seq[PathRef]] =
formatter.jvm.localClasspath()
formatter.jvm().localClasspath()

override def resolvedRunIvyDeps: T[Agg[PathRef]] =
formatter.jvm.resolvedRunIvyDeps()
formatter.jvm().resolvedRunIvyDeps()

override def publishXmlDeps = T.task { Agg.empty[Dependency] }

Expand All @@ -263,12 +300,12 @@ object formatter extends BaseModule { outer =>

object `java-api` extends BaseJavaModule {
override def unmanagedClasspath = T {
super.unmanagedClasspath() ++ Agg(formatter.jvm.shaded.jar())
super.unmanagedClasspath() ++ Agg(formatter.jvm().shaded.jar())
}
override def publishXmlDeps = T.task {
Agg(
mill.scalalib.publish.Dependency(
formatter.jvm.shaded.publishSelfDependency(),
formatter.jvm().shaded.publishSelfDependency(),
Scope.Compile
)
)
Expand All @@ -277,7 +314,8 @@ object formatter extends BaseModule { outer =>
}
}

object js extends BaseScalaJSModule {
object js extends Cross[JsModule](scalaVersions)
trait JsModule extends BaseScalaJSModule {
override def ivyDeps = T { super.ivyDeps() ++ deps }
override def millSourcePath = outer.millSourcePath

Expand Down Expand Up @@ -311,14 +349,18 @@ object traits extends BaseJavaModule {
)
}

object tests
object tests extends Cross[TestsModule](scalaVersions)
trait TestsModule
extends JavaModuleTests
with ScalaVersionModule
with BaseMunitTests
}

object `readme-validator` extends BaseScalaNoPublishModule {
def moduleDeps = Seq(openapi, proto.core, `json-schema`)
//object `readme-validator`
// extends Cross[`readme-validator-module`](scalaVersions)
object `readme-validator` extends BaseScala213NoPublishModule {
def moduleDeps =
Seq(openapi("2.13.12"), proto.core("2.13.12"), `json-schema`("2.13.12"))

def ivyDeps = Agg(
Deps.cats.parse,
Expand All @@ -338,13 +380,14 @@ object `readme-validator` extends BaseScalaNoPublishModule {
}

object proto extends Module {

object core extends BaseScalaModule {
object core extends Cross[CoreModule](scalaVersions)
trait CoreModule extends BaseScalaModule {
def ivyDeps = Agg(
Deps.smithy.model,
Deps.alloy.core
Deps.alloy.core,
Deps.collectionsCompat
)
def moduleDeps = Seq(traits, transitive)
def moduleDeps = Seq(traits, transitive())
object tests
extends this.ScalaTests
with BaseMunitTests
Expand Down Expand Up @@ -377,7 +420,7 @@ object proto extends Module {
}
}

object examples extends BaseScalaModule with ScalaPBModule {
object examples extends BaseScala213Module with ScalaPBModule {
def scalaPBVersion = Deps.scalapb.version

def smithyFiles = T.sources {
Expand Down Expand Up @@ -409,12 +452,18 @@ object proto extends Module {
}
}

object transitive extends BaseScalaModule {
object transitive extends Cross[TransitiveModule](scalaVersions)
trait TransitiveModule extends BaseScalaModule {
def ivyDeps = Agg(
Deps.smithy.model,
Deps.smithy.build
Deps.smithy.build,
Deps.collectionsCompat
)
object tests extends ScalaTests with BaseMunitTests
object tests extends ScalaTests with BaseMunitTests {
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.scalaJavaCompat
)
}
}

object Deps {
Expand Down Expand Up @@ -454,6 +503,11 @@ object Deps {
val ujson = ivy"com.lihaoyi::ujson:3.1.3"
}

val collectionsCompat =
ivy"org.scala-lang.modules::scala-collection-compat:2.11.0"

val scalaJavaCompat = ivy"org.scala-lang.modules::scala-java8-compat:1.0.2"

val munitVersion = "1.0.0-M10"
object grpc {
val version = "1.59.1"
Expand Down Expand Up @@ -557,7 +611,7 @@ private val allScalacOptions = Seq(
ScalacOption("-Wunused:implicits", isSupported = version => v213 <= version && version < v300), // ^ Replaces the above
ScalacOption("-Wunused:explicits", isSupported = version => v213 <= version && version < v300), // Warn if an explicit parameter is unused.
ScalacOption("-Ywarn-unused:imports", isSupported = version => v212 <= version && version < v213), // Warn if an import selector is not referenced.
ScalacOption("-Wunused:imports", isSupported = version => v213 <= version && version < v300), // ^ Replaces the above
// ScalacOption("-Wunused:imports", isSupported = version => v213 <= version && version < v300), // ^ Replaces the above
ScalacOption("-Ywarn-unused:locals", isSupported = version => v212 <= version && version < v213), // Warn if a local definition is unused.
ScalacOption("-Wunused:locals", isSupported = version => v213 <= version && version < v300), // ^ Replaces the above
ScalacOption("-Ywarn-unused:params", isSupported = version => v212 <= version && version < v213), // Warn if a value parameter is unused.
Expand Down
4 changes: 2 additions & 2 deletions modules/cli/src/runners/OpenApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object OpenApi {
def runOpenApi(opts: OpenAPIJsonSchemaOpts): Unit = {
val transformers = TransformerLookup.getAll()

val report = ReportResult(opts.outputPath, opts.outputJson).apply
val report = ReportResult(opts.outputPath, opts.outputJson).apply _

report(
ParseAndCompile.openapi(
Expand All @@ -42,7 +42,7 @@ object OpenApi {
def runJsonSchema(opts: OpenAPIJsonSchemaOpts): Unit = {
val transformers = TransformerLookup.getAll()

val report = ReportResult(opts.outputPath, opts.outputJson).apply
val report = ReportResult(opts.outputPath, opts.outputJson).apply _
report(
ParseAndCompile.jsonSchema(
opts.inputFiles,
Expand Down
3 changes: 2 additions & 1 deletion modules/cli/src/runners/Proto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private object Deps {
.assemble()
.unwrap()

modelBuilder.addShapes(upstreamModel): Unit
modelBuilder.addShapes(upstreamModel)
()
}
}
}
2 changes: 2 additions & 0 deletions modules/cli/src/runners/formatter/Formatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import software.amazon.smithy.model.Model

import scala.util.Try

import scala.collection.compat._

object Formatter {

def run(formatOpts: FormatOpts): Unit = {
Expand Down
2 changes: 1 addition & 1 deletion modules/cli/src/runners/openapi/ReportResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final case class ReportResult(outputPath: os.Path, outputJson: Boolean) {
".smithy"
) // convert e.g. my.namespace.test.smithy to my/namespace/test.smithy
val path = outputPath / os.SubPath(subPath)
path -> in._2
(path, os.Source.WritableSource(in._2))
}

def apply(result: OpenApiCompiler.Result[Model], debug: Boolean): Unit = {
Expand Down
1 change: 0 additions & 1 deletion modules/formatter/src/parsers/MetadataParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import smithytranslate.formatter.parsers.NodeParser.{
node_object_key,
node_value
}
import smithytranslate.formatter.parsers.equal
import smithytranslate.formatter.parsers.WhitespaceParser.{br, sp, sp0}
import cats.parse.{Parser, Parser0}
import smithytranslate.formatter.ast.MetadataStatement
Expand Down
2 changes: 1 addition & 1 deletion modules/formatter/src/parsers/ShapeIdParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object ShapeIdParser {
.map(Namespace.tupled)
val absolute_root_shape_id: Parser[AbsoluteRootShapeId] =
((namespace <* Parser.char('#')) ~ identifier)
.map(AbsoluteRootShapeId.apply.tupled)
.map { case (ns, id) => AbsoluteRootShapeId.apply(ns, id) }
val root_shape_id: Parser[RootShapeId] =
absolute_root_shape_id.backtrack
.eitherOr(identifier)
Expand Down
1 change: 0 additions & 1 deletion modules/formatter/src/parsers/ShapeParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import smithytranslate.formatter.ast.shapes.ShapeStatementsCase.{
ApplyStatementCase,
ShapeStatementCase
}
import smithytranslate.formatter.parsers.equal
import smithytranslate.formatter.parsers.WhitespaceParser.{br, sp, sp0, ws}
import smithytranslate.formatter.parsers.NodeParser._
import smithytranslate.formatter.parsers.ShapeIdParser._
Expand Down
1 change: 0 additions & 1 deletion modules/formatter/src/parsers/SmithyParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package formatter
package parsers

import smithytranslate.formatter.ast.Idl
import smithytranslate.formatter.parsers.IdlParser
import cats.syntax.all._

trait SmithyParser {
Expand Down
2 changes: 1 addition & 1 deletion modules/formatter/src/parsers/WhitespaceParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object WhitespaceParser {
val commentType: Parser[CommentType] =
documentation_comment.backtrack | line_comment
val comment: Parser[Comment] =
(commentType ~ not_newline <* nl).map(Comment.apply.tupled)
(commentType ~ not_newline <* nl).map { case (a, b) => Comment.apply(a, b) }
private val commentOrNewline: Parser[Option[Comment]] =
comment.eitherOr(nl).map(_.toOption)

Expand Down
4 changes: 2 additions & 2 deletions modules/formatter/src/writer/NodeWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ object NodeWriter {
}
implicit val quotedTextWriter: Writer[QuotedText] = Writer.write {
case QuotedText(text) =>
s"\"${text.map(_.write).mkString}\""
s"""\"${text.map(_.write).mkString}\""""
}
implicit val textBlockContentWriter: Writer[TextBlockContent] = Writer.write {
case TextBlockContent(quotes, text) =>
quotes.writeN + text.write
}
implicit val textBlockWriter: Writer[TextBlock] = Writer.write {
case TextBlock(text) =>
s"\"\"\"\n${text.writeN}\"\"\""
s"""\"\"\"\n${text.writeN}\"\"\""""
}
implicit val fracWriter: Writer[Frac] = Writer.write { case Frac(frac) =>
s".${frac.write}"
Expand Down
1 change: 1 addition & 0 deletions modules/formatter/src/writer/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import smithytranslate.formatter.writers.NodeWriter.{
}
import smithytranslate.formatter.writers.WhiteSpaceWriter.wsWriter
import smithytranslate.formatter.writers.Writer.WriterOps
import scala.collection.compat._

package object writers {
val traitKeyValueLimitLength = 80
Expand Down
Loading

0 comments on commit 1673e68

Please sign in to comment.