Skip to content

Commit

Permalink
GenIdea: Use synthetic scala-SDK entry for compiler setup
Browse files Browse the repository at this point in the history
Fix #2867
  • Loading branch information
lefou committed May 16, 2024
1 parent c327a8a commit 68c435e
Show file tree
Hide file tree
Showing 22 changed files with 249 additions and 98 deletions.
158 changes: 82 additions & 76 deletions idea/src/mill/idea/GenIdeaImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,16 @@ case class GenIdeaImpl(
mod.resolveDeps(allIvyDeps, sources = true)()
}

val (scalacPluginsIvyDeps, allScalacOptions) = mod match {
val (scalacPluginsIvyDeps, allScalacOptions, scalaVersion) = mod match {
case mod: ScalaModule => (
T.task(mod.scalacPluginIvyDeps()),
T.task(mod.allScalacOptions())
T.task(mod.allScalacOptions()),
T.task { Some(mod.scalaVersion()) }
)
case _ => (
T.task {
Agg[Dep]()
},
T.task {
Seq()
}
T.task(Agg[Dep]()),
T.task(Seq[String]()),
T.task(None)
)
}

Expand Down Expand Up @@ -215,6 +213,7 @@ case class GenIdeaImpl(
val resolvedConfigFileContributions: Seq[IdeaConfigFile] =
configFileContributions()
val resolvedCompilerOutput = compilerOutput()
val resolvedScalaVersion = scalaVersion()

ResolvedModule(
path = path,
Expand All @@ -224,12 +223,13 @@ case class GenIdeaImpl(
module = mod,
pluginClasspath = resolvedSp.map(_.path).filter(_.ext == "jar"),
scalaOptions = scalacOpts,
compilerClasspath = resolvedCompilerCp.map(_.path),
scalaCompilerClasspath = resolvedCompilerCp.map(_.path),
libraryClasspath = resolvedLibraryCp.map(_.path),
facets = resolvedFacets,
configFileContributions = resolvedConfigFileContributions,
compilerOutput = resolvedCompilerOutput.path,
evaluator = evaluator
evaluator = evaluator,
scalaVersion = resolvedScalaVersion
)
}
}
Expand All @@ -255,11 +255,6 @@ case class GenIdeaImpl(
.distinct
.sorted

val librariesProperties: Map[os.Path, Agg[os.Path]] =
resolvedModules
.flatMap(rm => rm.libraryClasspath.map(_ -> rm.compilerClasspath))
.toMap

val (wholeFileConfigs, configFileContributions) =
resolvedModules
.flatMap(_.configFileContributions)
Expand Down Expand Up @@ -466,8 +461,8 @@ case class GenIdeaImpl(
* @note `:` in path isn't supported on Windows ~ https://github.com/com-lihaoyi/mill/issues/2243<br>
* It comes from [[sbtLibraryNameFromPom]]
*/
def libraryNameToFileSystemPathPart(name: String): String = {
name.replaceAll("""[-.:]""", "_")
def libraryNameToFileSystemPathPart(name: String, ext: String): os.SubPath = {
os.sub / s"${name.replaceAll("""[-.:]""", "_")}.${ext}"
}

val libraries: Seq[(os.SubPath, Elem)] =
Expand All @@ -480,47 +475,31 @@ case class GenIdeaImpl(
}
for (name <- names)
yield {
val compilerCp: Agg[os.Path] = librariesProperties.getOrElse(resolved.path, Agg.empty)
val languageLevel = name match {
case _ if compilerCp.iterator.isEmpty => None
case _ if name.startsWith("scala3-library_3-3.3.") => Some("Scala_3_3")
case _ if name.startsWith("scala3-library_3-3.2.") => Some("Scala_3_2")
case _ if name.startsWith("scala3-library_3-3.1.") => Some("Scala_3_1")
case _ if name.startsWith("scala3-library_3-3.0.") => Some("Scala_3_0")
case _ if name.startsWith("scala-library-2.13.") => Some("Scala_2_13")
case _ if name.startsWith("scala-library-2.12.") => Some("Scala_2_12")
case _ if name.startsWith("scala-library-2.11.") => Some("Scala_2_11")
case _ if name.startsWith("scala-library-2.10.") => Some("Scala_2_10")
case _ if name.startsWith("scala-library-2.9.") => Some("Scala_2_9")
case _ if name.startsWith("dotty-library-0.27") => Some("Scala_0_27")
case _ => None
}
Tuple2(
os.sub / "libraries" / s"${libraryNameToFileSystemPathPart(name)}.xml",
os.sub / "libraries" / libraryNameToFileSystemPathPart(name, "xml"),
libraryXmlTemplate(
name = name,
path = resolved.path,
sources = sources,
scalaCompilerClassPath = compilerCp,
languageLevel = languageLevel
sources = sources
)
)
}
}

val moduleFiles: Seq[(os.SubPath, Elem)] = resolvedModules.map {
val moduleFiles: Seq[(os.SubPath, Elem)] = resolvedModules.flatMap {
case ResolvedModule(
path,
resolvedDeps,
mod,
_,
_,
_,
compilerClasspath,
_,
facets,
_,
compilerOutput,
evaluator
evaluator,
scalaVersion
) =>
val Seq(
resourcesPathRefs: Seq[PathRef],
Expand All @@ -541,19 +520,6 @@ case class GenIdeaImpl(
.map(_.path)
.toSet -- generatedSourcePaths.toSet).toSeq

val scalaVersionOpt = mod match {
case x: ScalaModule =>
Some(
evaluator.evalOrThrow(
exceptionFactory = r =>
GenIdeaException(
s"Failure during evaluation of the scalaVersion: ${Evaluator.formatFailing(r)}"
)
)(x.scalaVersion)
)
case _ => None
}

val sanizedDeps: Seq[ScopedOrd[String]] = {
resolvedDeps
.map((s: Scoped[os.Path]) => pathToLibName(s.value) -> s.scope)
Expand Down Expand Up @@ -592,9 +558,9 @@ case class GenIdeaImpl(

val isTest = mod.isInstanceOf[TestModule]

val elem = moduleXmlTemplate(
val moduleXml = moduleXmlTemplate(
basePath = mod.intellijModulePath,
scalaVersionOpt = scalaVersionOpt,
scalaVersionOpt = scalaVersion,
resourcePaths = Strict.Agg.from(resourcesPathRefs.map(_.path)),
normalSourcePaths = Strict.Agg.from(normalSourcePaths),
generatedSourcePaths = Strict.Agg.from(generatedSourcePaths),
Expand All @@ -605,10 +571,32 @@ case class GenIdeaImpl(
facets = facets
)

Tuple2(
val moduleFile = Tuple2(
os.sub / "mill_modules" / s"${moduleName(path)}.iml",
elem
moduleXml
)

val scalaSdkFile = {
Option.when(scalaVersion.isDefined && compilerClasspath.nonEmpty) {
val name = s"scala-SDK-${scalaVersion.get}"
val languageLevel =
scalaVersion.map(_.split("[.]", 3).take(2).mkString("Scala_", "_", ""))

Tuple2(
os.sub / "libraries" / libraryNameToFileSystemPathPart(name, "xml"),
scalaSdkTemplate(
name = name,
languageLevel = languageLevel,
scalaCompilerClassPath = compilerClasspath,
// FIXME: fill in these fields
compilerBridgeJar = None,
scaladocExtraClasspath = None
)
)
}
}

Seq(moduleFile) ++ scalaSdkFile
}

{
Expand Down Expand Up @@ -764,32 +752,44 @@ case class GenIdeaImpl(
}
}

def libraryXmlTemplate(
def scalaSdkTemplate(
name: String,
path: os.Path,
sources: Option[os.Path],
languageLevel: Option[String],
scalaCompilerClassPath: Agg[os.Path],
languageLevel: Option[String]
compilerBridgeJar: Option[os.Path],
scaladocExtraClasspath: Agg[os.Path]
): Elem = {
val isScalaLibrary = scalaCompilerClassPath.iterator.nonEmpty
<component name="libraryTable">
<library name={name} type={if (isScalaLibrary) "Scala" else null}>
{
if (isScalaLibrary) {
<library name={name} type="Scala">
<properties>
{
if (languageLevel.isDefined) <language-level>{languageLevel.get}</language-level>
}
<compiler-classpath>
{
scalaCompilerClassPath.iterator.toSeq.sortBy(_.wrapped).map(p =>
<root url={relativeFileUrl(p)}/>
)
}
</compiler-classpath>
</properties>
}
if (languageLevel.isDefined) <language-level>{languageLevel.get}</language-level>
}
<compiler-classpath>
{
scalaCompilerClassPath.iterator.toSeq.sortBy(_.wrapped).map(p =>
<root url={relativeFileUrl(p)}/>
)
}
</compiler-classpath>
{
if (compilerBridgeJar.isDefined) <compiler-bridge-binary-jar>{
relativeFileUrl(compilerBridgeJar.get)
}</compiler-bridge-binary-jar>
}
</properties>
</library>
</component>
}

def libraryXmlTemplate(
name: String,
path: os.Path,
sources: Option[os.Path]
): Elem = {
<component name="libraryTable">
<library name={name}>
<CLASSES>
<root url={relativeJarUrl(path)}/>
</CLASSES>
Expand Down Expand Up @@ -892,6 +892,11 @@ case class GenIdeaImpl(
}
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
{
for {
scalaVersion <- scalaVersionOpt.toSeq
} yield <orderEntry type="library" name={s"scala-SDK-${scalaVersion}"} level="project" />
}

{
for (name <- libNames.sorted)
Expand Down Expand Up @@ -1011,12 +1016,13 @@ object GenIdeaImpl {
module: JavaModule,
pluginClasspath: Agg[os.Path],
scalaOptions: Seq[String],
compilerClasspath: Agg[os.Path],
scalaCompilerClasspath: Agg[os.Path],
libraryClasspath: Agg[os.Path],
facets: Seq[JavaFacet],
configFileContributions: Seq[IdeaConfigFile],
compilerOutput: os.Path,
evaluator: Evaluator
evaluator: Evaluator,
scalaVersion: Option[String]
)

case class GenIdeaException(msg: String) extends RuntimeException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="scala3-library_3-3.0.2.jar">
<CLASSES>
<root url="jar://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.0.2/scala3-library_3-3.0.2.jar!/"/>
</CLASSES>
<SOURCES>
<root url="jar://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.0.2/scala3-library_3-3.0.2-sources.jar!/"/>
</SOURCES>
</library>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<component name="libraryTable">
<library name="scala-SDK-2.13.6" type="Scala">
<properties>
<language-level>Scala_2_13</language-level>
<compiler-classpath>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/net/java/dev/jna/jna/5.3.1/jna-5.3.1.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/jline/jline/3.19.0/jline-3.19.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.6/scala-compiler-2.13.6.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.6/scala-library-2.13.6.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.13.6/scala-reflect-2.13.6.jar"/>
</compiler-classpath>
</properties>
</library>
</component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<component name="libraryTable">
<library name="scala-SDK-3.0.2" type="Scala">
<properties>
<language-level>Scala_3_0</language-level>
<compiler-classpath>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.7.0/protobuf-java-3.7.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/net/java/dev/jna/jna/5.3.1/jna-5.3.1.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/jline/jline-reader/3.19.0/jline-reader-3.19.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/jline/jline-terminal-jna/3.19.0/jline-terminal-jna-3.19.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/jline/jline-terminal/3.19.0/jline-terminal-3.19.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/modules/scala-asm/9.1.0-scala-1/scala-asm-9.1.0-scala-1.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.6/scala-library-2.13.6.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/3.0.2/scala3-compiler_3-3.0.2.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-interfaces/3.0.2/scala3-interfaces-3.0.2.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.0.2/scala3-library_3-3.0.2.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/tasty-core_3/3.0.2/tasty-core_3-3.0.2.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-sbt/compiler-interface/1.3.5/compiler-interface-1.3.5.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-sbt/util-interface/1.3.0/util-interface-1.3.0.jar"/>
</compiler-classpath>
</properties>
</library>
</component>
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<component name="libraryTable">
<library name="scala-library-2.13.6.jar" type="Scala">
<properties>
<language-level>Scala_2_13</language-level>
<compiler-classpath>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/net/java/dev/jna/jna/5.3.1/jna-5.3.1.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/jline/jline/3.19.0/jline-3.19.0.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.6/scala-compiler-2.13.6.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.6/scala-library-2.13.6.jar"/>
<root url="file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.13.6/scala-reflect-2.13.6.jar"/>
</compiler-classpath>
</properties>
<library name="scala-library-2.13.6.jar">
<CLASSES>
<root url="jar://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.6/scala-library-2.13.6.jar!/"/>
</CLASSES>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
<orderEntry type="library" name="scala-SDK-2.13.6" level="project"/>
<orderEntry type="library" name="scala-library-2.13.6.jar" level="project"/>
</component>
<component name="FacetManager">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
<orderEntry type="library" name="scala-SDK-3.0.2" level="project"/>
<orderEntry type="library" name="scala-library-2.13.6.jar" level="project"/>
<orderEntry type="library" name="scala3-library_3-3.0.2.jar" level="project"/>
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
<orderEntry type="library" name="scala-SDK-2.13.6" level="project"/>
<orderEntry type="library" name="scala-library-2.13.6.jar" level="project"/>
<orderEntry type="module" module-name="helloworld" exported=""/>
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
<orderEntry type="library" name="scala-SDK-<!-- IGNORE -->" level="project"/>
<!-- IGNORE -->
</component>
</module>
6 changes: 5 additions & 1 deletion integration/feature/gen-idea/repo/hello-idea/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ trait HelloIdeaModule extends scalalib.ScalaModule {
}
}

object HelloIdea extends HelloIdeaModule
object HelloIdea extends HelloIdeaModule {
object scala3 extends HelloIdeaModule {
def scalaVersion = "3.3.1"
}
}

object HiddenIdea extends HelloIdeaModule {
override def skipIdea = true
Expand Down
Loading

0 comments on commit 68c435e

Please sign in to comment.