IDEAConfig move to idea module (circular dependency).
Slightly modify Config class.
This commit is contained in:
+54
-11
@@ -14,18 +14,21 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.k2js.config;
|
package org.jetbrains.jet.plugin.project;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
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 org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
|
import org.jetbrains.k2js.config.Config;
|
||||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.net.URI;
|
||||||
import java.io.InputStream;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -37,22 +40,62 @@ import java.util.zip.ZipFile;
|
|||||||
*/
|
*/
|
||||||
public final class IDEAConfig extends Config {
|
public final class IDEAConfig extends Config {
|
||||||
|
|
||||||
@NotNull
|
@Nullable
|
||||||
private final String pathToLibZip;
|
private final String pathToLibZip;
|
||||||
|
|
||||||
public IDEAConfig(@NotNull Project project, @NotNull String pathToLibZip) {
|
public IDEAConfig(@NotNull Project project) {
|
||||||
super(project);
|
super(project);
|
||||||
this.pathToLibZip = pathToLibZip;
|
this.pathToLibZip = getLibLocationForProject(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDEAConfig(@NotNull Project project) {
|
//TODO: refactor
|
||||||
//TODO: testing purposes. Should not get anywhere near production
|
@Nullable
|
||||||
this(project, "C:\\Dev\\Projects\\Kotlin\\clean_jet\\js\\js.libraries\\src\\k2jslib.zip");
|
private static String getLibLocationForProject(@NotNull Project project) {
|
||||||
|
VirtualFile indicationFile = JsModuleDetector.findIndicationFileInContextRoots(project);
|
||||||
|
if (indicationFile == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
InputStream stream = indicationFile.getInputStream();
|
||||||
|
String path = FileUtil.loadTextAndClose(stream);
|
||||||
|
String pathToLibFile = getFirstLine(path);
|
||||||
|
if (pathToLibFile == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
URI pathToLibFileUri = new URI(pathToLibFile);
|
||||||
|
URI pathToIndicationFileUri = new URI(indicationFile.getPath());
|
||||||
|
return pathToIndicationFileUri.resolve(pathToLibFileUri).toString();
|
||||||
|
}
|
||||||
|
catch (URISyntaxException e) {
|
||||||
|
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: util
|
||||||
|
@Nullable
|
||||||
|
private static String getFirstLine(@NotNull String path) throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new StringReader(path));
|
||||||
|
try {
|
||||||
|
return reader.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
finally {
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public List<JetFile> getLibFiles() {
|
public List<JetFile> generateLibFiles() {
|
||||||
|
if (pathToLibZip == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
File file = new File(pathToLibZip);
|
File file = new File(pathToLibZip);
|
||||||
ZipFile zipFile = new ZipFile(file);
|
ZipFile zipFile = new ZipFile(file);
|
||||||
@@ -71,7 +71,7 @@ public final class TestConfig extends Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<JetFile> getLibFiles() {
|
public List<JetFile> generateLibFiles() {
|
||||||
if (jsLibFiles == null) {
|
if (jsLibFiles == null) {
|
||||||
jsLibFiles = initLibFiles(getProject());
|
jsLibFiles = initLibFiles(getProject());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<orderEntry type="module" module-name="frontend" />
|
<orderEntry type="module" module-name="frontend" />
|
||||||
<orderEntry type="library" name="js-libs" level="project" />
|
<orderEntry type="library" name="js-libs" level="project" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
||||||
|
<orderEntry type="module" module-name="idea" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package org.jetbrains.k2js.analyze;
|
|||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.base.Predicates;
|
import com.google.common.base.Predicates;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -37,13 +39,10 @@ import org.jetbrains.jet.lang.resolve.*;
|
|||||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
||||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||||
|
import org.jetbrains.jet.plugin.project.IDEAConfig;
|
||||||
import org.jetbrains.k2js.config.Config;
|
import org.jetbrains.k2js.config.Config;
|
||||||
import org.jetbrains.k2js.config.IDEAConfig;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
@@ -101,9 +100,9 @@ public enum AnalyzerFacadeForJS implements AnalyzerFacade {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
|
public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
|
||||||
List<JetFile> allFiles = new ArrayList<JetFile>();
|
Set<JetFile> allFiles = Sets.newHashSet();
|
||||||
allFiles.addAll(files);
|
allFiles.addAll(toOriginal(files));
|
||||||
allFiles.addAll(config.getLibFiles());
|
allFiles.addAll(toOriginal(config.getLibFiles()));
|
||||||
return allFiles;
|
return allFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,12 +112,21 @@ public enum AnalyzerFacadeForJS implements AnalyzerFacade {
|
|||||||
@Override
|
@Override
|
||||||
public boolean apply(@Nullable PsiFile file) {
|
public boolean apply(@Nullable PsiFile file) {
|
||||||
assert file instanceof JetFile;
|
assert file instanceof JetFile;
|
||||||
@SuppressWarnings("UnnecessaryLocalVariable") boolean notLibFile = !jsLibFiles.contains(file);
|
@SuppressWarnings("UnnecessaryLocalVariable") boolean notLibFile = !jsLibFiles.contains(file.getOriginalFile());
|
||||||
return notLibFile;
|
return notLibFile;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static Collection<JetFile> toOriginal(@NotNull Collection<JetFile> files) {
|
||||||
|
Collection<JetFile> result = Lists.newArrayList();
|
||||||
|
for (JetFile file : files) {
|
||||||
|
result.add(file);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
//TODO: exclude?
|
//TODO: exclude?
|
||||||
@NotNull
|
@NotNull
|
||||||
public static BindingContext analyzeNamespace(@NotNull JetFile file) {
|
public static BindingContext analyzeNamespace(@NotNull JetFile file) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.config;
|
|||||||
|
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -52,6 +53,8 @@ public abstract class Config {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Project project;
|
private final Project project;
|
||||||
|
@Nullable
|
||||||
|
private List<JetFile> libFiles = null;
|
||||||
|
|
||||||
public Config(@NotNull Project project) {
|
public Config(@NotNull Project project) {
|
||||||
this.project = project;
|
this.project = project;
|
||||||
@@ -63,5 +66,13 @@ public abstract class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract List<JetFile> getLibFiles();
|
protected abstract List<JetFile> generateLibFiles();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public final List<JetFile> getLibFiles() {
|
||||||
|
if (libFiles == null) {
|
||||||
|
libFiles = generateLibFiles();
|
||||||
|
}
|
||||||
|
return libFiles;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ import org.jetbrains.jet.lang.psi.JetFile;
|
|||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||||
import org.jetbrains.jet.plugin.JetMainDetector;
|
import org.jetbrains.jet.plugin.JetMainDetector;
|
||||||
|
import org.jetbrains.jet.plugin.project.IDEAConfig;
|
||||||
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
|
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
|
||||||
import org.jetbrains.k2js.config.Config;
|
import org.jetbrains.k2js.config.Config;
|
||||||
import org.jetbrains.k2js.config.IDEAConfig;
|
|
||||||
import org.jetbrains.k2js.generate.CodeGenerator;
|
import org.jetbrains.k2js.generate.CodeGenerator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.utils.GenerationUtils;
|
import org.jetbrains.k2js.utils.GenerationUtils;
|
||||||
@@ -50,8 +50,7 @@ public final class K2JSTranslator {
|
|||||||
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
|
public static void translateWithCallToMainAndSaveToFile(@NotNull List<JetFile> files,
|
||||||
@NotNull String outputPath,
|
@NotNull String outputPath,
|
||||||
@NotNull Project project) throws Exception {
|
@NotNull Project project) throws Exception {
|
||||||
K2JSTranslator translator = new K2JSTranslator(new IDEAConfig(project,
|
K2JSTranslator translator = new K2JSTranslator(new IDEAConfig(project));
|
||||||
"C:\\Dev\\Projects\\Kotlin\\clean_jet\\js\\js.libraries\\src\\k2jslib.zip"));
|
|
||||||
String programCode = translator.generateProgramCode(files) + "\n";
|
String programCode = translator.generateProgramCode(files) + "\n";
|
||||||
JetFile fileWithMain = JetMainDetector.getFileWithMain(files);
|
JetFile fileWithMain = JetMainDetector.getFileWithMain(files);
|
||||||
if (fileWithMain == null) {
|
if (fileWithMain == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user