From a2ebaf7d1829dee4b8479d792a41ad57f2a22414 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 25 Apr 2016 22:00:01 +0300 Subject: [PATCH] Add Maven importer to IDEA --- .../impl/teavm-metaprogramming-impl.iml | 1 + .../java/org/teavm/tooling/TeaVMTool.java | 4 +- .../teavm/idea/maven/TeaVMMavenImporter.java | 107 ++++++++++++++++++ .../src/main/resources/META-INF/plugin.xml | 6 + 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tools/idea/src/main/java/org/teavm/idea/maven/TeaVMMavenImporter.java diff --git a/metaprogramming/impl/teavm-metaprogramming-impl.iml b/metaprogramming/impl/teavm-metaprogramming-impl.iml index a7b1b7d81..ba78f6942 100644 --- a/metaprogramming/impl/teavm-metaprogramming-impl.iml +++ b/metaprogramming/impl/teavm-metaprogramming-impl.iml @@ -7,6 +7,7 @@ + diff --git a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java index c619ab206..a7bf85646 100644 --- a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -222,7 +222,7 @@ public class TeaVMTool implements BaseTeaVMTool { } public Collection getClasses() { - return vm != null ? vm.getClasses() : Collections.emptyList(); + return vm != null ? vm.getClasses() : Collections.emptyList(); } public Collection getUsedResources() { @@ -361,6 +361,7 @@ public class TeaVMTool implements BaseTeaVMTool { TeaVMProblemRenderer.describeProblems(vm, log); } if (debugInformationGenerated) { + assert debugEmitter != null; DebugInformation debugInfo = debugEmitter.getDebugInformation(); try (OutputStream debugInfoOut = new FileOutputStream(new File(targetDirectory, targetFileName + ".teavmdbg"))) { @@ -369,6 +370,7 @@ public class TeaVMTool implements BaseTeaVMTool { log.info("Debug information successfully written"); } if (sourceMapsFileGenerated) { + assert debugEmitter != null; DebugInformation debugInfo = debugEmitter.getDebugInformation(); String sourceMapsFileName = targetFileName + ".map"; writer.append("\n//# sourceMappingURL=").append(sourceMapsFileName); diff --git a/tools/idea/src/main/java/org/teavm/idea/maven/TeaVMMavenImporter.java b/tools/idea/src/main/java/org/teavm/idea/maven/TeaVMMavenImporter.java new file mode 100644 index 000000000..3e53ad1ab --- /dev/null +++ b/tools/idea/src/main/java/org/teavm/idea/maven/TeaVMMavenImporter.java @@ -0,0 +1,107 @@ +/* + * Copyright 2016 Alexey Andreev. + * + * 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.teavm.idea.maven; + +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleServiceManager; +import java.util.List; +import java.util.Map; +import org.jdom.Element; +import org.jetbrains.idea.maven.importing.MavenImporter; +import org.jetbrains.idea.maven.importing.MavenRootModelAdapter; +import org.jetbrains.idea.maven.model.MavenPlugin; +import org.jetbrains.idea.maven.project.MavenProject; +import org.jetbrains.idea.maven.project.MavenProjectChanges; +import org.jetbrains.idea.maven.project.MavenProjectsProcessorTask; +import org.jetbrains.idea.maven.project.MavenProjectsTree; +import org.teavm.idea.TeaVMConfigurationStorage; +import org.teavm.idea.jps.model.TeaVMJpsConfiguration; + +public class TeaVMMavenImporter extends MavenImporter { + private static final Logger logger = Logger.getInstance(TeaVMMavenImporter.class); + + public TeaVMMavenImporter() { + super("org.teavm", "teavm-maven-plugin"); + } + + @Override + public void preProcess(Module module, MavenProject mavenProject, MavenProjectChanges changes, + IdeModifiableModelsProvider modifiableModelsProvider) { + } + + @Override + public void process(IdeModifiableModelsProvider modifiableModelsProvider, Module module, + MavenRootModelAdapter rootModel, MavenProjectsTree mavenModel, MavenProject mavenProject, + MavenProjectChanges changes, Map mavenProjectToModuleName, + List postTasks) { + TeaVMConfigurationStorage configurationStorage = ModuleServiceManager.getService(module, + TeaVMConfigurationStorage.class); + if (configurationStorage == null) { + logger.warn("Could not load component to retrieve TeaVM build configuration"); + return; + } + + TeaVMJpsConfiguration configuration = configurationStorage.getState(); + + for (MavenPlugin mavenPlugin : mavenProject.getPlugins()) { + if (mavenPlugin.getGroupId().equals(myPluginGroupID) + && mavenPlugin.getArtifactId().equals(myPluginArtifactID)) { + updateConfiguration(mavenPlugin, configuration); + } + } + + configurationStorage.loadState(configuration); + } + + private void updateConfiguration(MavenPlugin plugin, TeaVMJpsConfiguration configuration) { + if (plugin.getConfigurationElement() != null) { + updateConfiguration(plugin.getConfigurationElement(), configuration); + } + for (MavenPlugin.Execution execution : plugin.getExecutions()) { + if (execution.getGoals().contains("compile")) { + if (execution.getConfigurationElement() != null) { + updateConfiguration(execution.getConfigurationElement(), configuration); + } + break; + } + } + } + + private void updateConfiguration(Element source, TeaVMJpsConfiguration configuration) { + configuration.setEnabled(true); + for (Element child : source.getChildren()) { + switch (child.getName()) { + case "sourceFilesCopied": + configuration.setSourceFilesCopied(Boolean.parseBoolean(child.getTextTrim())); + break; + case "sourceMapsGenerated": + configuration.setSourceMapsFileGenerated(Boolean.parseBoolean(child.getTextTrim())); + break; + case "minifying": + configuration.setMinifying(Boolean.parseBoolean(child.getTextTrim())); + break; + case "targetDirectory": + configuration.setTargetDirectory(child.getTextTrim()); + break; + case "mainClass": + configuration.setMainClass(child.getTextTrim()); + break; + } + } + } +} diff --git a/tools/idea/src/main/resources/META-INF/plugin.xml b/tools/idea/src/main/resources/META-INF/plugin.xml index 7a7b6b784..daaa70f4e 100644 --- a/tools/idea/src/main/resources/META-INF/plugin.xml +++ b/tools/idea/src/main/resources/META-INF/plugin.xml @@ -4,6 +4,8 @@ 1.0 TeaVM community + org.jetbrains.idea.maven + most HTML tags may be used @@ -30,4 +32,8 @@ + + + + \ No newline at end of file