@Nullable
  private VirtualFile setupGradleSettingsFile(
      @NotNull VirtualFile modelContentRootDir, @NotNull ModifiableRootModel model)
      throws ConfigurationException {
    VirtualFile file = null;
    if (myWizardContext.isCreatingNewProject()) {
      final String moduleDirName =
          VfsUtilCore.getRelativePath(modelContentRootDir, model.getProject().getBaseDir(), '/');
      file =
          getExternalProjectConfigFile(
              model.getProject().getBasePath(), GradleConstants.SETTINGS_FILE_NAME);
      if (file == null) return null;

      Map<String, String> attributes = ContainerUtil.newHashMap();
      final String projectName = model.getProject().getName();
      attributes.put(TEMPLATE_ATTRIBUTE_PROJECT_NAME, projectName);
      attributes.put(TEMPLATE_ATTRIBUTE_MODULE_DIR_NAME, moduleDirName);
      attributes.put(TEMPLATE_ATTRIBUTE_MODULE_NAME, model.getModule().getName());
      saveFile(file, TEMPLATE_GRADLE_SETTINGS, attributes);
    } else {
      Map<String, Module> moduleMap = ContainerUtil.newHashMap();
      for (Module module : ModuleManager.getInstance(model.getProject()).getModules()) {
        for (ContentEntry contentEntry : model.getContentEntries()) {
          if (contentEntry.getFile() != null) {
            moduleMap.put(contentEntry.getFile().getPath(), module);
          }
        }
      }

      VirtualFile virtualFile = modelContentRootDir;
      Module module = null;
      while (virtualFile != null && module == null) {
        module = moduleMap.get(virtualFile.getPath());
        virtualFile = virtualFile.getParent();
      }

      if (module != null) {
        String rootProjectPath =
            module.getOptionValue(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY);

        if (!StringUtil.isEmpty(rootProjectPath)) {
          VirtualFile rootProjectFile = VfsUtil.findFileByIoFile(new File(rootProjectPath), true);
          if (rootProjectFile == null) return null;

          final String moduleDirName =
              VfsUtilCore.getRelativePath(modelContentRootDir, rootProjectFile, '/');
          file = getExternalProjectConfigFile(rootProjectPath, GradleConstants.SETTINGS_FILE_NAME);
          if (file == null) return null;

          Map<String, String> attributes = ContainerUtil.newHashMap();
          attributes.put(TEMPLATE_ATTRIBUTE_MODULE_DIR_NAME, moduleDirName);
          attributes.put(TEMPLATE_ATTRIBUTE_MODULE_NAME, model.getModule().getName());
          appendToFile(file, TEMPLATE_GRADLE_SETTINGS_MERGE, attributes);
        }
      }
    }
    return file;
  }
  @Override
  public void setupRootModel(final ModifiableRootModel modifiableRootModel)
      throws ConfigurationException {
    String contentEntryPath = getContentEntryPath();
    if (StringUtil.isEmpty(contentEntryPath)) {
      return;
    }
    File contentRootDir = new File(contentEntryPath);
    FileUtilRt.createDirectory(contentRootDir);
    LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    VirtualFile modelContentRootDir = fileSystem.refreshAndFindFileByIoFile(contentRootDir);
    if (modelContentRootDir == null) {
      return;
    }

    modifiableRootModel.addContentEntry(modelContentRootDir);
    modifiableRootModel.inheritSdk();

    final Project project = modifiableRootModel.getProject();

    setupGradleBuildFile(modelContentRootDir);
    setupGradleSettingsFile(modelContentRootDir, modifiableRootModel);

    if (myWizardContext.isCreatingNewProject()) {
      String externalProjectPath = FileUtil.toCanonicalPath(project.getBasePath());
      getExternalProjectSettings().setExternalProjectPath(externalProjectPath);
      AbstractExternalSystemSettings settings =
          ExternalSystemApiUtil.getSettings(project, GradleConstants.SYSTEM_ID);
      //noinspection unchecked
      settings.linkProject(getExternalProjectSettings());
    } else {
      FileDocumentManager.getInstance().saveAllDocuments();
      ExternalSystemUtil.refreshProjects(project, GradleConstants.SYSTEM_ID, false);
    }
  }
 public void ensureXtendLibAvailable(
     final ModifiableRootModel rootModel, @Nullable final PsiFile context) {
   Project _project = rootModel.getProject();
   final Runnable _function =
       new Runnable() {
         @Override
         public void run() {
           XtendLibraryConfigurator.this.doEnsureXtendLibAvailable(rootModel, context);
         }
       };
   this._projectLifecycleUtil.executeWhenProjectReady(_project, _function);
 }
  private boolean askAndRemoveDuplicatedLibraryEntry(
      @NotNull ModifiableRootModel rootModel, @NotNull CustomLibraryDescription description) {
    List<OrderEntry> existingEntries = new ArrayList<OrderEntry>();
    final LibrariesContainer container = myModel.getLibrariesContainer();
    for (OrderEntry entry : rootModel.getOrderEntries()) {
      if (!(entry instanceof LibraryOrderEntry)) continue;
      final Library library = ((LibraryOrderEntry) entry).getLibrary();
      if (library == null) continue;

      if (LibraryPresentationManager.getInstance()
          .isLibraryOfKind(library, container, description.getSuitableLibraryKinds())) {
        existingEntries.add(entry);
      }
    }

    if (!existingEntries.isEmpty()) {
      String message;
      if (existingEntries.size() > 1) {
        message =
            "There are already "
                + existingEntries.size()
                + " "
                + myFrameworkType.getPresentableName()
                + " libraries.\n Do you want to replace they?";
      } else {
        final String name = existingEntries.get(0).getPresentableName();
        message =
            "There is already a "
                + myFrameworkType.getPresentableName()
                + " library '"
                + name
                + "'.\n Do you want to replace it?";
      }
      final int result =
          Messages.showYesNoCancelDialog(
              rootModel.getProject(),
              message,
              "Library Already Exists",
              "&Replace",
              "&Add",
              "&Cancel",
              null);
      if (result == 0) {
        for (OrderEntry entry : existingEntries) {
          rootModel.removeOrderEntry(entry);
        }
      } else if (result != 1) {
        return false;
      }
    }

    return true;
  }
 private void initOrderEntries() {
   for (OrderEntry e : myRootModel.getOrderEntries()) {
     if (e instanceof ModuleSourceOrderEntry || e instanceof JdkOrderEntry) continue;
     if (e instanceof LibraryOrderEntry) {
       if (!isMavenLibrary(((LibraryOrderEntry) e).getLibrary())) continue;
     }
     if (e instanceof ModuleOrderEntry) {
       Module m = ((ModuleOrderEntry) e).getModule();
       if (m != null
           && !MavenProjectsManager.getInstance(myRootModel.getProject()).isMavenizedModule(m))
         continue;
     }
     myRootModel.removeOrderEntry(e);
   }
 }
  @Override
  public void setupRootModel(ModifiableRootModel model) throws ConfigurationException {
    String contentPath = getContentEntryPath();
    if (StringUtil.isEmpty(contentPath)) {
      return;
    }
    File contentRootDir = new File(contentPath);
    FileUtilRt.createDirectory(contentRootDir);
    LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    VirtualFile vContentRootDir = fileSystem.refreshAndFindFileByIoFile(contentRootDir);
    if (vContentRootDir == null) {
      return;
    }

    model.addContentEntry(vContentRootDir);

    VirtualFile configFile = getExternalProjectConfigFile(vContentRootDir);
    if (configFile != null && myTemplateConfigName != null) {
      FileTemplateManager manager = FileTemplateManager.getInstance();
      FileTemplate template = manager.getInternalTemplate(myTemplateConfigName);
      try {
        VfsUtil.saveText(configFile, template.getText());
      } catch (IOException e) {
        LOG.warn(
            String.format(
                "Unexpected exception on applying template %s config",
                myExternalSystemId.getReadableName()),
            e);
        throw new ConfigurationException(
            e.getMessage(),
            String.format(
                "Can't apply %s template config text", myExternalSystemId.getReadableName()));
      }
    }

    AbstractExternalSystemSettings settings =
        ExternalSystemApiUtil.getSettings(model.getProject(), myExternalSystemId);
    S externalProjectSettings = createSettings();
    if (myExternalProjectSettingsControl != null) {
      String errorMessage = myExternalProjectSettingsControl.apply(externalProjectSettings);
      myExternalProjectSettingsControl.disposeUIResources();
      if (errorMessage != null) {
        throw new ConfigurationException(errorMessage);
      }
    }
    //noinspection unchecked
    settings.linkProject(externalProjectSettings);
  }
 protected void doEnsureXtendLibAvailable(
     final ModifiableRootModel rootModel, @Nullable final PsiFile context) {
   final Module module = rootModel.getModule();
   final GlobalSearchScope scope =
       GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
   Project _project = rootModel.getProject();
   JavaPsiFacade _instance = JavaPsiFacade.getInstance(_project);
   String _name = Data.class.getName();
   final PsiClass psiClass = _instance.findClass(_name, scope);
   boolean _equals = Objects.equal(psiClass, null);
   if (_equals) {
     final boolean testScope = this.isTestScope(context);
     boolean _and = false;
     boolean _isMavenInstalled = this._platformUtil.isMavenInstalled();
     if (!_isMavenInstalled) {
       _and = false;
     } else {
       boolean _isMavenizedModule = this._mavenUtility.isMavenizedModule(module);
       _and = _isMavenizedModule;
     }
     if (_and) {
       this._mavenUtility.addXtendLibMavenDependency(module, testScope);
     } else {
       boolean _and_1 = false;
       boolean _isGradleInstalled = this._platformUtil.isGradleInstalled();
       if (!_isGradleInstalled) {
         _and_1 = false;
       } else {
         boolean _isGradleedModule = this._gradleBuildFileUtility.isGradleedModule(module);
         _and_1 = _isGradleedModule;
       }
       if (_and_1) {
         this._gradleBuildFileUtility.addXtendLibGradleDependency(module, testScope);
       } else {
         this.addJavaRuntimeLibrary(module, rootModel);
       }
     }
   }
 }