Search in sources :

Example 1 with CodeParameter

use of com.thecoderscorner.menu.editorui.generator.parameters.CodeParameter in project tcMenu by davetcc.

the class PluginRequiredFileProcessor method generatePluginsForCreator.

protected void generatePluginsForCreator(CodePluginItem item, Path directory) throws TcMenuConversionException {
    var expando = new CodeParameter(CodeParameter.NO_TYPE, null, true, "");
    var filteredSourceFiles = item.getRequiredSourceFiles().stream().filter(sf -> sf.getApplicability().isApplicable(context.getProperties())).collect(Collectors.toList());
    for (var srcFile : filteredSourceFiles) {
        try {
            var fileName = expando.expandExpression(context, srcFile.getFileName());
            // get the source (either from the plugin or from the tcMenu library)
            String fileNamePart;
            String fileData;
            Path location = item.getConfig().getPath().resolve(fileName);
            try (var sourceInputStream = new FileInputStream(location.toFile())) {
                fileData = new String(sourceInputStream.readAllBytes());
                fileNamePart = Paths.get(fileName).getFileName().toString();
            } catch (Exception e) {
                throw new TcMenuConversionException("Unable to locate file in plugin: " + srcFile, e);
            }
            Path resolvedOutputFile = directory.resolve(fileNamePart);
            if (!srcFile.isOverwritable() && Files.exists(resolvedOutputFile)) {
                uiLogger.accept(WARNING, "Source file " + srcFile.getFileName() + " already exists and overwrite is false, skipping");
            } else {
                uiLogger.accept(INFO, "Copy plugin file: " + srcFile.getFileName());
                for (var cr : srcFile.getReplacementList()) {
                    if (cr.getApplicability().isApplicable(context.getProperties())) {
                        uiLogger.accept(DEBUG, "Plugin file replacement: " + cr.getFind() + " to " + cr.getReplace());
                        var replacement = StringHelper.escapeRex(expando.expandExpression(context, cr.getReplace()));
                        fileData = fileData.replaceAll(cr.getFind(), replacement);
                    }
                }
                Files.write(resolvedOutputFile, fileData.getBytes(), TRUNCATE_EXISTING, CREATE);
            }
        // and copy into the destination
        } catch (Exception e) {
            throw new TcMenuConversionException("Unexpected exception processing " + srcFile, e);
        }
    }
}
Also used : StringHelper(com.thecoderscorner.menu.editorui.util.StringHelper) Files(java.nio.file.Files) TRUNCATE_EXISTING(java.nio.file.StandardOpenOption.TRUNCATE_EXISTING) CodeParameter(com.thecoderscorner.menu.editorui.generator.parameters.CodeParameter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) List(java.util.List) CodePluginItem(com.thecoderscorner.menu.editorui.generator.plugin.CodePluginItem) Paths(java.nio.file.Paths) CREATE(java.nio.file.StandardOpenOption.CREATE) BiConsumer(java.util.function.BiConsumer) RequiredSourceFile(com.thecoderscorner.menu.editorui.generator.plugin.RequiredSourceFile) Path(java.nio.file.Path) Level(java.lang.System.Logger.Level) Path(java.nio.file.Path) CodeParameter(com.thecoderscorner.menu.editorui.generator.parameters.CodeParameter) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Aggregations

CodeParameter (com.thecoderscorner.menu.editorui.generator.parameters.CodeParameter)1 CodePluginItem (com.thecoderscorner.menu.editorui.generator.plugin.CodePluginItem)1 RequiredSourceFile (com.thecoderscorner.menu.editorui.generator.plugin.RequiredSourceFile)1 StringHelper (com.thecoderscorner.menu.editorui.util.StringHelper)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Level (java.lang.System.Logger.Level)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 CREATE (java.nio.file.StandardOpenOption.CREATE)1 TRUNCATE_EXISTING (java.nio.file.StandardOpenOption.TRUNCATE_EXISTING)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 Collectors (java.util.stream.Collectors)1