From 23c72f4e2778083907e1180c09c3c85d27c38b0f Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Fri, 21 Oct 2011 18:37:36 +0200 Subject: [PATCH] KotlinCompiler accepts directory --- .../org/jetbrains/jet/cli/KotlinCompiler.java | 112 +++++++++++------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java index 9d943679e2b..67d88d00740 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/KotlinCompiler.java @@ -6,6 +6,7 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import org.jetbrains.jet.codegen.ClassFileFactory; @@ -28,12 +29,13 @@ import java.util.List; /** * @author yole + * @author alex.tkachman */ public class KotlinCompiler { public static void main(String[] args) { System.setProperty("java.awt.headless", "true"); if (args.length < 1) { - System.out.println("Usage: KotlinCompiler "); + System.out.println("Usage: KotlinCompiler or "); return; } @@ -44,61 +46,36 @@ public class KotlinCompiler { }; JavaCoreEnvironment environment = new JavaCoreEnvironment(root); - String javaHome = System.getenv("JAVA_HOME"); - File rtJar = null; - if (javaHome == null) { - ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); - if(systemClassLoader instanceof URLClassLoader) { - URLClassLoader loader = (URLClassLoader) systemClassLoader; - for(URL url: loader.getURLs()) { - if("file".equals(url.getProtocol())) { - if(url.getFile().endsWith("/lib/rt.jar")) { - rtJar = new File(url.getFile()); - break; - } - if(url.getFile().endsWith("/Classes/classes.jar")) { - rtJar = new File(url.getFile()).getAbsoluteFile(); - break; - } - } - } - } - - if(rtJar == null) { - System.out.println("JAVA_HOME environment variable needs to be defined"); - return; - } - } - else { - rtJar = findRtJar(javaHome); - } - - if (rtJar == null || !rtJar.exists()) { - System.out.print("No rt.jar found under JAVA_HOME=" + javaHome); - return; - } + File rtJar = initJdk(); + if (rtJar == null) return; environment.addToClasspath(rtJar); environment.registerFileType(JetFileType.INSTANCE, "kt"); - environment.registerFileType(JetFileType.INSTANCE, "jet"); environment.registerParserDefinition(new JetParserDefinition()); + VirtualFile vFile = environment.getLocalFileSystem().findFileByPath(args [0]); if (vFile == null) { - System.out.print("File not found: " + args[0]); + System.out.print("File/directory not found: " + args[0]); return; } Project project = environment.getProject(); GenerationState generationState = new GenerationState(project, false); List namespaces = Lists.newArrayList(); - PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile); - if (psiFile instanceof JetFile) { - namespaces.add(((JetFile) psiFile).getRootNamespace()); + if(vFile.isDirectory()) { + File dir = new File(vFile.getPath()); + addFiles(environment, project, namespaces, dir); } else { - System.out.print("Not a Kotlin file: " + vFile.getPath()); - return; + PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile); + if (psiFile instanceof JetFile) { + namespaces.add(((JetFile) psiFile).getRootNamespace()); + } + else { + System.out.print("Not a Kotlin file: " + vFile.getPath()); + return; + } } BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).analyzeNamespaces(project, namespaces, JetControlFlowDataTraceFactory.EMPTY); @@ -137,6 +114,59 @@ public class KotlinCompiler { } + private static void addFiles(JavaCoreEnvironment environment, Project project, List namespaces, File dir) { + for(File file : dir.listFiles()) { + if(!file.isDirectory()) { + VirtualFile virtualFile = environment.getLocalFileSystem().findFileByPath(file.getAbsolutePath()); + PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); + if (psiFile instanceof JetFile) { + namespaces.add(((JetFile) psiFile).getRootNamespace()); + System.out.println(file.getAbsolutePath()); + } + } + else { + addFiles(environment, project, namespaces, file); + } + } + } + + private static File initJdk() { + String javaHome = System.getenv("JAVA_HOME"); + File rtJar = null; + if (javaHome == null) { + ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); + if(systemClassLoader instanceof URLClassLoader) { + URLClassLoader loader = (URLClassLoader) systemClassLoader; + for(URL url: loader.getURLs()) { + if("file".equals(url.getProtocol())) { + if(url.getFile().endsWith("/lib/rt.jar")) { + rtJar = new File(url.getFile()); + break; + } + if(url.getFile().endsWith("/Classes/classes.jar")) { + rtJar = new File(url.getFile()).getAbsoluteFile(); + break; + } + } + } + } + + if(rtJar == null) { + System.out.println("JAVA_HOME environment variable needs to be defined"); + return null; + } + } + else { + rtJar = findRtJar(javaHome); + } + + if (rtJar == null || !rtJar.exists()) { + System.out.print("No rt.jar found under JAVA_HOME=" + javaHome); + return null; + } + return rtJar; + } + private static void report(Diagnostic diagnostic) { System.out.println(diagnostic.getMessage()); }