Search in sources :

Example 1 with RenderingContext

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);
    }
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) Recipe(com.android.tools.idea.templates.recipe.Recipe) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) File(java.io.File) RecipeExecutor(com.android.tools.idea.templates.recipe.RecipeExecutor)

Example 2 with RenderingContext

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;
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) IOException(java.io.IOException) FreemarkerUtils(com.android.tools.idea.templates.FreemarkerUtils) File(java.io.File) Pair(com.intellij.openapi.util.Pair) RecipeExecutor(com.android.tools.idea.templates.recipe.RecipeExecutor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with RenderingContext

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);
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) TemplateWizardState(com.android.tools.idea.wizard.template.TemplateWizardState) NewProjectWizardState(com.android.tools.idea.npw.NewProjectWizardState) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) IOException(java.io.IOException)

Example 4 with RenderingContext

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);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) IOException(java.io.IOException) File(java.io.File) RecipeExecutor(com.android.tools.idea.templates.recipe.RecipeExecutor)

Example 5 with RenderingContext

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);
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) File(java.io.File) Template(com.android.tools.idea.templates.Template)

Aggregations

RenderingContext (com.android.tools.idea.templates.recipe.RenderingContext)13 File (java.io.File)10 Template (com.android.tools.idea.templates.Template)5 Project (com.intellij.openapi.project.Project)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 IOException (java.io.IOException)4 RecipeExecutor (com.android.tools.idea.templates.recipe.RecipeExecutor)3 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)3 Module (com.intellij.openapi.module.Module)2 GradleInvocationResult (com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult)1 NewProjectWizardState (com.android.tools.idea.npw.NewProjectWizardState)1 FreemarkerUtils (com.android.tools.idea.templates.FreemarkerUtils)1 TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)1 Parameter (com.android.tools.idea.templates.Parameter)1 Recipe (com.android.tools.idea.templates.recipe.Recipe)1 TemplateWizardState (com.android.tools.idea.wizard.template.TemplateWizardState)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 LanguageLevelModuleExtension (com.intellij.openapi.roots.LanguageLevelModuleExtension)1 Pair (com.intellij.openapi.util.Pair)1 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)1