4 package com
.intellij
.ide
.impl
;
6 import com
.intellij
.openapi
.application
.ApplicationManager
;
7 import com
.intellij
.openapi
.module
.ModifiableModuleModel
;
8 import com
.intellij
.openapi
.module
.Module
;
9 import com
.intellij
.openapi
.module
.ModuleManager
;
10 import com
.intellij
.openapi
.project
.Project
;
11 import com
.intellij
.openapi
.roots
.*;
12 import com
.intellij
.openapi
.roots
.ex
.ProjectRootManagerEx
;
13 import com
.intellij
.openapi
.util
.io
.FileUtil
;
14 import com
.intellij
.openapi
.util
.text
.StringUtil
;
15 import com
.intellij
.openapi
.vfs
.VfsUtil
;
16 import com
.intellij
.openapi
.vfs
.VirtualFile
;
17 import org
.jetbrains
.annotations
.NonNls
;
19 import java
.util
.HashMap
;
20 import java
.util
.HashSet
;
23 import java
.util
.regex
.Pattern
;
25 public class PatchProjectUtil
{
26 private PatchProjectUtil() {
29 public static void patchProject(final Project project
) {
30 final Map
<Pattern
, Set
<Pattern
>> excludePatterns
= loadPatterns("idea.exclude.patterns");
31 final Map
<Pattern
, Set
<Pattern
>> includePatterns
= loadPatterns("idea.include.patterns");
33 if (excludePatterns
.isEmpty() && includePatterns
.isEmpty()) return;
34 final ProjectFileIndex index
= ProjectRootManager
.getInstance(project
).getFileIndex();
35 final ModifiableModuleModel modulesModel
= ModuleManager
.getInstance(project
).getModifiableModel();
36 final Module
[] modules
= modulesModel
.getModules();
37 final ModifiableRootModel
[] models
= new ModifiableRootModel
[modules
.length
];
38 for (int i
= 0; i
< modules
.length
; i
++) {
39 models
[i
] = ModuleRootManager
.getInstance(modules
[i
]).getModifiableModel();
41 final ContentEntry
[] contentEntries
= models
[i
].getContentEntries();
42 for (final ContentEntry contentEntry
: contentEntries
) {
43 final VirtualFile contentRoot
= contentEntry
.getFile();
44 if (contentRoot
== null) continue;
45 final Set
<VirtualFile
> included
= new HashSet
<VirtualFile
>();
46 iterate(contentRoot
, new ContentIterator() {
47 public boolean processFile(final VirtualFile fileOrDir
) {
48 String relativeName
= VfsUtil
.getRelativePath(fileOrDir
, contentRoot
, '/');
49 for (Pattern module
: excludePatterns
.keySet()) {
50 if (module
== null || module
.matcher(modules
[idx
].getName()).matches()) {
51 final Set
<Pattern
> dirPatterns
= excludePatterns
.get(module
);
52 for (Pattern pattern
: dirPatterns
) {
53 if (pattern
.matcher(relativeName
).matches()) {
54 contentEntry
.addExcludeFolder(fileOrDir
);
60 if (includePatterns
.isEmpty()) return true;
61 for (Pattern module
: includePatterns
.keySet()) {
62 if (module
== null || module
.matcher(modules
[idx
].getName()).matches()) {
63 final Set
<Pattern
> dirPatterns
= includePatterns
.get(module
);
64 for (Pattern pattern
: dirPatterns
) {
65 if (pattern
.matcher(relativeName
).matches()) {
66 included
.add(fileOrDir
);
75 processIncluded(contentEntry
, included
);
79 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
81 ProjectRootManagerEx
.getInstanceEx(project
).multiCommit(modulesModel
, models
);
86 public static void processIncluded(final ContentEntry contentEntry
, final Set
<VirtualFile
> included
) {
87 if (included
.isEmpty()) return;
88 final Set
<VirtualFile
> parents
= new HashSet
<VirtualFile
>();
89 for (VirtualFile file
: included
) {
90 if (file
== contentEntry
.getFile()) return;
91 final VirtualFile parent
= file
.getParent();
92 if (parent
== null || parents
.contains(parent
)) continue;
94 for (VirtualFile toExclude
: parent
.getChildren()) {
95 boolean toExcludeSibling
= true;
96 for (VirtualFile includeRoot
: included
) {
97 if (VfsUtil
.isAncestor(toExclude
, includeRoot
, false)) {
98 toExcludeSibling
= false;
101 if (toExcludeSibling
) {
102 contentEntry
.addExcludeFolder(toExclude
);
106 processIncluded(contentEntry
, parents
);
109 public static void iterate(VirtualFile contentRoot
, ContentIterator iterator
, ProjectFileIndex idx
) {
110 if (!iterator
.processFile(contentRoot
)) return;
111 if (idx
.getModuleForFile(contentRoot
) == null) return; //already excluded
112 final VirtualFile
[] files
= contentRoot
.getChildren();
113 for (VirtualFile file
: files
) {
114 iterate(file
, iterator
, idx
);
118 public static Map
<Pattern
, Set
<Pattern
>> loadPatterns(@NonNls String propertyKey
) {
119 final Map
<Pattern
, Set
<Pattern
>> result
= new HashMap
<Pattern
, Set
<Pattern
>>();
120 final String patterns
= System
.getProperty(propertyKey
);
121 if (patterns
!= null) {
122 final String
[] pathPatterns
= patterns
.split(";");
123 for (String excludedPattern
: pathPatterns
) {
124 String module
= null;
126 if (excludedPattern
.startsWith("[")) {
127 idx
= excludedPattern
.indexOf("]") + 1;
128 module
= excludedPattern
.substring(1, idx
- 1);
130 final Pattern modulePattern
= module
!= null ? Pattern
.compile(StringUtil
.replace(module
, "*", ".*")) : null;
131 final Pattern pattern
= Pattern
.compile(FileUtil
.convertAntToRegexp(excludedPattern
.substring(idx
)));
132 Set
<Pattern
> dirPatterns
= result
.get(modulePattern
);
133 if (dirPatterns
== null) {
134 dirPatterns
= new HashSet
<Pattern
>();
135 result
.put(modulePattern
, dirPatterns
);
137 dirPatterns
.add(pattern
);