KotlinCompiler accepts directory

This commit is contained in:
Alex Tkachman
2011-10-21 18:37:36 +02:00
parent 142322d3a1
commit 23c72f4e27
@@ -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 <filename>");
System.out.println("Usage: KotlinCompiler <filename> or <dirname>");
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<JetNamespace> 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<JetNamespace> 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());
}