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);
}
}
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);
}
}
Aggregations