Search in sources :

Example 11 with RenderingContext

use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.

the class AddAndroidActivityPath method performFinishingOperation.

private boolean performFinishingOperation(boolean dryRun, @Nullable List<File> filesToOpen, @Nullable List<File> filesToReformat) {
    TemplateEntry templateEntry = myState.get(KEY_SELECTED_TEMPLATE);
    final Project project = getProject();
    Module module = getModule();
    assert templateEntry != null;
    assert project != null && module != null;
    final Template template = templateEntry.getTemplate();
    File moduleRoot = getModuleRoot(module);
    if (moduleRoot == null) {
        return false;
    }
    Map<String, Object> parameterMap = getTemplateParameterMap(templateEntry.getMetadata());
    saveRecentValues(project, parameterMap);
    // @formatter:off
    final RenderingContext context = RenderingContext.Builder.newContext(template, project).withCommandName("New Activity").withDryRun(dryRun).withShowErrors(true).withModule(module).withParams(parameterMap).intoOpenFiles(filesToOpen).intoTargetFiles(filesToReformat).build();
    // @formatter:on
    return template.render(context);
}
Also used : Project(com.intellij.openapi.project.Project) RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 12 with RenderingContext

use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.

the class TemplateTest method ignored_testTemplateFormatting.

// This test is broken after the IntelliJ 2016.2.4 merge; investigate
// whether this is legitimate or whether it's due to changed formatting
// preferences in the platform
public void ignored_testTemplateFormatting() throws Exception {
    Template template = Template.createFromPath(new File(getTestDataPath(), FileUtil.join("templates", "TestTemplate")).getCanonicalFile());
    RenderingContext context = RenderingContext.Builder.newContext(template, myFixture.getProject()).withOutputRoot(new File(myFixture.getTempDirPath())).withModuleRoot(new File("dummy")).build();
    template.render(context);
    FileDocumentManager.getInstance().saveAllDocuments();
    LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    VirtualFile desired = fileSystem.findFileByIoFile(new File(getTestDataPath(), FileUtil.join("templates", "TestTemplate", "MergedStringsFile.xml")));
    VirtualFile actual = fileSystem.findFileByIoFile(new File(myFixture.getTempDirPath(), FileUtil.join("values", "TestTargetResourceFile.xml")));
    desired.refresh(false, false);
    actual.refresh(false, false);
    PlatformTestUtil.assertFilesEqual(desired, actual);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File)

Example 13 with RenderingContext

use of com.android.tools.idea.templates.recipe.RenderingContext in project android by JetBrains.

the class TemplateTest method checkProject.

private void checkProject(@NonNull String projectName, @NonNull NewProjectWizardState projectValues, @Nullable TemplateWizardState templateValues) throws Exception {
    String modifiedProjectName = projectName + "!@#$^&()_+=-,.`~你所有的基地都属于我们";
    ourCount++;
    projectValues.put(ATTR_RES_OUT, null);
    projectValues.put(ATTR_SRC_OUT, null);
    projectValues.put(ATTR_MANIFEST_OUT, null);
    projectValues.put(ATTR_TEST_OUT, null);
    JavaCodeInsightTestFixture fixture = null;
    File projectDir = null;
    try {
        projectValues.put(ATTR_MODULE_NAME, modifiedProjectName);
        IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
        TestFixtureBuilder<IdeaProjectTestFixture> projectBuilder = factory.createFixtureBuilder(modifiedProjectName);
        fixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(projectBuilder.getFixture());
        fixture.setUp();
        Project project = fixture.getProject();
        projectDir = Projects.getBaseDirPath(project);
        projectValues.put(ATTR_PROJECT_LOCATION, projectDir.getPath());
        // We only need to sync the model if lint needs to look at the synced project afterwards
        boolean syncModel = CHECK_LINT;
        //noinspection ConstantConditions
        createProject(fixture, projectValues, syncModel);
        if (templateValues != null && !projectValues.getBoolean(ATTR_CREATE_ACTIVITY)) {
            templateValues.put(ATTR_PROJECT_LOCATION, projectDir.getPath());
            ApplicationManager.getApplication().runWriteAction(() -> {
                File projectRoot = virtualToIoFile(project.getBaseDir());
                Template template = templateValues.getTemplate();
                assert template != null;
                File moduleRoot = new File(projectRoot, modifiedProjectName);
                templateValues.put(ATTR_MODULE_NAME, moduleRoot.getName());
                templateValues.populateDirectoryParameters();
                RenderingContext context = RenderingContext.Builder.newContext(template, project).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withParams(templateValues.getParameters()).build();
                template.render(context);
                // Add in icons if necessary
                if (templateValues.getTemplateMetadata() != null && templateValues.getTemplateMetadata().getIconName() != null) {
                    File drawableFolder = new File(FileUtil.join(templateValues.getString(ATTR_RES_OUT)), FileUtil.join("drawable"));
                    drawableFolder.mkdirs();
                    String fileName = myStringEvaluator.evaluate(templateValues.getTemplateMetadata().getIconName(), templateValues.getParameters());
                    File iconFile = new File(drawableFolder, fileName + DOT_XML);
                    File sourceFile = new File(getTestDataPath(), FileUtil.join("drawables", "progress_horizontal.xml"));
                    try {
                        FileUtil.copy(sourceFile, iconFile);
                    } catch (IOException e) {
                        fail(e.getMessage());
                    }
                }
            });
        }
        assertNotNull(project);
        System.out.println("Checking project " + projectName + " in " + project.getBaseDir());
        GradleInvocationResult result = invokeGradleTasks(project, "assembleDebug");
        assertThat(result.isBuildSuccessful()).isTrue();
        if (CHECK_LINT) {
            assertLintsCleanly(project, Severity.INFORMATIONAL, Sets.newHashSet(ManifestDetector.TARGET_NEWER));
        // TODO: Check for other warnings / inspections, such as unused imports?
        }
    } finally {
        if (fixture != null) {
            fixture.tearDown();
        }
        Project[] openProjects = ProjectManagerEx.getInstanceEx().getOpenProjects();
        // 1: the project created by default by the test case
        assertTrue(openProjects.length <= 1);
        // Clean up; ensure that we don't bleed contents through to the next iteration
        if (projectDir != null && projectDir.exists()) {
            FileUtil.delete(projectDir);
            LocalFileSystem.getInstance().refreshAndFindFileByIoFile(projectDir);
        }
    }
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) GradleInvocationResult(com.android.tools.idea.gradle.project.build.invoker.GradleInvocationResult) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File)

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