Skip to content

Commit

Permalink
Added compiler-classpath properties to generated IntelliJ xml files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-pipicello-olx authored and lihaoyi committed Jan 31, 2019
1 parent 83ac3f9 commit f6b0553
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
62 changes: 51 additions & 11 deletions scalalib/src/GenIdeaImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import mill.api.Ctx.{Home, Log}
import mill.api.Strict.Agg
import mill.api.{Loose, Strict}
import mill.{T, scalalib}
import os.Path

import scala.util.Try

Expand Down Expand Up @@ -98,12 +99,33 @@ object GenIdeaImpl {
.filter(_.toIO.exists)
}.getOrElse(Seq())

case class ResolvedModule(
path: Segments,
classpath: Loose.Agg[Path],
module: JavaModule,
pluginClasspath: Loose.Agg[Path],
scalaOptions: Seq[String],
compilerClasspath: Loose.Agg[Path],
libraryClasspath: Loose.Agg[Path]
)

val resolved = for((path, mod) <- modules) yield {
val scalaLibraryIvyDeps = mod match{
case x: ScalaModule => x.scalaLibraryIvyDeps
case _ => T.task{Nil}
case _ => T.task{Loose.Agg.empty[Dep]}
}
val allIvyDeps = T.task{mod.transitiveIvyDeps() ++ scalaLibraryIvyDeps() ++ mod.compileIvyDeps()}

val scalaCompilerClasspath = mod match{
case x: ScalaModule => x.scalaCompilerClasspath
case _ => T.task{Loose.Agg.empty[PathRef]}
}


val externalLibraryDependencies = T.task{
mod.resolveDeps(scalaLibraryIvyDeps)()
}

val externalDependencies = T.task{
mod.resolveDeps(allIvyDeps)() ++
Task.traverse(mod.transitiveModuleDeps)(_.unmanagedClasspath)().flatten
Expand All @@ -124,19 +146,26 @@ object GenIdeaImpl {
val resolvedCp: Loose.Agg[PathRef] = evalOrElse(evaluator, externalDependencies, Loose.Agg.empty)
val resolvedSrcs: Loose.Agg[PathRef] = evalOrElse(evaluator, externalSources, Loose.Agg.empty)
val resolvedSp: Loose.Agg[PathRef] = evalOrElse(evaluator, scalacPluginDependencies, Loose.Agg.empty)
val resolvedCompilerCp: Loose.Agg[PathRef] = evalOrElse(evaluator, scalaCompilerClasspath, Loose.Agg.empty)
val resolvedLibraryCp: Loose.Agg[PathRef] = evalOrElse(evaluator, externalLibraryDependencies, Loose.Agg.empty)
val scalacOpts: Seq[String] = evalOrElse(evaluator, scalacOptions, Seq())

(
ResolvedModule(
path,
resolvedCp.map(_.path).filter(_.ext == "jar") ++ resolvedSrcs.map(_.path),
mod,
resolvedSp.map(_.path).filter(_.ext == "jar"),
scalacOpts
scalacOpts,
resolvedCompilerCp.map(_.path),
resolvedLibraryCp.map(_.path)
)
}

val moduleLabels = modules.map(_.swap).toMap

val allResolved = resolved.flatMap(_._2) ++ buildLibraryPaths ++ buildDepsPaths
val allResolved = resolved.flatMap(_.classpath) ++ buildLibraryPaths ++ buildDepsPaths

val librariesProperties = resolved.flatMap(x => x.libraryClasspath.map(_ -> x.compilerClasspath)).toMap

val commonPrefix =
if (allResolved.isEmpty) 0
Expand Down Expand Up @@ -216,8 +245,8 @@ object GenIdeaImpl {
val compilerSettings = resolved
.foldLeft(Map[(Loose.Agg[os.Path], Seq[String]), Vector[JavaModule]]()) {
(r, q) =>
val key = (q._4, q._5)
r + (key -> (r.getOrElse(key, Vector()) :+ q._3))
val key = (q.pluginClasspath, q.scalaOptions)
r + (key -> (r.getOrElse(key, Vector()) :+ q.module))
}

val allBuildLibraries : Set[ResolvedLibrary] =
Expand Down Expand Up @@ -249,16 +278,15 @@ object GenIdeaImpl {

val libraries = resolvedLibraries(allResolved).map{ resolved =>
import resolved.path
val url = if (path.ext == "jar") "jar://" + path + "!/" else "file://" + path
val name = libraryName(resolved)
val sources = resolved match {
case CoursierResolved(_, _, s) => s.map(p => "jar://" + p + "!/")
case OtherResolved(_) => None
}
Tuple2(os.rel/".idea"/'libraries/s"$name.xml", libraryXmlTemplate(name, url, sources))
Tuple2(os.rel/".idea"/'libraries/s"$name.xml", libraryXmlTemplate(name, path, sources, librariesProperties.getOrElse(path, Loose.Agg.empty)))
}

val moduleFiles = resolved.map{ case (path, resolvedDeps, mod, _, _) =>
val moduleFiles = resolved.map{ case ResolvedModule(path, resolvedDeps, mod, _, _, _, _) =>
val Seq(
resourcesPathRefs: Seq[PathRef],
sourcesPathRef: Seq[PathRef],
Expand Down Expand Up @@ -375,9 +403,21 @@ object GenIdeaImpl {
</component>
</module>
}
def libraryXmlTemplate(name: String, url: String, sources: Option[String]) = {
def libraryXmlTemplate(name: String, path: os.Path, sources: Option[String], compilerClassPath: Loose.Agg[Path]) = {
val url = if (path.ext == "jar") "jar://" + path + "!/" else "file://" + path
val isScalaLibrary = compilerClassPath.nonEmpty
<component name="libraryTable">
<library name={name} type={if(name.contains("scala-library-")) "Scala" else null}>
<library name={name} type={if(isScalaLibrary) "Scala" else null}>
{ if(isScalaLibrary) {
<properties>
<compiler-classpath>
{
compilerClassPath.toList.sortBy(_.wrapped).map(p => <root url={"file://" + p}/>)
}
</compiler-classpath>
</properties>
}
}
<CLASSES>
<root url={url}/>
</CLASSES>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<component name="libraryTable">
<library name="scala-library-2.12.4.jar" type="Scala">
<properties>
<compiler-classpath>
<root url="file://COURSIER_HOME/https/repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_2.12/1.0.6/scala-xml_2.12-1.0.6.jar"/>
<root url="file://COURSIER_HOME/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.4/scala-compiler-2.12.4.jar"/>
<root url="file://COURSIER_HOME/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.4/scala-library-2.12.4.jar"/>
<root url="file://COURSIER_HOME/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.4/scala-reflect-2.12.4.jar"/>
</compiler-classpath>
</properties>
<CLASSES>
<root url="jar://COURSIER_HOME/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.4/scala-library-2.12.4.jar!/"/>
</CLASSES>
Expand Down

0 comments on commit f6b0553

Please sign in to comment.