From 107ba88754673e08b030b9ba1c8f4e6f1eefd13d Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 22 Feb 2018 04:15:42 +0000 Subject: GenIdea updates including a fix for #147 (#149) * extract JDK version so you don't have to keep resetting it * give modules a base path looks much nicer in IntelliJ - the module is now in bold * exclude sbt target directories stops sbt artifacts appearing in search * Fix #147 - add IntelliJ scala library * exclude root sbt project and target folders from IJ search * only resort to full long path names for jars if the name is a duplicate duplicates are hard to find unless you are pulling the same thing from different repos (therefore different path) * update GenIdea tests * tidy GenIdea --- scalalib/src/mill/scalalib/GenIdea.scala | 85 +++++++++++++++------- scalalib/test/resources/gen-idea/idea_modules/iml | 6 +- .../test/resources/gen-idea/idea_modules/root.iml | 5 +- scalalib/test/src/mill/scalalib/GenIdeaTests.scala | 5 +- 4 files changed, 71 insertions(+), 30 deletions(-) (limited to 'scalalib') diff --git a/scalalib/src/mill/scalalib/GenIdea.scala b/scalalib/src/mill/scalalib/GenIdea.scala index 095daf31..9bcb35ed 100644 --- a/scalalib/src/mill/scalalib/GenIdea.scala +++ b/scalalib/src/mill/scalalib/GenIdea.scala @@ -9,6 +9,7 @@ import mill.{T, scalalib} import mill.util.Ctx.Log import mill.util.{Loose, PrintLogger, Strict} import mill.util.Strict.Agg +import scala.util.Try object GenIdeaModule extends ExternalModule { @@ -30,19 +31,34 @@ object GenIdea { rootModule: BaseModule, discover: Discover[_]): Unit = { val pp = new scala.xml.PrettyPrinter(999, 4) + + val jdkInfo = extractCurrentJdk(pwd / ".idea" / "misc.xml").getOrElse(("JDK_1_8", "1.8 (1)")) + rm! pwd/".idea" rm! pwd/".idea_modules" val evaluator = new Evaluator(pwd / 'out, pwd / 'out, rootModule, ctx.log) - for((relPath, xml) <- xmlFileLayout(evaluator, rootModule)){ + for((relPath, xml) <- xmlFileLayout(evaluator, rootModule, jdkInfo)){ write.over(pwd/relPath, pp.format(xml)) } } + def extractCurrentJdk(ideaPath: Path): Option[(String,String)] = { + import scala.xml.XML + Try { + val xml = XML.loadFile(ideaPath.toString) + (xml \\ "component") + .filter(x => x.attribute("project-jdk-type").map(_.text).contains("JavaSDK")) + .map { n => (n.attribute("languageLevel"), n.attribute("project-jdk-name")) } + .collectFirst{ case (Some(lang), Some(jdk)) => (lang.text, jdk.text) } + }.getOrElse(None) + } + def xmlFileLayout[T](evaluator: Evaluator[T], rootModule: mill.Module, + jdkInfo: (String,String), fetchMillModules: Boolean = true): Seq[(RelPath, scala.xml.Node)] = { val modules = rootModule.millInternal.segmentsToModules.values @@ -91,12 +107,25 @@ object GenIdea { .takeWhile(_.distinct.length == 1) .length + // only resort to full long path names if the jar name is a duplicate + val pathShortLibNameDuplicate = allResolved + .distinct + .map{p => p.segments.last -> p} + .groupBy(_._1) + .filter(_._2.size > 1) + .keySet + val pathToLibName = allResolved - .map{p => (p, p.segments.drop(commonPrefix).mkString("_"))} + .map{p => + if (pathShortLibNameDuplicate(p.segments.last)) + (p, p.segments.drop(commonPrefix).mkString("_")) + else + (p, p.segments.last) + } .toMap val fixedFiles = Seq( - Tuple2(".idea"/"misc.xml", miscXmlTemplate()), + Tuple2(".idea"/"misc.xml", miscXmlTemplate(jdkInfo)), Tuple2(".idea"/"scala_settings.xml", scalaSettingsTemplate()), Tuple2( ".idea"/"modules.xml", @@ -141,8 +170,11 @@ object GenIdea { evaluator.outPath, mod.compile.ctx.segments ) + val Seq(scalaVersion: String) = evaluator.evaluate(Agg(mod.scalaVersion)).values val elem = moduleXmlTemplate( + mod.millModuleBasePath.value, + scalaVersion, Strict.Agg.from(resourcesPathRefs.map(_.path)), Strict.Agg.from(normalSourcePaths), Strict.Agg.from(generatedSourcePaths), @@ -177,9 +209,9 @@ object GenIdea { } - def miscXmlTemplate() = { + def miscXmlTemplate(jdkInfo: (String,String)) = { - + @@ -206,7 +238,10 @@ object GenIdea { - + + + + @@ -226,7 +261,9 @@ object GenIdea { } - def moduleXmlTemplate(resourcePaths: Strict.Agg[Path], + def moduleXmlTemplate(basePath: Path, + scalaVersion: String, + resourcePaths: Strict.Agg[Path], normalSourcePaths: Strict.Agg[Path], generatedSourcePaths: Strict.Agg[Path], outputPath: Path, @@ -236,29 +273,27 @@ object GenIdea { - { - for (normalSourcePath <- normalSourcePaths.toSeq.sorted) - yield - + + { + for (normalSourcePath <- normalSourcePaths.toSeq.sorted) + yield - - } - { - for (generatedSourcePath <- generatedSourcePaths.toSeq.sorted) - yield - + } + { + for (generatedSourcePath <- generatedSourcePaths.toSeq.sorted) + yield - - } - { - for (resourcePath <- resourcePaths.toSeq.sorted) - yield - + } + { + for (resourcePath <- resourcePaths.toSeq.sorted) + yield - - } + } + + + { for(name <- libNames.toSeq.sorted) diff --git a/scalalib/test/resources/gen-idea/idea_modules/iml b/scalalib/test/resources/gen-idea/idea_modules/iml index 10c9fe26..1fe9be83 100644 --- a/scalalib/test/resources/gen-idea/idea_modules/iml +++ b/scalalib/test/resources/gen-idea/idea_modules/iml @@ -2,14 +2,14 @@ - + - - + + diff --git a/scalalib/test/resources/gen-idea/idea_modules/root.iml b/scalalib/test/resources/gen-idea/idea_modules/root.iml index dcf5f7fc..46fd8c3b 100644 --- a/scalalib/test/resources/gen-idea/idea_modules/root.iml +++ b/scalalib/test/resources/gen-idea/idea_modules/root.iml @@ -1,7 +1,10 @@ - + + + + diff --git a/scalalib/test/src/mill/scalalib/GenIdeaTests.scala b/scalalib/test/src/mill/scalalib/GenIdeaTests.scala index a6700824..0ce9533f 100644 --- a/scalalib/test/src/mill/scalalib/GenIdeaTests.scala +++ b/scalalib/test/src/mill/scalalib/GenIdeaTests.scala @@ -23,7 +23,10 @@ object GenIdeaTests extends TestSuite { 'genIdeaTests - { val pp = new scala.xml.PrettyPrinter(999, 4) - val layout = GenIdea.xmlFileLayout(helloWorldEvaluator.evaluator, HelloWorld, fetchMillModules = false) + val layout = GenIdea.xmlFileLayout( + helloWorldEvaluator.evaluator, + HelloWorld, + ("JDK_1_8", "1.8 (1)"), fetchMillModules = false) for((relPath, xml) <- layout){ write.over(millSourcePath/ "generated"/ relPath, pp.format(xml)) } -- cgit v1.2.3