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<Pair<Project, VirtualFile>> 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;
return buffer.toString();
}
- public void setCompilableResourceFileCondition(final Condition<Pair<Project, VirtualFile>> 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;
}
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<? extends Artifact> artifacts = ArtifactBySourceFileFinder.getInstance(project).findArtifacts(file);
+ return artifacts.isEmpty();
}
}
\ No newline at end of file
*/
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;
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
final MultiValuesMap<VirtualFile, Artifact> result = new MultiValuesMap<VirtualFile, Artifact>();
final ArtifactManager artifactManager = ArtifactManager.getInstance(myProject);
for (final Artifact artifact : artifactManager.getArtifacts()) {
- ArtifactUtil.processFileOrDirectoryCopyElements(artifact, new PackagingElementProcessor<FileOrDirectoryCopyPackagingElement<?>>() {
+ final PackagingElementResolvingContext context = artifactManager.getResolvingContext();
+ ArtifactUtil.processPackagingElements(artifact, null, new PackagingElementProcessor<PackagingElement<?>>() {
@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;
}
*/
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;
}
public static Collection<Trinity<Artifact, PackagingElementPath, String>> findContainingArtifactsWithOutputPaths(@NotNull final VirtualFile file, @NotNull Project project) {
+ final boolean isResourceFile = CompilerConfiguration.getInstance(project).isResourceFile(file);
final List<Trinity<Artifact, PackagingElementPath, String>> artifacts = new ArrayList<Trinity<Artifact, PackagingElementPath, String>>();
+ final PackagingElementResolvingContext context = ArtifactManager.getInstance(project).getResolvingContext();
for (final Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) {
- processFileOrDirectoryCopyElements(artifact, new PackagingElementProcessor<FileOrDirectoryCopyPackagingElement<?>>() {
+ processPackagingElements(artifact, null, new PackagingElementProcessor<PackagingElement<?>>() {
@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<VirtualFile> files = findSourceFilesByOutputPath(artifact.getRootElement(), outputPath, context, artifact.getArtifactType());
#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}.