From 3792bc307fdddbc2cc921c61c093e177581a7061 Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Wed, 16 Aug 2023 13:41:47 +0200 Subject: [PATCH] Refined MillBootstrapModule and GenIdeaImpl --- idea/src/mill/idea/GenIdeaImpl.scala | 37 ++++++------ .../feature/gen-idea/repo/extended/build.sc | 2 +- .../extended/idea/mill_modules/mill-build.iml | 3 + .../mill_modules/mill-build.mill-build.iml | 15 +++++ .../gen-idea/repo/extended/idea/modules.xml | 2 +- .../repo/extended/mill-build/build.sc | 6 ++ .../idea/mill_modules/mill-build.iml | 7 +-- .../repo/hello-world/idea/modules.xml | 1 - .../test/src/GenIdeaExtendedTests.scala | 3 +- runner/src/mill/runner/FileImportGraph.scala | 9 +++ .../src/mill/runner/MillBuildRootModule.scala | 60 ++++++++++++++----- 11 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.mill-build.iml create mode 100644 integration/feature/gen-idea/repo/extended/mill-build/build.sc diff --git a/idea/src/mill/idea/GenIdeaImpl.scala b/idea/src/mill/idea/GenIdeaImpl.scala index dcde5b11c11..8e96dd72c0c 100755 --- a/idea/src/mill/idea/GenIdeaImpl.scala +++ b/idea/src/mill/idea/GenIdeaImpl.scala @@ -68,20 +68,19 @@ case class GenIdeaImpl( fetchMillModules: Boolean = true ): Seq[(os.SubPath, scala.xml.Node)] = { - val rootModules = evaluators.map { ev => (ev.rootModule, ev) } - val transitive: Seq[(BaseModule, Seq[Module], Evaluator)] = rootModules - .map { case (rootModule, ev) => - (rootModule, JavaModuleUtils.transitiveModules(rootModule), ev) + val rootModules = evaluators.zipWithIndex.map { case (ev, idx) => (ev.rootModule, ev, idx) } + val transitive: Seq[(BaseModule, Seq[Module], Evaluator, Int)] = rootModules + .map { case (rootModule, ev, idx) => + (rootModule, JavaModuleUtils.transitiveModules(rootModule), ev, idx) } val foundModules: Seq[(Segments, Module, Evaluator)] = transitive - .flatMap { case (rootModule0, otherModules, ev) => - (Seq(rootModule0) ++ otherModules).collect { + .flatMap { case (rootMod, transModules, ev, idx) => + transModules.collect { case m: Module => - val segments: Seq[String] = - rootModule0.millSourcePath.relativeTo(workDir).segments ++ - m.millModuleSegments.parts - + val rootSegs = rootMod.millSourcePath.relativeTo(workDir).segments + val modSegs = m.millModuleSegments.parts + val segments: Seq[String] = rootSegs ++ modSegs (Segments(segments.map(Segment.Label)), m, ev) } } @@ -93,7 +92,6 @@ case class GenIdeaImpl( lazy val modulesByEvaluator: Map[Evaluator, Seq[(Segments, JavaModule)]] = modules .groupMap { case (_, _, ev) => ev } { case (s, m, _) => (s, m) } -// .toSeq // val modules: Seq[(Segments, JavaModule)] = // rootModule.millInternal.segmentsToModules.values @@ -458,10 +456,10 @@ case class GenIdeaImpl( modules.map { case (segments, mod, _) => moduleName(segments) }.sorted ) ), - Tuple2( - os.sub / "mill_modules" / "mill-build.iml", - rootXmlTemplate(allBuildLibraries.flatMap(lib => libraryNames(lib))) - ), +// Tuple2( +// os.sub / "mill_modules" / "mill-build.iml", +// rootXmlTemplate(allBuildLibraries.flatMap(lib => libraryNames(lib))) +// ), Tuple2( os.sub / "scala_compiler.xml", scalaCompilerTemplate(compilerSettings) @@ -698,10 +696,11 @@ case class GenIdeaImpl( - + { +// + } { for (selector <- selectors) yield { diff --git a/integration/feature/gen-idea/repo/extended/build.sc b/integration/feature/gen-idea/repo/extended/build.sc index e345f8d44cd..1f1e6cd14ae 100644 --- a/integration/feature/gen-idea/repo/extended/build.sc +++ b/integration/feature/gen-idea/repo/extended/build.sc @@ -1,4 +1,4 @@ -import $ivy.`org.scalameta::munit:0.7.29` +import $meta._ import mill._ import mill.api.PathRef diff --git a/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.iml b/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.iml index 92beb65794b..f4900de873b 100644 --- a/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.iml +++ b/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.iml @@ -5,6 +5,9 @@ + + + diff --git a/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.mill-build.iml b/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.mill-build.iml new file mode 100644 index 00000000000..7ae9ae8ba5c --- /dev/null +++ b/integration/feature/gen-idea/repo/extended/idea/mill_modules/mill-build.mill-build.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/integration/feature/gen-idea/repo/extended/idea/modules.xml b/integration/feature/gen-idea/repo/extended/idea/modules.xml index 26ce642df26..68f06794a08 100644 --- a/integration/feature/gen-idea/repo/extended/idea/modules.xml +++ b/integration/feature/gen-idea/repo/extended/idea/modules.xml @@ -1,11 +1,11 @@ - + diff --git a/integration/feature/gen-idea/repo/extended/mill-build/build.sc b/integration/feature/gen-idea/repo/extended/mill-build/build.sc new file mode 100644 index 00000000000..5927948a8cd --- /dev/null +++ b/integration/feature/gen-idea/repo/extended/mill-build/build.sc @@ -0,0 +1,6 @@ +import mill._ +import mill.scalalib._ + +object root extends MillBuildRootModule { + def ivyDeps = Agg(ivy"org.scalameta::munit:0.7.29") +} \ No newline at end of file diff --git a/integration/feature/gen-idea/repo/hello-world/idea/mill_modules/mill-build.iml b/integration/feature/gen-idea/repo/hello-world/idea/mill_modules/mill-build.iml index 92beb65794b..234a3233c87 100644 --- a/integration/feature/gen-idea/repo/hello-world/idea/mill_modules/mill-build.iml +++ b/integration/feature/gen-idea/repo/hello-world/idea/mill_modules/mill-build.iml @@ -5,11 +5,8 @@ - - - - - + + diff --git a/integration/feature/gen-idea/repo/hello-world/idea/modules.xml b/integration/feature/gen-idea/repo/hello-world/idea/modules.xml index 069c7a398f4..cab10e5c482 100644 --- a/integration/feature/gen-idea/repo/hello-world/idea/modules.xml +++ b/integration/feature/gen-idea/repo/hello-world/idea/modules.xml @@ -1,7 +1,6 @@ - diff --git a/integration/feature/gen-idea/test/src/GenIdeaExtendedTests.scala b/integration/feature/gen-idea/test/src/GenIdeaExtendedTests.scala index 7d37aabbacd..e6b2bcfcf1a 100644 --- a/integration/feature/gen-idea/test/src/GenIdeaExtendedTests.scala +++ b/integration/feature/gen-idea/test/src/GenIdeaExtendedTests.scala @@ -18,9 +18,10 @@ object GenIdeaExtendedTests extends IntegrationTestSuite { val checks = Seq( os.sub / "mill_modules" / "helloworld.iml", - os.sub / "mill_modules" / "helloworld.test.iml", os.sub / "mill_modules" / "helloworld.subscala3.iml", + os.sub / "mill_modules" / "helloworld.test.iml", os.sub / "mill_modules" / "mill-build.iml", + os.sub / "mill_modules" / "mill-build.mill-build.iml", os.sub / "libraries" / s"scala_library_${scalaVersionLibPart}_jar.xml", // NOTE: on IntelliJ Scala Plugin side there is a cosmetic issue: scala suffix is added even for Java libraries (notice `_2_13` suffix) // In future it might be fixed and `GenIdea` will need to be updated diff --git a/runner/src/mill/runner/FileImportGraph.scala b/runner/src/mill/runner/FileImportGraph.scala index d5b041d90ed..cc8f87964e1 100644 --- a/runner/src/mill/runner/FileImportGraph.scala +++ b/runner/src/mill/runner/FileImportGraph.scala @@ -4,6 +4,15 @@ import mill.api.internal import scala.reflect.NameTransformer.encode import scala.collection.mutable +/** + * + * @param seenScripts + * @param repos + * @param ivyDeps + * @param importGraphEdges + * @param errors + * @param millImport If `true`, a meta-build is enabled + */ @internal case class FileImportGraph( seenScripts: Map[os.Path, String], diff --git a/runner/src/mill/runner/MillBuildRootModule.scala b/runner/src/mill/runner/MillBuildRootModule.scala index 25adc79fe85..3ba45986802 100644 --- a/runner/src/mill/runner/MillBuildRootModule.scala +++ b/runner/src/mill/runner/MillBuildRootModule.scala @@ -7,11 +7,9 @@ import mill.define.{Caller, Discover, Target, Task} import mill.scalalib.{BoundDep, Dep, DepSyntax, Lib, ScalaModule} import mill.util.CoursierSupport import mill.util.Util.millProjectModule -import mill.scalalib.api.Versions -import os.{Path, rel} +import mill.scalalib.api.{CompilationResult, Versions} import pprint.Util.literalize import FileImportGraph.backtickWrap -import mill.codesig.CodeSig import mill.main.BuildInfo import scala.collection.immutable.SortedMap @@ -61,20 +59,26 @@ class MillBuildRootModule()(implicit override def scalaVersion: T[String] = "2.13.10" - def scriptSources = T.sources { - MillBuildRootModule - .parseBuildFiles(millBuildRootModuleInfo) - .seenScripts - .keys - .map(PathRef(_)) - .toSeq + def scriptSources = T { +// MillBuildRootModule +// .parseBuildFiles(millBuildRootModuleInfo) +// .seenScripts +// .keys +// .map(PathRef(_)) +// .toSeq + parseBuildFiles().seenScripts.keys.map(PathRef(_)).toSeq } - def parseBuildFiles = T { - scriptSources() + def parseBuildFiles: T[FileImportGraph] = T.input { +// scriptSources() MillBuildRootModule.parseBuildFiles(millBuildRootModuleInfo) } +// override def compile: T[CompilationResult] = T { +// println("compile") +// super.compile() +// } + override def repositoriesTask: Task[Seq[Repository]] = { val importedRepos = T.task { val repos = parseBuildFiles().repos.map { case (repo, srcFile) => @@ -139,7 +143,7 @@ class MillBuildRootModule()(implicit } } - def scriptImportGraph: T[Map[Path, (Int, Seq[Path])]] = T { + def scriptImportGraph: T[Map[os.Path, (Int, Seq[os.Path])]] = T { parseBuildFiles() .importGraphEdges .map { case (path, imports) => @@ -211,8 +215,26 @@ class MillBuildRootModule()(implicit result } + override def sources: T[Seq[PathRef]] = T { +// Lib.findSourceFiles(allSources(), Seq("scala", "java", "sc")).map(PathRef(_)) +// val files = parseBuildFiles().seenScripts.keys +// println(s"sources: ${files.map(_.relativeTo(T.workspace))}") +// files.map(PathRef(_)).toSeq + scriptSources() ++ { + if (parseBuildFiles().millImport) super.sources() + else Seq.empty[PathRef] + } + } + + override def resources: T[Seq[PathRef]] = T { + if (parseBuildFiles().millImport) super.resources() + else Seq.empty[PathRef] + } + override def allSourceFiles: T[Seq[PathRef]] = T { - Lib.findSourceFiles(allSources(), Seq("scala", "java", "sc")).map(PathRef(_)) + // We ignore the sc files, as we generate scala files for them + // Lib.findSourceFiles(allSources(), Seq("scala", "java", "sc")).map(PathRef(_)) + Lib.findSourceFiles(allSources(), Seq("scala", "java")).map(PathRef(_)) } def enclosingClasspath = T.sources { @@ -277,7 +299,7 @@ object MillBuildRootModule { cliImports: Seq[String] ) - def parseBuildFiles(millBuildRootModuleInfo: MillBuildRootModule.Info) = { + def parseBuildFiles(millBuildRootModuleInfo: MillBuildRootModule.Info): FileImportGraph = { FileImportGraph.parseBuildFiles( millBuildRootModuleInfo.topLevelProjectRoot, millBuildRootModuleInfo.projectRoot / os.up @@ -295,7 +317,13 @@ object MillBuildRootModule { ): Unit = { for (scriptSource <- scriptSources) { val relative = scriptSource.path.relativeTo(base) - val dest = targetDest / FileImportGraph.fileImportToSegments(base, scriptSource.path, false) + val dest0 = targetDest / FileImportGraph.fileImportToSegments(base, scriptSource.path, false) + val dest = + if (dest0.ext == "sc") { + dest0 / os.up / (dest0.baseName + ".scala") + } else { + dest0 + } val newSource = MillBuildRootModule.top( relative,