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