-
Notifications
You must be signed in to change notification settings - Fork 309
/
SbtInputs.scala
176 lines (152 loc) · 6.64 KB
/
SbtInputs.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.scalaide.core.internal.builder.zinc
import java.io.File
import java.util.zip.ZipFile
import scala.collection.JavaConverters.enumerationAsScalaIteratorConverter
import org.eclipse.core.resources.IContainer
import org.eclipse.core.runtime.IPath
import org.eclipse.core.runtime.SubMonitor
import org.scalaide.core.IScalaInstallation
import org.scalaide.core.IScalaProject
import org.scalaide.core.internal.ScalaPlugin
import org.scalaide.core.internal.project.ScalaInstallation.scalaInstanceForInstallation
import org.scalaide.ui.internal.preferences
import org.scalaide.ui.internal.preferences.ScalaPluginSettings.compileOrder
import org.scalaide.util.internal.SettingConverterUtil
import org.scalaide.util.internal.SettingConverterUtil.convertNameToProperty
import sbt.ClasspathOptions
import sbt.Logger.xlog2Log
import sbt.classpath.ClasspathUtilities
import sbt.compiler.AnalyzingCompiler
import sbt.compiler.CompilerCache
import sbt.compiler.IC
import sbt.inc.Analysis
import sbt.inc.ClassfileManager
import sbt.inc.Locate
import xsbti.Logger
import xsbti.Maybe
import xsbti.Reporter
import xsbti.compile._
import scala.tools.nsc.settings.SpecificScalaVersion
import scala.tools.nsc.settings.ScalaVersion
import java.net.URLClassLoader
import org.scalaide.core.internal.project.ScalaInstallation.scalaInstanceForInstallation
import org.scalaide.core.IScalaInstallation
import org.scalaide.core.internal.ScalaPlugin
import org.eclipse.core.resources.IContainer
import org.eclipse.core.runtime.IPath
/** Inputs-like class, but not implementing xsbti.compile.Inputs.
*
* We return a real IncOptions instance, instead of relying on the Java interface,
* based on String maps. This allows us to use the transactional classfile writer.
*/
class SbtInputs(installation: IScalaInstallation,
sourceFiles: Seq[File],
project: IScalaProject,
javaMonitor: SubMonitor,
scalaProgress: CompileProgress,
tempDir: File, // used to store classfiles between compilation runs to implement all-or-nothing semantics
logger: Logger,
addToClasspath: Seq[IPath] = Seq.empty,
srcOutputs: Seq[(IContainer, IContainer)] = Seq.empty) {
def cache = CompilerCache.fresh // May want to explore caching possibilities.
private val allProjects = project +: project.transitiveDependencies.flatMap(ScalaPlugin().asScalaProject)
def analysisMap(f: File): Maybe[Analysis] =
if (f.isFile)
Maybe.just(Analysis.Empty)
else {
val analysis = allProjects.collectFirst {
case project if project.buildManager.buildManagerOf(f).nonEmpty =>
project.buildManager.buildManagerOf(f).get.latestAnalysis(incOptions)
}
Maybe.just(analysis.getOrElse(Analysis.Empty))
}
def progress = Maybe.just(scalaProgress)
def incOptions: sbt.inc.IncOptions = {
sbt.inc.IncOptions.Default.
withApiDebug(apiDebug = project.storage.getBoolean(SettingConverterUtil.convertNameToProperty(preferences.ScalaPluginSettings.apiDiff.name))).
withRelationsDebug(project.storage.getBoolean(SettingConverterUtil.convertNameToProperty(preferences.ScalaPluginSettings.relationsDebug.name))).
withNewClassfileManager(ClassfileManager.transactional(tempDir, logger)).
withApiDumpDirectory(None).
withRecompileOnMacroDef(project.storage.getBoolean(SettingConverterUtil.convertNameToProperty(preferences.ScalaPluginSettings.recompileOnMacroDef.name))).
withNameHashing(project.storage.getBoolean(SettingConverterUtil.convertNameToProperty(preferences.ScalaPluginSettings.nameHashing.name)))
}
def options = new Options {
def outputFolders = srcOutputs.map {
case (_, out) => out.getRawLocation
}
def classpath = (project.scalaClasspath.userCp ++ addToClasspath ++ outputFolders)
.distinct
.map(_.toFile.getAbsoluteFile).toArray
def sources = sourceFiles.toArray
def output = new MultipleOutput {
private def sourceOutputFolders =
if (srcOutputs.nonEmpty) srcOutputs else project.sourceOutputFolders
def outputGroups = sourceOutputFolders.map {
case (src, out) => new MultipleOutput.OutputGroup {
def sourceDirectory = src.getLocation.toFile
def outputDirectory = out.getLocation.toFile
}
}.toArray
}
// remove arguments not understood by build compiler
def options =
if (project.isUsingCompatibilityMode())
project.scalacArguments.filter(buildCompilerOption).toArray
else
project.scalacArguments.toArray
/** Remove the source-level related arguments */
private def buildCompilerOption(arg: String): Boolean =
!arg.startsWith("-Xsource") && !(arg == "-Ymacro-expand:none")
def javacOptions = Array() // Not used.
import CompileOrder._
import SettingConverterUtil.convertNameToProperty
import preferences.ScalaPluginSettings.compileOrder
def order = project.storage.getString(convertNameToProperty(compileOrder.name)) match {
case "JavaThenScala" => JavaThenScala
case "ScalaThenJava" => ScalaThenJava
case _ => Mixed
}
}
/**
* @return Right-biased instance of Either (error message in Left, value in Right)
*/
def compilers: Either[String, Compilers[sbt.compiler.AnalyzingCompiler]] = {
val scalaInstance = scalaInstanceForInstallation(installation)
val store = ScalaPlugin().compilerInterfaceStore
store.compilerInterfaceFor(installation)(javaMonitor.newChild(10)).right.map {
compilerInterface =>
// prevent Sbt from adding things to the (boot)classpath
val cpOptions = new ClasspathOptions(false, false, false, autoBoot = false, filterLibrary = false)
new Compilers[AnalyzingCompiler] {
def javac = new JavaEclipseCompiler(project.underlying, javaMonitor)
def scalac = IC.newScalaCompiler(scalaInstance, compilerInterface.toFile, cpOptions, logger)
}
}
}
}
private[zinc] object Locator {
val NoClass = new DefinesClass {
def apply(className: String) = false
}
def apply(f: File): DefinesClass =
if (f.isDirectory)
new DirectoryLocator(f)
else if (f.exists && ClasspathUtilities.isArchive(f))
new JarLocator(f)
else
NoClass
class DirectoryLocator(dir: File) extends DefinesClass {
def apply(className: String): Boolean = Locate.classFile(dir, className).isFile
}
class JarLocator(jar: File) extends DefinesClass {
lazy val entries: Set[String] = {
val zipFile = new ZipFile(jar, ZipFile.OPEN_READ)
try {
import scala.collection.JavaConverters._
zipFile.entries.asScala.filterNot(_.isDirectory).map(_.getName).toSet
} finally
zipFile.close()
}
def apply(className: String): Boolean = entries.contains(className)
}
}