Skip to content

Commit

Permalink
Merge pull request #327 from eed3si9n/wip/brdige-problem
Browse files Browse the repository at this point in the history
Remove JDK-version dependent code out of the compiler bridge
  • Loading branch information
dwijnand authored Jun 30, 2017
2 parents d2ac89e + 82cdf80 commit d7ddfe8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
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

0 comments on commit d7ddfe8

Please sign in to comment.