Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resources to the configuration file #730

Merged
merged 2 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object NativeBridge {
if (workdir.isDirectory) Paths.delete(workdir)
Files.createDirectories(workdir.underlying)

val classpath = project.classpath.map(_.underlying)
val classpath = project.compilationClasspath.map(_.underlying)
val nativeLogger = NativeLogger(logger.debug _, logger.info _, logger.warn _, logger.error _)
val config = setUpNativeConfig(project, config0)
val nativeMode = config.mode match {
Expand Down Expand Up @@ -66,7 +66,7 @@ object NativeBridge {
if (config.nativelib.toString.nonEmpty) config.nativelib
else {
Discover
.nativelib(project.classpath.map(_.underlying))
.nativelib(project.compilationClasspath.map(_.underlying))
.getOrElse(sys.error("Fatal: nativelib is missing and could not be found."))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object JsBridge {
target: Path,
logger: BloopLogger
): Unit = {
val classpath = project.classpath.map(_.underlying)
val classpath = project.compilationClasspath.map(_.underlying)
val classpathIrFiles = classpath
.filter(Files.isDirectory(_))
.flatMap(findIrFiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object JsBridge {
}

val cache = new IRFileCache().newCache
val irClasspath = FileScalaJSIRContainer.fromClasspath(project.classpath.map(_.toFile))
val irClasspath = FileScalaJSIRContainer.fromClasspath(project.compilationClasspath.map(_.toFile))
val irFiles = cache.cached(irClasspath)

val moduleInitializers = mainClass match {
Expand Down
6 changes: 4 additions & 2 deletions config/src/main/scala/bloop/config/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ object Config {
classpath: List[Path],
out: Path,
classesDir: Path,
resources: Option[List[Path]],
`scala`: Option[Scala],
java: Option[Java],
sbt: Option[Sbt],
Expand All @@ -193,15 +194,15 @@ object Config {

object Project {
// FORMAT: OFF
private[bloop] val empty: Project = Project("", emptyPath, List(), List(), List(), emptyPath, emptyPath, None, None, None, None, None, None)
private[bloop] val empty: Project = Project("", emptyPath, List(), List(), List(), emptyPath, emptyPath, None, None, None, None, None, None, None)
// FORMAT: ON

def analysisFileName(projectName: String) = s"$projectName-analysis.bin"
}

case class File(version: String, project: Project)
object File {
final val LatestVersion = "1.1.0-M1"
final val LatestVersion = "1.1.0-M2"

private[bloop] val empty = File(LatestVersion, Project.empty)

Expand Down Expand Up @@ -237,6 +238,7 @@ object Config {
List(scalaLibraryJar),
outDir,
classesDir,
Some(List(outDir.resolve("resource1.xml"))),
Some(
Scala(
"org.scala-lang",
Expand Down
26 changes: 26 additions & 0 deletions config/src/main/scala/bloop/config/util/ConfigUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bloop.config.util

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

object ConfigUtil {
def pathsOutsideRoots(roots: Seq[Path], paths: Seq[Path]): Seq[Path] = {
paths.filterNot { path =>
roots.exists { root =>
var found: Boolean = false
val rootDirSize = root.toString.size
var currentTarget = (if (Files.isRegularFile(path)) path.getParent else path).toAbsolutePath
while (!found &&
currentTarget != null &&
// Use a heuristic to know if we should short-circuit and return false
currentTarget.toString.size >= rootDirSize) {
if (currentTarget == root) {
found = true
}

currentTarget = currentTarget.getParent
}
found
}
}
}
}
2 changes: 1 addition & 1 deletion frontend/src/main/scala/bloop/bsp/BloopBspServices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ final class BloopBspServices(
bsp.ScalacOptionsItem(
target = target,
options = project.scalacOptions.toList,
classpath = project.classpath.map(e => bsp.Uri(e.toBspUri)).toList,
classpath = project.compilationClasspath.map(e => bsp.Uri(e.toBspUri)).toList,
classDirectory = bsp.Uri(project.classesDir.toBspUri)
)
}.toList
Expand Down
21 changes: 20 additions & 1 deletion frontend/src/main/scala/bloop/data/Project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import xsbti.compile.{ClasspathOptions, CompileOrder}
import bloop.ScalaInstance
import bloop.bsp.ProjectUris
import bloop.config.{Config, ConfigEncoderDecoders}
import bloop.engine.Dag
import bloop.engine.tasks.toolchains.{JvmToolchain, ScalaJsToolchain, ScalaNativeToolchain}
import ch.epfl.scala.{bsp => Bsp}

Expand All @@ -19,6 +20,7 @@ final case class Project(
dependencies: List[String],
scalaInstance: Option[ScalaInstance],
rawClasspath: List[AbsolutePath],
resources: List[AbsolutePath],
compileSetup: Config.CompileSetup,
classesDir: AbsolutePath,
scalacOptions: List[String],
Expand All @@ -33,11 +35,12 @@ final case class Project(
resolution: Option[Config.Resolution],
origin: Origin
) {

/** The bsp uri associated with this project. */
val bspUri: Bsp.Uri = Bsp.Uri(ProjectUris.toURI(baseDirectory, name))

/** This project's full classpath (classes directory and raw classpath) */
val classpath: Array[AbsolutePath] = (classesDir :: rawClasspath).toArray
val compilationClasspath: Array[AbsolutePath] = (classesDir :: rawClasspath).toArray

val classpathOptions: ClasspathOptions = {
ClasspathOptions.of(
Expand All @@ -63,6 +66,20 @@ final case class Project(
case _ => false
}
}

def fullClasspathFor(dag: Dag[Project]): Array[AbsolutePath] = {
val cp = compilationClasspath.toBuffer
// Add the resources right after the classes directory if found in the classpath
Dag.dfs(dag).foreach { p =>
val index = cp.indexOf(p.classesDir)
// If there is an anomaly and the classes dir of a dependency is missing, add resource at end
if (index == -1) {
p.resources.foreach(r => cp.append(r))
}
else cp.insertAll(index, p.resources)
}
cp.toArray
}
}

object Project {
Expand Down Expand Up @@ -113,13 +130,15 @@ object Project {
val analysisOut = scala
.flatMap(_.analysis.map(AbsolutePath.apply))
.getOrElse(out.resolve(Config.Project.analysisFileName(project.name)))
val resources = project.resources.toList.flatten.map(AbsolutePath.apply)

Project(
project.name,
AbsolutePath(project.directory),
project.dependencies,
instance,
project.classpath.map(AbsolutePath.apply),
resources,
setup,
AbsolutePath(project.classesDir),
scala.map(_.options).getOrElse(Nil),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object CompilationTask {
instance,
compilerCache,
sources.toArray,
project.classpath,
project.compilationClasspath,
graphInputs.store,
project.classesDir,
project.out,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/main/scala/bloop/engine/tasks/Tasks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object Tasks {
import state.logger
project.scalaInstance match {
case Some(instance) =>
val classpath = project.classpath
val classpath = project.fullClasspathFor(state.build.getDagFor(project))
val entries = classpath.map(_.underlying.toFile).toSeq
logger.debug(s"Setting up the console classpath with ${entries.mkString(", ")}")(
DebugFilter.All)
Expand Down Expand Up @@ -182,7 +182,7 @@ object Tasks {
fqn: String,
args: Array[String]
): Task[State] = {
val classpath = project.classpath
val classpath = project.fullClasspathFor(state.build.getDagFor(project))
val processConfig = Forker(javaEnv, classpath)
val runTask = processConfig.runMain(cwd, fqn, args, state.logger, state.commonOptions)
runTask.map { exitCode =>
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/main/scala/bloop/engine/tasks/TestTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bloop.engine.tasks
import bloop.cli.ExitStatus
import bloop.config.Config
import bloop.data.{Platform, Project}
import bloop.engine.{Feedback, State}
import bloop.engine.{Dag, Feedback, State}
import bloop.engine.tasks.toolchains.ScalaJsToolchain
import bloop.exec.Forker
import bloop.io.AbsolutePath
Expand Down Expand Up @@ -129,7 +129,8 @@ object TestTask {
implicit val logContext: DebugFilter = DebugFilter.Test
project.platform match {
case Platform.Jvm(env, _, _) =>
val forker = Forker(env, project.classpath)
val classpath = project.fullClasspathFor(state.build.getDagFor(project))
val forker = Forker(env, classpath)
val testLoader = forker.newClassLoader(Some(TestInternals.filteredLoader))
val frameworks = project.testFrameworks.flatMap(f =>
TestInternals.loadFramework(testLoader, f.names, logger))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ object CompileGraph {

// Let's order the IRs exactly in the same order as provided in the classpath!
// Required for symbol clashes in dependencies (`AppLoader` in guardian/frontend)
val indexDirs = project.classpath.iterator.filter(_.isDirectory).zipWithIndex.toMap
val indexDirs = project.compilationClasspath.iterator.filter(_.isDirectory).zipWithIndex.toMap
val dependentStore = {
val transitiveStores =
results.flatMap(r => indexDirs.get(r.bundle.project.classesDir).iterator.map(i => i -> r.store))
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/test/scala/bloop/bsp/BspProtocolSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class BspProtocolSpec {
Assert.assertEquals(obtainedUri, expectedUri)
val obtainedOptions =
stringifyOptions(opts.options, opts.classpath, opts.classDirectory)
val classpath = p.classpath.iterator.map(i => bsp.Uri(i.toBspUri)).toList
val classpath = p.compilationClasspath.iterator.map(i => bsp.Uri(i.toBspUri)).toList
val classesDir = bsp.Uri(p.classesDir.toBspUri)
val expectedOptions =
stringifyOptions(p.scalacOptions.toList, classpath, classesDir)
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/test/scala/bloop/engine/DagSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class DagSpec {
// format: OFF
def dummyOrigin = TestUtil.syntheticOriginFor(dummyPath)
def dummyProject(name: String, dependencies: List[String]): Project =
Project(name, dummyPath, dependencies, Some(dummyInstance), Nil, compileOptions, dummyPath, Nil,
Nil, Nil, Nil, Config.TestOptions.empty, dummyPath, dummyPath,
Project(name, dummyPath, dependencies, Some(dummyInstance), Nil, Nil, compileOptions,
dummyPath, Nil, Nil, Nil, Nil, Config.TestOptions.empty, dummyPath, dummyPath,
Project.defaultPlatform(logger), None, None, dummyOrigin)
// format: ON

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/test/scala/bloop/exec/ForkerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ForkerSpec {
val cwdPath = AbsolutePath(cwd)
val project = TestUtil.getProject(TestUtil.RootProject, state)
val env = JavaEnv.default
val classpath = project.classpath
val classpath = project.fullClasspathFor(state.build.getDagFor(project))
val config = Forker(env, classpath)
val logger = new RecordingLogger
val opts = state.commonOptions.copy(env = TestUtil.runAndTestProperties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class IntegrationTestSuite(testDirectory: Path) {
dependencies = previousProjects.map(_.name),
scalaInstance = previousProjects.head.scalaInstance,
rawClasspath = Nil,
resources = Nil,
compileSetup = Config.CompileSetup.empty,
classesDir = classesDir,
scalacOptions = Nil,
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/test/scala/bloop/tasks/JsTestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class JsTestSpec(
@Test
def testsAreDetected(): Unit = {
// Load the project's classpath by filtering out unwanted FQNs to create the test loader
val classpathEntries = testProject.classpath.map(_.underlying.toUri.toURL)
val classpath = testProject.fullClasspathFor(testState.build.getDagFor(testProject))
val classpathEntries = classpath.map(_.underlying.toUri.toURL)
val testLoader = new URLClassLoader(classpathEntries, Some(TestInternals.filteredLoader).orNull)
def frameworks(classLoader: ClassLoader): List[Framework] = {
testProject.testFrameworks.flatMap(f =>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/test/scala/bloop/tasks/JvmTestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class JvmTestSpec(

private val processRunnerConfig: Forker = {
val javaEnv = JavaEnv.default
val classpath = testProject.classpath
val classpath = testProject.fullClasspathFor(testState.build.getDagFor(testProject))
val classpathEntries = classpath.map(_.underlying.toUri.toURL)
Forker(javaEnv, classpath)
}

Expand Down
2 changes: 0 additions & 2 deletions frontend/src/test/scala/bloop/tasks/TestResourcesSpec.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package bloop.tasks

import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.experimental.categories.Category

import sbt.internal.util.EscHelpers.removeEscapeSequences

import bloop.cli.Commands
import bloop.exec.JavaEnv
import bloop.tasks.TestUtil.{loadTestProject, runAndCheck}

@Category(Array(classOf[bloop.FastTests]))
Expand Down
1 change: 1 addition & 0 deletions frontend/src/test/scala/bloop/tasks/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ object TestUtil {
dependencies = dependencies.toList,
scalaInstance = scalaInstance,
rawClasspath = classpath,
resources = Nil,
compileSetup = Config.CompileSetup.empty.copy(order = compileOrder),
classesDir = AbsolutePath(target),
scalacOptions = Nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ final class BloopConverter(parameters: BloopParameters) {
classpath = classpath,
out = project.getBuildDir.toPath,
classesDir = classesDir,
resources = Some(getResources(sourceSet)),
`scala` = scalaConfig,
java = getJavaConfig(project, sourceSet),
sbt = None,
Expand All @@ -118,6 +119,9 @@ final class BloopConverter(parameters: BloopParameters) {
private def getSources(sourceSet: SourceSet): List[Path] =
sourceSet.getAllSource.getSrcDirs.asScala.map(_.toPath).toList

private def getResources(sourceSet: SourceSet): List[Path] =
sourceSet.getResources.getSrcDirs.asScala.map(_.toPath).toList

private def isProjectDependency(
projectDependencies: List[ProjectDependency],
resolvedArtifact: ResolvedArtifact
Expand Down
Loading