Skip to content

Commit

Permalink
Merge pull request scala#8772 from hrhino/backport/8670
Browse files Browse the repository at this point in the history
[backport] Build intellij task: don't prompt for confirmation
  • Loading branch information
dwijnand authored Mar 2, 2020
2 parents 7ca43d5 + d4e9062 commit da1bf67
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1506,48 +1506,51 @@ intellij := {
r
}

val ipr = (baseDirectory in ThisBuild).value / "src/intellij/scala.ipr"

var continue = false
val intellijDir = (baseDirectory in ThisBuild).value / "src/intellij"
val ipr = intellijDir / "scala.ipr"
backupIdea(intellijDir)
if (!ipr.exists) {
scala.Console.print(s"Could not find src/intellij/scala.ipr. Create new project files from src/intellij/*.SAMPLE (y/N)? ")
scala.Console.flush()
if (scala.io.StdIn.readLine() == "y") {
intellijCreateFromSample((baseDirectory in ThisBuild).value)
continue = true
}
} else {
scala.Console.print("Update library classpaths in the current src/intellij/scala.ipr (y/N)? ")
scala.Console.flush()
continue = scala.io.StdIn.readLine() == "y"
intellijCreateFromSample((baseDirectory in ThisBuild).value)
}
if (continue) {
s.log.info("Updating library classpaths in src/intellij/scala.ipr.")
val content = XML.loadFile(ipr)

val newStarr = replaceLibrary(content, "starr", Some("Scala"), starrDep(compilerScalaInstance.allJars))
val newModules = modules.foldLeft(newStarr)({
case (res, (modName, jars)) =>
if (jars.isEmpty) res // modules without dependencies
else replaceLibrary(res, s"$modName-deps", None, moduleDep(modName, jars))
})

XML.save(ipr.getAbsolutePath, newModules)
} else {
s.log.info("Aborting.")
s.log.info("Updating library classpaths in src/intellij/scala.ipr.")
val content = XML.loadFile(ipr)

val newStarr = replaceLibrary(content, "starr", Some("Scala"), starrDep(compilerScalaInstance.allJars))
val newModules = modules.foldLeft(newStarr)({
case (res, (modName, jars)) =>
if (jars.isEmpty) res // modules without dependencies
else replaceLibrary(res, s"$modName-deps", None, moduleDep(modName, jars))
})

// I can't figure out how to keep the entity escapes for \n in the attribute values after this use of XML transform.
// Patching the original version back in with more brutish parsing.
val R = """(?ims)(.*)(<copyright>.*</copyright>)(.*)""".r
val oldContents = IO.read(ipr)
XML.save(ipr.getAbsolutePath, newModules)
oldContents match {
case R(_, withEscapes, _) =>
val newContents = IO.read(ipr)
val R(pre, toReplace, post) = newContents
IO.write(ipr, pre + withEscapes + post)
case _ =>
// .ipr file hasn't been updated from `intellijFromSample` yet
}
}

lazy val intellijFromSample = taskKey[Unit]("Create fresh IntelliJ project files from src/intellij/*.SAMPLE.")

def backupIdea(ideaDir: File): Unit = {
val temp = IO.createTemporaryDirectory
IO.copyDirectory(ideaDir, temp)
println(s"Backed up existing src/intellij to $temp")
}

intellijFromSample := {
val s = streams.value
scala.Console.print(s"Create new project files from src/intellij/*.SAMPLE (y/N)? ")
scala.Console.flush()
if (scala.io.StdIn.readLine() == "y")
intellijCreateFromSample((baseDirectory in ThisBuild).value)
else
s.log.info("Aborting.")
val intellijDir = (baseDirectory in ThisBuild).value / "src/intellij"
val ipr = intellijDir / "scala.ipr"
backupIdea(intellijDir)
intellijCreateFromSample((baseDirectory in ThisBuild).value)
}

def intellijCreateFromSample(basedir: File): Unit = {
Expand All @@ -1560,17 +1563,14 @@ lazy val intellijToSample = taskKey[Unit]("Update src/intellij/*.SAMPLE using th

intellijToSample := {
val s = streams.value
scala.Console.print(s"Update src/intellij/*.SAMPLE using the current IntelliJ project files (y/N)? ")
scala.Console.flush()
if (scala.io.StdIn.readLine() == "y") {
val basedir = (baseDirectory in ThisBuild).value
val existing = basedir / "src/intellij" * "*.SAMPLE"
IO.delete(existing.get)
val current = basedir / "src/intellij" * ("*.iml" || "*.ipr")
val copies = current.get.map(f => (f, new File(f.getAbsolutePath + ".SAMPLE")))
IO.copy(copies)
} else
s.log.info("Aborting.")
val intellijDir = (baseDirectory in ThisBuild).value / "src/intellij"
val ipr = intellijDir / "scala.ipr"
backupIdea(intellijDir)
val existing =intellijDir * "*.SAMPLE"
IO.delete(existing.get)
val current = intellijDir * ("*.iml" || "*.ipr")
val copies = current.get.map(f => (f, new File(f.getAbsolutePath + ".SAMPLE")))
IO.copy(copies)
}

/** Find a specific module's JAR in a classpath, comparing only organization and name */
Expand Down

0 comments on commit da1bf67

Please sign in to comment.