diff --git a/plugins/gradle/java/src/action/ImportProjectFromScriptAction.kt b/plugins/gradle/java/src/action/ImportProjectFromScriptAction.kt new file mode 100644 index 000000000000..f6640ec4cb4c --- /dev/null +++ b/plugins/gradle/java/src/action/ImportProjectFromScriptAction.kt @@ -0,0 +1,48 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.gradle.action + +import com.intellij.ide.actions.ImportModuleAction.createFromWizard +import com.intellij.ide.actions.ImportModuleAction.createImportWizard +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.externalSystem.action.ExternalSystemAction +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.FileIndexFacade +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.projectImport.ProjectImportProvider +import org.jetbrains.plugins.gradle.util.GradleConstants + +class ImportProjectFromScriptAction: ExternalSystemAction() { + + override fun isEnabled(e: AnActionEvent?): Boolean = true + + override fun isVisible(e: AnActionEvent?): Boolean { + if (e == null) { + return false + } + val virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return false + val project = e.getData(CommonDataKeys.PROJECT) ?: return false + + if (virtualFile.name != GradleConstants.DEFAULT_SCRIPT_NAME + && virtualFile.name != GradleConstants.KOTLIN_DSL_SCRIPT_NAME) { + return false + } + + val containingModule = FileIndexFacade.getInstance(project).getModuleForFile(virtualFile) ?: return true + return ExternalSystemApiUtil.getExternalRootProjectPath(containingModule) == null + } + + override fun actionPerformed(e: AnActionEvent?) { + if (e == null) return + + val virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return + val project = e.getData(CommonDataKeys.PROJECT) ?: return + + val wizard = createImportWizard(project, null, virtualFile, + *ProjectImportProvider.PROJECT_IMPORT_PROVIDER.extensions) + if (wizard != null && (wizard.stepCount <= 0 || wizard.showAndGet())) { + createFromWizard(project, wizard) + } + } +} \ No newline at end of file diff --git a/plugins/gradle/java/testSources/importing/GradleImportActionTest.kt b/plugins/gradle/java/testSources/importing/GradleImportActionTest.kt new file mode 100644 index 000000000000..349e7c206d44 --- /dev/null +++ b/plugins/gradle/java/testSources/importing/GradleImportActionTest.kt @@ -0,0 +1,53 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.plugins.gradle.importing + +import com.intellij.ide.DataManager +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.CommonDataKeys +import org.jetbrains.plugins.gradle.action.ImportProjectFromScriptAction +import org.junit.Test + +class GradleImportActionTest: GradleImportingTestCase() { + + @Test fun `check action not visible if not build script`() { + val virtualFile = createProjectSubFile("some.txt") + + val action = ImportProjectFromScriptAction() + val defaultContext = DataManager.getInstance().dataContext + val actionEvent = AnActionEvent.createFromDataContext("ProjectViewPopup", null) { dataId -> + when { + CommonDataKeys.PROJECT.`is`(dataId) -> myProject + CommonDataKeys.VIRTUAL_FILE.`is`(dataId) -> virtualFile + else -> defaultContext.getData(dataId) + } + } + + action.update(actionEvent) + assertFalse(actionEvent.presentation.isVisible) + } + + + @Test + fun `check action visibility for imported project`() { + + val config = createProjectConfig(injectRepo("apply plugin: 'java'")) + + val action = ImportProjectFromScriptAction() + val defaultContext = DataManager.getInstance().dataContext + val actionEvent = AnActionEvent.createFromDataContext("ProjectViewPopup", null) { dataId -> + when { + CommonDataKeys.PROJECT.`is`(dataId) -> myProject + CommonDataKeys.VIRTUAL_FILE.`is`(dataId) -> config + else -> defaultContext.getData(dataId) + } + } + + action.update(actionEvent) + assertTrue("Action should be visible if project is not imported", actionEvent.presentation.isVisible) + + importProject() + + action.update(actionEvent) + assertFalse("Action should not be visible for imported project", actionEvent.presentation.isVisible) + } +} \ No newline at end of file diff --git a/plugins/gradle/plugin-resources/META-INF/plugin.xml b/plugins/gradle/plugin-resources/META-INF/plugin.xml index ed820de5b250..3e06b6996ffd 100644 --- a/plugins/gradle/plugin-resources/META-INF/plugin.xml +++ b/plugins/gradle/plugin-resources/META-INF/plugin.xml @@ -130,6 +130,12 @@ + + + +