modified the JS compiler so that it can have definitions and library sources specified or discovered on the classpath; so for example we can then discover the kotlin standard library kotlin that needs to be compiled; plus the JS library definitions (which map to the kotlin-lib.js file)

This commit is contained in:
James Strachan
2012-05-30 15:50:06 +01:00
parent ae12b4e5fc
commit 4cff5f9759
10 changed files with 206 additions and 116 deletions
@@ -36,10 +36,7 @@ import org.jetbrains.jet.cli.common.messages.PrintingMessageCollector;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
import org.jetbrains.k2js.config.ClassPathLibrarySourcesConfig;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.config.ZippedLibrarySourcesConfig;
import org.jetbrains.k2js.config.*;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
@@ -72,12 +69,35 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
return ExitCode.INTERNAL_ERROR;
}
JetCoreEnvironment environmentForJS = getEnvironment(arguments, rootDisposable);
Config config = getConfig(arguments, environmentForJS.getProject());
JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
Project project = environmentForJS.getProject();
Config config = getConfig(arguments, project);
if (analyzeAndReportErrors(messageCollector, environmentForJS.getSourceFiles(), config)) {
return ExitCode.COMPILATION_ERROR;
}
if (arguments.srcdir != null) {
environmentForJS.addSources(arguments.srcdir);
}
if (arguments.sourceFiles != null) {
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
}
ClassPathLibrarySourcesLoader sourceLoader = new ClassPathLibrarySourcesLoader(project);
List<JetFile> sourceFiles = sourceLoader.findSourceFiles();
environmentForJS.getSourceFiles().addAll(sourceFiles);
if (arguments.isVerbose()) {
Iterable<String> fileNames = Iterables.transform(environmentForJS.getSourceFiles(), new Function<JetFile, String>() {
@Override
public String apply(@Nullable JetFile file) {
return file.getName();
}
});
System.out.println("Compiling source files: " + Joiner.on(", ").join(fileNames));
}
String outputFile = arguments.outputFile;
if (outputFile == null) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION);
@@ -89,29 +109,6 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
return translateAndGenerateOutputFile(mainCallParameters, messageCollector, environmentForJS, config, outputFile);
}
@NotNull
private static JetCoreEnvironment getEnvironment(K2JSCompilerArguments arguments, Disposable rootDisposable) {
final JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
if (arguments.srcdir != null) {
environmentForJS.addSources(arguments.srcdir);
}
if (arguments.sourceFiles != null) {
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
}
if (arguments.isVerbose()) {
Iterable<String> fileNames = Iterables.transform(environmentForJS.getSourceFiles(), new Function<JetFile, String>() {
@Override
public String apply(@Nullable JetFile file) {
return file.getName();
}
});
System.out.println("Compiling source files: " + Joiner.on(", ").join(fileNames));
}
return environmentForJS;
}
@NotNull
private static ExitCode translateAndGenerateOutputFile(@NotNull MainCallParameters mainCall,
@NotNull PrintingMessageCollector messageCollector,
@@ -146,9 +143,8 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
private static Config getConfig(@NotNull K2JSCompilerArguments arguments, @NotNull Project project) {
EcmaVersion ecmaVersion = EcmaVersion.fromString(arguments.target);
if (arguments.libzip == null) {
// lets discover the JS library source on the classpath
return new ClassPathLibrarySourcesConfig(project, ecmaVersion);
//return Config.getEmptyConfig(project, ecmaVersion);
// lets discover the JS library definitions on the classpath
return new ClassPathLibraryDefintionsConfig(project, ecmaVersion);
}
return new ZippedLibrarySourcesConfig(project, arguments.libzip, ecmaVersion);
}
@@ -17,26 +17,17 @@
package org.jetbrains.k2js.test.semantics;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.js.K2JSCompiler;
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
import org.jetbrains.k2js.test.rhino.RhinoFunctionNativeObjectResultChecker;
import org.jetbrains.k2js.utils.JetFileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.EnumSet;
import java.util.List;
/**
*/
@@ -60,23 +51,18 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
String... stdLibFiles) throws Exception {
List<String> files = Lists.newArrayList();
File stdlibDir = new File("libraries/stdlib/src");
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
for (String file : stdLibFiles) {
files.add(new File(stdlibDir, file).getPath());
}
generateJavaScriptFiles(files, getTestName(false) + ".kt", mainCallParameters, ecmaVersions);
/*
runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions),
new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!"));
*/
// lets add the standard JS library files
for (String libFileName : Config.LIB_FILE_NAMES) {
System.out.println("Compiling " + libFileName);
files.add(Config.LIBRARIES_LOCATION + libFileName);
}
// now lets try invoke the compiler
for (EcmaVersion version : ecmaVersions) {
System.out.println("Compiling with version: " + version);
@@ -0,0 +1,40 @@
/*
* Copyright 2010-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 org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.psi.JetFile;
import java.util.*;
/**
* A Config implementation which is configured with a directory to find the standard library names from
*/
public class ClassPathLibraryDefintionsConfig extends Config {
public static final String META_INF_SERVICES_FILE = "META-INF/services/org.jetbrains.kotlin.js.libraryDefinitions";
public ClassPathLibraryDefintionsConfig(@NotNull Project project, @NotNull EcmaVersion version) {
super(project, version);
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
return MetaInfServices.loadServicesFiles(META_INF_SERVICES_FILE, getProject());
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-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.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import java.util.*;
/**
* A helper class to load the kotlin library sources to be compiled to JavaScript as part of a JavaScript build
*/
public class ClassPathLibrarySourcesLoader {
public static final String META_INF_SERVICES_FILE = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
@NotNull private final Project project;
public ClassPathLibrarySourcesLoader(@NotNull Project project) {
this.project = project;
}
@NotNull
public List<JetFile> findSourceFiles() {
return MetaInfServices.loadServicesFiles(META_INF_SERVICES_FILE, project);
}
}
@@ -16,13 +16,9 @@
package org.jetbrains.k2js.config;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.BufferedReader;
@@ -33,34 +29,24 @@ import java.net.URL;
import java.util.*;
/**
* A Config implementation which is configured with a directory to find the standard library names from
* A helper class to discover a META-INF/services file on the classpath and load the files referenced inside it
*/
public class ClassPathLibrarySourcesConfig extends Config {
public static final String LIBRARY_SOURCES_FILES = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
@Nullable
private /*var*/ List<JetFile> jsLibFiles = null;
public ClassPathLibrarySourcesConfig(@NotNull Project project, @NotNull EcmaVersion version) {
super(project, version);
}
@NotNull
private List<JetFile> initLibFiles(@NotNull Project project) {
public class MetaInfServices {
public static List<JetFile> loadServicesFiles(String metaInfServicesFile, Project project) {
List<JetFile> libFiles = new ArrayList<JetFile>();
Set<URL> urlsLoaded = new HashSet<URL>();
try {
Enumeration<URL> resources = getClass().getClassLoader().getResources(LIBRARY_SOURCES_FILES);
loadLibFiles(resources, project, urlsLoaded, libFiles);
resources = Thread.currentThread().getContextClassLoader().getResources(LIBRARY_SOURCES_FILES);
loadLibFiles(resources, project, urlsLoaded, libFiles);
Enumeration<URL> resources = MetaInfServices.class.getClassLoader().getResources(metaInfServicesFile);
loadLibFiles(resources, urlsLoaded, libFiles, project);
resources = Thread.currentThread().getContextClassLoader().getResources(metaInfServicesFile);
loadLibFiles(resources, urlsLoaded, libFiles, project);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return libFiles;
}
private void loadLibFiles(Enumeration<URL> resources, @NotNull Project project, Set<URL> urlsLoaded, List<JetFile> libFiles) throws IOException {
protected static void loadLibFiles(Enumeration<URL> resources, Set<URL> urlsLoaded, List<JetFile> libFiles, Project project) throws IOException {
while (resources != null && resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url != null) {
@@ -84,6 +70,7 @@ public class ClassPathLibrarySourcesConfig extends Config {
String text = FileUtil.loadTextAndClose(stream);
JetFile file = JetFileUtils.createPsiFile(line, text, project);
if (file != null) {
//System.out.println("Parsing file: " + text);
libFiles.add(file);
}
}
@@ -102,19 +89,10 @@ public class ClassPathLibrarySourcesConfig extends Config {
* Tries to load the given resource name on the classpath
*/
public static InputStream loadClasspathResource(String resourceName) {
InputStream answer = ClassPathLibrarySourcesConfig.class.getClassLoader().getResourceAsStream(resourceName);
InputStream answer = ClassPathLibraryDefintionsConfig.class.getClassLoader().getResourceAsStream(resourceName);
if (answer == null) {
answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
}
return answer;
}
@Override
@NotNull
public List<JetFile> generateLibFiles() {
if (jsLibFiles == null) {
jsLibFiles = initLibFiles(getProject());
}
return jsLibFiles;
}
}
@@ -48,6 +48,7 @@ public class ZippedLibrarySourcesConfig extends Config {
@NotNull
@Override
public List<JetFile> generateLibFiles() {
System.out.println("Parsing JS library source zip: " + pathToLibZip);
if (pathToLibZip == null) {
return Collections.emptyList();
}
@@ -62,6 +63,8 @@ public class ZippedLibrarySourcesConfig extends Config {
}
}
catch (IOException e) {
System.out.println("Failed to process " + pathToLibZip + ". Reason: " + e);
e.printStackTrace();
return Collections.emptyList();
}
}
@@ -76,6 +79,7 @@ public class ZippedLibrarySourcesConfig extends Config {
InputStream stream = file.getInputStream(entry);
String text = FileUtil.loadTextAndClose(stream);
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
System.out.println("Parsing file: " + entry.getName());
result.add(jetFile);
}
}
@@ -189,7 +189,6 @@ public final class BindingUtils {
public static DeclarationDescriptor getDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull JetReferenceExpression reference) {
DeclarationDescriptor referencedDescriptor = getNullableDescriptorForReferenceExpression(context, reference);
assert referencedDescriptor != null : "Reference expression must reference a descriptor.";
Preconditions.checkNotNull(referencedDescriptor, "Reference expression must reference a descriptor for reference %s at %s", reference.getText(),
DiagnosticUtils.atLocation(reference));
return referencedDescriptor;
+1 -4
View File
@@ -65,11 +65,8 @@
<module>examples/kotlin-java-example</module>
<!--
TODO temporary disabled until we can get the kotlin/dom code compiled as JS
<module>examples/browser-example</module>
-->
<module>examples/js-example</module>
<module>examples/browser-example</module>
</modules>
<dependencies>
+51 -26
View File
@@ -56,32 +56,57 @@
<version>2.9</version>
</plugin>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.5</version>
<configuration>
<projectsDirectory>src/it</projectsDirectory>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<!--This could speed up test by providing local repo as remote repo for test but might be tricky on different OS-->
<!--<settingsFile>src/it/settings.xml</settingsFile>-->
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<postBuildHookScript>verify.bsh</postBuildHookScript>
<streamLogs>true</streamLogs>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<!--<phase>package</phase>-->
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<!--
moved the integration test into a profile so we can disable this
for example this integration test doesn't work if offline
-->
<profile>
<id>integrationTest</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.5</version>
<configuration>
<projectsDirectory>src/it</projectsDirectory>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<!--This could speed up test by providing local repo as remote repo for test but might be tricky on different OS-->
<!--<settingsFile>src/it/settings.xml</settingsFile>-->
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<postBuildHookScript>verify.bsh</postBuildHookScript>
<streamLogs>true</streamLogs>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<!--<phase>package</phase>-->
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- allows the integration test to be disabled (for example if you are offline) -->
<profile>
<id>noTest</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</project>
@@ -5,6 +5,8 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;
import org.jetbrains.k2js.config.ClassPathLibraryDefintionsConfig;
import org.jetbrains.k2js.config.ClassPathLibrarySourcesLoader;
import java.io.File;
import java.io.FileWriter;
@@ -19,13 +21,21 @@ import java.io.PrintWriter;
*/
public class JSSourceJarMojo extends AbstractMojo {
/**
* The Kotlin JavaScript library source code
* The Kotlin JavaScript library source code; the library code to be compiled to JavaScript
*
* @required
* @parameter expression="${jsLibrarySourceDir}"
*/
private File librarySourceDir;
/**
* The Kotlin JavaScript definition source code; the kotlin code used to define APIs available in the kotlin-lib.js file
*
* @required
* @parameter expression="${jsDefinitionSourceDir}"
*/
private File definitionSourceDir;
/**
* The directory for the copied code
*
@@ -41,7 +51,7 @@ public class JSSourceJarMojo extends AbstractMojo {
getLog().warn("Source directory does not exist: " + librarySourceDir);
} else {
try {
File metaInfFile = new File(outputDir, "META-INF/services/org.jetbrains.kotlin.js.librarySource");
File metaInfFile = new File(outputDir, ClassPathLibrarySourcesLoader.META_INF_SERVICES_FILE);
metaInfFile.getParentFile().mkdirs();
FileUtils.copyDirectoryStructure(librarySourceDir, outputDir);
@@ -54,6 +64,21 @@ public class JSSourceJarMojo extends AbstractMojo {
throw new MojoFailureException(e.getMessage(), e);
}
}
if (definitionSourceDir.exists()) {
try {
File metaInfFile = new File(outputDir, ClassPathLibraryDefintionsConfig.META_INF_SERVICES_FILE);
metaInfFile.getParentFile().mkdirs();
FileUtils.copyDirectoryStructure(definitionSourceDir, outputDir);
// now lets generate the META-INF/services/org.jetbrains.kotlin.js.libraryDefinitions file
PrintWriter writer = new PrintWriter(new FileWriter(metaInfFile));
appendSourceFiles(writer, definitionSourceDir.getCanonicalPath(), definitionSourceDir);
writer.close();
} catch (IOException e) {
throw new MojoFailureException(e.getMessage(), e);
}
}
}
private void appendSourceFiles(PrintWriter writer, String rootPath, File currentDir) throws IOException {