use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.
the class DartFileListener method updatePackagesLibraryRoots.
@NotNull
public static Library updatePackagesLibraryRoots(@NotNull final Project project, @NotNull final DartLibInfo libInfo) {
final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
final Library existingLibrary = projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME);
final Library library = existingLibrary != null ? existingLibrary : WriteAction.compute(() -> {
final LibraryTableBase.ModifiableModel libTableModel = ProjectLibraryTable.getInstance(project).getModifiableModel();
final Library lib = libTableModel.createLibrary(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME, DartPackagesLibraryType.LIBRARY_KIND);
libTableModel.commit();
return lib;
});
final String[] existingUrls = library.getUrls(OrderRootType.CLASSES);
final Collection<String> libRootUrls = libInfo.getLibRootUrls();
if ((!libInfo.isProjectWithoutPubspec() && isBrokenPackageMap(((LibraryEx) library).getProperties())) || existingUrls.length != libRootUrls.size() || !libRootUrls.containsAll(Arrays.asList(existingUrls))) {
ApplicationManager.getApplication().runWriteAction(() -> {
final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx) library.getModifiableModel();
for (String url : existingUrls) {
model.removeRoot(url, OrderRootType.CLASSES);
}
for (String url : libRootUrls) {
model.addRoot(url, OrderRootType.CLASSES);
}
final DartPackagesLibraryProperties libraryProperties = new DartPackagesLibraryProperties();
libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap());
model.setProperties(libraryProperties);
model.commit();
});
}
return library;
}
use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.
the class FlexTestUtils method createConfigEditor.
public static FlexProjectConfigurationEditor createConfigEditor(final Module... modules) {
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final Map<Module, ModifiableRootModel> models = new FactoryMap<Module, ModifiableRootModel>() {
@Override
protected ModifiableRootModel create(final Module module) {
final ModifiableRootModel result = ModuleRootManager.getInstance(module).getModifiableModel();
Disposer.register(module, new Disposable() {
@Override
public void dispose() {
if (!result.isDisposed()) {
result.dispose();
}
}
});
return result;
}
};
return new FlexProjectConfigurationEditor(modules[0].getProject(), new FlexProjectConfigurationEditor.ProjectModifiableModelProvider() {
@Override
public Module[] getModules() {
return modules;
}
@Override
public ModifiableRootModel getModuleModifiableModel(Module module) {
return models.get(module);
}
@Override
public void addListener(FlexBCConfigurator.Listener listener, Disposable parentDisposable) {
// ignore
}
@Override
public void commitModifiableModels() throws ConfigurationException {
ApplicationManager.getApplication().runWriteAction(() -> {
for (ModifiableRootModel model : models.values()) {
if (model.isChanged()) {
model.commit();
}
}
});
}
public Library findSourceLibraryForLiveName(final String name, final String level) {
return findSourceLibrary(name, level);
}
public Library findSourceLibrary(final String name, final String level) {
return getLibrariesTable(level).getLibraryByName(name);
}
private LibraryTable getLibrariesTable(final String level) {
if (LibraryTablesRegistrar.APPLICATION_LEVEL.equals(level)) {
return ApplicationLibraryTable.getApplicationTable();
} else {
assert LibraryTablesRegistrar.PROJECT_LEVEL.equals(level);
return ProjectLibraryTable.getInstance(modules[0].getProject());
}
}
});
}
use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.
the class FlexTestUtils method doAddFlexLibrary.
private static void doAddFlexLibrary(boolean isProjectLibrary, Module module, String libraryName, boolean overwrite, String libraryRoot, @Nullable String classesPath, @Nullable String sourcesPath, @Nullable String asdocPath, LinkageType linkageType) {
ModifiableRootModel moduleModifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
WriteAction.run(() -> {
try {
// first let's create Flex library
final LibraryTable libraryTable;
if (isProjectLibrary) {
libraryTable = ProjectLibraryTable.getInstance(module.getProject());
} else {
libraryTable = moduleModifiableModel.getModuleLibraryTable();
}
Library library = libraryTable.getLibraryByName(libraryName);
if (library != null && overwrite) {
libraryTable.removeLibrary(library);
library = null;
}
if (library == null) {
LibraryTable.ModifiableModel libraryTableModifiableModel = libraryTable.getModifiableModel();
library = libraryTableModifiableModel.createLibrary(libraryName, FlexLibraryType.FLEX_LIBRARY);
LibraryEx.ModifiableModelEx libraryModel = (LibraryEx.ModifiableModelEx) library.getModifiableModel();
libraryModel.setProperties(new FlexLibraryProperties(FlexLibraryIdGenerator.generateId()));
addRootIfNotNull(libraryRoot, classesPath, libraryModel, OrderRootType.CLASSES, ".swc", ".zip");
addRootIfNotNull(libraryRoot, sourcesPath, libraryModel, OrderRootType.SOURCES, ".zip");
addRootIfNotNull(libraryRoot, asdocPath, libraryModel, JavadocOrderRootType.getInstance(), ".zip");
libraryModel.commit();
libraryTableModifiableModel.commit();
}
moduleModifiableModel.commit();
// then add Flex library to build configuration dependency
final String committedLibraryId;
if (isProjectLibrary) {
committedLibraryId = FlexProjectRootsUtil.getLibraryId(ProjectLibraryTable.getInstance(module.getProject()).getLibraryByName(libraryName));
} else {
final OrderEntry entry = ContainerUtil.find(ModuleRootManager.getInstance(module).getOrderEntries(), orderEntry -> orderEntry instanceof LibraryOrderEntry && ((LibraryOrderEntry) orderEntry).getLibraryName().equals(libraryName));
committedLibraryId = FlexProjectRootsUtil.getLibraryId(((LibraryOrderEntry) entry).getLibrary());
}
if (ModuleType.get(module) == FlexModuleType.getInstance()) {
modifyConfigs(module.getProject(), e -> {
final ModifiableFlexBuildConfiguration[] bcs = e.getConfigurations(module);
final ModifiableDependencyEntry dependencyEntry;
if (isProjectLibrary) {
dependencyEntry = e.createSharedLibraryEntry(bcs[0].getDependencies(), libraryName, LibraryTablesRegistrar.PROJECT_LEVEL);
} else {
dependencyEntry = e.createModuleLibraryEntry(bcs[0].getDependencies(), committedLibraryId);
}
dependencyEntry.getDependencyType().setLinkageType(linkageType);
bcs[0].getDependencies().getModifiableEntries().add(dependencyEntry);
});
}
} finally {
if (!moduleModifiableModel.isDisposed()) {
moduleModifiableModel.dispose();
}
}
});
}
use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.
the class FlexProjectConfigTest method createModuleLibrary.
private String createModuleLibrary() {
final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
final LibraryTable.ModifiableModel libraryTable = modifiableModel.getModuleLibraryTable().getModifiableModel();
LibraryEx library = (LibraryEx) libraryTable.createLibrary("test", FlexLibraryType.FLEX_LIBRARY);
String libraryId = UUID.randomUUID().toString();
((FlexLibraryProperties) library.getProperties()).setId(libraryId);
ApplicationManager.getApplication().runWriteAction(() -> {
libraryTable.commit();
modifiableModel.commit();
});
return libraryId;
}
Aggregations