Add Gradle JavaScript configurator

This commit is contained in:
Dmitry Jemerov
2017-01-26 13:51:25 +01:00
parent c6251fdad7
commit c7ea4eead7
9 changed files with 90 additions and 25 deletions
+1
View File
@@ -41,6 +41,7 @@
<gradleProjectImportHandler implementation="org.jetbrains.kotlin.noarg.ide.NoArgGradleProjectImportHandler"/> <gradleProjectImportHandler implementation="org.jetbrains.kotlin.noarg.ide.NoArgGradleProjectImportHandler"/>
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleModuleConfigurator"/> <projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleModuleConfigurator"/>
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinJsGradleModuleConfigurator"/>
<versionInfoProvider implementation="org.jetbrains.kotlin.idea.configuration.GradleKotlinVersionInfoProvider"/> <versionInfoProvider implementation="org.jetbrains.kotlin.idea.configuration.GradleKotlinVersionInfoProvider"/>
</extensions> </extensions>
</idea-plugin> </idea-plugin>
@@ -92,7 +92,9 @@ class GradleKotlinJavaFrameworkSupportProvider : GradleKotlinFrameworkSupportPro
} }
class GradleKotlinJSFrameworkSupportProvider : GradleKotlinFrameworkSupportProvider("KOTLIN_JS", "Kotlin (JavaScript)") { class GradleKotlinJSFrameworkSupportProvider : GradleKotlinFrameworkSupportProvider("KOTLIN_JS", "Kotlin (JavaScript)") {
override fun getPluginDefinition() = "apply plugin: 'kotlin2js'" override fun getPluginDefinition(): String {
return KotlinJsGradleModuleConfigurator.APPLY_KOTLIN_JS
}
override fun getRuntimeLibrary(rootModel: ModifiableRootModel) = override fun getRuntimeLibrary(rootModel: ModifiableRootModel) =
KotlinWithGradleConfigurator.getDependencySnippet(MAVEN_JS_STDLIB_ID) KotlinWithGradleConfigurator.getDependencySnippet(MAVEN_JS_STDLIB_ID)
@@ -16,12 +16,9 @@
package org.jetbrains.kotlin.idea.configuration; package org.jetbrains.kotlin.idea.configuration;
import com.intellij.openapi.module.Module;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.KotlinPluginUtil;
import org.jetbrains.kotlin.resolve.TargetPlatform; import org.jetbrains.kotlin.resolve.TargetPlatform;
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform; import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
public class KotlinGradleModuleConfigurator extends KotlinWithGradleConfigurator { public class KotlinGradleModuleConfigurator extends KotlinWithGradleConfigurator {
public static final String NAME = "gradle"; public static final String NAME = "gradle";
@@ -46,27 +43,11 @@ public class KotlinGradleModuleConfigurator extends KotlinWithGradleConfigurator
return "Gradle"; return "Gradle";
} }
@Override
public boolean isApplicable(@NotNull Module module) {
return KotlinPluginUtil.isGradleModule(module) &&
!KotlinPluginUtil.isAndroidGradleModule(module);
}
@Override @Override
protected String getApplyPluginDirective() { protected String getApplyPluginDirective() {
return APPLY_KOTLIN; return APPLY_KOTLIN;
} }
@Override
protected boolean addElementsToFile(@NotNull GroovyFile groovyFile, boolean isTopLevelProjectFile, @NotNull String version) {
if (!isTopLevelProjectFile) {
boolean wasModified = Companion.addElementsToProjectFile(groovyFile, version);
wasModified |= addElementsToModuleFile(groovyFile, version);
return wasModified;
}
return false;
}
KotlinGradleModuleConfigurator() { KotlinGradleModuleConfigurator() {
} }
} }
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2017 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.idea.configuration
import com.intellij.openapi.projectRoots.Sdk
import org.jetbrains.kotlin.idea.versions.MAVEN_JS_STDLIB_ID
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.resolve.TargetPlatform
class KotlinJsGradleModuleConfigurator : KotlinWithGradleConfigurator() {
override val name: String = "gradle-js"
override val presentableText: String = "Gradle (JavaScript)"
override val targetPlatform: TargetPlatform = JsPlatform
override val applyPluginDirective: String = APPLY_KOTLIN_JS
override fun getDependencyDirective(sdk: Sdk?): String {
return KotlinWithGradleConfigurator.getDependencySnippet(MAVEN_JS_STDLIB_ID)
}
companion object {
val APPLY_KOTLIN_JS = "apply plugin: 'kotlin2js'"
}
}
@@ -71,7 +71,9 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
return ConfigureKotlinStatus.BROKEN return ConfigureKotlinStatus.BROKEN
} }
protected abstract fun isApplicable(module: Module): Boolean protected open fun isApplicable(module: Module): Boolean {
return KotlinPluginUtil.isGradleModule(module) && !KotlinPluginUtil.isAndroidGradleModule(module)
}
private fun isFileConfigured(projectGradleFile: GroovyFile): Boolean { private fun isFileConfigured(projectGradleFile: GroovyFile): Boolean {
val fileText = projectGradleFile.text val fileText = projectGradleFile.text
@@ -146,18 +148,29 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
val dependenciesBlock = getDependenciesBlock(file) val dependenciesBlock = getDependenciesBlock(file)
val sdk = ModuleUtil.findModuleForPsiElement(file)?.let { ModuleRootManager.getInstance(it).sdk } val sdk = ModuleUtil.findModuleForPsiElement(file)?.let { ModuleRootManager.getInstance(it).sdk }
wasModified = wasModified or addExpressionInBlockIfNeeded(getRuntimeLibrary(sdk), dependenciesBlock, false) wasModified = wasModified or addExpressionInBlockIfNeeded(getDependencyDirective(sdk), dependenciesBlock, false)
return wasModified return wasModified
} }
protected open fun getDependencyDirective(sdk: Sdk?) = getRuntimeLibrary(sdk)
protected abstract val applyPluginDirective: String protected abstract val applyPluginDirective: String
protected abstract fun addElementsToFile( protected open fun addElementsToFile(
groovyFile: GroovyFile, groovyFile: GroovyFile,
isTopLevelProjectFile: Boolean, isTopLevelProjectFile: Boolean,
version: String version: String
): Boolean ): Boolean {
if (!isTopLevelProjectFile) {
var wasModified = addElementsToProjectFile(groovyFile, version)
wasModified = wasModified or addElementsToModuleFile(groovyFile, version)
return wasModified
}
return false
}
fun changeGradleFile( fun changeGradleFile(
groovyFile: GroovyFile, groovyFile: GroovyFile,
+18
View File
@@ -0,0 +1,18 @@
version = '1.0'
repositories {
mavenCentral()
}
buildscript {
ext.kotlin_version = '$VERSION$'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
dependencies {
compile "org.jetbrains.kotlin:kotlin-js-library:$kotlin_version"
}
+5
View File
@@ -0,0 +1,5 @@
version = '1.0'
repositories {
mavenCentral()
}
@@ -23,7 +23,8 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
abstract class AbstractGradleConfigureProjectByChangingFileTest : AbstractConfigureProjectByChangingFileTest<KotlinWithGradleConfigurator>() { abstract class AbstractGradleConfigureProjectByChangingFileTest : AbstractConfigureProjectByChangingFileTest<KotlinWithGradleConfigurator>() {
fun doTestGradle(path: String) { fun doTestGradle(path: String) {
doTest(path, path.replace("before", "after"), KotlinGradleModuleConfigurator()) doTest(path, path.replace("before", "after"),
if ("js" in path) KotlinJsGradleModuleConfigurator() else KotlinGradleModuleConfigurator())
} }
override fun runConfigurator(module: Module, file: PsiFile, configurator: KotlinWithGradleConfigurator, version: String, collector: NotificationMessageCollector) { override fun runConfigurator(module: Module, file: PsiFile, configurator: KotlinWithGradleConfigurator, version: String, collector: NotificationMessageCollector) {
@@ -48,6 +48,12 @@ public class GradleConfigureProjectByChangingFileTestGenerated extends AbstractG
doTestGradle(fileName); doTestGradle(fileName);
} }
@TestMetadata("js_before.gradle")
public void testJs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/gradle/js_before.gradle");
doTestGradle(fileName);
}
@TestMetadata("m04Version_before.gradle") @TestMetadata("m04Version_before.gradle")
public void testM04Version() throws Exception { public void testM04Version() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/gradle/m04Version_before.gradle"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/gradle/m04Version_before.gradle");