Skip to content

Commit

Permalink
Refined implementation
Browse files Browse the repository at this point in the history
I now get some semanticDB files, but the path still contains to many sub directories, representing the full path of the source location.
  • Loading branch information
lefou committed Jan 3, 2023
1 parent b9a60e9 commit 3d227b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
13 changes: 12 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ object Deps {
val scalacScoverage2Serializer =
ivy"org.scoverage::scalac-scoverage-serializer:${scoverage2Version}"
val semanticDB = ivy"org.scalameta:::semanticdb-scalac:4.6.0"
val semanticDbJava = ivy"com.sourcegraph:semanticdb-java:0.8.9"
val sourcecode = ivy"com.lihaoyi::sourcecode:0.3.0"
val upickle = ivy"com.lihaoyi::upickle:2.0.0"
val utest = ivy"com.lihaoyi::utest:0.7.11"
Expand Down Expand Up @@ -484,7 +485,7 @@ object scalalib extends MillModule {
s"""package mill.scalalib
|
|/**
| * Dependency versions.
| * Dependency versions as they where defined at Mill compile time.
| * Generated from mill in build.sc.
| */
|object Versions {
Expand All @@ -494,6 +495,8 @@ object scalalib extends MillModule {
| val zinc = "${Deps.zinc.dep.version}"
| /** SemanticDB version. */
| val semanticDBVersion = "${Deps.semanticDB.dep.version}"
| /** Java SemanticDB plugin version. */
| val semanticDbJavaVersion = "${Deps.semanticDbJava.dep.version}"
|}
|
|""".stripMargin
Expand Down Expand Up @@ -1562,3 +1565,11 @@ def validate(ev: Evaluator): Command[Unit] = T.command {
)(identity))()
()
}

object DependencyFetchDummy extends JavaModule {
def compileIvyDeps = Agg(
Deps.semanticDbJava,
Deps.semanticDB,
Deps.asciidoctorj
)
}
71 changes: 32 additions & 39 deletions scalalib/src/mill/scalalib/SemanticDbJavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
).asInstanceOf[String]
}

def javaSemanticDbVersion: Input[String] = T.input {
def semanticDbJavaVersion: Input[String] = T.input {
T.env.getOrElse(
"JAVASEMANTICDB_VERSION",
SemanticDbJavaModule.contextJavaSemanticDbVersion.get()
Expand Down Expand Up @@ -56,14 +56,14 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
}
}

private def javaSemanticDbPluginIvyDeps: Target[Agg[Dep]] = T {
val sv = javaSemanticDbVersion()
private def semanticDbJavaPluginIvyDeps: Target[Agg[Dep]] = T {
val sv = semanticDbJavaVersion()
if (sv.isEmpty) {
val msg =
"""|
|You must provide a javaSemanticDbVersion
|
|def javaSemanticDbVersion = ???
|def semanticDbJavaVersion = ???
|""".stripMargin
Result.Failure(msg)
} else {
Expand Down Expand Up @@ -99,23 +99,34 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
}
}

private def resolvedJavaSemanticDbPluginIvyDeps: Target[Agg[PathRef]] = T {
resolveDeps(T.task { javaSemanticDbPluginIvyDeps().map(bindDependency()) })()
private def resolvedSemanticDbJavaPluginIvyDeps: Target[Agg[PathRef]] = T {
resolveDeps(T.task { semanticDbJavaPluginIvyDeps().map(bindDependency()) })()
}

def semanticDbData: T[PathRef] = {
// TODO: add extra options for Java 17+
def javacOptionsTask(m: JavaModule): Task[Seq[String]] = T.task {
val more = if(T.log.debugEnabled) " -verbose" else ""
m.javacOptions() ++ Seq(
s"-Xplugin:semanticdb -sourceroot:${T.workspace} -targetroot:${T.dest / "classes"}"
s"-Xplugin:semanticdb -sourceroot:${T.workspace} -targetroot:${T.dest / "classes"} -build-tool:sbt" + more
)
}
def compileClasspathTask(m: JavaModule): Task[Agg[PathRef]] = T.task {
m.compileClasspath() ++ resolvedJavaSemanticDbPluginIvyDeps()
m.compileClasspath() ++ resolvedSemanticDbJavaPluginIvyDeps()
}
def cleanedClassesDir(classesDir: os.Path): PathRef = {
os.walk(classesDir, preOrder = true)
.filter(os.isFile)
.foreach { p =>
if (p.ext != "semanticdb") {
os.remove(p)
}
}
PathRef(classesDir)
}

hostModule match {
// TODO: also generate semanticDB for Java and Mixed-Java projects
// TODO: Avoid fetching the Java semanticdb version when there are no Java sources (better detect mixed)
case m: ScalaModule =>
T.persistent {
val sv = m.scalaVersion()
Expand All @@ -137,8 +148,7 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
.filterNot(_ == "-Xfatal-warnings")
T.log.debug(s"effective scalac options: ${scalacOptions}")

zincWorker
.worker()
zincWorker.worker()
.compileMixed(
upstreamCompileOutput = upstreamCompileOutput(),
sources = m.allSourceFiles().map(_.path),
Expand All @@ -151,45 +161,28 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
scalacPluginClasspath = semanticDbPluginClasspath(),
reporter = T.reporter.apply(hashCode)
)
.map(_.classes)
.map(r => cleanedClassesDir(r.classes.path))
}
case m: JavaModule =>
T.persistent {
val compileClasspath = m.compileClasspath() ++ resolvedJavaSemanticDbPluginIvyDeps()
val javacOpts = javacOptionsTask(m)()

// we currently assume, we don't do incremental java compilation
val semDbPath = T.dest / "_semanticdb"
os.remove.all(semDbPath)
os.remove.all(T.dest / "classes")
// val semDbPath = T.dest / "_semanticdb"
// os.remove.all(semDbPath)

T.log.debug(s"effective javac options: ${javacOpts}")

zincWorker
.worker()
zincWorker.worker()
.compileJava(
upstreamCompileOutput(),
allSourceFiles().map(_.path),
compileClasspathTask(m)().map(_.path),
javacOptionsTask(m)(),
javacOpts,
T.reporter.apply(m.hashCode())
).map { compileResult =>
val classes = compileResult.classes.path

// move generated semanticdb files into separate folder
os.walk(
classes,
skip = p => !(os.isFile(p) && p.ext == "semanticdb"),
preOrder = true
).filter(os.isFile)
.foreach { p =>
os.copy(
from = p,
to = semDbPath / p.relativeTo(classes),
createFolders = true
)
}
// and only return folder with semanticdb files
PathRef(semDbPath)
}
).map(r => cleanedClassesDir(r.classes.path))
}

}
}

Expand Down Expand Up @@ -231,7 +224,7 @@ trait SemanticDbJavaModule extends CoursierModule { hostModule: JavaModule =>
}

object SemanticDbJavaModule {
val buildTimeJavaSemanticDbVersion = "0.8.2"
val buildTimeJavaSemanticDbVersion = Versions.semanticDbJavaVersion
val buildTimeSemanticDbVersion = Versions.semanticDBVersion

private[mill] val contextSemanticDbVersion: InheritableThreadLocal[Option[String]] =
Expand Down

0 comments on commit 3d227b8

Please sign in to comment.