summaryrefslogtreecommitdiff
path: root/platform/lang-api/src/com/intellij/openapi/module/ModuleUtil.java
blob: bf56114a2467484804303fe58df36e88818cfccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author cdr
 */
package com.intellij.openapi.module;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.ParameterizedCachedValue;
import com.intellij.psi.util.ParameterizedCachedValueProvider;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.module.JpsModuleSourceRootType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ModuleUtil extends ModuleUtilCore {
  private static final ParameterizedCachedValueProvider<MultiMap<ModuleType<?>,Module>,Project> MODULE_BY_TYPE_VALUE_PROVIDER =
    new ParameterizedCachedValueProvider<MultiMap<ModuleType<?>, Module>, Project>() {
      @Nullable
      @Override
      public CachedValueProvider.Result<MultiMap<ModuleType<?>, Module>> compute(Project param) {
        MultiMap<ModuleType<?>, Module> map = new MultiMap<ModuleType<?>, Module>();
        for (Module module : ModuleManager.getInstance(param).getModules()) {
          map.putValue(ModuleType.get(module), module);
        }
        return CachedValueProvider.Result.createSingleDependency(map, ProjectRootManager.getInstance(param));
      }
    };
  private static final Key<ParameterizedCachedValue<MultiMap<ModuleType<?>, Module>, Project>> MODULES_BY_TYPE_KEY = Key.create("MODULES_BY_TYPE");

  private ModuleUtil() {}

  /**
   * @deprecated use ModuleManager#getModuleDependentModules(com.intellij.openapi.module.Module) instead
   */
  @Nullable
  public static Module getParentModuleOfType(ModuleType expectedModuleType, Module module) {
    if (module == null) return null;
    if (expectedModuleType.equals(ModuleType.get(module))) return module;
    final List<Module> parents = getParentModulesOfType(expectedModuleType, module);
    return parents.isEmpty() ? null : parents.get(0);
  }

  /**
   * @deprecated use ModuleManager#getModuleDependentModules(com.intellij.openapi.module.Module) instead
   */
  @NotNull
  public static List<Module> getParentModulesOfType(ModuleType expectedModuleType, Module module) {
    final List<Module> parents = ModuleManager.getInstance(module.getProject()).getModuleDependentModules(module);
    ArrayList<Module> modules = new ArrayList<Module>();
    for (Module parent : parents) {
      if (expectedModuleType.equals(ModuleType.get(parent))) {
        modules.add(parent);
      }
    }
    return modules;
  }

  @NotNull
  public static Collection<Module> getModulesOfType(@NotNull Project project, @NotNull ModuleType<?> moduleType) {
    return CachedValuesManager.getManager(project).getParameterizedCachedValue(project, MODULES_BY_TYPE_KEY, MODULE_BY_TYPE_VALUE_PROVIDER,
                                                                               false, project).get(moduleType);
  }

  public static boolean hasModulesOfType(@NotNull Project project, @NotNull ModuleType<?> module) {
    return !getModulesOfType(project, module).isEmpty();
  }

  public static boolean isSupportedRootType(Project project, JpsModuleSourceRootType sourceRootType) {
    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
      if (ModuleType.get(module).isSupportedRootType(sourceRootType)) {
        return true;
      }
    }
    return modules.length == 0;
  }

  @Nullable
  public static ModuleType getModuleType(@NotNull Module module) {
    String type = module.getOptionValue(Module.ELEMENT_TYPE);
    return ModuleTypeManager.getInstance().findByID(type);
  }

  public static void updateExcludedFoldersInWriteAction(final Module module,
                                                        @NotNull final VirtualFile contentRoot,
                                                        final Collection<String> urlsToUnExclude,
                                                        final Collection<String> urlsToExclude) {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      public void run() {
        final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
        try {
          for (final ContentEntry contentEntry : modifiableModel.getContentEntries()) {
            if (contentRoot.equals(contentEntry.getFile())) {
              for (String url : urlsToUnExclude) {
                contentEntry.removeExcludeFolder(url);
              }
              for (String url : urlsToExclude) {
                contentEntry.addExcludeFolder(url);
              }
              break;
            }
          }
          modifiableModel.commit();
        }
        catch (Exception e) {
          modifiableModel.dispose();
          throw new RuntimeException(e.getMessage(), e);
        }
      }
    });
  }
}