public SolutionIdea(@NotNull Module module, SolutionDescriptor descriptor) { super(descriptor, null); myModule = module; // TODO: simply set solution descriptor local variable? setModuleDescriptor(descriptor); myConnection = myModule.getProject().getMessageBus().connect(); myConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyModuleRootListener()); myConnection.subscribe(FacetManager.FACETS_TOPIC, new MyFacetManagerAdapter()); final ProjectLibraryTable projectLibraryTable = (ProjectLibraryTable) ProjectLibraryTable.getInstance(myModule.getProject()); ModelAccess.instance() .runReadAction( new Runnable() { @Override public void run() { for (final Library library : projectLibraryTable.getLibraries()) { if (ModuleLibraryType.isModuleLibrary(library)) { library.getRootProvider().addRootSetChangedListener(myRootSetListener); } } for (SModel model : getModels()) { ((SModelInternal) model).addModelListener(MODEL_RUNTIME_IMPORTER); } } }); projectLibraryTable.addListener(myLibrariesListener); addModuleListener(myModule.getProject().getComponent(PsiModelReloadListener.class)); addModuleListener(MODULE_RUNTIME_IMPORTER); }
@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 : ApplicationManager.getApplication() .runWriteAction( new Computable<Library>() { @Override public Library 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( new Runnable() { @Override public void run() { 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; }
public void assertProjectLibraries(String... expectedNames) { List<String> actualNames = new ArrayList<String>(); for (Library each : ProjectLibraryTable.getInstance(myProject).getLibraries()) { String name = each.getName(); actualNames.add(name == null ? "<unnamed>" : name); } assertUnorderedElementsAreEqual(actualNames, expectedNames); }
@NotNull public static Collection<Library> getLibraries(SModuleReference reference, Project project) { Set<Library> libraries = new HashSet<Library>(); for (Library library : ProjectLibraryTable.getInstance(project).getLibraries()) { if (hasModule(library, reference)) { libraries.add(library); } } return libraries; }
@Override public void dispose() { final ProjectLibraryTable projectLibraryTable = (ProjectLibraryTable) ProjectLibraryTable.getInstance(myModule.getProject()); ModelAccess.instance() .runReadAction( new Runnable() { @Override public void run() { for (final Library library : projectLibraryTable.getLibraries()) { if (ModuleLibraryType.isModuleLibrary(library)) { library.getRootProvider().removeRootSetChangedListener(myRootSetListener); } } } }); projectLibraryTable.removeListener(myLibrariesListener); ApplicationManager.getApplication() .getComponent(JdkStubSolutionManager.class) .releaseSdk(myModule); super.dispose(); myConnection.disconnect(); }
private static void removeDartPackagesLibraryAndDependencies(@NotNull final Project project) { for (Module module : ModuleManager.getInstance(project).getModules()) { removeDependencyOnDartPackagesLibrary(module); } final Library library = ProjectLibraryTable.getInstance(project) .getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME); if (library != null) { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { ProjectLibraryTable.getInstance(project).removeLibrary(library); } }); } }
private static Library addProjectLibrary( Module module, ModifiableRootModel model, String libName, List<VirtualFile> classesRoots, List<VirtualFile> sourceRoots) { LibraryTable libraryTable = ProjectLibraryTable.getInstance(module.getProject()); RunResult<Library> result = new WriteAction<Library>() { @Override protected void run(@NotNull Result<Library> result) throws Throwable { Library library = libraryTable.createLibrary(libName); Library.ModifiableModel libraryModel = library.getModifiableModel(); try { for (VirtualFile root : classesRoots) { libraryModel.addRoot(root, OrderRootType.CLASSES); } for (VirtualFile root : sourceRoots) { libraryModel.addRoot(root, OrderRootType.SOURCES); } libraryModel.commit(); } catch (Throwable t) { //noinspection SSBasedInspection libraryModel.dispose(); throw t; } model.addLibraryEntry(library); OrderEntry[] orderEntries = model.getOrderEntries(); OrderEntry last = orderEntries[orderEntries.length - 1]; System.arraycopy(orderEntries, 0, orderEntries, 1, orderEntries.length - 1); orderEntries[0] = last; model.rearrangeOrderEntries(orderEntries); result.setResult(library); } }.execute(); result.throwException(); return result.getResultObject(); }