Java Code Examples for com.intellij.openapi.roots.libraries.Library#getModifiableModel()

The following examples show how to use com.intellij.openapi.roots.libraries.Library#getModifiableModel() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private Library createLibraryFromRoots(ModifiableModuleRootLayer layer, List<OrderRoot> roots, @Nullable final LibraryType libraryType) {
  final LibraryTable.ModifiableModel moduleLibraryModel = layer.getModuleLibraryTable().getModifiableModel();

  final PersistentLibraryKind kind = libraryType == null ? null : libraryType.getKind();
  final Library library = ((LibraryTableBase.ModifiableModelEx)moduleLibraryModel).createLibrary(null, kind);
  final LibraryEx.ModifiableModelEx libModel = (LibraryEx.ModifiableModelEx)library.getModifiableModel();

  for (OrderRoot root : roots) {
    if (root.isJarDirectory()) {
      libModel.addJarDirectory(root.getFile(), false, root.getType());
    }
    else {
      libModel.addRoot(root.getFile(), root.getType());
    }
  }
  libModel.commit();
  return library;
}
 
Example 2
Source Project: consulo   File: CreateNewLibraryAction.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public static Library createLibrary(@Nullable final LibraryType type,
                                    @Nonnull final JComponent parentComponent,
                                    @Nonnull final Project project,
                                    @Nonnull final LibrariesModifiableModel modifiableModel) {
  final NewLibraryConfiguration configuration = createNewLibraryConfiguration(type, parentComponent, project);
  if (configuration == null) return null;
  final LibraryType<?> libraryType = configuration.getLibraryType();
  final Library library = modifiableModel
    .createLibrary(LibraryEditingUtil.suggestNewLibraryName(modifiableModel, configuration.getDefaultLibraryName()),
                   libraryType != null ? libraryType.getKind() : null);

  final NewLibraryEditor editor = new NewLibraryEditor(libraryType, configuration.getProperties());
  configuration.addRoots(editor);
  final Library.ModifiableModel model = library.getModifiableModel();
  editor.applyTo((LibraryEx.ModifiableModelEx)model);
  WriteAction.run(model::commit);
  return library;
}
 
Example 3
Source Project: consulo   File: OrderEntryUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void addLibraryToRoots(@Nonnull Module module, @Nonnull Library library) {
  final ModuleRootManager manager = ModuleRootManager.getInstance(module);
  final ModifiableRootModel rootModel = manager.getModifiableModel();

  if (library.getTable() == null) {
    final Library jarLibrary = rootModel.getModuleLibraryTable().createLibrary();
    final Library.ModifiableModel libraryModel = jarLibrary.getModifiableModel();
    for (OrderRootType orderRootType : OrderRootType.getAllTypes()) {
      VirtualFile[] files = library.getFiles(orderRootType);
      for (VirtualFile jarFile : files) {
        libraryModel.addRoot(jarFile, orderRootType);
      }
    }
    libraryModel.commit();
  }
  else {
    rootModel.addLibraryEntry(library);
  }
  rootModel.commit();
}
 
Example 4
Source Project: Intellij-Plugin   File: GaugeLibHelper.java    License: Apache License 2.0 5 votes vote down vote up
private void updateLibrary(Library library, ProjectLib newLib) {
    VirtualFile lib = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(newLib.getDir());
    Library.ModifiableModel model = library.getModifiableModel();
    if (lib != null) {
        model.removeRoot(getClassesRootFrom(model), CLASSES);
        model.addJarDirectory(lib, true, CLASSES);
    }
    model.commit();
}
 
Example 5
Source Project: Intellij-Plugin   File: GaugeLibHelper.java    License: Apache License 2.0 5 votes vote down vote up
private void addLib(ProjectLib lib, ModifiableRootModel modifiableRootModel) {
    final Library library = modifiableRootModel.getModuleLibraryTable().createLibrary(lib.getLibName());
    final VirtualFile libDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(lib.getDir());
    if (libDir != null) {
        final Library.ModifiableModel libModel = library.getModifiableModel();
        libModel.addJarDirectory(libDir, true);
        libModel.commit();
    }
}
 
Example 6
@Nonnull
private static Library createLibraryInTable(final @Nonnull NewLibraryEditor editor, final LibraryTable table) {
  LibraryTableBase.ModifiableModelEx modifiableModel = (LibraryTableBase.ModifiableModelEx) table.getModifiableModel();
  final String name = StringUtil.isEmpty(editor.getName()) ? null : getUniqueLibraryName(editor.getName(), modifiableModel);
  final LibraryType<?> type = editor.getType();
  Library library = modifiableModel.createLibrary(name, type == null ? null : type.getKind());
  final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)library.getModifiableModel();
  editor.applyTo(model);
  model.commit();
  modifiableModel.commit();
  return library;
}
 
Example 7
Source Project: consulo   File: CreateNewLibraryDialog.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public Library createLibrary() {
  final LibraryTableBase.ModifiableModelEx modifiableModel = (LibraryTableBase.ModifiableModelEx)getTableModifiableModel();
  final LibraryType<?> type = myLibraryEditor.getType();
  final Library library = modifiableModel.createLibrary(myLibraryEditor.getName(), type != null ? type.getKind() : null);
  final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx)library.getModifiableModel();
  myLibraryEditor.applyTo(model);
  WriteAction.run(model::commit);
  return library;
}
 
Example 8
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();

    //Module root model seems to store .iml files root dependencies. (src, test, lib)
    logger.info("Starting setupRootModel in ProjectFromSourcesBuilderImplModified");
    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));
                }
            }
        }
    }
    logger.info("Inherits compiler output path from project");
    compilerModuleExtension.inheritCompilerOutputPath(true);

    logger.info("Starting to create module level libraries");
    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();
            }
        }
    }
    logger.info("Ending setupRootModel in ProjectFromSourcesBuilderImplModified");
}