From ee36777dba441c6d487bf626766ca69b7ec36ea4 Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 26 Nov 2009 14:08:01 +0300 Subject: [PATCH] IDEADEV-41464: Package File does nothing --- .../intellij/compiler/actions/CompileAction.java | 22 ++++----- .../artifacts/ArtifactBySourceFileFinderImpl.java | 41 +++++++++++++--- .../packaging/impl/artifacts/ArtifactUtil.java | 55 +++++++++++++++++----- .../src/messages/CompilerBundle.properties | 2 +- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/actions/CompileAction.java b/java/compiler/impl/src/com/intellij/compiler/actions/CompileAction.java index 98de5796ce..bf752c2dad 100644 --- a/java/compiler/impl/src/com/intellij/compiler/actions/CompileAction.java +++ b/java/compiler/impl/src/com/intellij/compiler/actions/CompileAction.java @@ -27,19 +27,18 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.packaging.artifacts.Artifact; +import com.intellij.packaging.impl.artifacts.ArtifactBySourceFileFinder; import com.intellij.psi.*; import java.util.ArrayList; +import java.util.Collection; import java.util.List; public class CompileAction extends CompileActionBase { - private Condition> myCompilableResourceFileCondition; - protected void doAction(DataContext dataContext, Project project) { final Module module = LangDataKeys.MODULE_CONTEXT.getData(dataContext); final boolean trackDependencies = CompilerWorkspaceConfiguration.getInstance(project).COMPILE_DEPENDENT_FILES; @@ -154,11 +153,7 @@ public class CompileAction extends CompileActionBase { return buffer.toString(); } - public void setCompilableResourceFileCondition(final Condition> compilableResourceFileCondition) { - myCompilableResourceFileCondition = compilableResourceFileCondition; - } - - private VirtualFile[] getCompilableFiles(Project project, VirtualFile[] files) { + private static VirtualFile[] getCompilableFiles(Project project, VirtualFile[] files) { if (files == null || files.length == 0) { return VirtualFile.EMPTY_ARRAY; } @@ -192,8 +187,11 @@ public class CompileAction extends CompileActionBase { return VfsUtil.toVirtualFileArray(filesToCompile); } - private boolean isCompilableResourceFile(final Project project, final CompilerConfiguration compilerConfiguration, final VirtualFile file) { - return compilerConfiguration.isResourceFile(file) && - (myCompilableResourceFileCondition == null || myCompilableResourceFileCondition.value(Pair.create(project, file))); + private static boolean isCompilableResourceFile(final Project project, final CompilerConfiguration compilerConfiguration, final VirtualFile file) { + if (!compilerConfiguration.isResourceFile(file)) { + return false; + } + final Collection artifacts = ArtifactBySourceFileFinder.getInstance(project).findArtifacts(file); + return artifacts.isEmpty(); } } \ No newline at end of file diff --git a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactBySourceFileFinderImpl.java b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactBySourceFileFinderImpl.java index 3c5876239a..aad584cacd 100644 --- a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactBySourceFileFinderImpl.java +++ b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactBySourceFileFinderImpl.java @@ -15,15 +15,22 @@ */ package com.intellij.packaging.impl.artifacts; +import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModuleRootModel; +import com.intellij.openapi.roots.SourceFolder; import com.intellij.openapi.util.ModificationTracker; import com.intellij.openapi.util.MultiValuesMap; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.Artifact; import com.intellij.packaging.artifacts.ArtifactManager; import com.intellij.packaging.elements.ComplexPackagingElementType; +import com.intellij.packaging.elements.PackagingElement; import com.intellij.packaging.elements.PackagingElementFactory; +import com.intellij.packaging.elements.PackagingElementResolvingContext; import com.intellij.packaging.impl.elements.FileOrDirectoryCopyPackagingElement; +import com.intellij.packaging.impl.elements.ModuleOutputPackagingElement; import com.intellij.psi.util.CachedValue; import com.intellij.psi.util.CachedValueProvider; import com.intellij.psi.util.CachedValuesManager; @@ -31,7 +38,10 @@ import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; /** * @author nik @@ -66,16 +76,33 @@ public class ArtifactBySourceFileFinderImpl extends ArtifactBySourceFileFinder { final MultiValuesMap result = new MultiValuesMap(); final ArtifactManager artifactManager = ArtifactManager.getInstance(myProject); for (final Artifact artifact : artifactManager.getArtifacts()) { - ArtifactUtil.processFileOrDirectoryCopyElements(artifact, new PackagingElementProcessor>() { + final PackagingElementResolvingContext context = artifactManager.getResolvingContext(); + ArtifactUtil.processPackagingElements(artifact, null, new PackagingElementProcessor>() { @Override - public boolean process(@NotNull FileOrDirectoryCopyPackagingElement element, @NotNull PackagingElementPath path) { - final VirtualFile root = element.findFile(); - if (root != null) { - result.put(root, artifact); + public boolean process(@NotNull PackagingElement element, @NotNull PackagingElementPath path) { + if (element instanceof FileOrDirectoryCopyPackagingElement) { + final VirtualFile root = ((FileOrDirectoryCopyPackagingElement)element).findFile(); + if (root != null) { + result.put(root, artifact); + } + } + else if (element instanceof ModuleOutputPackagingElement) { + final Module module = ((ModuleOutputPackagingElement)element).findModule(context); + if (module != null) { + final ModuleRootModel rootModel = context.getModulesProvider().getRootModel(module); + for (ContentEntry contentEntry : rootModel.getContentEntries()) { + for (SourceFolder sourceFolder : contentEntry.getSourceFolders()) { + final VirtualFile sourceRoot = sourceFolder.getFile(); + if (sourceRoot != null && !sourceFolder.isTestSource()) { + result.put(sourceRoot, artifact); + } + } + } + } } return true; } - }, artifactManager.getResolvingContext(), true); + }, context, true); } return result; } diff --git a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactUtil.java b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactUtil.java index 560d317b8f..8d9ea13d3a 100644 --- a/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactUtil.java +++ b/java/compiler/impl/src/com/intellij/packaging/impl/artifacts/ArtifactUtil.java @@ -15,10 +15,12 @@ */ package com.intellij.packaging.impl.artifacts; +import com.intellij.compiler.CompilerConfiguration; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.CompilerProjectExtension; import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModuleRootModel; import com.intellij.openapi.roots.SourceFolder; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Pair; @@ -303,31 +305,60 @@ public class ArtifactUtil { } public static Collection> findContainingArtifactsWithOutputPaths(@NotNull final VirtualFile file, @NotNull Project project) { + final boolean isResourceFile = CompilerConfiguration.getInstance(project).isResourceFile(file); final List> artifacts = new ArrayList>(); + final PackagingElementResolvingContext context = ArtifactManager.getInstance(project).getResolvingContext(); for (final Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) { - processFileOrDirectoryCopyElements(artifact, new PackagingElementProcessor>() { + processPackagingElements(artifact, null, new PackagingElementProcessor>() { @Override - public boolean process(@NotNull FileOrDirectoryCopyPackagingElement element, @NotNull PackagingElementPath path) { - final VirtualFile root = element.findFile(); - if (root != null && VfsUtil.isAncestor(root, file, false)) { - final String relativePath; - if (root.equals(file) && element instanceof FileCopyPackagingElement) { - relativePath = ((FileCopyPackagingElement)element).getOutputFileName(); + public boolean process(@NotNull PackagingElement element, @NotNull PackagingElementPath path) { + if (element instanceof FileOrDirectoryCopyPackagingElement) { + final VirtualFile root = ((FileOrDirectoryCopyPackagingElement)element).findFile(); + if (root != null && VfsUtil.isAncestor(root, file, false)) { + final String relativePath; + if (root.equals(file) && element instanceof FileCopyPackagingElement) { + relativePath = ((FileCopyPackagingElement)element).getOutputFileName(); + } + else { + relativePath = VfsUtil.getRelativePath(file, root, '/'); + } + artifacts.add(Trinity.create(artifact, path, relativePath)); + return false; } - else { - relativePath = VfsUtil.getRelativePath(file, root, '/'); + } + else if (isResourceFile && element instanceof ModuleOutputPackagingElement) { + final String relativePath = getRelativePathInSources(file, (ModuleOutputPackagingElement)element, context); + if (relativePath != null) { + artifacts.add(Trinity.create(artifact, path, relativePath)); + return false; } - artifacts.add(Trinity.create(artifact, path, relativePath)); - return false; } return true; } - }, ArtifactManager.getInstance(project).getResolvingContext(), true); + }, context, true); } return artifacts; } @Nullable + private static String getRelativePathInSources(@NotNull VirtualFile file, final @NotNull ModuleOutputPackagingElement moduleElement, + @NotNull PackagingElementResolvingContext context) { + final Module module = moduleElement.findModule(context); + if (module != null) { + final ModuleRootModel rootModel = context.getModulesProvider().getRootModel(module); + for (ContentEntry entry : rootModel.getContentEntries()) { + for (SourceFolder folder : entry.getSourceFolders()) { + final VirtualFile sourceRoot = folder.getFile(); + if (!folder.isTestSource() && sourceRoot != null && VfsUtil.isAncestor(sourceRoot, file, true)) { + return VfsUtil.getRelativePath(file, sourceRoot, '/'); + } + } + } + } + return null; + } + + @Nullable public static VirtualFile findSourceFileByOutputPath(Artifact artifact, String outputPath, PackagingElementResolvingContext context) { final List files = findSourceFilesByOutputPath(artifact.getRootElement(), outputPath, context, artifact.getArtifactType()); return files.isEmpty() ? null : files.get(0); diff --git a/resources-en/src/messages/CompilerBundle.properties b/resources-en/src/messages/CompilerBundle.properties index bb93c51e97..a3d26d1312 100644 --- a/resources-en/src/messages/CompilerBundle.properties +++ b/resources-en/src/messages/CompilerBundle.properties @@ -226,7 +226,7 @@ chooser.description.select.output.directory.for.0.artifact=Select output directo #package file action action.name.package.file=Package file -action.description.package.file=Update the file into the corresponding artifacts +action.description.package.file=Update the file in the corresponding artifacts message.tect.package.file.io.error=IO Error:\n{0} command.name.package.file=Package file status.text.file.has.been.packaged={0, choice, 1#File|2#Files} {1} {0, choice, 1#has|2#have} been packaged at {2}. -- 2.11.4.GIT