use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.
the class ServiceXmlParser method analyzeRecipe.
private void analyzeRecipe(boolean findOnlyReferences, @Nullable Collection<File> openFiles, @Nullable SetMultimap<String, String> dependencies, @Nullable Collection<String> classpathEntries, @Nullable Collection<String> plugins, @Nullable Collection<File> sourceFiles, @Nullable Collection<File> targetFiles) {
RenderingContext context = null;
try {
File moduleRoot = new File(myModule.getModuleFilePath()).getParentFile();
// @formatter:off
context = RenderingContext.Builder.newContext(myRootPath, myModule.getProject()).withParams(myContext.toValueMap()).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withFindOnlyReferences(findOnlyReferences).withGradleSync(false).intoOpenFiles(openFiles).intoDependencies(dependencies).intoClasspathEntries(classpathEntries).intoPlugins(plugins).intoSourceFiles(sourceFiles).intoTargetFiles(targetFiles).build();
// @formatter:on
String xml = FreemarkerUtils.processFreemarkerTemplate(context, myRecipeFile, null);
Recipe recipe = Recipe.parse(new StringReader(xml));
RecipeExecutor recipeExecutor = context.getRecipeExecutor();
recipe.execute(recipeExecutor);
} catch (TemplateProcessingException e) {
// TODO(b/31039466): Extra logging to track down a rare issue.
LOG.warn("Template processing exception with context in the following state: " + context);
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.
the class RecipeUtils method getRecipeMetadata.
@NotNull
public static RecipeMetadata getRecipeMetadata(@NotNull Recipe recipe, @NotNull Module module) {
Pair key = new Pair(recipe, module.getProject());
if (myRecipeMetadataCache.containsKey(key)) {
List<RecipeMetadata> metadataSet = myRecipeMetadataCache.get(key);
for (RecipeMetadata metadata : metadataSet) {
if (metadata.getRecipe().equals(recipe)) {
return metadata;
}
}
}
SetMultimap<String, String> dependencies = LinkedHashMultimap.create();
Set<String> classpathEntries = Sets.newHashSet();
Set<String> plugins = Sets.newHashSet();
List<File> sourceFiles = Lists.newArrayList();
List<File> targetFiles = Lists.newArrayList();
RecipeMetadata metadata = new RecipeMetadata(recipe, module);
RenderingContext context = null;
try {
File moduleRoot = new File(module.getModuleFilePath()).getParentFile();
// TODO: do we care about this path?
File RootPath = new File(FileUtil.generateRandomTemporaryPath(), "unused");
RootPath.deleteOnExit();
context = RenderingContext.Builder.newContext(RootPath, module.getProject()).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withFindOnlyReferences(true).withGradleSync(false).intoDependencies(dependencies).intoClasspathEntries(classpathEntries).intoPlugins(plugins).intoSourceFiles(sourceFiles).intoTargetFiles(targetFiles).build();
RecipeExecutor recipeExecutor = context.getRecipeExecutor();
recipe.execute(recipeExecutor);
} catch (FreemarkerUtils.TemplateProcessingException e) {
// TODO(b/31039466): Extra logging to track down a rare issue.
getLog().warn("Template processing exception with context in the following state: " + context);
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Ignore test configurations here.
for (String d : dependencies.get(SdkConstants.GRADLE_COMPILE_CONFIGURATION)) {
metadata.addDependency(d);
}
for (String c : classpathEntries) {
metadata.addClasspathEntry(c);
}
for (String p : plugins) {
metadata.addPlugin(p);
}
for (File f : sourceFiles) {
if (f.getName().equals(SdkConstants.FN_ANDROID_MANIFEST_XML)) {
parseManifestForPermissions(f, metadata);
}
}
for (File f : targetFiles) {
metadata.addModifiedFile(f);
}
return metadata;
}
use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.
the class TemplateTest method createProject.
private static void createProject(@NotNull final NewModuleWizardState wizardState, @NotNull Project project, @Nullable AssetStudioAssetGenerator assetGenerator) {
List<String> errors = Lists.newArrayList();
try {
wizardState.populateDirectoryParameters();
String moduleName = wizardState.getString(ATTR_MODULE_NAME);
String projectName = wizardState.getString(ATTR_APP_TITLE);
File projectRoot = new File(wizardState.getString(ATTR_PROJECT_LOCATION));
File moduleRoot = new File(projectRoot, moduleName);
if (FileUtilRt.createDirectory(projectRoot)) {
if (wizardState.getBoolean(ATTR_CREATE_ICONS) && assetGenerator != null) {
assetGenerator.outputImagesIntoDefaultVariant(moduleRoot);
}
wizardState.updateParameters();
wizardState.updateDependencies();
// If this is a new project, instantiate the project-level files
if (wizardState instanceof NewProjectWizardState) {
Template projectTemplate = ((NewProjectWizardState) wizardState).getProjectTemplate();
final RenderingContext projectContext = RenderingContext.Builder.newContext(projectTemplate, project).withOutputRoot(projectRoot).withModuleRoot(moduleRoot).withParams(wizardState.myParameters).build();
projectTemplate.render(projectContext);
AndroidGradleModuleUtils.setGradleWrapperExecutable(projectRoot);
}
final RenderingContext context = RenderingContext.Builder.newContext(wizardState.myTemplate, project).withOutputRoot(projectRoot).withModuleRoot(moduleRoot).withParams(wizardState.myParameters).build();
wizardState.myTemplate.render(context);
if (wizardState.getBoolean(ATTR_CREATE_ACTIVITY)) {
TemplateWizardState activityTemplateState = wizardState.getActivityTemplateState();
activityTemplateState.populateRelativePackage(null);
Template template = activityTemplateState.getTemplate();
assert template != null;
final RenderingContext activityContext = RenderingContext.Builder.newContext(template, project).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withParams(activityTemplateState.myParameters).build();
template.render(activityContext);
context.getFilesToOpen().addAll(activityContext.getFilesToOpen());
}
} else {
errors.add(String.format("Unable to create directory '%1$s'.", projectRoot.getPath()));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
assertEmpty(errors);
}
use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.
the class RecipeUtils method execute.
public static void execute(@NotNull Recipe recipe, @NotNull Module module) {
List<File> filesToOpen = Lists.newArrayList();
File moduleRoot = new File(module.getModuleFilePath()).getParentFile();
File RootPath = null;
try {
RootPath = new File(FileUtil.generateRandomTemporaryPath(), "unused");
} catch (IOException e) {
throw new RuntimeException(e);
}
RenderingContext context = RenderingContext.Builder.newContext(RootPath, module.getProject()).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).intoOpenFiles(filesToOpen).build();
RecipeExecutor recipeExecutor = context.getRecipeExecutor();
new WriteCommandAction.Simple(module.getProject(), "Executing recipe instructions") {
@Override
protected void run() throws Throwable {
recipe.execute(recipeExecutor);
}
}.execute();
TemplateUtils.openEditors(module.getProject(), filesToOpen, true);
}
use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.
the class RenderTemplateModel method renderTemplate.
private boolean renderTemplate(boolean dryRun, @NotNull Project project, @NotNull AndroidProjectPaths paths, @Nullable List<File> filesToOpen, @Nullable List<File> filesToReformat) {
final Template template = myTemplateHandle.getTemplate();
File moduleRoot = paths.getModuleRoot();
if (moduleRoot == null) {
return false;
}
// @formatter:off
final RenderingContext context = RenderingContext.Builder.newContext(template, project).withCommandName(myCommandName).withDryRun(dryRun).withShowErrors(true).withModuleRoot(paths.getModuleRoot()).withParams(myTemplateValues).intoOpenFiles(filesToOpen).intoTargetFiles(filesToReformat).build();
// @formatter:on
return template.render(context);
}
Aggregations