Search in sources :

Example 1 with RecipeExecutor

use of com.android.tools.idea.templates.recipe.RecipeExecutor 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 RecipeExecutor

use of com.android.tools.idea.templates.recipe.RecipeExecutor 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 RecipeExecutor

use of com.android.tools.idea.templates.recipe.RecipeExecutor 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)

Aggregations

RecipeExecutor (com.android.tools.idea.templates.recipe.RecipeExecutor)3 RenderingContext (com.android.tools.idea.templates.recipe.RenderingContext)3 File (java.io.File)3 IOException (java.io.IOException)2 FreemarkerUtils (com.android.tools.idea.templates.FreemarkerUtils)1 TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)1 Recipe (com.android.tools.idea.templates.recipe.Recipe)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Pair (com.intellij.openapi.util.Pair)1 StringReader (java.io.StringReader)1 JAXBException (javax.xml.bind.JAXBException)1 NotNull (org.jetbrains.annotations.NotNull)1