diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java index c73e7e78d53..a181a454eae 100644 --- a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java +++ b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java @@ -1,5 +1,22 @@ +/* + * 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.kotlin.maven; +import org.apache.maven.plugin.MojoExecutionException; import org.jetbrains.jet.cli.CompilerArguments; /** @@ -8,8 +25,7 @@ import org.jetbrains.jet.cli.CompilerArguments; * @goal js * @phase compile */ -public class K2JSCompilerMojo extends KotlinBaseMojo { - +public class K2JSCompilerMojo extends KotlinCompileMojo { /** * The output JS file name * @@ -19,18 +35,13 @@ public class K2JSCompilerMojo extends KotlinBaseMojo { private String outFile; @Override - protected void configureCompilerArguments(CompilerArguments arguments) { + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { super.configureCompilerArguments(arguments); + K2JSCompilerPlugin plugin = new K2JSCompilerPlugin(); plugin.setOutFile(outFile); arguments.getCompilerPlugins().add(plugin); + getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile); } - - @Override - protected CompilerArguments createCompilerArguments() { - CompilerArguments answer = new CompilerArguments(); - return answer; - - } } diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java index 7a6fddb30a0..95ead29ba93 100644 --- a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java +++ b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerPlugin.java @@ -1,7 +1,23 @@ +/* + * 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.kotlin.maven; +import com.google.common.io.Files; import com.intellij.openapi.project.Project; -import jet.Function1; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.compiler.CompilerPlugin; import org.jetbrains.jet.compiler.CompilerPluginContext; @@ -11,13 +27,11 @@ import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.facade.K2JSTranslator; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; -import static kotlin.io.namespace.use; - /** * Compiles Kotlin code to JavaScript */ @@ -30,6 +44,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin { Project project = context.getProject(); BindingContext bindingContext = context.getContext(); List sources = context.getFiles(); + if (bindingContext != null && sources != null && project != null) { Config config = new Config(project) { @NotNull @@ -38,6 +53,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin { return new ArrayList(); } }; + K2JSTranslator translator = new K2JSTranslator(config); final String code = translator.generateProgramCode(sources); File file = new File(outFile); @@ -46,20 +62,9 @@ public class K2JSCompilerPlugin implements CompilerPlugin { outDir.mkdirs(); } try { - use(new FileWriter(file), new Function1() { - @Override - public Object invoke(FileWriter writer) { - try { - writer.write(code); - } catch (IOException e) { - throw new RuntimeException(e); - } - return null; - } - }); + Files.write(code, file, Charset.forName("UTF-8")); } catch (IOException e) { - System.out.println("Error: " + e); - e.printStackTrace(); + throw new RuntimeException(e); } } } diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinBaseMojo.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinBaseMojo.java deleted file mode 100644 index fbcbc726a99..00000000000 --- a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinBaseMojo.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.jetbrains.kotlin.maven; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.jetbrains.jet.cli.CompilerArguments; -import org.jetbrains.jet.cli.KotlinCompiler; - -/** - * Abstract base class for Kotlin maven plugins - */ -public abstract class KotlinBaseMojo extends AbstractMojo { - private KotlinCompiler compiler = new KotlinCompiler(); - private CompilerArguments arguments; - - /** - * The source directory to compile - * - * @required - * @parameter default-value="${basedir}/src/main/kotlin" - */ - private String src; - - /** - * The output directory for bytecode classes - * - * @required - * @parameter default-value="${project.build.directory}/classes" - */ - private String outputDir; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - arguments = createCompilerArguments(); - - if (src != null) { - arguments.setSrc(src); - } - if (outputDir != null) { - arguments.setOutputDir(outputDir); - } - configureCompilerArguments(arguments); - - compiler.exec(System.err, arguments); - } - - - /** - * Derived classes can create custom compiler argument implementations - * such as for KDoc - */ - protected CompilerArguments createCompilerArguments() { - return new CompilerArguments(); - } - - /** - * Derived classes can register custom plugins or configurations - */ - protected void configureCompilerArguments(CompilerArguments arguments) { - } - -} diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java new file mode 100644 index 00000000000..0f51af4d26a --- /dev/null +++ b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java @@ -0,0 +1,35 @@ +/* + * 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.kotlin.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.jetbrains.jet.cli.CompilerArguments; + +/** + * Compiles kotlin sources + * + * @goal compile + * @phase compile + * @requiresDependencyResolution compile + */ +public class KotlinCompileMojo extends KotlinCompileMojoBase { + @Override + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { + configureBaseCompilerArguments(getLog(), arguments, module, sources, classpath, output); + } +} + diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java new file mode 100644 index 00000000000..8e52e43e71d --- /dev/null +++ b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java @@ -0,0 +1,167 @@ +/* + * 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.kotlin.maven; + +import com.google.common.base.Joiner; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.jetbrains.jet.cli.CompilerArguments; +import org.jetbrains.jet.cli.KotlinCompiler; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public abstract class KotlinCompileMojoBase extends AbstractMojo { + /** + * The source directories containing the sources to be compiled. + * + * @parameter default-value="${project.compileSourceRoots}" + * @required + * @readonly + */ + public List sources; + + /** + * The source directories containing the sources to be compiled for tests. + * + * @parameter default-value="${project.testCompileSourceRoots}" + * @required + * @readonly + */ + protected List testSources; + + /** + * Project classpath. + * + * @parameter default-value="${project.compileClasspathElements}" + * @required + * @readonly + */ + public List classpath; + + /** + * Project test classpath. + * + * @parameter default-value="${project.testClasspathElements}" + * @required + * @readonly + */ + protected List testClasspath; + + /** + * The directory for compiled classes. + * + * @parameter default-value="${project.build.outputDirectory}" + * @required + * @readonly + */ + public String output; + + /** + * The directory for compiled tests classes. + * + * @parameter default-value="${project.build.testOutputDirectory}" + * @required + * @readonly + */ + public String testOutput; + + /** + * Kotlin compilation module, as alternative to source files or folders. + * + * @parameter + */ + public String module; + + /** + * Kotlin compilation module, as alternative to source files or folders (for tests). + * + * @parameter + */ + public String testModule; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + final CompilerArguments arguments = createCompilerArguments(); + + configureCompilerArguments(arguments); + + final KotlinCompiler compiler = createCompiler(); + + final KotlinCompiler.ExitCode exitCode = compiler.exec(System.err, arguments); + + switch (exitCode) { + case COMPILATION_ERROR: + throw new MojoExecutionException("Compilation error. See log for more details"); + + case INTERNAL_ERROR: + throw new MojoExecutionException("Internal compiler error. See log for more details"); + } + } + + protected KotlinCompiler createCompiler() { + return new KotlinCompiler(); + } + + /** + * Derived classes can create custom compiler argument implementations + * such as for KDoc + */ + protected CompilerArguments createCompilerArguments() { + return new CompilerArguments(); + } + + /** + * Derived classes can register custom plugins or configurations + */ + protected abstract void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException; + + protected static void configureBaseCompilerArguments(Log log, CompilerArguments arguments, String module, + List sources, List classpath, String output) throws MojoExecutionException { + if (module != null) { + log.info("Compiling Kotlin module " + module); + arguments.setModule(module); + } else { + if (sources.size() <= 0) + throw new MojoExecutionException("No source roots to compile"); + + if (sources.size() > 1) + throw new MojoExecutionException("Multiple source roots are not supported yet"); + + final String src = sources.get(0); + + log.info("Compiling Kotlin sources from " + src); + arguments.setSrc(src); + } + + final ArrayList classpathList = new ArrayList(classpath); + + if (classpathList.remove(output)) { + log.debug("Removed target directory from classpath (" + output + ")"); + } + + final String classPathString = Joiner.on(File.pathSeparator).join(classpathList); + log.info("Classpath: " + classPathString); + arguments.setClasspath(classPathString); + + log.info("Classes directory is " + output); + arguments.setOutputDir(output); + } +} diff --git a/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java new file mode 100644 index 00000000000..1029cd23a89 --- /dev/null +++ b/libraries/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java @@ -0,0 +1,53 @@ +/* + * 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.kotlin.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.jetbrains.jet.cli.CompilerArguments; + +/** + * Compiles Kotlin test sources + * + * @goal test-compile + * @phase test-compile + * @requiresDependencyResolution test + */ +public class KotlinTestCompileMojo extends KotlinCompileMojoBase { + /** + * Flag to allow test compilation to be skipped. + * + * @parameter expression="${maven.test.skip}" default-value="false" + * @noinspection UnusedDeclaration + */ + private boolean skip; + + public void execute() throws MojoExecutionException, MojoFailureException { + if (skip) { + getLog().info("Test compilation is skipped"); + } else { + super.execute(); + } + } + + @Override + protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException { + configureBaseCompilerArguments( + getLog(), arguments, + testModule, testSources, testClasspath, testOutput); + } +} diff --git a/libraries/kotlin-maven-plugin/src/test/java/org/jetbrains/kotlin/maven/MojoTest.java b/libraries/kotlin-maven-plugin/src/test/java/org/jetbrains/kotlin/maven/MojoTest.java index e7ed736d099..a3b38e59d90 100644 --- a/libraries/kotlin-maven-plugin/src/test/java/org/jetbrains/kotlin/maven/MojoTest.java +++ b/libraries/kotlin-maven-plugin/src/test/java/org/jetbrains/kotlin/maven/MojoTest.java @@ -1,3 +1,19 @@ +/* + * 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.kotlin.maven; import org.junit.Test;