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

Remove JDK-version dependent code out of the compiler bridge #327

Merged
merged 1 commit into from
Jun 30, 2017
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 @@ -7,9 +7,7 @@

package xsbti.compile;

import xsbti.F0;
import xsbti.Logger;

import java.io.File;

/**
Expand All @@ -20,44 +18,6 @@
* compile them and then define the sbt component, which is reused across different sbt projects.
*/
public interface CompilerBridgeProvider {

/**
* Defines a constant {@link CompilerBridgeProvider} that returns an already compiled bridge.
* <p>
* This method is useful for external build tools that want full control over the retrieval
* and compilation of the compiler bridge, as well as the Scala instance to be used.
*
* @param file The jar or directory of the compiled Scala bridge.
* @return A provider that always returns the same compiled bridge.
*/
static CompilerBridgeProvider constant(File file, ScalaInstance scalaInstance) {
return new CompilerBridgeProvider() {
@Override
public File fetchCompiledBridge(ScalaInstance scalaInstance, Logger logger) {
logger.debug(new F0<String>() {
@Override
public String apply() {
String bridgeName = file.getAbsolutePath();
return "Returning already retrieved and compiled bridge: " + bridgeName + ".";
}
});
return file;
}

@Override
public ScalaInstance fetchScalaInstance(String scalaVersion, Logger logger) {
logger.debug(new F0<String>() {
@Override
public String apply() {
String instance = scalaInstance.toString();
return "Returning default scala instance:\n\t" + instance;
}
});
return scalaInstance;
}
};
}

/**
* Get the location of the compiled Scala compiler bridge for a concrete Scala version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final class IncHandler(directory: File, cacheDir: File, scriptedLog: ManagedLogg

private final val unit = (_: Seq[String]) => ()
def scalaCompiler(instance: xsbti.compile.ScalaInstance, bridgeJar: File): AnalyzingCompiler = {
val bridgeProvider = CompilerBridgeProvider.constant(bridgeJar, instance)
val bridgeProvider = ZincUtil.constantBridgeProvider(instance, bridgeJar)
val classpath = ClasspathOptionsUtil.boot
new AnalyzingCompiler(instance, bridgeProvider, classpath, unit, IncHandler.classLoaderCache)
}
Expand Down
40 changes: 40 additions & 0 deletions zinc/src/main/java/xsbti/compile/ZincCompilerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package xsbti.compile;

import java.io.File;
import xsbti.F0;
import xsbti.Logger;

/**
* Defines a util interface to get Scala compilers and the default implementation
Expand Down Expand Up @@ -51,4 +53,42 @@ public static ScalaCompiler scalaCompiler(ScalaInstance scalaInstance,
File compilerBridgeJar) {
return sbt.internal.inc.ZincUtil.scalaCompiler(scalaInstance, compilerBridgeJar);
}

/**
* Defines a constant {@link CompilerBridgeProvider} that returns an already compiled bridge.
* <p>
* This method is useful for external build tools that want full control over the retrieval
* and compilation of the compiler bridge, as well as the Scala instance to be used.
*
* @param file The jar or directory of the compiled Scala bridge.
* @return A provider that always returns the same compiled bridge.
*/
public static CompilerBridgeProvider constantBridgeProvider(ScalaInstance scalaInstance,
File compilerBridgeJar) {
return new CompilerBridgeProvider() {
@Override
public File fetchCompiledBridge(ScalaInstance scalaInstance, Logger logger) {
logger.debug(new F0<String>() {
@Override
public String apply() {
String bridgeName = compilerBridgeJar.getAbsolutePath();
return "Returning already retrieved and compiled bridge: " + bridgeName + ".";
}
});
return compilerBridgeJar;
}

@Override
public ScalaInstance fetchScalaInstance(String scalaVersion, Logger logger) {
logger.debug(new F0<String>() {
@Override
public String apply() {
String instance = scalaInstance.toString();
return "Returning default scala instance:\n\t" + instance;
}
});
return scalaInstance;
}
};
}
}
6 changes: 5 additions & 1 deletion zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object ZincUtil {
compilerBridgeJar: File,
classpathOptions: ClasspathOptions
): AnalyzingCompiler = {
val bridgeProvider = CompilerBridgeProvider.constant(compilerBridgeJar, scalaInstance)
val bridgeProvider = constantBridgeProvider(scalaInstance, compilerBridgeJar)
val emptyHandler = (_: Seq[String]) => ()
val loader = Some(new ClassLoaderCache(new URLClassLoader(Array())))
new AnalyzingCompiler(
Expand Down Expand Up @@ -126,4 +126,8 @@ object ZincUtil {
def compilers(javaTools: XJavaTools, scalac: ScalaCompiler): Compilers = {
new Compilers(scalac, javaTools)
}

def constantBridgeProvider(scalaInstance: xsbti.compile.ScalaInstance,
compilerBridgeJar: File): xsbti.compile.CompilerBridgeProvider =
ZincCompilerUtil.constantBridgeProvider(scalaInstance, compilerBridgeJar)
}
2 changes: 1 addition & 1 deletion zinc/src/test/scala/sbt/inc/BaseCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
}

def scalaCompiler(instance: xsbti.compile.ScalaInstance, bridgeJar: File): AnalyzingCompiler = {
val bridgeProvider = CompilerBridgeProvider.constant(bridgeJar, instance)
val bridgeProvider = ZincUtil.constantBridgeProvider(instance, bridgeJar)
val classpath = ClasspathOptionsUtil.boot
val cache = Some(new ClassLoaderCache(new URLClassLoader(Array())))
new AnalyzingCompiler(instance, bridgeProvider, classpath, _ => (), cache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class MultiProjectIncrementalSpec extends BridgeProviderSpecification {
}

def scalaCompiler(instance: ScalaInstance, bridgeJar: File): AnalyzingCompiler = {
val bridgeProvider = CompilerBridgeProvider.constant(bridgeJar, instance)
val bridgeProvider = ZincUtil.constantBridgeProvider(instance, bridgeJar)
val classpath = ClasspathOptionsUtil.boot
val cache = Some(new ClassLoaderCache(new URLClassLoader(Array())))
new AnalyzingCompiler(instance, bridgeProvider, classpath, _ => (), cache)
Expand Down