|
5 | 5 | package com.microsoft.azure.toolkit.intellij.common; |
6 | 6 |
|
7 | 7 | import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo; |
| 8 | +import com.intellij.openapi.module.Module; |
8 | 9 | import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.roots.ProjectFileIndex; |
| 11 | +import com.intellij.openapi.vfs.LocalFileSystem; |
9 | 12 | import com.intellij.openapi.vfs.VirtualFile; |
10 | 13 | import com.intellij.packaging.artifacts.Artifact; |
11 | 14 | import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation; |
|
15 | 18 | import org.apache.commons.compress.utils.FileNameUtils; |
16 | 19 | import org.apache.commons.lang3.StringUtils; |
17 | 20 | import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.annotations.Nullable; |
18 | 22 | import org.jetbrains.idea.maven.model.MavenConstants; |
19 | 23 | import org.jetbrains.idea.maven.project.MavenProject; |
20 | 24 | import org.jetbrains.idea.maven.project.MavenProjectsManager; |
21 | 25 | import org.jetbrains.plugins.gradle.model.ExternalProject; |
22 | 26 | import org.jetbrains.plugins.gradle.service.project.data.ExternalProjectDataCache; |
23 | 27 |
|
| 28 | +import java.io.File; |
24 | 29 | import java.util.*; |
25 | 30 | import java.util.function.Predicate; |
26 | 31 | import java.util.stream.Collectors; |
27 | 32 |
|
| 33 | +import static com.microsoft.azure.toolkit.intellij.common.AzureArtifactType.File; |
| 34 | + |
28 | 35 | public class AzureArtifactManager { |
29 | 36 | private static Map<Project, AzureArtifactManager> projectAzureArtifactManagerMap = new HashMap<>(); |
30 | 37 | private final Project project; |
@@ -93,7 +100,7 @@ public AzureArtifact getAzureArtifactById(String artifactId) { |
93 | 100 | } |
94 | 101 |
|
95 | 102 | public AzureArtifact getAzureArtifactById(AzureArtifactType azureArtifactType, String artifactId) { |
96 | | - return azureArtifactType == AzureArtifactType.File ? AzureArtifact.createFromFile(artifactId) : |
| 103 | + return azureArtifactType == File ? AzureArtifact.createFromFile(artifactId) : |
97 | 104 | getAllSupportedAzureArtifacts().stream().filter(artifact -> StringUtils.equals(getArtifactIdentifier( |
98 | 105 | artifact), artifactId)).findFirst().orElse(null); |
99 | 106 | } |
@@ -121,6 +128,28 @@ public boolean equalsAzureArtifactIdentifier(AzureArtifact artifact1, AzureArtif |
121 | 128 | return StringUtils.equals(getArtifactIdentifier(artifact1), getArtifactIdentifier(artifact2)); |
122 | 129 | } |
123 | 130 |
|
| 131 | + @Nullable |
| 132 | + @AzureOperation( |
| 133 | + name = "common|artifact.get_module", |
| 134 | + type = AzureOperation.Type.TASK |
| 135 | + ) |
| 136 | + public Module getModuleFromAzureArtifact(AzureArtifact azureArtifact) { |
| 137 | + if (azureArtifact == null || azureArtifact.getReferencedObject() == null) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + switch (azureArtifact.getType()) { |
| 141 | + case Gradle: |
| 142 | + final String gradleModulePath = ((ExternalProjectPojo) azureArtifact.getReferencedObject()).getPath(); |
| 143 | + final VirtualFile gradleVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(new File(gradleModulePath)); |
| 144 | + return ProjectFileIndex.getInstance(project).getModuleForFile(gradleVirtualFile); |
| 145 | + case Maven: |
| 146 | + return ProjectFileIndex.getInstance(project).getModuleForFile(((MavenProject) azureArtifact.getReferencedObject()).getFile()); |
| 147 | + default: |
| 148 | + // IntelliJ artifact is bind to project, can not get the related module, same for File artifact |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + |
124 | 153 | private String getGradleProjectId(ExternalProjectPojo gradleProjectPojo) { |
125 | 154 | ExternalProject externalProject = getRelatedExternalProject(gradleProjectPojo); |
126 | 155 | return Objects.nonNull(externalProject) ? externalProject.getQName() : null; |
|
0 commit comments