ch.lambdaj.function.convert.Converter Java Examples

The following examples show how to use ch.lambdaj.function.convert.Converter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Meta.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
/**
 * Converter method to process supporting files: execute with mustache, or simply copy to
 * destination directory
 *
 * @param targetDir - destination directory
 * @param data - map with additional params needed to process templates
 * @return converter object to pass to lambdaj
 */
private static Converter<SupportingFile, File> processFiles(final File targetDir,
        final Map<String, Object> data) {
    return new Converter<SupportingFile, File>() {
        private DefaultGenerator generator = new DefaultGenerator();

        @Override
        public File convert(SupportingFile support) {
            try {
                File destinationFolder =
                        new File(new File(targetDir.getAbsolutePath()), support.folder);
                File outputFile = new File(destinationFolder, support.destinationFilename);

                String template =
                        generator.readTemplate(new File(TEMPLATE_DIR_CLASSPATH,
                                support.templateFile).getPath());
                String formatted = template;

                if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
                    LOGGER.info("writing file to {}", outputFile.getAbsolutePath());
                    formatted =
                            Mustache.compiler().withLoader(loader(generator)).defaultValue("")
                                    .compile(template).execute(data);
                } else {
                    LOGGER.info("copying file to {}", outputFile.getAbsolutePath());
                }

                FileUtils.writeStringToFile(outputFile, formatted);
                return outputFile;

            } catch (IOException e) {
                throw new RuntimeException("Can't generate project", e);
            }
        }
    };
}
 
Example #2
Source File: Meta.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Converter method to process supporting files: execute with mustache, or simply copy to
 * destination directory
 *
 * @param targetDir - destination directory
 * @param data - map with additional params needed to process templates
 * @return converter object to pass to lambdaj
 */
private static Converter<SupportingFile, File> processFiles(final File targetDir,
        final Map<String, Object> data) {
    return support -> {
        try {
            File destinationFolder =
                    new File(new File(targetDir.getAbsolutePath()), support.folder);
            File outputFile = new File(destinationFolder, support.destinationFilename);

            TemplateManager templateProcessor = new TemplateManager(
                    new TemplateManagerOptions(false, false),
                    new MustacheEngineAdapter(),
                    new TemplatePathLocator[]{ new CommonTemplateContentLocator("codegen") }
            );

            String template = templateProcessor.readTemplate(new File(TEMPLATE_DIR_CLASSPATH, support.templateFile).getPath());

            String formatted = template;

            Mustache.TemplateLoader loader = name -> templateProcessor.getTemplateReader(name.concat(MUSTACHE_EXTENSION));

            if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
                LOGGER.info("writing file to {}", outputFile.getAbsolutePath());
                formatted =
                        Mustache.compiler().withLoader(loader).defaultValue("")
                                .compile(template).execute(data);
            } else {
                LOGGER.info("copying file to {}", outputFile.getAbsolutePath());
            }

            FileUtils.writeStringToFile(outputFile, formatted, StandardCharsets.UTF_8);
            return outputFile;

        } catch (IOException e) {
            throw new RuntimeException("Can't generate project", e);
        }
    };
}