use of com.intellij.ide.util.importProject.LibraryDescriptor in project intellij-community by JetBrains.
the class ProjectFromSourcesBuilderImpl method setupRootModel.
private static void setupRootModel(ProjectDescriptor projectDescriptor, final ModuleDescriptor descriptor, final ModifiableRootModel rootModel, final Map<LibraryDescriptor, Library> projectLibs) {
final CompilerModuleExtension compilerModuleExtension = rootModel.getModuleExtension(CompilerModuleExtension.class);
compilerModuleExtension.setExcludeOutput(true);
rootModel.inheritSdk();
final Set<File> contentRoots = descriptor.getContentRoots();
for (File contentRoot : contentRoots) {
final LocalFileSystem lfs = LocalFileSystem.getInstance();
VirtualFile moduleContentRoot = lfs.refreshAndFindFileByPath(FileUtil.toSystemIndependentName(contentRoot.getPath()));
if (moduleContentRoot != null) {
final ContentEntry contentEntry = rootModel.addContentEntry(moduleContentRoot);
final Collection<DetectedSourceRoot> sourceRoots = descriptor.getSourceRoots(contentRoot);
for (DetectedSourceRoot srcRoot : sourceRoots) {
final String srcpath = FileUtil.toSystemIndependentName(srcRoot.getDirectory().getPath());
final VirtualFile sourceRoot = lfs.refreshAndFindFileByPath(srcpath);
if (sourceRoot != null) {
contentEntry.addSourceFolder(sourceRoot, shouldBeTestRoot(srcRoot.getDirectory()), getPackagePrefix(srcRoot));
}
}
}
}
compilerModuleExtension.inheritCompilerOutputPath(true);
final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable();
for (LibraryDescriptor libDescriptor : ModuleInsight.getLibraryDependencies(descriptor, projectDescriptor.getLibraries())) {
final Library projectLib = projectLibs.get(libDescriptor);
if (projectLib != null) {
rootModel.addLibraryEntry(projectLib);
} else {
// add as module library
final Collection<File> jars = libDescriptor.getJars();
for (File file : jars) {
Library library = moduleLibraryTable.createLibrary();
Library.ModifiableModel modifiableModel = library.getModifiableModel();
modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot(file), OrderRootType.CLASSES);
modifiableModel.commit();
}
}
}
}
use of com.intellij.ide.util.importProject.LibraryDescriptor in project intellij-community by JetBrains.
the class ProjectFromSourcesBuilderImpl method commit.
@Override
public List<Module> commit(@NotNull final Project project, final ModifiableModuleModel model, final ModulesProvider modulesProvider) {
final boolean fromProjectStructure = model != null;
ModifiableModelsProvider modelsProvider = new IdeaModifiableModelsProvider();
final LibraryTable.ModifiableModel projectLibraryTable = modelsProvider.getLibraryTableModifiableModel(project);
final Map<LibraryDescriptor, Library> projectLibs = new HashMap<>();
final List<Module> result = new ArrayList<>();
try {
WriteAction.run(() -> {
// create project-level libraries
for (ProjectDescriptor projectDescriptor : getSelectedDescriptors()) {
for (LibraryDescriptor lib : projectDescriptor.getLibraries()) {
final Collection<File> files = lib.getJars();
final Library projectLib = projectLibraryTable.createLibrary(lib.getName());
final Library.ModifiableModel libraryModel = projectLib.getModifiableModel();
for (File file : files) {
libraryModel.addRoot(VfsUtil.getUrlForLibraryRoot(file), OrderRootType.CLASSES);
}
libraryModel.commit();
projectLibs.put(lib, projectLib);
}
}
if (!fromProjectStructure) {
projectLibraryTable.commit();
}
});
} catch (Exception e) {
LOG.info(e);
Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
}
final Map<ModuleDescriptor, Module> descriptorToModuleMap = new HashMap<>();
try {
WriteAction.run(() -> {
final ModifiableModuleModel moduleModel = fromProjectStructure ? model : ModuleManager.getInstance(project).getModifiableModel();
for (ProjectDescriptor descriptor : getSelectedDescriptors()) {
for (final ModuleDescriptor moduleDescriptor : descriptor.getModules()) {
final Module module;
if (moduleDescriptor.isReuseExistingElement()) {
final ExistingModuleLoader moduleLoader = ExistingModuleLoader.setUpLoader(FileUtil.toSystemIndependentName(moduleDescriptor.computeModuleFilePath()));
module = moduleLoader.createModule(moduleModel);
} else {
module = createModule(descriptor, moduleDescriptor, projectLibs, moduleModel);
}
result.add(module);
descriptorToModuleMap.put(moduleDescriptor, module);
}
}
if (!fromProjectStructure) {
moduleModel.commit();
}
});
} catch (Exception e) {
LOG.info(e);
Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
}
// setup dependencies between modules
try {
WriteAction.run(() -> {
for (ProjectDescriptor data : getSelectedDescriptors()) {
for (final ModuleDescriptor descriptor : data.getModules()) {
final Module module = descriptorToModuleMap.get(descriptor);
if (module == null) {
continue;
}
final Set<ModuleDescriptor> deps = descriptor.getDependencies();
if (deps.size() == 0) {
continue;
}
final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
for (ModuleDescriptor dependentDescriptor : deps) {
final Module dependentModule = descriptorToModuleMap.get(dependentDescriptor);
if (dependentModule != null) {
rootModel.addModuleOrderEntry(dependentModule);
}
}
rootModel.commit();
}
}
});
} catch (Exception e) {
LOG.info(e);
Messages.showErrorDialog(IdeBundle.message("error.adding.module.to.project", e.getMessage()), IdeBundle.message("title.add.module"));
}
WriteAction.run(() -> {
ModulesProvider updatedModulesProvider = fromProjectStructure ? modulesProvider : new DefaultModulesProvider(project);
for (ProjectConfigurationUpdater updater : myUpdaters) {
updater.updateProject(project, modelsProvider, updatedModulesProvider);
}
});
return result;
}
Aggregations