public static PsiElement createFromTemplate(
     final Project project,
     String templateName,
     String fileName,
     @NotNull PsiDirectory directory,
     Properties properties)
     throws Exception {
   FileTemplateManager manager = FileTemplateManager.getInstance(project);
   FileTemplate template = manager.getInternalTemplate(templateName);
   return FileTemplateUtil.createFromTemplate(template, fileName, properties, directory);
 }
  @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);
  }
 private static void saveFile(
     @NotNull VirtualFile file, @NotNull String templateName, @Nullable Map templateAttributes)
     throws ConfigurationException {
   FileTemplateManager manager = FileTemplateManager.getInstance();
   FileTemplate template = manager.getInternalTemplate(templateName);
   try {
     VfsUtil.saveText(
         file,
         templateAttributes != null ? template.getText(templateAttributes) : template.getText());
   } catch (IOException e) {
     LOG.warn(
         String.format(
             "Unexpected exception on applying template %s config",
             GradleConstants.SYSTEM_ID.getReadableName()),
         e);
     throw new ConfigurationException(
         e.getMessage(),
         String.format(
             "Can't apply %s template config text", GradleConstants.SYSTEM_ID.getReadableName()));
   }
 }