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

[OLD PoC] Source generators #3035

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions modules/build/src/main/scala/scala/build/Project.scala
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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", "--")
)))
)
}

Expand Down
1 change: 1 addition & 0 deletions source-generators-example/Foo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Foo
3 changes: 3 additions & 0 deletions source-generators-example/Hello.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Hello {
val hello = "Hello"
}
12 changes: 12 additions & 0 deletions source-generators-example/Main.scala
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 26 additions & 0 deletions source-generators-example/addressbook.proto
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions source-generators-example/generators/gen.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 1 addition & 0 deletions source-generators-example/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//> using exclude "generators/gen.scala"
Loading