add context menu to import new gradle projects IDEA-162677

This commit is contained in:
Nikita Skvortsov
2018-07-24 15:52:58 +03:00
parent b32cced7a4
commit 3adc3a7164
3 changed files with 107 additions and 0 deletions
@@ -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<VirtualFile>(CommonDataKeys.VIRTUAL_FILE) ?: return false
val project = e.getData<Project>(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<VirtualFile>(CommonDataKeys.VIRTUAL_FILE) ?: return
val project = e.getData<Project>(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)
}
}
}
@@ -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)
}
}
@@ -130,6 +130,12 @@
<action id="Gradle.RefreshDependencies" class="org.jetbrains.plugins.gradle.action.GradleRefreshProjectDependenciesAction"
text="Refresh dependencies" description="Refresh dependencies in the Gradle cache using --refresh-dependencies argument"/>
<action id="Gradle.ImportExternalProject" class = "org.jetbrains.plugins.gradle.action.ImportProjectFromScriptAction"
text="Import Gradle project" description="Import Gradle project described by this file"
icon="GradleIcons.Gradle">
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
</action>
<group>
<separator/>
<reference id="Gradle.ExecuteTask"/>