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