diff --git a/idea/idea-android/src/org/jetbrains/kotlin/android/configure/PlatformAndroidGradleDetector.kt b/idea/idea-android/src/org/jetbrains/kotlin/android/configure/PlatformAndroidGradleDetector.kt new file mode 100644 index 00000000000..4cdde939e87 --- /dev/null +++ b/idea/idea-android/src/org/jetbrains/kotlin/android/configure/PlatformAndroidGradleDetector.kt @@ -0,0 +1,51 @@ +/* + * 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. + */ + +/* + * 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.android.configure + +import com.android.tools.idea.gradle.AndroidProjectKeys +import com.intellij.openapi.externalSystem.model.DataNode +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil +import org.jetbrains.kotlin.idea.inspections.gradle.KotlinPlatformGradleDetector + +class PlatformAndroidGradleDetector : KotlinPlatformGradleDetector { + override fun getResolvedKotlinStdlibVersionByModuleData(moduleData: DataNode<*>, libraryIds: List): String? { + ExternalSystemApiUtil + .findAllRecursively(moduleData, AndroidProjectKeys.JAVA_PROJECT).asSequence() + .flatMap { it.data.jarLibraryDependencies.asSequence() } + .forEach { + val libraryName = it.name + if (libraryName.substringBeforeLast("-") in libraryIds) return libraryName.substringAfterLast("-") + } + return null + } +} \ No newline at end of file diff --git a/idea/src/META-INF/android.xml b/idea/src/META-INF/android.xml index 003c7c434ea..068b816060f 100644 --- a/idea/src/META-INF/android.xml +++ b/idea/src/META-INF/android.xml @@ -42,6 +42,7 @@ + diff --git a/idea/src/META-INF/extensions/common.xml b/idea/src/META-INF/extensions/common.xml index 2bba11e0ebc..569e7713a2d 100644 --- a/idea/src/META-INF/extensions/common.xml +++ b/idea/src/META-INF/extensions/common.xml @@ -41,5 +41,7 @@ interface="org.jetbrains.kotlin.idea.facet.KotlinFacetConfigurationExtension"/> + diff --git a/idea/src/META-INF/gradle.xml b/idea/src/META-INF/gradle.xml index 6e6c3b6f485..f163df2d899 100644 --- a/idea/src/META-INF/gradle.xml +++ b/idea/src/META-INF/gradle.xml @@ -44,5 +44,6 @@ + diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/DifferentStdlibGradleVersionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/DifferentStdlibGradleVersionInspection.kt index 1a2654f6242..28f30f4d840 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/DifferentStdlibGradleVersionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/DifferentStdlibGradleVersionInspection.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.inspections.gradle import com.intellij.openapi.externalSystem.model.DataNode import com.intellij.openapi.externalSystem.model.ProjectKeys -import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil import com.intellij.openapi.roots.ProjectRootManager import com.intellij.psi.PsiFile import org.jetbrains.kotlin.idea.versions.MAVEN_STDLIB_ID @@ -117,13 +116,7 @@ class DifferentStdlibGradleVersionInspection : GradleBaseInspection() { } internal fun DataNode<*>.getResolvedKotlinStdlibVersionByModuleData(libraryIds: List): String? { - for (libraryDependencyData in ExternalSystemApiUtil.findAllRecursively(this, ProjectKeys.LIBRARY_DEPENDENCY)) { - for (libraryId in libraryIds) { - val libraryNameMarker = "org.jetbrains.kotlin:$libraryId:" - if (libraryDependencyData.data.externalName.startsWith(libraryNameMarker)) { - return libraryDependencyData.data.externalName.substringAfter(libraryNameMarker) - } - } - } - return null + return KotlinPlatformGradleDetector.EP_NAME.extensions.asSequence() + .mapNotNull { it.getResolvedKotlinStdlibVersionByModuleData(this, libraryIds) } + .firstOrNull() } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/KotlinPlatformGradleDetector.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/KotlinPlatformGradleDetector.kt new file mode 100644 index 00000000000..172fdf1f4c1 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/gradle/KotlinPlatformGradleDetector.kt @@ -0,0 +1,60 @@ +/* + * 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. + */ + +/* + * 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.inspections.gradle + +import com.intellij.openapi.extensions.ExtensionPointName +import com.intellij.openapi.externalSystem.model.DataNode +import com.intellij.openapi.externalSystem.model.ProjectKeys +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil + +interface KotlinPlatformGradleDetector { + companion object { + val EP_NAME: ExtensionPointName = ExtensionPointName.create("org.jetbrains.kotlin.platformGradleDetector")!! + } + + fun getResolvedKotlinStdlibVersionByModuleData(moduleData: DataNode<*>, libraryIds: List): String? +} + +class DefaultPlatformGradleDetector : KotlinPlatformGradleDetector { + override fun getResolvedKotlinStdlibVersionByModuleData(moduleData: DataNode<*>, libraryIds: List): String? { + for (libraryDependencyData in ExternalSystemApiUtil.findAllRecursively(moduleData, ProjectKeys.LIBRARY_DEPENDENCY)) { + for (libraryId in libraryIds) { + val libraryNameMarker = "org.jetbrains.kotlin:$libraryId:" + if (libraryDependencyData.data.externalName.startsWith(libraryNameMarker)) { + return libraryDependencyData.data.externalName.substringAfter(libraryNameMarker) + } + } + } + return null + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest.kt index 0e999a9a725..e09d35069ed 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest.kt @@ -23,15 +23,15 @@ import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.junit.Assert import org.junit.Test +internal fun GradleImportingTestCase.facetSettings(moduleName: String) = KotlinFacet.get(getModule(moduleName))!!.configuration.settings + +internal val GradleImportingTestCase.facetSettings: KotlinFacetSettings + get() = facetSettings("project_main") + +internal val GradleImportingTestCase.testFacetSettings: KotlinFacetSettings + get() = facetSettings("project_test") + class GradleFacetImportTest : GradleImportingTestCase() { - private fun facetSettings(moduleName: String) = KotlinFacet.get(getModule(moduleName))!!.configuration.settings - - private val facetSettings: KotlinFacetSettings - get() = facetSettings("project_main") - - private val testFacetSettings: KotlinFacetSettings - get() = facetSettings("project_test") - @Test fun testJvmImport() { createProjectSubFile("build.gradle", """ diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest_3_3.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest_3_3.kt new file mode 100644 index 00000000000..82f25f86b4a --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/gradle/GradleFacetImportTest_3_3.kt @@ -0,0 +1,155 @@ +/* + * 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.codeInsight.gradle + +import com.intellij.openapi.util.text.StringUtil +import org.jetbrains.kotlin.config.TargetPlatformKind +import org.junit.Assert +import org.junit.Test +import java.io.File + +class GradleFacetImportTest_3_3 : GradleImportingTestCase() { + override fun setUp() { + gradleVersion = "3.3" + super.setUp() + } + + @Test + fun testAndroidGradleJsDetection() { + createProjectSubFile("android-module/build.gradle", """ + group 'Again' + version '1.0-SNAPSHOT' + + buildscript { + repositories { + jcenter() + } + dependencies { + classpath "com.android.tools.build:gradle:2.3.0-rc1" + } + } + + apply plugin: 'com.android.application' + + android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 11 + targetSdkVersion 23 + versionCode 1002003 + versionName version + } + + dataBinding { + enabled = true + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } + + buildTypes { + debug { + applicationIdSuffix ".debug" + versionNameSuffix "-debug" + } + release { + minifyEnabled true + shrinkResources true + } + } + } + """) + createProjectSubFile("android-module/src/main/AndroidManifest.xml", """ + + + """) + createProjectSubFile("js-module/build.gradle", """ + group 'Again' + version '1.0-SNAPSHOT' + + buildscript { + repositories { + mavenCentral() + maven { + url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1' + } + } + + dependencies { + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0") + } + } + + apply plugin: 'kotlin2js' + + dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-js:1.1.0" + } + """) + createProjectSubFile("build.gradle", """ + group 'Again' + version '1.0-SNAPSHOT' + + buildscript { + repositories { + mavenLocal() + maven { + url='https://dl.bintray.com/kotlin/kotlin-eap-1.1' + } + jcenter() + } + dependencies { + classpath "com.android.tools.build:gradle:2.3.0-rc1" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0" + } + } + + ext { + androidBuildToolsVersion = '23.0.1' + } + + allprojects { + repositories { + mavenLocal() + maven { + url='https://dl.bintray.com/kotlin/kotlin-eap-1.1' + } + jcenter() + } + } + """) + createProjectSubFile("settings.gradle", """ + rootProject.name = "android-js-test" + include ':android-module' + include ':js-module' + """) + createProjectSubFile("local.properties", """ + sdk.dir=/${StringUtil.escapeBackSlashes(File(homePath).parent + "/dependencies/androidSDK")} + """) + importProject() + + with (facetSettings("js-module")) { + Assert.assertEquals(TargetPlatformKind.JavaScript, targetPlatformKind) + } + } +}