refactor compiler launcher
* method should not modify input * replace String with File * add @NotNull and @Nullable annotations
This commit is contained in:
@@ -97,8 +97,10 @@ public class BytecodeCompiler {
|
||||
*/
|
||||
public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) {
|
||||
try {
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, false, true
|
||||
/* Last arg is ignored anyway */);
|
||||
K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath);
|
||||
configuration.getEnvironment().addSources(src);
|
||||
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, null, new File(output), false, true);
|
||||
if ( ! success ) {
|
||||
throw new CompileEnvironmentException( errorMessage( src, false ));
|
||||
}
|
||||
@@ -120,7 +122,10 @@ public class BytecodeCompiler {
|
||||
*/
|
||||
public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) {
|
||||
try {
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, false, includeRuntime);
|
||||
K2JVMCompileEnvironmentConfiguration configuration = env(stdlib, classpath);
|
||||
configuration.getEnvironment().addSources(src);
|
||||
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration, new File(jar), null, false, includeRuntime);
|
||||
if ( ! success ) {
|
||||
throw new CompileEnvironmentException( errorMessage( src, false ));
|
||||
}
|
||||
@@ -145,7 +150,7 @@ public class BytecodeCompiler {
|
||||
K2JVMCompileEnvironmentConfiguration env = env(stdlib, classpath);
|
||||
List<Module> modules = CompileEnvironmentUtil.loadModuleScript(module, env.getMessageCollector());
|
||||
File directory = new File(module).getParentFile();
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, jar, null, includeRuntime);
|
||||
boolean success = KotlinToJVMBytecodeCompiler.compileModules(env, modules, directory, new File(jar), null, includeRuntime);
|
||||
if ( ! success ) {
|
||||
throw new CompileEnvironmentException( errorMessage( module, false ));
|
||||
}
|
||||
|
||||
@@ -106,6 +106,9 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
|
||||
try {
|
||||
configureEnvironment(configuration, arguments);
|
||||
|
||||
File jar = arguments.jar != null ? new File(arguments.jar) : null;
|
||||
File outputDir = arguments.outputDir != null ? new File(arguments.outputDir) : null;
|
||||
|
||||
boolean noErrors;
|
||||
if (arguments.module != null) {
|
||||
boolean oldVerbose = messageCollector.isVerbose();
|
||||
@@ -115,28 +118,30 @@ public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments, K2JVMComp
|
||||
messageCollector.setVerbose(oldVerbose);
|
||||
File directory = new File(arguments.module).getParentFile();
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
|
||||
directory, arguments.jar, arguments.outputDir,
|
||||
directory, jar, outputDir,
|
||||
arguments.includeRuntime);
|
||||
}
|
||||
else {
|
||||
// TODO ideally we'd unify to just having a single field that supports multiple files/dirs
|
||||
if (arguments.getSourceDirs() != null) {
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSourceDirectories(configuration,
|
||||
arguments.getSourceDirs(), arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
|
||||
arguments.getSourceDirs(), jar, outputDir, arguments.script, arguments.includeRuntime);
|
||||
}
|
||||
else {
|
||||
List<String> sources = Lists.newArrayList();
|
||||
if (arguments.src != null) {
|
||||
sources.add(arguments.src);
|
||||
configuration.getEnvironment().addSources(arguments.src);
|
||||
}
|
||||
if (arguments.script) {
|
||||
sources.add(arguments.freeArgs.get(0));
|
||||
configuration.getEnvironment().addSources(arguments.freeArgs.get(0));
|
||||
}
|
||||
else {
|
||||
sources.addAll(arguments.freeArgs);
|
||||
for (String freeArg : arguments.freeArgs) {
|
||||
configuration.getEnvironment().addSources(freeArg);
|
||||
}
|
||||
}
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(configuration,
|
||||
sources, arguments.jar, arguments.outputDir, arguments.script, arguments.includeRuntime);
|
||||
|
||||
noErrors = KotlinToJVMBytecodeCompiler.compileBunchOfSources(
|
||||
configuration, jar, outputDir, arguments.script, arguments.includeRuntime);
|
||||
}
|
||||
}
|
||||
return noErrors ? OK : COMPILATION_ERROR;
|
||||
|
||||
@@ -276,7 +276,7 @@ public class CompileEnvironmentUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeToOutputDirectory(ClassFileFactory factory, final String outputDir) {
|
||||
public static void writeToOutputDirectory(ClassFileFactory factory, @NotNull File outputDir) {
|
||||
List<String> files = factory.files();
|
||||
for (String file : files) {
|
||||
File target = new File(outputDir, file);
|
||||
|
||||
+8
-8
@@ -90,8 +90,8 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
K2JVMCompileEnvironmentConfiguration configuration,
|
||||
@NotNull List<Module> modules,
|
||||
@NotNull File directory,
|
||||
@Nullable String jarPath,
|
||||
@Nullable String outputDir,
|
||||
@Nullable File jarPath,
|
||||
@Nullable File outputDir,
|
||||
boolean jarRuntime) {
|
||||
|
||||
for (Module moduleBuilder : modules) {
|
||||
@@ -108,7 +108,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(moduleFactory, outputDir);
|
||||
}
|
||||
else {
|
||||
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
|
||||
File path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar");
|
||||
FileOutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = new FileOutputStream(path);
|
||||
@@ -131,10 +131,10 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean compileBunchOfSources(
|
||||
public static boolean compileBunchOfSources(
|
||||
K2JVMCompileEnvironmentConfiguration configuration,
|
||||
String jar,
|
||||
String outputDir,
|
||||
@Nullable File jar,
|
||||
@Nullable File outputDir,
|
||||
boolean script,
|
||||
boolean includeRuntime
|
||||
) {
|
||||
@@ -202,7 +202,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
|
||||
public static boolean compileBunchOfSources(
|
||||
K2JVMCompileEnvironmentConfiguration configuration,
|
||||
List<String> sourceFilesOrDirs, String jar, String outputDir, boolean script, boolean includeRuntime) {
|
||||
List<String> sourceFilesOrDirs, File jar, File outputDir, boolean script, boolean includeRuntime) {
|
||||
for (String sourceFileOrDir : sourceFilesOrDirs) {
|
||||
configuration.getEnvironment().addSources(sourceFileOrDir);
|
||||
}
|
||||
@@ -213,7 +213,7 @@ public class KotlinToJVMBytecodeCompiler {
|
||||
public static boolean compileBunchOfSourceDirectories(
|
||||
K2JVMCompileEnvironmentConfiguration configuration,
|
||||
|
||||
List<String> sources, String jar, String outputDir, boolean script, boolean includeRuntime) {
|
||||
List<String> sources, File jar, File outputDir, boolean script, boolean includeRuntime) {
|
||||
for (String source : sources) {
|
||||
configuration.getEnvironment().addSources(source);
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class CompileJavaAgainstKotlinTest extends TestCaseWithTmpdir {
|
||||
|
||||
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
|
||||
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath());
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
|
||||
|
||||
+2
-2
@@ -92,7 +92,7 @@ public class CompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir {
|
||||
|
||||
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
|
||||
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, aDir.getPath());
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, aDir);
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class CompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir {
|
||||
|
||||
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
|
||||
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, bDir.getPath());
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, bDir);
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public class ReadKotlinBinaryClassTest extends TestCaseWithTmpdir {
|
||||
|
||||
ClassFileFactory classFileFactory = state.getFactory();
|
||||
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath());
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
|
||||
|
||||
NamespaceDescriptor namespaceFromSource = state.getBindingContext().get(BindingContext.FILE_TO_NAMESPACE, psiFile);
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ public class WriteSignatureTest extends TestCaseWithTmpdir {
|
||||
|
||||
ClassFileFactory classFileFactory = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, CompilerSpecialMode.REGULAR);
|
||||
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir.getPath());
|
||||
CompileEnvironmentUtil.writeToOutputDirectory(classFileFactory, tmpdir);
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user