use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-community by JetBrains.
the class MvcModuleStructureUtil method createAuxiliaryModule.
@NotNull
public static Module createAuxiliaryModule(@NotNull Module appModule, final String moduleName, final MvcFramework framework) {
ModuleManager moduleManager = ModuleManager.getInstance(appModule.getProject());
final ModifiableModuleModel moduleModel = moduleManager.getModifiableModel();
final String moduleFilePath = new File(appModule.getModuleFilePath()).getParent() + "/" + moduleName + ".iml";
final VirtualFile existing = LocalFileSystem.getInstance().findFileByPath(moduleFilePath);
if (existing != null) {
try {
existing.delete("Grails/Griffon plugins maintenance");
} catch (IOException e) {
LOG.error(e);
}
}
moduleModel.newModule(moduleFilePath, StdModuleTypes.JAVA.getId());
moduleModel.commit();
Module pluginsModule = moduleManager.findModuleByName(moduleName);
assert pluginsModule != null;
ModifiableRootModel newRootModel = ModuleRootManager.getInstance(pluginsModule).getModifiableModel();
ModifiableRootModel appModel = ModuleRootManager.getInstance(appModule).getModifiableModel();
copySdkAndLibraries(appModel, newRootModel, framework);
newRootModel.commit();
appModel.commit();
return pluginsModule;
}
use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-community by JetBrains.
the class PatchProjectUtil method patchProject.
/**
* Excludes folders specified in patterns in the {@code idea.exclude.patterns} system property from the project.
*
* <p>Pattern syntax:
* <br>
*
* <ul>
* <li>{@code patterns := pattern(';'pattern)*}
* <li>{@code pattern := ('['moduleRegEx']')? directoryAntPattern}
* </ul>
*
* Where
* <ul>
* <li> {@code moduleRegex} - regular expression to match module name.
* <li> {@code directoryAntPattern} - ant-style pattern to match folder in a module.
* {@code directoryAntPattern} considers paths <b>relative</b> to a content root of a module.
* </ul>
*
*
* <p>
* Example:<br>
* {@code
* -Didea.exclude.patterns=testData/**;.reports/**;[sql]/test/*.sql;[graph]/**;[graph-openapi]/**
* }
* <br>
*
* In this example the {@code testData/**} pattern is applied to all modules
* and the pattern {@code /test/*.sql} to applied to the module {@code sql} only.
*
* @param project project to patch
* @see <a href="http://ant.apache.org/manual/dirtasks.html">http://ant.apache.org/manual/dirtasks.html</a>
*/
public static void patchProject(final Project project) {
final Map<Pattern, Set<Pattern>> excludePatterns = loadPatterns("idea.exclude.patterns");
final Map<Pattern, Set<Pattern>> includePatterns = loadPatterns("idea.include.patterns");
if (excludePatterns.isEmpty() && includePatterns.isEmpty())
return;
final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
final ModifiableModuleModel modulesModel = ModuleManager.getInstance(project).getModifiableModel();
final Module[] modules = modulesModel.getModules();
final ModifiableRootModel[] models = new ModifiableRootModel[modules.length];
for (int i = 0; i < modules.length; i++) {
models[i] = ModuleRootManager.getInstance(modules[i]).getModifiableModel();
final int idx = i;
final ContentEntry[] contentEntries = models[i].getContentEntries();
for (final ContentEntry contentEntry : contentEntries) {
final VirtualFile contentRoot = contentEntry.getFile();
if (contentRoot == null)
continue;
final Set<VirtualFile> included = new HashSet<>();
iterate(contentRoot, fileOrDir -> {
String relativeName = VfsUtilCore.getRelativePath(fileOrDir, contentRoot, '/');
for (Pattern module : excludePatterns.keySet()) {
if (module == null || module.matcher(modules[idx].getName()).matches()) {
final Set<Pattern> dirPatterns = excludePatterns.get(module);
for (Pattern pattern : dirPatterns) {
if (pattern.matcher(relativeName).matches()) {
contentEntry.addExcludeFolder(fileOrDir);
return false;
}
}
}
}
if (includePatterns.isEmpty())
return true;
for (Pattern module : includePatterns.keySet()) {
if (module == null || module.matcher(modules[idx].getName()).matches()) {
final Set<Pattern> dirPatterns = includePatterns.get(module);
for (Pattern pattern : dirPatterns) {
if (pattern.matcher(relativeName).matches()) {
included.add(fileOrDir);
return true;
}
}
}
}
return true;
}, index);
processIncluded(contentEntry, included);
}
}
ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(models, modulesModel));
}
use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-community by JetBrains.
the class EclipseImportBuilder method commit.
@Override
public List<Module> commit(final Project project, ModifiableModuleModel model, ModulesProvider modulesProvider, ModifiableArtifactModel artifactModel) {
final Collection<String> unknownLibraries = new TreeSet<>();
final Collection<String> unknownJdks = new TreeSet<>();
final Set<String> refsToModules = new HashSet<>();
final List<Module> result = new ArrayList<>();
final Map<Module, Set<String>> module2NatureNames = new HashMap<>();
try {
final ModifiableModuleModel moduleModel = model != null ? model : ModuleManager.getInstance(project).getModifiableModel();
final ModifiableRootModel[] rootModels = new ModifiableRootModel[getParameters().projectsToConvert.size()];
final Set<File> files = new HashSet<>();
final Set<String> moduleNames = new THashSet<>(getParameters().projectsToConvert.size());
for (String path : getParameters().projectsToConvert) {
String modulesDirectory = getParameters().converterOptions.commonModulesDirectory;
if (modulesDirectory == null) {
modulesDirectory = path;
}
final String moduleName = EclipseProjectFinder.findProjectName(path);
moduleNames.add(moduleName);
final File imlFile = new File(modulesDirectory + File.separator + moduleName + ModuleManagerImpl.IML_EXTENSION);
if (imlFile.isFile()) {
files.add(imlFile);
}
final File emlFile = new File(modulesDirectory + File.separator + moduleName + EclipseXml.IDEA_SETTINGS_POSTFIX);
if (emlFile.isFile()) {
files.add(emlFile);
}
}
if (!files.isEmpty()) {
final int resultCode = Messages.showYesNoCancelDialog(ApplicationNamesInfo.getInstance().getFullProductName() + " module files found:\n" + StringUtil.join(files, file -> file.getPath(), "\n") + ".\n Would you like to reuse them?", "Module Files Found", Messages.getQuestionIcon());
if (resultCode != Messages.YES) {
if (resultCode == Messages.NO) {
final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
for (File file : files) {
final VirtualFile virtualFile = localFileSystem.findFileByIoFile(file);
if (virtualFile != null) {
ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<Void, IOException>() {
@Override
public Void compute() throws IOException {
virtualFile.delete(this);
return null;
}
});
} else {
FileUtil.delete(file);
}
}
} else {
return result;
}
}
}
int idx = 0;
for (String path : getParameters().projectsToConvert) {
String modulesDirectory = getParameters().converterOptions.commonModulesDirectory;
if (modulesDirectory == null) {
modulesDirectory = path;
}
final Module module = moduleModel.newModule(modulesDirectory + "/" + EclipseProjectFinder.findProjectName(path) + ModuleManagerImpl.IML_EXTENSION, StdModuleTypes.JAVA.getId());
result.add(module);
final Set<String> natures = collectNatures(path);
if (natures.size() > 0) {
module2NatureNames.put(module, natures);
}
final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
rootModels[idx++] = rootModel;
final File classpathFile = new File(path, EclipseXml.DOT_CLASSPATH_EXT);
final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, getParameters().projectsToConvert, moduleNames);
classpathReader.init(rootModel);
if (classpathFile.exists()) {
Element classpathElement = JDOMUtil.load(classpathFile);
classpathReader.readClasspath(rootModel, unknownLibraries, unknownJdks, refsToModules, getParameters().converterOptions.testPattern, classpathElement);
} else {
EclipseClasspathReader.setOutputUrl(rootModel, path + "/bin");
}
ClasspathStorage.setStorageType(rootModel, getParameters().linkConverted ? JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID : ClassPathStorageUtil.DEFAULT_STORAGE);
if (model != null) {
ApplicationManager.getApplication().runWriteAction(() -> rootModel.commit());
}
}
if (model == null) {
ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(rootModels, moduleModel));
}
} catch (Exception e) {
LOG.error(e);
}
scheduleNaturesImporting(project, module2NatureNames);
createEclipseLibrary(project, unknownLibraries, IdeaXml.ECLIPSE_LIBRARY);
StringBuilder message = new StringBuilder();
refsToModules.removeAll(getParameters().existingModuleNames);
for (String path : getParameters().projectsToConvert) {
final String projectName = EclipseProjectFinder.findProjectName(path);
if (projectName != null) {
refsToModules.remove(projectName);
getParameters().existingModuleNames.add(projectName);
}
}
if (!refsToModules.isEmpty()) {
message.append("Unknown modules detected");
for (String module : refsToModules) {
message.append("\n").append(module);
}
}
if (!unknownJdks.isEmpty()) {
if (message.length() > 0) {
message.append("\nand jdks");
} else {
message.append("Imported project refers to unknown jdks");
}
for (String unknownJdk : unknownJdks) {
message.append("\n").append(unknownJdk);
}
}
if (!unknownLibraries.isEmpty()) {
final StringBuilder buf = new StringBuilder();
buf.append("<html><body>");
buf.append(EclipseBundle.message("eclipse.import.warning.undefinded.libraries"));
for (String name : unknownLibraries) {
buf.append("<br>").append(name);
}
if (model == null) {
buf.append("<br><b>Please export Eclipse user libraries and import them now from resulted .userlibraries file</b>");
buf.append("</body></html>");
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false) {
@Override
public boolean isFileSelectable(VirtualFile file) {
return super.isFileSelectable(file) && Comparing.strEqual(file.getExtension(), "userlibraries");
}
};
descriptor.setDescription(buf.toString());
descriptor.setTitle(getTitle());
final VirtualFile selectedFile = FileChooser.chooseFile(descriptor, project, project.getBaseDir());
if (selectedFile != null) {
try {
EclipseUserLibrariesHelper.readProjectLibrariesContent(selectedFile, project, unknownLibraries);
} catch (Exception e) {
LOG.error(e);
}
}
}
}
if (message.length() > 0) {
Messages.showErrorDialog(project, message.toString(), getTitle());
}
return result;
}
use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-community by JetBrains.
the class MiscImportingTest method testCheckingIfModuleIsNotDisposedBeforeCommitOnImport.
public void testCheckingIfModuleIsNotDisposedBeforeCommitOnImport() throws Exception {
if (ignore())
return;
importProject("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<modules>" + " <module>m1</module>" + " <module>m2</module>" + "</modules>");
createModulePom("m1", "<groupId>test</groupId>" + "<artifactId>m1</artifactId>" + "<version>1</version>");
createModulePom("m2", "<groupId>test</groupId>" + "<artifactId>m2</artifactId>" + "<version>1</version>");
importProject();
assertModules("project", "m1", "m2");
myProjectsManager.scheduleImportInTests(myProjectsManager.getProjectsFiles());
myProjectsManager.importProjects(new IdeModifiableModelsProviderImpl(myProject) {
@Override
public void commit() {
ModifiableModuleModel model = ModuleManager.getInstance(myProject).getModifiableModel();
model.disposeModule(model.findModuleByName("m1"));
model.disposeModule(model.findModuleByName("m2"));
model.commit();
super.commit();
}
});
}
use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-community by JetBrains.
the class MavenModuleBuilderTest method createNewModule.
private void createNewModule(MavenId id) throws Exception {
myBuilder.setProjectId(id);
new WriteAction() {
protected void run(@NotNull Result result) throws Throwable {
ModifiableModuleModel model = ModuleManager.getInstance(myProject).getModifiableModel();
myBuilder.createModule(model);
model.commit();
}
}.execute();
resolveDependenciesAndImport();
}
Aggregations