summaryrefslogtreecommitdiff
path: root/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java
blob: 0b4f135837c9fbc90fa9f26d1cf19cfa9781ec0e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * 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.
 */
package com.intellij.openapi.roots;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author nik
 */
public class ModuleRootModificationUtil {
  public static void addModuleLibrary(Module module, String libName, List<String> classesRoots, List<String> sourceRoots) {
    addModuleLibrary(module, libName, classesRoots, sourceRoots, DependencyScope.COMPILE);
  }

  public static void addModuleLibrary(final Module module,
                                      final String libName,
                                      final List<String> classesRoots,
                                      final List<String> sourceRoots,
                                      final DependencyScope scope) {
    addModuleLibrary(module, libName, classesRoots, sourceRoots, Collections.<String>emptyList(), scope);
  }

  public static void addModuleLibrary(final Module module, final String libName,
                                      final List<String> classesRoots,
                                      final List<String> sourceRoots,
                                      final List<String> excludedRoots,
                                      final DependencyScope scope) {
    addModuleLibrary(module, libName, classesRoots, sourceRoots, excludedRoots, scope, false);
  }
  public static void addModuleLibrary(final Module module, final String libName,
                                      final List<String> classesRoots,
                                      final List<String> sourceRoots,
                                      final List<String> excludedRoots,
                                      final DependencyScope scope, 
                                      final boolean exported) {
    updateModel(module, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(final ModifiableRootModel model) {
        final LibraryEx library = (LibraryEx)model.getModuleLibraryTable().createLibrary(libName);
        final LibraryEx.ModifiableModelEx libraryModel = library.getModifiableModel();

        for (String root : classesRoots) {
          libraryModel.addRoot(root, OrderRootType.CLASSES);
        }
        for (String root : sourceRoots) {
          libraryModel.addRoot(root, OrderRootType.SOURCES);
        }
        for (String excluded : excludedRoots) {
          libraryModel.addExcludedRoot(excluded);
        }

        LibraryOrderEntry entry = model.findLibraryOrderEntry(library);
        assert entry != null : library;
        entry.setScope(scope);
        entry.setExported(exported);

        doWriteAction(new Runnable() {
          @Override
          public void run() {
            libraryModel.commit();
          }
        });
      }
    });
  }

  public static void addModuleLibrary(Module module, String classesRootUrl) {
    addModuleLibrary(module, null, Collections.singletonList(classesRootUrl), Collections.<String>emptyList());
  }

  public static void addDependency(Module module, Library library) {
    addDependency(module, library, DependencyScope.COMPILE, false);
  }

  public static void addDependency(Module module, final Library library, final DependencyScope scope, final boolean exported) {
    updateModel(module, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(ModifiableRootModel model) {
        LibraryOrderEntry entry = model.addLibraryEntry(library);
        entry.setExported(exported);
        entry.setScope(scope);
      }
    });
  }

  public static void setModuleSdk(Module module, @Nullable final Sdk sdk) {
    updateModel(module, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(ModifiableRootModel model) {
        model.setSdk(sdk);
      }
    });
  }

  public static void setSdkInherited(Module module) {
    updateModel(module, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(ModifiableRootModel model) {
        model.inheritSdk();
      }
    });
  }

  public static void addDependency(final Module from, final Module to) {
    addDependency(from, to, DependencyScope.COMPILE, false);
  }

  public static void addDependency(Module from, final Module to, final DependencyScope scope, final boolean exported) {
    updateModel(from, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(ModifiableRootModel model) {
        ModuleOrderEntry entry = model.addModuleOrderEntry(to);
        entry.setScope(scope);
        entry.setExported(exported);
      }
    });
  }

  public static void updateModel(@NotNull final Module module, @NotNull Consumer<ModifiableRootModel> task) {
    final ModifiableRootModel model = ApplicationManager.getApplication().runReadAction(new Computable<ModifiableRootModel>() {
      @Override
      public ModifiableRootModel compute() {
        return ModuleRootManager.getInstance(module).getModifiableModel();
      }
    });
    try {
      task.consume(model);
      doWriteAction(new Runnable() {
        @Override
        public void run() {
          model.commit();
        }
      });
    }
    catch (RuntimeException e) {
      model.dispose();
      throw e;
    }
    catch (Error e) {
      model.dispose();
      throw e;
    }
  }

  public static void updateExcludedFolders(final Module module,
                                                        @NotNull final VirtualFile contentRoot,
                                                        final Collection<String> urlsToUnExclude,
                                                        final Collection<String> urlsToExclude) {
    updateModel(module, new Consumer<ModifiableRootModel>() {
      @Override
      public void consume(ModifiableRootModel modifiableModel) {
        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;
          }
        }
      }
    });
  }

  private static void doWriteAction(final Runnable action) {
    final Application application = ApplicationManager.getApplication();
    application.invokeAndWait(new Runnable() {
      @Override
      public void run() {
        application.runWriteAction(action);
      }
    }, application.getDefaultModalityState());
  }
}