Skip to content

Commit d5b6bce

Browse files
committed
Fix logic to get module in Azure service linker
1 parent a84be15 commit d5b6bce

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/common/AzureArtifactManager.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
package com.microsoft.azure.toolkit.intellij.common;
66

77
import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo;
8+
import com.intellij.openapi.module.Module;
89
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.roots.ProjectFileIndex;
11+
import com.intellij.openapi.vfs.LocalFileSystem;
912
import com.intellij.openapi.vfs.VirtualFile;
1013
import com.intellij.packaging.artifacts.Artifact;
1114
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
@@ -15,16 +18,20 @@
1518
import org.apache.commons.compress.utils.FileNameUtils;
1619
import org.apache.commons.lang3.StringUtils;
1720
import org.jetbrains.annotations.NotNull;
21+
import org.jetbrains.annotations.Nullable;
1822
import org.jetbrains.idea.maven.model.MavenConstants;
1923
import org.jetbrains.idea.maven.project.MavenProject;
2024
import org.jetbrains.idea.maven.project.MavenProjectsManager;
2125
import org.jetbrains.plugins.gradle.model.ExternalProject;
2226
import org.jetbrains.plugins.gradle.service.project.data.ExternalProjectDataCache;
2327

28+
import java.io.File;
2429
import java.util.*;
2530
import java.util.function.Predicate;
2631
import java.util.stream.Collectors;
2732

33+
import static com.microsoft.azure.toolkit.intellij.common.AzureArtifactType.File;
34+
2835
public class AzureArtifactManager {
2936
private static Map<Project, AzureArtifactManager> projectAzureArtifactManagerMap = new HashMap<>();
3037
private final Project project;
@@ -93,7 +100,7 @@ public AzureArtifact getAzureArtifactById(String artifactId) {
93100
}
94101

95102
public AzureArtifact getAzureArtifactById(AzureArtifactType azureArtifactType, String artifactId) {
96-
return azureArtifactType == AzureArtifactType.File ? AzureArtifact.createFromFile(artifactId) :
103+
return azureArtifactType == File ? AzureArtifact.createFromFile(artifactId) :
97104
getAllSupportedAzureArtifacts().stream().filter(artifact -> StringUtils.equals(getArtifactIdentifier(
98105
artifact), artifactId)).findFirst().orElse(null);
99106
}
@@ -121,6 +128,28 @@ public boolean equalsAzureArtifactIdentifier(AzureArtifact artifact1, AzureArtif
121128
return StringUtils.equals(getArtifactIdentifier(artifact1), getArtifactIdentifier(artifact2));
122129
}
123130

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+
124153
private String getGradleProjectId(ExternalProjectPojo gradleProjectPojo) {
125154
ExternalProject externalProject = getRelatedExternalProject(gradleProjectPojo);
126155
return Objects.nonNull(externalProject) ? externalProject.getQName() : null;

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/LinkAzureServiceBeforeRunProvider.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import com.intellij.openapi.actionSystem.DataContext;
1616
import com.intellij.openapi.diagnostic.Logger;
1717
import com.intellij.openapi.module.Module;
18-
import com.intellij.openapi.roots.ProjectFileIndex;
1918
import com.intellij.openapi.util.Key;
20-
import com.intellij.openapi.vfs.LocalFileSystem;
21-
import com.intellij.openapi.vfs.VirtualFile;
2219
import com.microsoft.azure.toolkit.intellij.common.AzureArtifact;
2320
import com.microsoft.azure.toolkit.intellij.common.AzureArtifactManager;
2421
import com.microsoft.azure.toolkit.intellij.webapp.runner.webappconfig.WebAppConfiguration;
@@ -107,8 +104,7 @@ private String getModuleName(@NotNull RunConfiguration runConfiguration) {
107104
WebAppConfiguration webAppConfiguration = (WebAppConfiguration) runConfiguration;
108105
final AzureArtifact azureArtifact = AzureArtifactManager.getInstance(runConfiguration.getProject())
109106
.getAzureArtifactById(webAppConfiguration.getAzureArtifactType(), webAppConfiguration.getArtifactIdentifier());
110-
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(azureArtifact.getTargetPath());
111-
Module module = ProjectFileIndex.getInstance(runConfiguration.getProject()).getModuleForFile(virtualFile, false);
107+
final Module module = AzureArtifactManager.getInstance(runConfiguration.getProject()).getModuleFromAzureArtifact(azureArtifact);
112108
return Objects.nonNull(module) ? module.getName() : StringUtils.EMPTY;
113109
}
114110
if (runConfiguration instanceof AbstractRunConfiguration

0 commit comments

Comments
 (0)