From 3fe14303238db2165c9491de64e7aad4c4a44519 Mon Sep 17 00:00:00 2001 From: Les Hazlewood Date: Tue, 18 Oct 2016 18:41:07 -0700 Subject: [PATCH] updating for 0.3.0 release --- README.md | 8 +- .../scms/core/DefaultProcessor.groovy | 211 +++++++++--------- 2 files changed, 111 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index f97dfc8..9633b33 100644 --- a/README.md +++ b/README.md @@ -37,15 +37,15 @@ command is in your `$PATH`. Ok, moving on... ### Download SCMS -You may [download the latest stable SCMS version](http://repo.maven.apache.org/maven2/com/leshazlewood/scms/scms/0.2.0/scms-0.2.0.zip) from Maven Central. +You may [download the latest stable SCMS version](http://repo.maven.apache.org/maven2/com/leshazlewood/scms/scms/0.3.0/scms-0.3.0.zip) from Maven Central. ### Install SCMS -Unzip the SCMS distribution .zip file (e.g. scms-0.2.0.zip). Add the resulting `scms-0.2.0/bin` directory +Unzip the SCMS distribution .zip file (e.g. scms-0.3.0.zip). Add the resulting `scms-0.3.0/bin` directory to your `PATH` environment variable. For example, on Unix, Linux and Mac OS X*: - $ unzip scms-0.2.0.zip - $ export PATH=scms-0.2.0/bin:$PATH + $ unzip scms-0.3.0.zip + $ export PATH=scms-0.3.0/bin:$PATH It is recommended that you set this in `~/.bash_profile` so you don't have to do this manually every time you open a new terminal prompt. diff --git a/core/src/main/groovy/com/leshazlewood/scms/core/DefaultProcessor.groovy b/core/src/main/groovy/com/leshazlewood/scms/core/DefaultProcessor.groovy index 78444ed..5e0d1dd 100644 --- a/core/src/main/groovy/com/leshazlewood/scms/core/DefaultProcessor.groovy +++ b/core/src/main/groovy/com/leshazlewood/scms/core/DefaultProcessor.groovy @@ -99,10 +99,10 @@ class DefaultProcessor implements Processor { } } - private static Map asRendererMap(Collection c) { + private static Map asRendererMap(Collection c) { - Map m = [:] - for(Renderer r : c) { + Map m = [:] + for (Renderer r : c) { if (r instanceof FileRenderer) { m[r.inputFileExtension] = r } @@ -113,11 +113,7 @@ class DefaultProcessor implements Processor { @Override public void run() { - try { - recurse(sourceDir); - } catch (Exception e) { - throw new RuntimeException(e); - } + recurse(sourceDir); } private void ensureDirectory(File f) throws IOException { @@ -231,122 +227,132 @@ class DefaultProcessor implements Processor { continue; } - String relPath = getRelativePath(sourceDir, f); - if (f.isDirectory()) { + String relPath = getRelativePath(sourceDir, f); File copiedDir = new File(destDir, relPath); ensureDirectory(copiedDir); recurse(f); } else { + try { + renderFile(f); + } catch (Exception e) { + throw new IOException("Unable to render file $f: ${e.message}") + } + } + } + } - Map config = (Map)deepcopy(this.config as Map) + private void renderFile(File f) throws IOException { - Map model = [:] + String relPath = getRelativePath(sourceDir, f); - if (config.containsKey('model') && config.model instanceof Map) { - model = config.model as Map - } else { - config.model = model - } + Map config = (Map) deepcopy(this.config as Map) - String relDirPath = getRelativeDirectoryPath(relPath); - if ("".equals(relDirPath)) { - //still need to reference it with a separator char in the file: - relDirPath = "."; - } + Map model = [:] - model.root = relDirPath + if (config.containsKey('model') && config.model instanceof Map) { + model = config.model as Map + } else { + config.model = model + } - Map patterns = Collections.emptyMap() + String relDirPath = getRelativeDirectoryPath(relPath); + if ("".equals(relDirPath)) { + //still need to reference it with a separator char in the file: + relDirPath = "."; + } - if (config.containsKey('patterns')) { - assert config.patterns instanceof Map: "scms.patterns must be a map" - patterns = config.patterns - } + model.root = relDirPath - String action = 'render' //default unless overridden + Map patterns = Collections.emptyMap() - for (Map.Entry patternEntry : patterns.entrySet()) { + if (config.containsKey('patterns')) { + assert config.patterns instanceof Map: "scms.patterns must be a map" + patterns = config.patterns + } - String pattern = patternEntry.getKey(); + String action = 'render' //default unless overridden - if (patternMatcher.matches(pattern, relPath)) { + for (Map.Entry patternEntry : patterns.entrySet()) { - assert patternEntry.value instanceof Map: "Entry for pattern '$pattern' must be a map." - Map patternConfig = patternEntry.value as Map - config << patternConfig + String pattern = patternEntry.getKey(); - //pattern-specific model - if (patternConfig.model && patternConfig.model instanceof Map) { - model << (patternConfig.model as Map) - } + if (patternMatcher.matches(pattern, relPath)) { - if (patternConfig.containsKey('render')) { - action = patternConfig.render - } + assert patternEntry.value instanceof Map: "Entry for pattern '$pattern' must be a map." + Map patternConfig = patternEntry.value as Map + config << patternConfig - break; //stop pattern iteration - first match always wins - } + //pattern-specific model + if (patternConfig.model && patternConfig.model instanceof Map) { + model << (patternConfig.model as Map) } - if (action == 'skip') { - continue; - } else if (action == 'copy') { - File destFile = new File(destDir, relPath); - ensureFile(destFile); - copy(f, destFile); - continue; + if (patternConfig.containsKey('render')) { + action = patternConfig.render } - //otherwise we need to render: - Reader content = null - String destRelPath = relPath; //assume same unless it is itself a template - - String extension = getExtension(destRelPath); - if (extension) { - Renderer renderer = getRenderer(model, destRelPath) - if (renderer) { - content = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8) - destRelPath = relPath.substring(0, relPath.length() - (extension.length() + 1)) - - if (!hasExtension(destRelPath)) { - if (renderer instanceof FileRenderer) { - destRelPath += ".$renderer.outputFileExtension" - } - } - - content = render(renderer, model, destRelPath, content) - } - } + break; //stop pattern iteration - first match always wins + } + } + config.model = model - if (config.template) { //a template will be used to render the contents - String template = config.template as String - Renderer renderer = getRenderer(template) - if (renderer) { - if (content == null) { - content = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8) - } - model.content = content.getText() - content = Files.newBufferedReader(new File(template).toPath(), StandardCharsets.UTF_8) - content = render(renderer, model, destRelPath, content) - } - } + if (action == 'skip') { + return; + } else if (action == 'copy') { + File destFile = new File(destDir, relPath); + ensureFile(destFile); + copy(f, destFile); + return; + } + + //otherwise we need to render: + Reader content = null + String destRelPath = relPath; //assume same unless it is itself a template + + Renderer renderer = getRenderer(config, destRelPath) + if (renderer) { + String extension = getExtension(destRelPath) + content = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8) + destRelPath = relPath.substring(0, relPath.length() - (extension.length() + 1)) + + String destExtension = (renderer instanceof FileRenderer) ? renderer.outputFileExtension : extension; + + if (config.outputFileExtension) { + destExtension = config.outputFileExtension + } + + destRelPath += ".$destExtension" - File destFile = new File(destDir, destRelPath); - ensureFile(destFile); - - if (content != null) { - //write out the rendered content to the destination file: - BufferedWriter writer = new BufferedWriter(new FileWriter(destFile)); - copy(content, writer) - content.close() - writer.close() - } else { - //just copy the file over: - copy(f, destFile); + content = render(renderer, model, destRelPath, content) + } + + if (config.template) { //a template will be used to render the contents + String template = config.template as String + renderer = getRenderer(template) + if (renderer) { + if (content == null) { + content = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8) } + model.content = content.getText() + content = Files.newBufferedReader(new File(template).toPath(), StandardCharsets.UTF_8) + content = render(renderer, model, destRelPath, content) } } + + File destFile = new File(destDir, destRelPath); + ensureFile(destFile); + + if (content != null) { + //write out the rendered content to the destination file: + BufferedWriter writer = new BufferedWriter(new FileWriter(destFile)); + copy(content, writer) + content.close() + writer.close() + } else { + //just copy the file over: + copy(f, destFile); + } } def Map deepcopy(Map map) { @@ -354,7 +360,7 @@ class DefaultProcessor implements Processor { return null } Map copy = [:] - for(Map.Entry e : map.entrySet()) { + for (Map.Entry e : map.entrySet()) { Object value = e.value if (value instanceof Collection) { value = deepcopy(value as Collection) @@ -372,7 +378,7 @@ class DefaultProcessor implements Processor { return list; } List copy = [] - for(Object o : list) { + for (Object o : list) { Object value = o; if (o instanceof Collection) { value = deepcopy(o as Collection) @@ -386,7 +392,6 @@ class DefaultProcessor implements Processor { } - private boolean hasExtension(String path) { return getExtension(path) != null } @@ -405,14 +410,14 @@ class DefaultProcessor implements Processor { return renderersByExtension[extension] } - Renderer getRenderer(Map model, String path) { - if ('velocity'.equals(model.renderer)) { + Renderer getRenderer(Map config, String path) { + if ('velocity'.equals(config.renderer)) { return velocityRenderer; - } else if ('pegdown'.equals(model.renderer)) { + } else if ('pegdown'.equals(config.renderer)) { return pegdownRenderer; } - for(Renderer r : renderers) { + for (Renderer r : renderers) { if (r instanceof FileRenderer && r.supports(path)) { return r; } @@ -431,8 +436,6 @@ class DefaultProcessor implements Processor { return new StringReader(resultWriter.toString()); } - - /** * Reads all characters from a Reader and writes them to a Writer. */