我目前正在为我在Scala中编写的特定于领域的语言(DSL)开发一个编译器插件。结果,我的存储库中有一个“示例”目录(SBT子项目),其中包含一个使用DSL编写的示例程序。我可以很好地编译此子项目,并且可以按预期工作。
我的插件将分析此示例程序并生成一个输出文件(只需将其写出到文件即可生成)。我为相同的“示例”目录创建了一个新的子项目。它具有特殊的命令行选项,可指示编译器使用插件。一切运行正常,但是尽管进行了大量搜索,但我仍无法弄清楚生成输出文件的位置。我怀疑它被扔了。
下面是我的build.sbt
:
lazy val commonSettings = Seq(
organization := "com.bitbucket.bitstream-dsl",
scalaVersion := "2.12.6"
)
lazy val root = (project in file("."))
.settings(
commonSettings,
version := "0.1.0-SNAPSHOT",
name := "example-project"
)
lazy val examples = (project in file("examples"))
.settings(
commonSettings,
name := "examples"
)
.dependsOn(root)
lazy val examplesPlugin = (project in file("examples"))
.settings(
commonSettings,
scalacOptions += "-Xplugin:plugin/target/scala-2.12/plugin_2.12-0.1-SNAPSHOT.jar",
name := "examples_plugin",
target := baseDirectory.value / "target-plugin",
publishArtifact in Compile := true
)
.dependsOn(root)
lazy val plugin = (project in file("plugin"))
.settings(
commonSettings,
scalacOptions += "-J-Xss256m",
name := "plugin",
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value
)
.dependsOn(root)
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
这是我写出文件的方式:
def printFile(funcBody: String) : Unit = {
// open a file to be written
val file = new File("test.v")
val bw = new BufferedWriter(new FileWriter(file))
bw.write(funcBody + "\n")
//close the output file
bw.close()
}