Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jun 24, 2024
1 parent f07e02b commit 8a4e818
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ final class BuildTargets private (
def javaTarget(id: BuildTargetIdentifier): Option[JavaTarget] =
data.fromOptions(_.javaTarget(id))

def jvmTarget(id: BuildTargetIdentifier): Option[JvmTarget] =
data.fromOptions(_.jvmTarget(id))

def fullClasspath(
id: BuildTargetIdentifier,
cancelPromise: Promise[Unit],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package scala.meta.internal.metals
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath

import ch.epfl.scala.bsp4j.BuildTargetIdentifier

trait JvmTarget {

def displayName: String

def id: BuildTargetIdentifier

/**
* If the build server supports lazy classpath resolution, we will
* not get any classpath data eagerly and we should not
Expand Down
61 changes: 61 additions & 0 deletions metals/src/main/scala/scala/meta/internal/metals/ManifestJar.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package scala.meta.internal.metals

import java.nio.file.Files
import java.nio.file.Path
import java.util.jar.Attributes
import java.util.jar.JarOutputStream
import java.util.jar.Manifest

import scala.concurrent.ExecutionContext
import scala.util.Using

import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.mtags.URIEncoderDecoder
import scala.meta.internal.process.SystemProcess
import scala.meta.io.AbsolutePath

object ManifestJar {
def withTempManifestJar(
classpath: Seq[Path]
)(
op: AbsolutePath => SystemProcess
)(implicit ec: ExecutionContext): SystemProcess = {
val manifestJar =
createManifestJar(
AbsolutePath(
Files.createTempFile("jvm-forker-manifest", ".jar").toAbsolutePath
),
classpath,
)

val process = op(manifestJar)
process.complete.onComplete { case _ =>
manifestJar.delete()
}
process
}

def createManifestJar(
manifestJar: AbsolutePath,
classpath: Seq[Path],
): AbsolutePath = {
if (!manifestJar.exists) {
manifestJar.touch()
manifestJar.toNIO.toFile().deleteOnExit()
}

val classpathStr =
classpath
.map(path => URIEncoderDecoder.encode(path.toUri().toString()))
.mkString(" ")

val manifest = new Manifest()
manifest.getMainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0")
manifest.getMainAttributes.put(Attributes.Name.CLASS_PATH, classpathStr)

val out = Files.newOutputStream(manifestJar.toNIO)
Using.resource(new JarOutputStream(out, manifest))(identity)
manifestJar
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -332,32 +332,28 @@ class DebugProvider(
if (buildServer.isDebuggingProvider || buildServer.isSbt) {
buildServer.startDebugSession(params, cancelPromise)
} else {
def getDebugee: Option[Future[MetalsDebuggee]] =
def getDebugee: Option[MetalsDebuggee] =
params.getDataKind() match {
case b.DebugSessionParamsDataKind.SCALA_MAIN_CLASS =>
for {
id <- params.getTargets().asScala.headOption
projectInfo <- debugConfigCreator.create(id, cancelPromise)
projectInfo <- debugConfigCreator.create(id)
scalaMainClass <- params.asScalaMainClass()
} yield {
projectInfo.map(
new MainClassDebugAdapter(
workspace,
scalaMainClass,
_,
userConfig().javaHome,
)
)
}
} yield new MainClassDebugAdapter(
workspace,
scalaMainClass,
projectInfo,
userConfig().javaHome,
)
case _ => None
}

for {
_ <- compilations.compileTargets(params.getTargets().asScala.toSeq)
debuggee <- getDebugee.getOrElse(
} yield {
val debuggee = getDebugee.getOrElse(
throw new RuntimeException(s"Can't resolve debugee")
)
} yield {
val dapLogger = new DebugLogger()
val resolver = new MetalsDebugToolsResolver()
val handler =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package scala.meta.internal.metals.debug

import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.jar

import scala.util.Properties
import scala.util.Try
import scala.util.Using

import scala.meta.internal.metals.Directories
import scala.meta.internal.metals.ManifestJar
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.mtags.MD5
import scala.meta.internal.mtags.URIEncoderDecoder
import scala.meta.io.AbsolutePath

import ch.epfl.scala.bsp4j.ScalaMainClass
Expand Down Expand Up @@ -87,39 +84,16 @@ object ExtendedScalaMainClass {
classpath: List[String],
workspace: AbsolutePath,
): String = {

val classpathDigest = MD5.compute(classpath.mkString)
val manifestJar =
workspace
.resolve(Directories.tmp)
.resolve(s"classpath_${classpathDigest}.jar")

if (!manifestJar.exists) {

manifestJar.touch()
manifestJar.toNIO.toFile().deleteOnExit()

val classpathStr =
classpath
.map(path =>
URIEncoderDecoder.encode(Paths.get(path).toUri().toString())
)
.mkString(" ")

val manifest = new jar.Manifest()
manifest.getMainAttributes.put(
jar.Attributes.Name.MANIFEST_VERSION,
"1.0",
)
manifest.getMainAttributes.put(
jar.Attributes.Name.CLASS_PATH,
classpathStr,
)

val out = Files.newOutputStream(manifestJar.toNIO)
Using.resource(new jar.JarOutputStream(out, manifest))(identity)

ManifestJar.createManifestJar(manifestJar, classpath.map(Paths.get(_)))
}

manifestJar.toString()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package scala.meta.internal.metals.debug.server

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.Promise

import scala.meta.internal.metals.BuildTargets
import scala.meta.internal.metals.JavaTarget
import scala.meta.internal.metals.JvmTarget
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ScalaTarget

Expand All @@ -19,19 +15,10 @@ import ch.epfl.scala.debugadapter.SourceJar
import ch.epfl.scala.debugadapter.StandaloneSourceFile
import ch.epfl.scala.debugadapter.UnmanagedEntry

class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
ec: ExecutionContext
) {
def create(
id: BuildTargetIdentifier,
cancelPromise: Promise[Unit],
): Option[Future[DebugeeProject]] = {
val optScalaTarget = buildTargets.scalaTarget(id)
val optJavaTarget = buildTargets.javaTarget(id)
class DebugeeParamsCreator(buildTargets: BuildTargets) {
def create(id: BuildTargetIdentifier): Option[DebugeeProject] = {
for {
name <- optScalaTarget
.map(_.displayName)
.orElse(optJavaTarget.map(_.displayName))
target <- buildTargets.jvmTarget(id)
data <- buildTargets.targetData(id)
} yield {

Expand All @@ -42,38 +29,27 @@ class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
val debugLibs = libraries.flatMap(createLibrary(_))
val includedInLibs = debugLibs.map(_.absolutePath).toSet

val optClasspath =
buildTargets
.targetClasspath(id, cancelPromise)
.getOrElse(Future.successful(Nil))
.map(
_.filter(_.endsWith(".jar")).toAbsoluteClasspath.map(_.toNIO).toSeq
)
val classpath = buildTargets.targetJarClasspath(id).getOrElse(Nil)

for (classpath <- optClasspath) yield {
val filteredClassPath = classpath.collect {
case path if !includedInLibs(path) => UnmanagedEntry(path)
}.toList
val filteredClassPath = classpath.collect {
case path if !includedInLibs(path.toNIO) => UnmanagedEntry(path.toNIO)
}.toList

val modules = buildTargets
.allInverseDependencies(id)
.flatMap(id =>
buildTargets.scalaTarget(id).map(createModule(_)).orElse {
buildTargets.javaTarget(id).map(createModule(_))
}
)
.toSeq
val modules = buildTargets
.allInverseDependencies(id)
.flatMap(buildTargets.jvmTarget)
.map(createModule(_))
.toSeq

val scalaVersion = optScalaTarget.map(_.scalaVersion)
val scalaVersion = buildTargets.scalaTarget(id).map(_.scalaVersion)

new DebugeeProject(
scalaVersion,
name,
modules,
debugLibs,
filteredClassPath,
)
}
new DebugeeProject(
scalaVersion,
target.displayName,
modules,
debugLibs,
filteredClassPath,
)
}
}

Expand All @@ -93,26 +69,25 @@ class DebugeeParamsCreator(buildTargets: BuildTargets)(implicit
)
}

def createModule(target: ScalaTarget): Module = {
val scalaVersion = ScalaVersion(target.scalaVersion)
def createModule(target: JvmTarget): Module = {
val (scalaVersion, scalacOptions) =
target match {
case scalaTarget: ScalaTarget =>
(
Some(ScalaVersion(scalaTarget.scalaVersion)),
scalaTarget.scalac.getOptions().asScala.toSeq,
)
case _ => (None, Nil)
}
new Module(
target.displayName,
Some(scalaVersion),
target.scalac.getOptions().asScala.toSeq,
scalaVersion,
scalacOptions,
target.classDirectory.toAbsolutePath.toNIO,
sources(target.id),
)
}

def createModule(target: JavaTarget) =
new Module(
target.displayName,
None,
Nil,
target.classDirectory.toAbsolutePath.toNIO,
sources(target.id),
)

private def sources(id: BuildTargetIdentifier) =
buildTargets.sourceItemsToBuildTargets
.filter(_._2.iterator.asScala.contains(id))
Expand Down
Loading

0 comments on commit 8a4e818

Please sign in to comment.