Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic to get module in Azure service linker #5055

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package com.microsoft.azure.toolkit.intellij.common;

import com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.artifacts.Artifact;
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
Expand All @@ -15,16 +18,20 @@
import org.apache.commons.compress.utils.FileNameUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.plugins.gradle.model.ExternalProject;
import org.jetbrains.plugins.gradle.service.project.data.ExternalProjectDataCache;

import java.io.File;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.microsoft.azure.toolkit.intellij.common.AzureArtifactType.File;

public class AzureArtifactManager {
private static Map<Project, AzureArtifactManager> projectAzureArtifactManagerMap = new HashMap<>();
private final Project project;
Expand Down Expand Up @@ -93,7 +100,7 @@ public AzureArtifact getAzureArtifactById(String artifactId) {
}

public AzureArtifact getAzureArtifactById(AzureArtifactType azureArtifactType, String artifactId) {
return azureArtifactType == AzureArtifactType.File ? AzureArtifact.createFromFile(artifactId) :
return azureArtifactType == File ? AzureArtifact.createFromFile(artifactId) :
getAllSupportedAzureArtifacts().stream().filter(artifact -> StringUtils.equals(getArtifactIdentifier(
artifact), artifactId)).findFirst().orElse(null);
}
Expand Down Expand Up @@ -121,6 +128,28 @@ public boolean equalsAzureArtifactIdentifier(AzureArtifact artifact1, AzureArtif
return StringUtils.equals(getArtifactIdentifier(artifact1), getArtifactIdentifier(artifact2));
}

@Nullable
@AzureOperation(
name = "common|artifact.get_module",
type = AzureOperation.Type.TASK
)
public Module getModuleFromAzureArtifact(AzureArtifact azureArtifact) {
if (azureArtifact == null || azureArtifact.getReferencedObject() == null) {
return null;
}
switch (azureArtifact.getType()) {
case Gradle:
final String gradleModulePath = ((ExternalProjectPojo) azureArtifact.getReferencedObject()).getPath();
final VirtualFile gradleVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(new File(gradleModulePath));
return ProjectFileIndex.getInstance(project).getModuleForFile(gradleVirtualFile);
case Maven:
return ProjectFileIndex.getInstance(project).getModuleForFile(((MavenProject) azureArtifact.getReferencedObject()).getFile());
default:
// IntelliJ artifact is bind to project, can not get the related module, same for File artifact
return null;
}
}

private String getGradleProjectId(ExternalProjectPojo gradleProjectPojo) {
ExternalProject externalProject = getRelatedExternalProject(gradleProjectPojo);
return Objects.nonNull(externalProject) ? externalProject.getQName() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.microsoft.azure.toolkit.intellij.common.AzureArtifact;
import com.microsoft.azure.toolkit.intellij.common.AzureArtifactManager;
import com.microsoft.azure.toolkit.intellij.webapp.runner.webappconfig.WebAppConfiguration;
Expand Down Expand Up @@ -107,8 +104,7 @@ private String getModuleName(@NotNull RunConfiguration runConfiguration) {
WebAppConfiguration webAppConfiguration = (WebAppConfiguration) runConfiguration;
final AzureArtifact azureArtifact = AzureArtifactManager.getInstance(runConfiguration.getProject())
.getAzureArtifactById(webAppConfiguration.getAzureArtifactType(), webAppConfiguration.getArtifactIdentifier());
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(azureArtifact.getTargetPath());
Module module = ProjectFileIndex.getInstance(runConfiguration.getProject()).getModuleForFile(virtualFile, false);
final Module module = AzureArtifactManager.getInstance(runConfiguration.getProject()).getModuleFromAzureArtifact(azureArtifact);
return Objects.nonNull(module) ? module.getName() : StringUtils.EMPTY;
}
if (runConfiguration instanceof AbstractRunConfiguration
Expand Down