Move TestConfig to js.tests module. Remove K2JSTranslationUtils. Remove core.Dummy class. TestConfig references library files using project file structure instead of getResourceAsStream.

This commit is contained in:
pTalanov
2012-02-28 15:18:16 +04:00
parent 8c2d20f303
commit 57c9de776e
5 changed files with 41 additions and 144 deletions
@@ -1,114 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.config;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import core.Dummy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Pavel Talanov
*/
//TODO: review/refactor
public final class TestConfig extends Config {
//TODO: provide some generic way to get the files of the project
@NotNull
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
"/core/annotations.kt",
"/jquery/common.kt",
"/jquery/ui.kt",
"/core/javautil.kt",
"/core/javalang.kt",
"/core/core.kt",
"/core/math.kt",
"/core/json.kt",
"/raphael/raphael.kt",
"/html5/canvas.kt",
"/html5/files.kt",
"/html5/image.kt"
);
@Nullable
private /*var*/ List<JetFile> jsLibFiles = null;
@NotNull
private final Project project;
public TestConfig(@NotNull Project project) {
this.project = project;
}
@NotNull
@Override
public Project getProject() {
return project;
}
@NotNull
private static List<JetFile> initLibFiles(@NotNull Project project) {
List<JetFile> libFiles = new ArrayList<JetFile>();
for (String libFileName : LIB_FILE_NAMES) {
JetFile file = null;
//TODO: close stream?
InputStream stream = Dummy.class.getResourceAsStream(libFileName);
try {
String text = FileUtil.loadTextAndClose(stream);
file = JetFileUtils.createPsiFile(libFileName, text, project);
} catch (IOException e) {
e.printStackTrace();
}
libFiles.add(file);
}
return libFiles;
}
@NotNull
public List<JetFile> getLibFiles() {
if (jsLibFiles == null) {
jsLibFiles = initLibFiles(getProject());
}
return jsLibFiles;
}
// @NotNull
// private static String readString(@NotNull InputStream is) throws IOException {
// char[] buf = new char[2048];
// Reader r = new InputStreamReader(is, "UTF-8");
// StringBuilder s = new StringBuilder();
// while (true) {
// int n = r.read(buf);
// if (n < 0)
// break;
// s.append(buf, 0, n);
// }
// return s.toString();
// }
}
@@ -26,7 +26,6 @@ import org.jetbrains.jet.plugin.JetMainDetector;
import org.jetbrains.k2js.analyze.Analyzer;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.IDEAConfig;
import org.jetbrains.k2js.config.TestConfig;
import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.translate.general.Translation;
import org.jetbrains.k2js.utils.GenerationUtils;
@@ -35,10 +34,12 @@ import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
//TODO: clean up the code
@@ -1,74 +0,0 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.facade;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.TestConfig;
/**
* Created by IntelliJ IDEA.
* User: Natalia.Ukhorskaya
* Date: 2/9/12
* Time: 7:49 PM
*/
public class K2JSTranslatorUtils {
@SuppressWarnings("FieldCanBeLocal")
private static String EXCEPTION = "exception=";
@Nullable
public String translateToJS(@NotNull Project project, @NotNull String code, @NotNull String arguments) {
try {
return generateJSCode(project, code, arguments);
} catch (AssertionError e) {
reportException(e);
return EXCEPTION + "Translation error.";
} catch (UnsupportedOperationException e) {
reportException(e);
return EXCEPTION + "Unsupported feature.";
} catch (Throwable e) {
reportException(e);
return EXCEPTION + "Unexpected exception.";
}
}
@Nullable
public BindingContext getBindingContext(@NotNull Project project, @NotNull String programText) {
try {
K2JSTranslator k2JSTranslator = new K2JSTranslator(new TestConfig(project));
return k2JSTranslator.analyzeProgramCode(programText);
} catch (Throwable e) {
e.printStackTrace();
reportException(e);
return null;
}
}
@NotNull
private String generateJSCode(@NotNull Project project, @NotNull String code, @NotNull String arguments) {
String generatedCode = (new K2JSTranslator(new TestConfig(project))).translateStringWithCallToMain(code, arguments);
return generatedCode;
}
private void reportException(@NotNull Throwable e) {
System.out.println("Exception in translateToJS!!!");
e.printStackTrace();
}
}