Java Code Examples for com.intellij.openapi.roots.libraries.Library#ModifiableModel

The following examples show how to use com.intellij.openapi.roots.libraries.Library#ModifiableModel . 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
Source File: CreateNewLibraryAction.java    From consulo with 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 2
Source File: OrderEntryUtil.java    From consulo with 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 3
Source File: BlazeJarLibrary.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public void modifyLibraryModel(
    Project project,
    ArtifactLocationDecoder artifactLocationDecoder,
    Library.ModifiableModel libraryModel) {
  JarCache jarCache = JarCache.getInstance(project);
  File jar = jarCache.getCachedJar(artifactLocationDecoder, this);
  if (jar != null) {
    libraryModel.addRoot(pathToUrl(jar), OrderRootType.CLASSES);
  } else {
    logger.error("No local jar file found for " + libraryArtifact.jarForIntellijLibrary());
  }

  AttachedSourceJarManager sourceJarManager = AttachedSourceJarManager.getInstance(project);
  for (AttachSourcesFilter decider : AttachSourcesFilter.EP_NAME.getExtensions()) {
    if (decider.shouldAlwaysAttachSourceJar(this)) {
      sourceJarManager.setHasSourceJarAttached(key, true);
    }
  }

  if (!sourceJarManager.hasSourceJarAttached(key)) {
    return;
  }
  for (ArtifactLocation srcJar : libraryArtifact.getSourceJars()) {
    File sourceJar = jarCache.getCachedSourceJar(artifactLocationDecoder, srcJar);
    if (sourceJar != null) {
      libraryModel.addRoot(pathToUrl(sourceJar), OrderRootType.SOURCES);
    }
  }
}
 
Example 4
Source File: GaugeLibHelper.java    From Intellij-Plugin with 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 File: GaugeLibHelper.java    From Intellij-Plugin with 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
Source File: LibraryDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("MethodMayBeStatic")
public void registerPaths(@Nonnull final Map<OrderRootType, Collection<File>> libraryFiles, @Nonnull Library.ModifiableModel model, @Nonnull String libraryName) {
  for (Map.Entry<OrderRootType, Collection<File>> entry : libraryFiles.entrySet()) {
    for (File file : entry.getValue()) {
      VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
      if (virtualFile == null) {
        if (ExternalSystemConstants.VERBOSE_PROCESSING && entry.getKey() == BinariesOrderRootType.getInstance()) {
          LOG.warn(String.format("Can't find %s of the library '%s' at path '%s'", entry.getKey(), libraryName, file.getAbsolutePath()));
        }
        String url = VfsUtil.getUrlForLibraryRoot(file);
        model.addRoot(url, entry.getKey());
        continue;
      }
      if (virtualFile.isDirectory()) {
        model.addRoot(virtualFile, entry.getKey());
      }
      else {
        VirtualFile archiveRoot = ArchiveVfsUtil.getArchiveRootForLocalFile(virtualFile);
        if (archiveRoot == null) {
          LOG.warn(String.format("Can't parse contents of the jar file at path '%s' for the library '%s''", file.getAbsolutePath(), libraryName));
          continue;
        }
        model.addRoot(archiveRoot, entry.getKey());
      }
    }
  }
}
 
Example 7
Source File: ProjectFromSourcesBuilderImplModified.java    From tmc-intellij with MIT License 4 votes vote down vote up
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");
}
 
Example 8
Source File: ProtostuffPluginController.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
private void clear(Library.ModifiableModel libraryModel) {
    String[] urls = libraryModel.getUrls(ORDER_ROOT_TYPE);
    for (String url : urls) {
        libraryModel.removeRoot(url, ORDER_ROOT_TYPE);
    }
}
 
Example 9
Source File: BlazeLibrary.java    From intellij with Apache License 2.0 4 votes vote down vote up
public abstract void modifyLibraryModel(
Project project,
ArtifactLocationDecoder artifactLocationDecoder,
Library.ModifiableModel libraryModel);
 
Example 10
Source File: GaugeLibHelper.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
private String getClassesRootFrom(Library.ModifiableModel model) {
    return model.getUrls(CLASSES)[0];
}
 
Example 11
Source File: LibrariesModifiableModel.java    From consulo with Apache License 2.0 4 votes vote down vote up
public Library.ModifiableModel getLibraryModifiableModel(final Library library) {
  return getLibraryEditor(library).getModel();
}
 
Example 12
Source File: ModuleEditor.java    From consulo with Apache License 2.0 4 votes vote down vote up
LibraryModifiableModelInvocationHandler(Library.ModifiableModel delegateModel) {
  myDelegateModel = delegateModel;
}
 
Example 13
Source File: RenameLibraryHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private Library.ModifiableModel renameLibrary(String inputString) {
  final Library.ModifiableModel modifiableModel = myLibrary.getModifiableModel();
  modifiableModel.setName(inputString);
  return modifiableModel;
}