Skip to content

Commit

Permalink
Log a message at boot when cwd influences which projects are active
Browse files Browse the repository at this point in the history
use `Array` instead of `List` for some project computations at boot. ignore that they are mutable until an immutable array wrapper is adopted
  • Loading branch information
oyvindberg committed Nov 6, 2022
1 parent e157d5c commit d7ea459
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 54 deletions.
12 changes: 6 additions & 6 deletions bleep-cli/src/scala/bleep/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ object Main {
Opts
.arguments(metavars.projectNameNoCross)(argumentFrom(metavars.projectNameNoCross, Some(started.globs.projectNamesNoCrossMap)))

val projectNames: Opts[List[model.CrossProjectName]] =
val projectNames: Opts[Array[model.CrossProjectName]] =
Opts
.arguments(metavars.projectName)(argumentFrom(metavars.projectName, Some(started.globs.projectNameMap)))
.map(_.toList.flatten)
.map(_.toList.toArray.flatten)
.orNone
.map(started.chosenProjects)

val projectName: Opts[model.CrossProjectName] =
Opts.argument(metavars.projectNameExact)(argumentFrom(metavars.projectNameExact, Some(started.globs.exactProjectMap)))

val testProjectNames: Opts[List[model.CrossProjectName]] =
val testProjectNames: Opts[Array[model.CrossProjectName]] =
Opts
.arguments(metavars.testProjectName)(argumentFrom(metavars.testProjectName, Some(started.globs.testProjectNameMap)))
.map(_.toList.flatten)
.map(_.toList.toArray.flatten)
.orNone
.map(started.chosenTestProjects)

Expand Down Expand Up @@ -163,7 +163,7 @@ object Main {
groupId = groupId,
version = version,
publishTarget = publishTarget,
projects
projects.toList
)
commands.PublishLocal(started, options)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ object Main {
Opts(commands.InstallTabCompletions(logger))
)

def setupIdeCmd(buildPaths: BuildPaths, logger: Logger, projectNameMap: Option[Map[String, Iterable[model.CrossProjectName]]]): Opts[BleepCommand] = {
def setupIdeCmd(buildPaths: BuildPaths, logger: Logger, projectNameMap: Option[Map[String, Array[model.CrossProjectName]]]): Opts[BleepCommand] = {
val projectNamesNoExpand: Opts[Option[List[String]]] =
Opts
.arguments(metavars.projectName)(argumentFrom(metavars.projectName, projectNameMap.map(_.map { case (s, _) => (s, s) })))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import bleep.internal.FileUtils

import java.nio.file.{Files, Path}

case class BuildCreateDirectories(started: Started, projects: List[model.CrossProjectName]) extends BleepCommand {
case class BuildCreateDirectories(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
val dirsWithProject: Map[Path, List[model.CrossProjectName]] =
val dirsWithProject: Map[Path, Array[model.CrossProjectName]] =
projects
.flatMap { crossName =>
val paths = started.projectPaths(crossName)
Expand Down
4 changes: 2 additions & 2 deletions bleep-cli/src/scala/bleep/commands/BuildShow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object BuildShow {
Right(())
}
}
case class Exploded(started: Started, projects: List[model.CrossProjectName]) extends BleepCommand {
case class Exploded(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
projects.foreach { crossProjectName =>
val p0 = started.build.explodedProjects(crossProjectName)
Expand All @@ -32,7 +32,7 @@ object BuildShow {
}
}

case class Bloop(started: Started, projects: List[model.CrossProjectName]) extends BleepCommand {
case class Bloop(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
projects.foreach { crossProjectName =>
val f = started.bloopFiles(crossProjectName).forceGet
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/BleepCommandRemote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class BleepCommandRemote(started: Started) extends BleepCommand {
buildClient.failed match {
case empty if empty.isEmpty => Right(())
case failed =>
Left(new BspCommandFailed("Failed", failed.map(projectFromBuildTarget).toList, BspCommandFailed.NoDetails))
Left(new BspCommandFailed("Failed", failed.map(projectFromBuildTarget).toArray, BspCommandFailed.NoDetails))
}
}
finally
Expand Down
6 changes: 3 additions & 3 deletions bleep-core/src/scala/bleep/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class Commands(started: Started) {
cmd.run().orThrow

def clean(projects: List[model.CrossProjectName]): Unit =
force(commands.Clean(started, projects))
force(commands.Clean(started, projects.toArray))

def compile(projects: List[model.CrossProjectName]): Unit =
force(commands.Compile(started, projects))
force(commands.Compile(started, projects.toArray))

def run(project: model.CrossProjectName, maybeOverriddenMain: Option[String] = None, args: List[String] = Nil): Unit =
force(commands.Run(started, project, maybeOverriddenMain, args))

def test(projects: List[model.CrossProjectName]): Unit =
force(commands.Test(started, projects))
force(commands.Test(started, projects.toArray))

def script(name: model.ScriptName, args: List[String]): Unit =
force(commands.Script(started, name, args))
Expand Down
10 changes: 5 additions & 5 deletions bleep-core/src/scala/bleep/Started.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ case class Started(
rewrites: List[BuildRewrite],
build: model.Build,
bloopFiles: GenBloopFiles.Files,
activeProjectsFromPath: List[model.CrossProjectName],
activeProjectsFromPath: Option[Array[model.CrossProjectName]],
lazyConfig: Lazy[model.BleepConfig],
resolver: Lazy[CoursierResolver],
executionContext: ExecutionContext
Expand All @@ -40,16 +40,16 @@ case class Started(
FetchJvm(new BleepCacheLogger(logger), jvm, executionContext)
}

def chosenProjects(maybeFromCommandLine: Option[List[model.CrossProjectName]]): List[model.CrossProjectName] =
def chosenProjects(maybeFromCommandLine: Option[Array[model.CrossProjectName]]): Array[model.CrossProjectName] =
maybeFromCommandLine match {
case Some(fromCommandLine) => fromCommandLine.sorted
case None =>
activeProjectsFromPath match {
case Nil => build.explodedProjects.keys.toList.sorted
case nonEmpty => nonEmpty
case None => build.explodedProjects.keys.toArray.sorted
case Some(nonEmpty) => nonEmpty
}
}

def chosenTestProjects(maybeFromCommandLine: Option[List[model.CrossProjectName]]): List[model.CrossProjectName] =
def chosenTestProjects(maybeFromCommandLine: Option[Array[model.CrossProjectName]]): Array[model.CrossProjectName] =
chosenProjects(maybeFromCommandLine).filter(projectName => build.explodedProjects(projectName).isTestProject.getOrElse(false))
}
25 changes: 17 additions & 8 deletions bleep-core/src/scala/bleep/bootstrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,23 @@ object bootstrap {
val lazyResolver = lazyConfig.map(bleepConfig => resolver(pre, bleepConfig, buildFile))
val build = rewrites.foldLeft[model.Build](model.Build.FileBacked(buildFile)) { case (b, rewrite) => rewrite(b) }

val activeProjects: List[model.CrossProjectName] =
if (pre.buildPaths.cwd == pre.buildPaths.buildDir) build.explodedProjects.keys.toList
else
build.explodedProjects.flatMap { case (crossProjectName, p) =>
val folder = pre.buildPaths.buildDir / p.folder.getOrElse(RelPath.force(crossProjectName.name.value))
if (folder.startsWith(pre.buildPaths.cwd)) Some(crossProjectName)
else None
}.toList
val activeProjects: Option[Array[model.CrossProjectName]] =
if (pre.buildPaths.cwd == pre.buildPaths.buildDir) None
else {
val chosen =
build.explodedProjects.flatMap { case (crossProjectName, p) =>
val folder = pre.buildPaths.project(crossProjectName, p).dir
if (folder.startsWith(pre.buildPaths.cwd)) Some(crossProjectName)
else None
}.toArray

if (chosen.length != build.explodedProjects.size) {
pre.logger.info(
s"${chosen.length} of ${build.explodedProjects.size} projects active from ${pre.buildPaths.cwd}. run `bleep projects` to see which"
)
}
Some(chosen).filter(_.nonEmpty)
}

val ec = ExecutionContext.global
val fetchNode = new FetchNode(new BleepCacheLogger(pre.logger), ec)
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/bsp/BspCommandFailed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import bleep.{model, BleepException}
import ch.epfl.scala.bsp4j
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError

class BspCommandFailed(what: String, projects: List[model.CrossProjectName], reason: BspCommandFailed.Reason)
class BspCommandFailed(what: String, projects: Array[model.CrossProjectName], reason: BspCommandFailed.Reason)
extends BleepException(s"$what ${projects.map(_.value).mkString(", ")} ${reason.str}", reason.throwable.orNull)

object BspCommandFailed {
Expand Down
6 changes: 3 additions & 3 deletions bleep-core/src/scala/bleep/commands/Clean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import bleep.internal.FileUtils
import java.nio.file.{Files, Path}
import scala.util.Properties

case class Clean(started: Started, projects: List[model.CrossProjectName]) extends BleepCommand {
case class Clean(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
val outDirectories: List[Path] =
val outDirectories: Array[Path] =
projects.map(projectName => started.bloopFiles(projectName).forceGet.project.out).filter(Files.exists(_))

Right {
Expand All @@ -23,7 +23,7 @@ case class Clean(started: Started, projects: List[model.CrossProjectName]) exten
cli(
action = "clean files",
cwd = FileUtils.TempDir,
cmd = List(List("rm", "-Rf"), outDirectories.map(_.toString)).flatten,
cmd = List(Array("rm", "-Rf"), outDirectories.map(_.toString)).flatten,
cliLogger = cli.CliLogger(started.logger)
)
outDirectories.foreach(directory => started.logger.info(s"Deleted $directory"))
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/commands/Compile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ch.epfl.scala.bsp4j

import scala.build.bloop.BloopServer

case class Compile(started: Started, projects: List[model.CrossProjectName]) extends BleepCommandRemote(started) {
case class Compile(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommandRemote(started) {
override def runWithServer(bloop: BloopServer): Either[BleepException, Unit] = {
val targets = buildTargets(started.buildPaths, projects)
val result = bloop.server.buildTargetCompile(new bsp4j.CompileParams(targets)).get()
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/commands/Dist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Dist {
case class Dist(started: Started, options: Dist.Options) extends BleepCommandRemote(started) {
override def runWithServer(bloop: BloopServer): Either[BleepException, Unit] =
for {
_ <- Compile(started, List(options.project)).runWithServer(bloop)
_ <- Compile(started, Array(options.project)).runWithServer(bloop)
mainClass <- options.overrideMain match {
case Some(x) => Right(x)
case None =>
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/commands/PublishLocal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object PublishLocal {

case class PublishLocal(started: Started, options: PublishLocal.Options) extends BleepCommandRemote(started) {
override def runWithServer(bloop: BloopServer): Either[BleepException, Unit] =
Compile(started, options.projects).runWithServer(bloop).map { case () =>
Compile(started, options.projects.toArray).runWithServer(bloop).map { case () =>
val packagedLibraries: SortedMap[model.CrossProjectName, PackagedLibrary] =
packageLibraries(
started,
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/commands/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ case class Run(
started.logger.debug(params.toString)

def failed(reason: BspCommandFailed.Reason) =
Left(new BspCommandFailed("Run", List(project), reason))
Left(new BspCommandFailed("Run", Array(project), reason))

Try(bloop.server.buildTargetRun(params).get().getStatusCode) match {
case Success(bsp4j.StatusCode.OK) => Right(started.logger.info("Run succeeded"))
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/scala/bleep/commands/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ch.epfl.scala.bsp4j.CompileParams

import scala.build.bloop.BloopServer

case class Test(started: Started, projects: List[model.CrossProjectName]) extends BleepCommandRemote(started) {
case class Test(started: Started, projects: Array[model.CrossProjectName]) extends BleepCommandRemote(started) {
override def runWithServer(bloop: BloopServer): Either[BleepException, Unit] = {
val targets = buildTargets(started.buildPaths, projects)
// workaround for https://github.com/scalacenter/bloop/pull/1839
Expand Down
32 changes: 16 additions & 16 deletions bleep-model/src/scala/bleep/model/ProjectGlobs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package bleep.model

/** Represents named groups of projects based on scala version and platforms
*/
class ProjectGlobs(activeProjectsFromPath: List[CrossProjectName], explodedProjects: Map[CrossProjectName, Project]) {
def projectCompletions(projects: Iterable[CrossProjectName]): Map[String, Iterable[CrossProjectName]] = {
val crossNames: Map[String, Iterable[CrossProjectName]] =
projects.map(projectName => projectName.value -> List(projectName)).toMap
val projectNames: Map[String, Iterable[CrossProjectName]] =
class ProjectGlobs(activeProjectsFromPath: Option[Array[CrossProjectName]], explodedProjects: Map[CrossProjectName, Project]) {
def projectCompletions(projects: Array[CrossProjectName]): Map[String, Array[CrossProjectName]] = {
val crossNames: Map[String, Array[CrossProjectName]] =
projects.map(projectName => projectName.value -> Array(projectName)).toMap
val projectNames: Map[String, Array[CrossProjectName]] =
projects.groupBy { case CrossProjectName(name, _) => name.value }
val crossIds: Map[String, Iterable[CrossProjectName]] =
val crossIds: Map[String, Array[CrossProjectName]] =
projects
.groupBy { case name @ CrossProjectName(_, crossId) =>
crossId.orElse {
Expand All @@ -28,20 +28,20 @@ class ProjectGlobs(activeProjectsFromPath: List[CrossProjectName], explodedProje
def exactProjectMap: Map[String, CrossProjectName] =
explodedProjects.map { case (crossName, _) => crossName.value -> crossName }

def projectNameMap: Map[String, Iterable[CrossProjectName]] = {
val projects: Iterable[CrossProjectName] =
def projectNameMap: Map[String, Array[CrossProjectName]] = {
val projects: Array[CrossProjectName] =
activeProjectsFromPath match {
case Nil => explodedProjects.keys
case nonEmpty => nonEmpty
case None => explodedProjects.keys.toArray
case Some(nonEmpty) => nonEmpty
}
projectCompletions(projects)
}

def testProjectNameMap: Map[String, Iterable[CrossProjectName]] = {
val projects: Iterable[CrossProjectName] =
def testProjectNameMap: Map[String, Array[CrossProjectName]] = {
val projects: Array[CrossProjectName] =
activeProjectsFromPath match {
case Nil => explodedProjects.keys
case nonEmpty => nonEmpty
case None => explodedProjects.keys.toArray
case Some(nonEmpty) => nonEmpty
}

val testProjects = projects.filter(projectName => explodedProjects(projectName).isTestProject.getOrElse(false))
Expand All @@ -52,8 +52,8 @@ class ProjectGlobs(activeProjectsFromPath: List[CrossProjectName], explodedProje
def projectNamesNoCrossMap: Map[String, ProjectName] = {
val projects: Iterable[CrossProjectName] =
activeProjectsFromPath match {
case Nil => explodedProjects.keys
case nonEmpty => nonEmpty
case None => explodedProjects.keys
case Some(nonEmpty) => nonEmpty
}
projects.map(p => (p.name.value, p.name)).toMap
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ case class keepSelectedProjects(selectedProjectGlobs: List[String]) extends Buil
b.result()
}
protected def newExplodedProjects(oldBuild: model.Build): Map[model.CrossProjectName, model.Project] = {
val globs = new model.ProjectGlobs(Nil, oldBuild.explodedProjects)
val selectedProjectNames = selectedProjectGlobs.flatMap(str => globs.projectNameMap.getOrElse(str, Nil))
val globs = new model.ProjectGlobs(None, oldBuild.explodedProjects)
val selectedProjectNames = selectedProjectGlobs.flatMap(str => globs.projectNameMap.getOrElse(str, Array.empty[model.CrossProjectName]))
val withTransitive = selectedPlusTransitiveDeps(selectedProjectNames, oldBuild)

val chosen = oldBuild.explodedProjects.filter { case (name, _) => withTransitive(name) }
Expand Down

0 comments on commit d7ea459

Please sign in to comment.