Search in sources :

Example 6 with Template

use of io.quarkus.qute.Template in project automatiko-engine by automatiko-io.

the class SendEmailService method send.

/**
 * Sends email to the given list of recipients with given subject and body that is created based on the given template.
 * Optionally given attachments will be put on the message as well
 *
 * In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
 * so it can be used within workflow definition to handle it
 *
 * In case template cannot be found with given name a <code>ServiceExecutionError</code> will
 * be thrown with error code set to <code>emailTemplateNotFound</code>
 *
 * @param tos comma separated list of recipients
 * @param subject subject of the email
 * @param templateName name of the email template to be used to create the body of the email message
 * @param body body of the email
 * @param attachments optional attachments
 */
public void send(String tos, String subject, String templateName, Object body, File<byte[]>... attachments) {
    Template template = getTemplate(templateName);
    try {
        Map<String, Object> templateData = new HashMap<>();
        templateData.put("body", body);
        String content = template.instance().data(templateData).render();
        for (String to : tos.split(",")) {
            Mail mail = Mail.withHtml(to, subject, content);
            for (File<byte[]> attachment : attachments) {
                if (attachment == null) {
                    continue;
                }
                mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
            }
            mailer.send(mail);
        }
    } catch (Exception e) {
        throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
    }
}
Also used : ServiceExecutionError(io.automatiko.engine.api.workflow.ServiceExecutionError) Mail(io.quarkus.mailer.Mail) HashMap(java.util.HashMap) Template(io.quarkus.qute.Template)

Example 7 with Template

use of io.quarkus.qute.Template in project quarkus-openapi-generator by quarkiverse.

the class QuteTemplatingEngineAdapter method compileTemplate.

@Override
public String compileTemplate(TemplatingExecutor executor, Map<String, Object> bundle, String templateFile) throws IOException {
    this.cacheTemplates(executor);
    Template template = engine.getTemplate(templateFile);
    if (template == null) {
        template = engine.parse(executor.getFullTemplateContents(templateFile));
        engine.putTemplate(templateFile, template);
    }
    return template.data(bundle).render();
}
Also used : Template(io.quarkus.qute.Template)

Example 8 with Template

use of io.quarkus.qute.Template in project jbang by jbangdev.

the class Init method doCall.

@Override
public Integer doCall() throws IOException {
    requireScriptArgument();
    dev.jbang.catalog.Template tpl = dev.jbang.catalog.Template.get(initTemplate);
    if (tpl == null) {
        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Could not find init template named: " + initTemplate + ". Try run with --fresh to get latest catalog updates.");
    }
    boolean absolute = new File(scriptOrFile).isAbsolute();
    Path outFile = Util.getCwd().resolve(scriptOrFile);
    Path outDir = outFile.getParent();
    String outName = outFile.getFileName().toString();
    properties.put("scriptref", scriptOrFile);
    properties.put("baseName", Util.getBaseName(Paths.get(scriptOrFile).getFileName().toString()));
    properties.put("dependencies", dependencies);
    List<RefTarget> refTargets = tpl.fileRefs.entrySet().stream().map(e -> new AbstractMap.SimpleEntry<>(resolveBaseName(e.getKey(), e.getValue(), outName), tpl.resolve(e.getValue()))).map(e -> RefTarget.create(tpl.catalog.catalogRef.getFile().getAbsolutePath(), e.getValue(), e.getKey())).collect(Collectors.toList());
    applyTemplateProperties(tpl);
    if (!force) {
        // Check if any of the files already exist
        for (RefTarget refTarget : refTargets) {
            Path target = refTarget.to(outDir);
            if (Files.exists(target)) {
                warn("File " + target + " already exists. Will not initialize.");
                return EXIT_GENERIC_ERROR;
            }
        }
    }
    try {
        for (RefTarget refTarget : refTargets) {
            if (refTarget.getSource().getOriginalResource().endsWith(".qute")) {
                // TODO fix outFile path handling
                Path out = refTarget.to(outDir);
                renderQuteTemplate(out, refTarget.getSource(), properties);
            } else {
                refTarget.copy(outDir);
            }
        }
    } catch (IOException e) {
        // Clean up any files we already created
        for (RefTarget refTarget : refTargets) {
            Util.deletePath(refTarget.to(outDir), true);
        }
    }
    String renderedScriptOrFile = getRenderedScriptOrFile(tpl.fileRefs, refTargets, outDir, absolute);
    info("File initialized. You can now run it with 'jbang " + renderedScriptOrFile + "' or edit it using 'jbang edit --open=[editor] " + renderedScriptOrFile + "' where [editor] is your editor or IDE, e.g. '" + Edit.knownEditors[new Random().nextInt(Edit.knownEditors.length)] + "'");
    return EXIT_OK;
}
Also used : Path(java.nio.file.Path) Util(dev.jbang.util.Util) TemplateInstance(io.quarkus.qute.TemplateInstance) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Random(java.util.Random) Collectors(java.util.stream.Collectors) File(java.io.File) SourceVersion(javax.lang.model.SourceVersion) AbstractMap(java.util.AbstractMap) List(java.util.List) RefTarget(dev.jbang.source.RefTarget) Template(io.quarkus.qute.Template) Paths(java.nio.file.Paths) TemplateProperty(dev.jbang.catalog.TemplateProperty) Map(java.util.Map) ResourceRef(dev.jbang.source.ResourceRef) Optional(java.util.Optional) TemplateEngine(dev.jbang.util.TemplateEngine) Path(java.nio.file.Path) CommandLine(picocli.CommandLine) RefTarget(dev.jbang.source.RefTarget) IOException(java.io.IOException) AbstractMap(java.util.AbstractMap) Random(java.util.Random) File(java.io.File)

Example 9 with Template

use of io.quarkus.qute.Template in project jbang by jbangdev.

the class Init method renderQuteTemplate.

void renderQuteTemplate(Path outFile, String templatePath, Map<String, Object> properties) throws IOException {
    Template template = TemplateEngine.instance().getTemplate(templatePath);
    if (template == null) {
        throw new ExitException(EXIT_INVALID_INPUT, "Could not find or load template: " + templatePath);
    }
    if (outFile.toString().endsWith(".java")) {
        String basename = Util.getBaseName(outFile.getFileName().toString());
        if (!SourceVersion.isIdentifier(basename)) {
            throw new ExitException(EXIT_INVALID_INPUT, "'" + basename + "' is not a valid class name in java. Remove the special characters");
        }
    }
    Files.createDirectories(outFile.getParent());
    try (BufferedWriter writer = Files.newBufferedWriter(outFile)) {
        TemplateInstance templateWithData = template.instance();
        properties.forEach(templateWithData::data);
        String result = templateWithData.render();
        writer.write(result);
        outFile.toFile().setExecutable(true);
    }
}
Also used : Template(io.quarkus.qute.Template) BufferedWriter(java.io.BufferedWriter) TemplateInstance(io.quarkus.qute.TemplateInstance)

Example 10 with Template

use of io.quarkus.qute.Template in project jbang by jbangdev.

the class TrustedSources method createTrustedSources.

public static void createTrustedSources() {
    Path trustedSourcesFile = Settings.getTrustedSourcesFile();
    if (Files.notExists(trustedSourcesFile)) {
        String templateName = "trusted-sources.qute";
        Template template = TemplateEngine.instance().getTemplate(templateName);
        if (template == null)
            throw new ExitException(1, "Could not locate template named: '" + templateName + "'");
        String result = template.render();
        try {
            Util.writeString(trustedSourcesFile, result);
        } catch (IOException e) {
            Util.errorMsg("Could not create initial trusted-sources file at " + trustedSourcesFile, e);
        }
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException) Template(io.quarkus.qute.Template)

Aggregations

Template (io.quarkus.qute.Template)16 HashMap (java.util.HashMap)8 Mail (io.quarkus.mailer.Mail)7 ServiceExecutionError (io.automatiko.engine.api.workflow.ServiceExecutionError)6 TemplateInstance (io.quarkus.qute.TemplateInstance)4 Path (java.nio.file.Path)4 IOException (java.io.IOException)3 TemplateEngine (dev.jbang.util.TemplateEngine)2 Util (dev.jbang.util.Util)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 Files (java.nio.file.Files)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 CommandLine (picocli.CommandLine)2 Cache (dev.jbang.Cache)1 Settings (dev.jbang.Settings)1 CP_SEPARATOR (dev.jbang.Settings.CP_SEPARATOR)1 TemplateProperty (dev.jbang.catalog.TemplateProperty)1 ExitException (dev.jbang.cli.ExitException)1