diff --git a/build.sc b/build.sc index 86728351c2..d366f8afb9 100644 --- a/build.sc +++ b/build.sc @@ -892,6 +892,10 @@ trait Cli extends CrossSbtModule with ProtoBuildModule with CliLaunchers def localRepoJar = `local-repo`.localRepoJar() + def forkArgs = T { + super.forkArgs() ++ Seq("-agentlib:jdwp=transport=dt_socket,server=y,address=localhost:5050,suspend=y") + } + object test extends ScalaCliTests with ScalaCliScalafixModule { def moduleDeps = super.moduleDeps ++ Seq( `build-module`(crossScalaVersion).test diff --git a/modules/build/src/main/scala/scala/build/Project.scala b/modules/build/src/main/scala/scala/build/Project.scala index 6e2bd585fd..4c3d52190d 100644 --- a/modules/build/src/main/scala/scala/build/Project.scala +++ b/modules/build/src/main/scala/scala/build/Project.scala @@ -1,15 +1,16 @@ package scala.build -import _root_.bloop.config.{Config => BloopConfig, ConfigCodecs => BloopCodecs} -import _root_.coursier.{Dependency => CsDependency, core => csCore, util => csUtil} -import com.github.plokhotnyuk.jsoniter_scala.core.{writeToArray => writeAsJsonToArray} +import _root_.bloop.config.{Config as BloopConfig, ConfigCodecs as BloopCodecs} +import _root_.coursier.{Dependency as CsDependency, core as csCore, util as csUtil} +import bloop.config.Config.{SourceGenerator, SourcesGlobs} +import bloop.config.PlatformFiles +import com.github.plokhotnyuk.jsoniter_scala.core.writeToArray as writeAsJsonToArray import coursier.core.Classifier import java.io.ByteArrayOutputStream import java.nio.charset.StandardCharsets import java.nio.file.Path import java.util.Arrays - import scala.build.options.{ScalacOpt, Scope, ShadowingSeq} final case class Project( @@ -67,7 +68,12 @@ final case class Project( platform = Some(platform), `scala` = scalaConfigOpt, java = Some(BloopConfig.Java(javacOptions)), - resolution = resolution + resolution = resolution, + sourceGenerators = Some(List(SourceGenerator( + List(SourcesGlobs(PlatformFiles.getPath(workspace.toString), None, "glob:*.proto" :: Nil, Nil)), + PlatformFiles.getPath(workspace.toString), + List("scala-cli", "{some_path}/scala-cli-sandbox/gen.scala", "--server=false", "--") + ))) ) } diff --git a/source-generators-example/Foo.scala b/source-generators-example/Foo.scala new file mode 100644 index 0000000000..d37c104563 --- /dev/null +++ b/source-generators-example/Foo.scala @@ -0,0 +1 @@ +object Foo \ No newline at end of file diff --git a/source-generators-example/Hello.scala b/source-generators-example/Hello.scala new file mode 100644 index 0000000000..dffd1d089f --- /dev/null +++ b/source-generators-example/Hello.scala @@ -0,0 +1,3 @@ +object Hello { + val hello = "Hello" +} \ No newline at end of file diff --git a/source-generators-example/Main.scala b/source-generators-example/Main.scala new file mode 100644 index 0000000000..b97e122bfe --- /dev/null +++ b/source-generators-example/Main.scala @@ -0,0 +1,12 @@ +////> using plugin "com.thesamet.scalapb::compilerplugin:0.11.3" +//> using dep "com.thesamet.scalapb::scalapb-runtime:0.11.3" + +//> using source.generator scalapbc + +import tutorial.addressbook.{AddressBook, Person} + +object Main extends App { + println("Main") + val person = Person(name = "John Doe", id = 123, email = Some("XD")) + println(person) +} \ No newline at end of file diff --git a/source-generators-example/addressbook.proto b/source-generators-example/addressbook.proto new file mode 100644 index 0000000000..958f721391 --- /dev/null +++ b/source-generators-example/addressbook.proto @@ -0,0 +1,26 @@ +syntax = "proto2"; + +package tutorial; + +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + enum PhoneType { + MOBILE = 0; + HOME = 1; + WORK = 2; + } + + message PhoneNumber { + required string number = 1; + optional PhoneType type = 2 [default = HOME]; + } + + repeated PhoneNumber phones = 4; +} + +message AddressBook { + repeated Person people = 1; +} diff --git a/source-generators-example/generators/gen.scala b/source-generators-example/generators/gen.scala new file mode 100644 index 0000000000..f00779e727 --- /dev/null +++ b/source-generators-example/generators/gen.scala @@ -0,0 +1,35 @@ +//> using scala "2.13" +//> using dep "com.thesamet.scalapb::scalapbc:0.11.13" + +import scalapb.ScalaPBC + +class ProtobufGenerator { + def metadataJson: String = + s"""{ + | "name" : "Protobuf generator", + | "version" : "1.0.0", + | "supportedExtensions" : ["proto"], + | "isReferentiallyTransparent" : true + |}""".stripMargin + + def generate(inputLocation: String, outputLocation: String): Unit = { + ScalaPBC.main(Array( + s"--scala_out=${outputLocation}", + "-I", "/Users/mgajek/Projects/scala-cli-sandbox/source-generators/", + inputLocation + )) + } +} + +object Main { + def main(args: Array[String]) = { + val outputDir = args.headOption.getOrElse(throw new Exception("Output dir not specified")) + val sources = args.tail.toList + val generator = new ProtobufGenerator() + + sources.foreach { source => + println(s"Running protobuf generation from [$source] to [$outputDir]") + generator.generate(source, outputDir) + } + } +} diff --git a/source-generators-example/project.scala b/source-generators-example/project.scala new file mode 100644 index 0000000000..649ced0d1b --- /dev/null +++ b/source-generators-example/project.scala @@ -0,0 +1 @@ +//> using exclude "generators/gen.scala"