Skip to content

Commit

Permalink
Refined MillBootstrapModule and GenIdeaImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Aug 16, 2023
1 parent 5efa6fb commit 3792bc3
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 44 deletions.
37 changes: 18 additions & 19 deletions idea/src/mill/idea/GenIdeaImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -698,10 +696,11 @@ case class GenIdeaImpl(
<project version={"" + ideaConfigVersion}>
<component name="ProjectModuleManager">
<modules>
<module
fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"
filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"
/>
{
// <module
// fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"
// filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"/>
}
{
for (selector <- selectors)
yield {
Expand Down
2 changes: 1 addition & 1 deletion integration/feature/gen-idea/repo/extended/build.sc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $ivy.`org.scalameta::munit:0.7.29`
import $meta._

import mill._
import mill.api.PathRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<content url="file://$MODULE_DIR$/../../out/mill-build/generateScriptSources.dest">
<sourceFolder url="file://$MODULE_DIR$/../../out/mill-build/generateScriptSources.dest" isTestSource="false" generated="true"/>
</content>
<content url="file://$MODULE_DIR$/../../build.sc">
<sourceFolder url="file://$MODULE_DIR$/../../build.sc" isTestSource="false"/>
</content>
<content url="file://$MODULE_DIR$/../../mill-build/src">
<sourceFolder url="file://$MODULE_DIR$/../../mill-build/src" isTestSource="false"/>
</content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/../../out/mill-build/mill-build/ideaCompileOutput.dest/classes"/>
<exclude-output/>
<content url="file://$MODULE_DIR$/../../out/mill-build/mill-build/generateScriptSources.dest">
<sourceFolder url="file://$MODULE_DIR$/../../out/mill-build/mill-build/generateScriptSources.dest" isTestSource="false" generated="true"/>
</content>
<content url="file://$MODULE_DIR$/../../mill-build/build.sc">
<sourceFolder url="file://$MODULE_DIR$/../../mill-build/build.sc" isTestSource="false"/>
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
<!-- IGNORE -->
</component>
</module>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/helloworld.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/helloworld.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/helloworld.subscala3.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/helloworld.subscala3.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/helloworld.test.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/helloworld.test.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.mill-build.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.mill-build.iml"/>
</modules>
</component>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mill._
import mill.scalalib._

object root extends MillBuildRootModule {
def ivyDeps = Agg(ivy"org.scalameta::munit:0.7.29")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
<content url="file://$MODULE_DIR$/../../out/mill-build/generateScriptSources.dest">
<sourceFolder url="file://$MODULE_DIR$/../../out/mill-build/generateScriptSources.dest" isTestSource="false" generated="true"/>
</content>
<content url="file://$MODULE_DIR$/../../mill-build/src">
<sourceFolder url="file://$MODULE_DIR$/../../mill-build/src" isTestSource="false"/>
</content>
<content url="file://$MODULE_DIR$/../../mill-build/resources">
<sourceFolder url="file://$MODULE_DIR$/../../mill-build/resources" type="java-resource"/>
<content url="file://$MODULE_DIR$/../../build.sc">
<sourceFolder url="file://$MODULE_DIR$/../../build.sc" isTestSource="false"/>
</content>
<orderEntry type="inheritedJdk"/>
<orderEntry type="sourceFolder" forTests="false"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/helloworld.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/helloworld.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/helloworld.test.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/helloworld.test.iml"/>
<module fileurl="file://$PROJECT_DIR$/.idea/mill_modules/mill-build.iml" filepath="$PROJECT_DIR$/.idea/mill_modules/mill-build.iml"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions runner/src/mill/runner/FileImportGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
60 changes: 44 additions & 16 deletions runner/src/mill/runner/MillBuildRootModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 3792bc3

Please sign in to comment.