public boolean tryToSetUpGroovyFacetOnTheFly(final Module module) {
    final Project project = module.getProject();
    final Library[] libraries = getAllSDKLibraries(project);
    if (libraries.length > 0) {
      final Library library = libraries[0];
      int result =
          Messages.showOkCancelDialog(
              GroovyBundle.message(
                  "groovy.like.library.found.text",
                  module.getName(),
                  library.getName(),
                  getSDKLibVersion(library)),
              GroovyBundle.message("groovy.like.library.found"),
              JetgroovyIcons.Groovy.Groovy_32x32);
      if (result == Messages.OK) {
        AccessToken accessToken = WriteAction.start();

        try {
          ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
          LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]);
          LibrariesUtil.placeEntryToCorrectPlace(model, entry);
          model.commit();
          return true;
        } finally {
          accessToken.finish();
        }
      }
    }
    return false;
  }
  public void addLibraryDependency(
      MavenArtifact artifact,
      DependencyScope scope,
      MavenModifiableModelsProvider provider,
      MavenProject project) {
    String libraryName = artifact.getLibraryName();

    Library library = provider.getLibraryByName(libraryName);
    if (library == null) {
      library = provider.createLibrary(libraryName);
    }
    Library.ModifiableModel libraryModel = provider.getLibraryModel(library);

    updateUrl(libraryModel, OrderRootType.CLASSES, artifact, null, null, true);
    if (!MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope())) {
      updateUrl(
          libraryModel,
          OrderRootType.SOURCES,
          artifact,
          MavenExtraArtifactType.SOURCES,
          project,
          false);
      updateUrl(
          libraryModel,
          JavadocOrderRootType.getInstance(),
          artifact,
          MavenExtraArtifactType.DOCS,
          project,
          false);
    }

    LibraryOrderEntry e = myRootModel.addLibraryEntry(library);
    e.setScope(scope);
  }
Example #3
0
 @Override
 @Nullable
 protected ClasspathTableItem<?> createTableItem(final Library item) {
   // clear invalid order entry corresponding to added library if any
   final ModifiableRootModel rootModel = myClasspathPanel.getRootModel();
   final OrderEntry[] orderEntries = rootModel.getOrderEntries();
   for (OrderEntry orderEntry : orderEntries) {
     if (orderEntry instanceof LibraryOrderEntry) {
       final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
       if (item.equals(libraryOrderEntry.getLibrary())) {
         return ClasspathTableItem.createItem(libraryOrderEntry, myContext);
       }
       String name = item.getName();
       if (name != null && name.equals(libraryOrderEntry.getLibraryName())) {
         if (orderEntry.isValid()) {
           Messages.showErrorDialog(
               ProjectBundle.message("classpath.message.library.already.added", item.getName()),
               ProjectBundle.message("classpath.title.adding.dependency"));
           return null;
         } else {
           rootModel.removeOrderEntry(orderEntry);
         }
       }
     }
   }
   final LibraryOrderEntry orderEntry = rootModel.addLibraryEntry(item);
   DependencyScope defaultScope = getDefaultScope(item);
   if (defaultScope != null) {
     orderEntry.setScope(defaultScope);
   }
   return ClasspathTableItem.createItem(orderEntry, myContext);
 }
  private static void addDependencyOnDartPackagesLibrary(
      @NotNull final Module module, @NotNull final Library library) {
    final ModifiableRootModel modifiableModel =
        ModuleRootManager.getInstance(module).getModifiableModel();
    try {
      for (final OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
        if (orderEntry instanceof LibraryOrderEntry
            && LibraryTablesRegistrar.PROJECT_LEVEL.equals(
                ((LibraryOrderEntry) orderEntry).getLibraryLevel())
            && DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals(
                ((LibraryOrderEntry) orderEntry).getLibraryName())) {
          return; // dependency already exists
        }
      }

      modifiableModel.addLibraryEntry(library);

      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                @Override
                public void run() {
                  modifiableModel.commit();
                }
              });
    } finally {
      if (!modifiableModel.isDisposed()) {
        modifiableModel.dispose();
      }
    }
  }
 private void importMissing(
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull LibraryTable moduleLibraryTable,
     @NotNull LibraryTable libraryTable,
     @NotNull Module module) {
   for (LibraryDependencyData dependencyData : toImport) {
     LibraryData libraryData = dependencyData.getTarget();
     if (libraryData.isUnresolved()) {
       continue;
     }
     switch (dependencyData.getLevel()) {
       case MODULE:
         @SuppressWarnings("ConstantConditions")
         Library moduleLib = moduleLibraryTable.createLibrary(dependencyData.getName());
         Library.ModifiableModel libModel = moduleLib.getModifiableModel();
         try {
           Map<OrderRootType, Collection<File>> files =
               myLibraryManager.prepareLibraryFiles(libraryData);
           myLibraryManager.registerPaths(files, libModel, dependencyData.getName());
         } finally {
           libModel.commit();
         }
         break;
       case PROJECT:
         final Library projectLib = libraryTable.getLibraryByName(dependencyData.getName());
         if (projectLib == null) {
           assert false;
           continue;
         }
         LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
         LOG.info(
             String.format(
                 "Adding library dependency '%s' to module '%s'",
                 projectLib.getName(), module.getName()));
         orderEntry.setExported(dependencyData.isExported());
         orderEntry.setScope(dependencyData.getScope());
         LOG.info(
             String.format(
                 "Configuring library dependency '%s' of module '%s' to be%s exported and have scope %s",
                 projectLib.getName(),
                 module.getName(),
                 dependencyData.isExported() ? " not" : "",
                 dependencyData.getScope()));
     }
   }
 }
 public void addJavaRuntimeLibrary(final Module module, final ModifiableRootModel rootModel) {
   final Library library = this.createOrGetXtendJavaLibrary(rootModel, module);
   boolean _and = false;
   boolean _notEquals = (!Objects.equal(library, null));
   if (!_notEquals) {
     _and = false;
   } else {
     LibraryOrderEntry _findLibraryOrderEntry = rootModel.findLibraryOrderEntry(library);
     boolean _tripleEquals = (_findLibraryOrderEntry == null);
     _and = _tripleEquals;
   }
   if (_and) {
     boolean _isWritable = rootModel.isWritable();
     if (_isWritable) {
       rootModel.addLibraryEntry(library);
     }
   }
 }
 private void importMissing(
     @NotNull IdeModifiableModelsProvider modelsProvider,
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap,
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull LibraryTable moduleLibraryTable,
     @NotNull Module module) {
   for (final LibraryDependencyData dependencyData : toImport) {
     final LibraryData libraryData = dependencyData.getTarget();
     final String libraryName = libraryData.getInternalName();
     switch (dependencyData.getLevel()) {
       case MODULE:
         final Library moduleLib;
         if (libraryName.isEmpty()) {
           moduleLib = moduleLibraryTable.createLibrary();
         } else {
           moduleLib = moduleLibraryTable.createLibrary(libraryName);
         }
         final LibraryOrderEntry existingLibraryDependency =
             syncExistingLibraryDependency(
                 modelsProvider, dependencyData, moduleLib, moduleRootModel, module);
         orderEntryDataMap.put(existingLibraryDependency, dependencyData);
         break;
       case PROJECT:
         final Library projectLib = modelsProvider.getLibraryByName(libraryName);
         if (projectLib == null) {
           final LibraryOrderEntry existingProjectLibraryDependency =
               syncExistingLibraryDependency(
                   modelsProvider,
                   dependencyData,
                   moduleLibraryTable.createLibrary(libraryName),
                   moduleRootModel,
                   module);
           orderEntryDataMap.put(existingProjectLibraryDependency, dependencyData);
           break;
         }
         LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
         orderEntryDataMap.put(orderEntry, dependencyData);
         setLibraryScope(orderEntry, projectLib, module, dependencyData);
     }
   }
 }
 @Override
 protected void addNamedLibrary(
     final ModifiableRootModel rootModel,
     final Collection<String> unknownLibraries,
     final boolean exported,
     final String name,
     final boolean applicationLevel) {
   Library lib = findLibraryByName(myProject, name);
   if (lib != null) {
     rootModel.addLibraryEntry(lib).setExported(exported);
   } else {
     unknownLibraries.add(name);
     rootModel
         .addInvalidLibrary(
             name,
             applicationLevel
                 ? LibraryTablesRegistrar.APPLICATION_LEVEL
                 : LibraryTablesRegistrar.PROJECT_LEVEL)
         .setExported(exported);
   }
 }
  private void addSupport(
      final Module module,
      final ModifiableRootModel rootModel,
      FrameworkSupportModel frameworkSupportModel,
      String sdkPath,
      @Nullable PersistenceApi persistenceApi) {
    super.addSupport(module, rootModel, null, null);

    final AppEngineFacet appEngineFacet = AppEngineFacet.getAppEngineFacetByModule(module);
    LOG.assertTrue(appEngineFacet != null);

    AppEngineWebIntegration webIntegration = AppEngineWebIntegration.getInstance();
    webIntegration.registerFrameworkInModel(frameworkSupportModel, appEngineFacet);
    final AppEngineFacetConfiguration facetConfiguration = appEngineFacet.getConfiguration();
    facetConfiguration.setSdkHomePath(sdkPath);
    final AppEngineSdk sdk = appEngineFacet.getSdk();
    final Artifact webArtifact = findOrCreateWebArtifact(appEngineFacet);

    final VirtualFile webDescriptorDir =
        webIntegration.suggestParentDirectoryForAppEngineWebXml(module, rootModel);
    if (webDescriptorDir != null) {
      VirtualFile descriptor =
          createFileFromTemplate(
              AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_WEB_XML_TEMPLATE,
              webDescriptorDir,
              AppEngineUtil.APP_ENGINE_WEB_XML_NAME);
      if (descriptor != null) {
        webIntegration.addDescriptor(webArtifact, module.getProject(), descriptor);
      }
    }

    final Project project = module.getProject();
    webIntegration.addDevServerToModuleDependencies(rootModel, sdk);

    final Library apiJar =
        addProjectLibrary(
            module, "AppEngine API", sdk.getUserLibraryPaths(), VirtualFile.EMPTY_ARRAY);
    rootModel.addLibraryEntry(apiJar);
    webIntegration.addLibraryToArtifact(apiJar, webArtifact, project);

    if (persistenceApi != null) {
      facetConfiguration.setRunEnhancerOnMake(true);
      facetConfiguration.setPersistenceApi(persistenceApi);
      facetConfiguration
          .getFilesToEnhance()
          .addAll(AppEngineUtil.getDefaultSourceRootsToEnhance(rootModel));
      try {
        final VirtualFile[] sourceRoots = rootModel.getSourceRoots();
        final VirtualFile sourceRoot;
        if (sourceRoots.length > 0) {
          sourceRoot = sourceRoots[0];
        } else {
          sourceRoot = findOrCreateChildDirectory(rootModel.getContentRoots()[0], "src");
        }
        VirtualFile metaInf = findOrCreateChildDirectory(sourceRoot, "META-INF");
        if (persistenceApi == PersistenceApi.JDO || persistenceApi == PersistenceApi.JDO3) {
          createFileFromTemplate(
              AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JDO_CONFIG_TEMPLATE,
              metaInf,
              AppEngineUtil.JDO_CONFIG_XML_NAME);
        } else {
          final VirtualFile file =
              createFileFromTemplate(
                  AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JPA_CONFIG_TEMPLATE,
                  metaInf,
                  AppEngineUtil.JPA_CONFIG_XML_NAME);
          if (file != null) {
            webIntegration.setupJpaSupport(module, file);
          }
        }
      } catch (IOException e) {
        LOG.error(e);
      }
      final Library library =
          addProjectLibrary(
              module,
              "AppEngine ORM",
              Collections.singletonList(sdk.getOrmLibDirectoryPath()),
              sdk.getOrmLibSources());
      rootModel.addLibraryEntry(library);
      webIntegration.addLibraryToArtifact(library, webArtifact, project);
    }
  }