JS backend: LibrarySourcesConfig accepts source dir as source.
Idea send files in any case, but for easy debug run configurations we want to specify only source dir. (cherry picked from commit e194435)
This commit is contained in:
@@ -24,13 +24,11 @@ import org.jetbrains.k2js.facade.MainCallParameters;
|
||||
|
||||
/**
|
||||
* NOTE: for now K2JSCompiler supports only minimal amount of parameters required to launch it from the plugin.
|
||||
* You can specify path to the file where generated file will be stored, path to zipped library sources.
|
||||
*/
|
||||
public class K2JSCompilerArguments extends CompilerArguments {
|
||||
@Argument(value = "output", description = "Output file path")
|
||||
public String outputFile;
|
||||
|
||||
//NOTE: may well be a subject to change soon
|
||||
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
|
||||
public String[] libraryFiles;
|
||||
|
||||
|
||||
@@ -35,14 +35,10 @@ import org.jetbrains.k2js.test.config.TestConfigFactory;
|
||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
|
||||
|
||||
//TODO: use method object
|
||||
public final class TranslationUtils {
|
||||
|
||||
@@ -103,14 +99,14 @@ public final class TranslationUtils {
|
||||
@NotNull String outputFile,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull EcmaVersion version, TestConfigFactory configFactory) throws Exception {
|
||||
List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
|
||||
List<JetFile> jetFiles = createJetFileList(project, inputFiles, null);
|
||||
K2JSTranslator translator = new K2JSTranslator(getConfig(project, version, configFactory));
|
||||
FileUtil.writeToFile(new File(outputFile), translator.generateProgramCode(psiFiles, mainCallParameters));
|
||||
FileUtil.writeToFile(new File(outputFile), translator.generateProgramCode(jetFiles, mainCallParameters));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<JetFile> initLibFiles(@NotNull Project project) {
|
||||
return getLibFiles(project, Config.LIB_FILE_NAMES);
|
||||
return createJetFileList(project, Config.LIB_FILE_NAMES, Config.LIBRARIES_LOCATION);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -124,28 +120,21 @@ public final class TranslationUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<JetFile> getLibFiles(@NotNull Project project, @NotNull List<String> list) {
|
||||
private static List<JetFile> createJetFileList(@NotNull Project project, @NotNull List<String> list, @Nullable String root) {
|
||||
List<JetFile> libFiles = Lists.newArrayList();
|
||||
for (String libFileName : list) {
|
||||
JetFile file = null;
|
||||
try {
|
||||
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
||||
InputStream stream = new FileInputStream(Config.LIBRARIES_LOCATION + libFileName);
|
||||
try {
|
||||
String text = FileUtil.loadTextAndClose(stream);
|
||||
file = JetFileUtils.createPsiFile(libFileName, text, project);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
libFiles.add(file);
|
||||
String path = root == null ? libFileName : (root + libFileName);
|
||||
String text = FileUtil.loadFile(new File(path));
|
||||
JetFile jetFile = JetFileUtils.createJetFile(path, text, project);
|
||||
libFiles.add(jetFile);
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (IOException e) {
|
||||
//TODO: throw generic exception
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
return libFiles;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -21,11 +21,13 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.*;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
@@ -37,7 +39,7 @@ import java.util.zip.ZipFile;
|
||||
|
||||
public class LibrarySourcesConfig extends Config {
|
||||
@NotNull
|
||||
public static final Key<String> EXTERNAL_MODULE_NAME = new Key<String>("externalModule");
|
||||
public static final Key<String> EXTERNAL_MODULE_NAME = Key.create("externalModule");
|
||||
@NotNull
|
||||
public static final String UNKNOWN_EXTERNAL_MODULE_NAME = "<unknown>";
|
||||
|
||||
@@ -60,41 +62,53 @@ public class LibrarySourcesConfig extends Config {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<JetFile> generateLibFiles() {
|
||||
protected List<JetFile> generateLibFiles() {
|
||||
if (files.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<JetFile> jetFiles = new ArrayList<JetFile>();
|
||||
final List<JetFile> jetFiles = new ArrayList<JetFile>();
|
||||
String moduleName = UNKNOWN_EXTERNAL_MODULE_NAME;
|
||||
VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
|
||||
final PsiManager psiManager = PsiManager.getInstance(getProject());
|
||||
for (String path : files) {
|
||||
File file = new File(path);
|
||||
if (!file.exists()) continue; // The path here may be copying from a library root configuration, i.e. may be absent
|
||||
try {
|
||||
String name = file.getName();
|
||||
if (path.charAt(0) == '@') {
|
||||
moduleName = path.substring(1);
|
||||
continue;
|
||||
if (path.charAt(0) == '@') {
|
||||
moduleName = path.substring(1);
|
||||
}
|
||||
else if (path.endsWith(".jar") || path.endsWith(".zip")) {
|
||||
try {
|
||||
jetFiles.addAll(readZip(path));
|
||||
}
|
||||
|
||||
if (name.endsWith(".jar") || name.endsWith(".zip")) {
|
||||
jetFiles.addAll(readZip(file));
|
||||
}
|
||||
else {
|
||||
JetFile psiFile = JetFileUtils.createPsiFile(path, FileUtil.loadFile(file), getProject());
|
||||
psiFile.putUserData(EXTERNAL_MODULE_NAME, moduleName);
|
||||
jetFiles.add(psiFile);
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error("While processing " + file, e);
|
||||
else {
|
||||
VirtualFile file = fileSystem.findFileByPath(path);
|
||||
assert file != null;
|
||||
if (file.isDirectory()) {
|
||||
final String currentModuleName = moduleName;
|
||||
VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {
|
||||
@Override
|
||||
public boolean visitFile(@NotNull VirtualFile file) {
|
||||
if (file.getName().endsWith(".kt")) {
|
||||
addJetFile(jetFiles, currentModuleName, psiManager, file);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
addJetFile(jetFiles, moduleName, psiManager, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return jetFiles;
|
||||
}
|
||||
|
||||
private List<JetFile> readZip(File file) throws IOException {
|
||||
private List<JetFile> readZip(String file) throws IOException {
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
try {
|
||||
return traverseArchive(zipFile);
|
||||
@@ -113,11 +127,18 @@ public class LibrarySourcesConfig extends Config {
|
||||
if (!entry.isDirectory() && entry.getName().endsWith(".kt")) {
|
||||
InputStream stream = file.getInputStream(entry);
|
||||
String text = FileUtil.loadTextAndClose(stream);
|
||||
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
|
||||
JetFile jetFile = JetFileUtils.createJetFile(entry.getName(), text, getProject());
|
||||
jetFile.putUserData(EXTERNAL_MODULE_NAME, UNKNOWN_EXTERNAL_MODULE_NAME);
|
||||
result.add(jetFile);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static void addJetFile(List<JetFile> jetFiles, String moduleName, PsiManager psiManager, VirtualFile file) {
|
||||
PsiFile psiFile = psiManager.findFile(file);
|
||||
assert psiFile != null;
|
||||
psiFile.putUserData(EXTERNAL_MODULE_NAME, moduleName);
|
||||
jetFiles.add((JetFile) psiFile);
|
||||
}
|
||||
}
|
||||
@@ -74,8 +74,7 @@ public final class MetaInfServices {
|
||||
InputStream stream = loadClasspathResource(line);
|
||||
if (stream != null) {
|
||||
String text = FileUtil.loadTextAndClose(stream);
|
||||
JetFile file = JetFileUtils.createPsiFile(line, text, project);
|
||||
libFiles.add(file);
|
||||
libFiles.add(JetFileUtils.createJetFile(line, text, project));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public final class K2JSTranslator {
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@NotNull
|
||||
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) throws TranslationException {
|
||||
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
|
||||
JetFile file = JetFileUtils.createJetFile("test", programText, getProject());
|
||||
String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString))) + "\n";
|
||||
return FLUSH_SYSTEM_OUT + programCode + GET_SYSTEM_OUT;
|
||||
}
|
||||
|
||||
@@ -17,74 +17,23 @@
|
||||
package org.jetbrains.k2js.utils;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.impl.PsiFileFactoryImpl;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class JetFileUtils {
|
||||
|
||||
private JetFileUtils() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String loadFile(@NotNull String path) throws IOException {
|
||||
return doLoadFile(path);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String doLoadFile(@NotNull String path) throws IOException {
|
||||
String text = FileUtil.loadFile(new File(path), CharsetToolkit.UTF8).trim();
|
||||
text = StringUtil.convertLineSeparators(text);
|
||||
return text;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetFile createPsiFile(@NotNull String name,
|
||||
@NotNull String text,
|
||||
@NotNull Project project) {
|
||||
String fileName = name.endsWith(".kt") ? name : name + ".jet";
|
||||
return (JetFile) createFile(fileName, text, project);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JetFile loadPsiFile(@NotNull String name, @NotNull Project project) {
|
||||
try {
|
||||
return createPsiFile(name, loadFile(name), project);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiFile createFile(@NotNull String name, @NotNull String text, @NotNull Project project) {
|
||||
LightVirtualFile virtualFile = new LightVirtualFile(name, JetLanguage.INSTANCE, text);
|
||||
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
|
||||
PsiFile result = ((PsiFileFactoryImpl) PsiFileFactory.getInstance(project))
|
||||
.trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
|
||||
assert result != null;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static List<JetFile> createPsiFileList(@NotNull List<String> inputFiles,
|
||||
@NotNull Project project) {
|
||||
List<JetFile> psiFiles = new ArrayList<JetFile>();
|
||||
for (String inputFile : inputFiles) {
|
||||
psiFiles.add(loadPsiFile(inputFile, project));
|
||||
}
|
||||
return psiFiles;
|
||||
public static JetFile createJetFile(
|
||||
@NotNull String name,
|
||||
@NotNull String text,
|
||||
@NotNull Project project
|
||||
) {
|
||||
return (JetFile) PsiFileFactory.getInstance(project)
|
||||
.createFileFromText(name.endsWith(".kt") ? name : name + ".jet", JetLanguage.INSTANCE, text, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user