KotlinCompiler accepts directory
This commit is contained in:
@@ -6,6 +6,7 @@ import com.intellij.openapi.Disposable;
|
|||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
import com.intellij.psi.PsiDirectory;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.psi.PsiManager;
|
import com.intellij.psi.PsiManager;
|
||||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||||
@@ -28,12 +29,13 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yole
|
* @author yole
|
||||||
|
* @author alex.tkachman
|
||||||
*/
|
*/
|
||||||
public class KotlinCompiler {
|
public class KotlinCompiler {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.setProperty("java.awt.headless", "true");
|
System.setProperty("java.awt.headless", "true");
|
||||||
if (args.length < 1) {
|
if (args.length < 1) {
|
||||||
System.out.println("Usage: KotlinCompiler <filename>");
|
System.out.println("Usage: KotlinCompiler <filename> or <dirname>");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,61 +46,36 @@ public class KotlinCompiler {
|
|||||||
};
|
};
|
||||||
JavaCoreEnvironment environment = new JavaCoreEnvironment(root);
|
JavaCoreEnvironment environment = new JavaCoreEnvironment(root);
|
||||||
|
|
||||||
String javaHome = System.getenv("JAVA_HOME");
|
File rtJar = initJdk();
|
||||||
File rtJar = null;
|
if (rtJar == null) return;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
environment.addToClasspath(rtJar);
|
environment.addToClasspath(rtJar);
|
||||||
|
|
||||||
environment.registerFileType(JetFileType.INSTANCE, "kt");
|
environment.registerFileType(JetFileType.INSTANCE, "kt");
|
||||||
environment.registerFileType(JetFileType.INSTANCE, "jet");
|
|
||||||
environment.registerParserDefinition(new JetParserDefinition());
|
environment.registerParserDefinition(new JetParserDefinition());
|
||||||
|
|
||||||
VirtualFile vFile = environment.getLocalFileSystem().findFileByPath(args [0]);
|
VirtualFile vFile = environment.getLocalFileSystem().findFileByPath(args [0]);
|
||||||
if (vFile == null) {
|
if (vFile == null) {
|
||||||
System.out.print("File not found: " + args[0]);
|
System.out.print("File/directory not found: " + args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Project project = environment.getProject();
|
Project project = environment.getProject();
|
||||||
GenerationState generationState = new GenerationState(project, false);
|
GenerationState generationState = new GenerationState(project, false);
|
||||||
List<JetNamespace> namespaces = Lists.newArrayList();
|
List<JetNamespace> namespaces = Lists.newArrayList();
|
||||||
PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
|
if(vFile.isDirectory()) {
|
||||||
if (psiFile instanceof JetFile) {
|
File dir = new File(vFile.getPath());
|
||||||
namespaces.add(((JetFile) psiFile).getRootNamespace());
|
addFiles(environment, project, namespaces, dir);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.print("Not a Kotlin file: " + vFile.getPath());
|
PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
|
||||||
return;
|
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);
|
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) {
|
private static void report(Diagnostic diagnostic) {
|
||||||
System.out.println(diagnostic.getMessage());
|
System.out.println(diagnostic.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user