Search in sources :

Example 21 with Template

use of org.apache.velocity.Template in project camel by apache.

the class AbstractGeneratorMojo method mergeTemplate.

protected void mergeTemplate(VelocityContext context, File outFile, String templateName) throws MojoExecutionException {
    // ensure parent directories exist
    final File outDir = outFile.getParentFile();
    if (!outDir.isDirectory() && !outDir.mkdirs()) {
        throw new MojoExecutionException("Error creating directory " + outDir);
    }
    // add generated date
    context.put("generatedDate", new Date().toString());
    // add output package
    context.put("packageName", outPackage);
    context.put("newLine", "\n");
    // load velocity template
    final Template template = getEngine().getTemplate(templateName, "UTF-8");
    // generate file
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(outFile));
        template.merge(context, writer);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (VelocityException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) Template(org.apache.velocity.Template) BufferedWriter(java.io.BufferedWriter)

Example 22 with Template

use of org.apache.velocity.Template in project gocd by gocd.

the class TestVelocityView method getTemplate.

@Override
protected Template getTemplate() throws Exception {
    Template realTemplateForTest = setupTemplate(loader, runtimeServices, "template1", getClass().getResourceAsStream(templatePath));
    List<Template> templates = new ArrayList<>();
    templates.add(realTemplateForTest);
    templates.addAll(additionalTemplates);
    for (Template template : templates) {
        template.process();
    }
    return templates.get(0);
}
Also used : ArrayList(java.util.ArrayList) Template(org.apache.velocity.Template)

Example 23 with Template

use of org.apache.velocity.Template in project gocd by gocd.

the class TestVelocityView method setupTemplate.

private Template setupTemplate(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, InputStream templateContents) {
    try {
        Template template = new Template();
        template.setRuntimeServices(runtimeServices);
        template.setResourceLoader(loader);
        template.setName(templateName);
        when(loader.getResourceStream(templateName)).thenReturn(templateContents);
        doReturn(template).when(runtimeServices).getTemplate(templateName);
        doReturn(template).when(runtimeServices).getTemplate(eq(templateName), Matchers.<String>any());
        return template;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Template(org.apache.velocity.Template)

Example 24 with Template

use of org.apache.velocity.Template in project jop by jop-devel.

the class Report method generateFile.

private void generateFile(String templateName, File outFile, VelocityContext ctx) throws Exception {
    Template template;
    try {
        template = Velocity.getTemplate(templateName);
    } catch (ResourceNotFoundException ignored) {
        template = Velocity.getTemplate("com/jopdesign/wcet/report/" + templateName);
    }
    FileWriter fw = new FileWriter(outFile);
    template.merge(ctx, fw);
    fw.close();
}
Also used : FileWriter(java.io.FileWriter) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 25 with Template

use of org.apache.velocity.Template in project stashbot by palantir.

the class JenkinsJobXmlFormatter method generateJobXml.

public String generateJobXml(JobTemplate jobTemplate, Repository repo) throws SQLException {
    final VelocityContext vc = velocityManager.getVelocityContext();
    final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
    final JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
    RepositoryCloneLinksRequest rclr = new RepositoryCloneLinksRequest.Builder().repository(repo).protocol("http").user(null).build();
    String repositoryUrl = rs.getCloneLinks(rclr).iterator().next().getHref();
    String cleanRepositoryUrl = repositoryUrl;
    // Handle the various Authentication modes
    switch(jsc.getAuthenticationMode()) {
        case USERNAME_AND_PASSWORD:
            // manually insert the username and pw we are configured to use
            repositoryUrl = repositoryUrl.replace("://", "://" + jsc.getStashUsername() + ":" + jsc.getStashPassword() + "@");
            break;
        case CREDENTIAL_MANUALLY_CONFIGURED:
            vc.put("credentialUUID", jsc.getStashPassword());
            break;
    }
    vc.put("repositoryUrl", repositoryUrl);
    vc.put("cleanRepositoryUrl", cleanRepositoryUrl);
    vc.put("prebuildCommand", prebuildCommand(rc.getPrebuildCommand()));
    // TODO: figure out build command some other way?
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
            vc.put("buildCommand", buildCommand(rc.getVerifyBuildCommand()));
            break;
        case VERIFY_PR:
            vc.put("buildCommand", buildCommand(rc.getVerifyBuildCommand()));
            break;
        case PUBLISH:
            vc.put("buildCommand", buildCommand(rc.getPublishBuildCommand()));
            break;
        case NOOP:
            vc.put("buildCommand", buildCommand("/bin/true"));
            break;
    }
    // Add email notification stuff for all build types
    vc.put("isEmailNotificationsEnabled", rc.getEmailNotificationsEnabled());
    vc.put("emailRecipients", rc.getEmailRecipients());
    vc.put("isEmailForEveryUnstableBuild", rc.getEmailForEveryUnstableBuild());
    vc.put("isEmailSendToIndividuals", rc.getEmailSendToIndividuals());
    vc.put("isEmailPerModuleEmail", rc.getEmailPerModuleEmail());
    vc.put("startedCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "inprogress"));
    vc.put("successCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "successful"));
    vc.put("failedCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "failed"));
    vc.put("repositoryLink", navBuilder.repo(repo).browse().buildAbsolute());
    vc.put("repositoryName", repo.getProject().getName() + " " + repo.getName());
    // Parameters are type-dependent for now
    ImmutableList.Builder<Map<String, String>> paramBuilder = new ImmutableList.Builder<Map<String, String>>();
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            break;
        case VERIFY_PR:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            // pullRequestId
            paramBuilder.add(ImmutableMap.of("name", "pullRequestId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the pull request Id", "defaultValue", ""));
            break;
        case PUBLISH:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            break;
        case NOOP:
            // no params
            break;
    }
    vc.put("paramaterList", paramBuilder.build());
    // Junit settings
    vc.put("isJunit", rc.getJunitEnabled());
    vc.put("junitPath", rc.getJunitPath());
    // Artifact settings
    vc.put("artifactsEnabled", rc.getArtifactsEnabled());
    vc.put("artifactsPath", rc.getArtifactsPath());
    // insert pinned data
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
        case VERIFY_PR:
            vc.put("isPinned", rc.getVerifyPinned());
            vc.put("label", rc.getVerifyLabel());
            break;
        case PUBLISH:
            vc.put("isPinned", rc.getPublishPinned());
            vc.put("label", rc.getPublishLabel());
            break;
        case NOOP:
            vc.put("isPinned", false);
            break;
    }
    StringWriter xml = new StringWriter();
    VelocityEngine ve = velocityManager.getVelocityEngine();
    Template template = ve.getTemplate(jobTemplate.getTemplateFile());
    template.merge(vc, xml);
    return xml.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ImmutableList(com.google.common.collect.ImmutableList) StashbotUrlBuilder(com.palantir.stash.stashbot.urlbuilder.StashbotUrlBuilder) NavBuilder(com.atlassian.stash.nav.NavBuilder) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) RepositoryCloneLinksRequest(com.atlassian.stash.repository.RepositoryCloneLinksRequest) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Template(org.apache.velocity.Template) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

Aggregations

Template (org.apache.velocity.Template)33 VelocityContext (org.apache.velocity.VelocityContext)20 StringWriter (java.io.StringWriter)11 IOException (java.io.IOException)9 VelocityEngine (org.apache.velocity.app.VelocityEngine)9 File (java.io.File)6 Writer (java.io.Writer)6 Properties (java.util.Properties)5 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)5 FileWriter (java.io.FileWriter)4 FileOutputStream (java.io.FileOutputStream)3 PrintWriter (java.io.PrintWriter)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringReader (java.io.StringReader)2 Map (java.util.Map)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 VelocityException (org.apache.velocity.exception.VelocityException)2