Search in sources :

Example 1 with TemplateUserVisibleException

use of com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException in project android by JetBrains.

the class DefaultRecipeExecutor method merge.

/**
   * Merges the given source file into the given destination file (or it just copies it over if
   * the destination file does not exist).
   * <p/>
   * Only XML and Gradle files are currently supported.
   */
@Override
public void merge(@NotNull File from, @NotNull File to) throws TemplateProcessingException {
    try {
        String targetText = null;
        File sourceFile = myContext.getLoader().getSourceFile(from);
        File targetFile = getTargetFile(to);
        if (!(hasExtension(targetFile, DOT_XML) || hasExtension(targetFile, DOT_GRADLE))) {
            throw new RuntimeException("Only XML or Gradle files can be merged at this point: " + targetFile);
        }
        if (targetFile.exists()) {
            if (myContext.getProject().isInitialized()) {
                VirtualFile toFile = findFileByIoFile(targetFile, true);
                final ReadonlyStatusHandler.OperationStatus status = myReadonlyStatusHandler.ensureFilesWritable(toFile);
                if (status.hasReadonlyFiles()) {
                    throw new TemplateUserVisibleException(String.format("Attempt to update file that is readonly: %1$s", targetFile.getAbsolutePath()));
                }
            }
            targetText = readTextFile(targetFile);
        }
        if (targetText == null) {
            // The target file doesn't exist: don't merge, just copy
            boolean instantiate = hasExtension(from, DOT_FTL);
            if (instantiate) {
                instantiate(from, targetFile);
            } else {
                copyTemplateResource(from, targetFile);
            }
            return;
        }
        String sourceText;
        if (hasExtension(from, DOT_FTL)) {
            // Perform template substitution of the template prior to merging
            sourceText = processFreemarkerTemplate(myContext, from, null);
        } else {
            sourceText = readTextFromDisk(sourceFile);
            if (sourceText == null) {
                return;
            }
        }
        String contents;
        if (targetFile.getName().equals(GRADLE_PROJECT_SETTINGS_FILE)) {
            contents = RecipeMergeUtils.mergeGradleSettingsFile(sourceText, targetText);
            myNeedsGradleSync = true;
        } else if (targetFile.getName().equals(FN_BUILD_GRADLE)) {
            String compileSdkVersion = (String) getParamMap().get(TemplateMetadata.ATTR_BUILD_API_STRING);
            contents = myIO.mergeGradleFiles(sourceText, targetText, myContext.getProject(), compileSdkVersion);
            myNeedsGradleSync = true;
        } else if (hasExtension(targetFile, DOT_XML)) {
            contents = RecipeMergeUtils.mergeXml(myContext, sourceText, targetText, targetFile);
        } else {
            throw new RuntimeException("Only XML or Gradle settings files can be merged at this point: " + targetFile);
        }
        myIO.writeFile(this, contents, targetFile);
        myReferences.addSourceFile(sourceFile);
        myReferences.addTargetFile(targetFile);
    } catch (IOException | TemplateException e) {
        throw new RuntimeException(e);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateUserVisibleException(com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleBuildModel.parseBuildFile(com.android.tools.idea.gradle.dsl.model.GradleBuildModel.parseBuildFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler)

Example 2 with TemplateUserVisibleException

use of com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException in project android by JetBrains.

the class Template method doRender.

/**
   * Render the template.
   * Warnings are only generated during a dry run i.e. no files are changed yet.
   * The user may select to proceed anyway in which case we expect another call
   * to render with dry run set to false.
   * Errors may be shown regardless of the dry run flag.
   */
private boolean doRender(@NotNull RenderingContext context) {
    TemplateMetadata metadata = getMetadata();
    assert metadata != null;
    enforceParameterTypes(metadata, context.getParamMap());
    try {
        processFile(context, new File(TEMPLATE_XML_NAME));
        if (!context.showWarnings() || context.getWarnings().isEmpty()) {
            return true;
        }
        if (!context.getProject().isInitialized() && myTemplateRoot.getPath().contains(GOOGLE_GLASS_PATH_19)) {
            // there are files that are overwritten during project creation by the Glass activity templates.
            return true;
        }
        // @formatter:off
        int result = Messages.showOkCancelDialog(context.getProject(), formatWarningMessage(context), String.format("%1$s %2$s", context.getCommandName(), StringUtil.pluralize("Warning")), "Proceed Anyway", "Cancel", Messages.getWarningIcon());
        // @formatter:on
        return result == Messages.OK;
    } catch (TemplateUserVisibleException e) {
        if (context.showErrors()) {
            // @formatter:off
            Messages.showErrorDialog(context.getProject(), formatErrorMessage(context, e), String.format("%1$s Failed", context.getCommandName()));
        // @formatter:on
        } else {
            throw new RuntimeException(e);
        }
        return false;
    } catch (TemplateProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : TemplateUserVisibleException(com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata) Constraint(com.android.tools.idea.templates.Parameter.Constraint)

Aggregations

TemplateUserVisibleException (com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException)2 File (java.io.File)2 GradleBuildModel.parseBuildFile (com.android.tools.idea.gradle.dsl.model.GradleBuildModel.parseBuildFile)1 TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)1 Constraint (com.android.tools.idea.templates.Parameter.Constraint)1 TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)1 ReadonlyStatusHandler (com.intellij.openapi.vfs.ReadonlyStatusHandler)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1