Skip to content

Commit

Permalink
updating for 0.3.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
lhazlewood committed Oct 19, 2016
1 parent e4c6eb2 commit 3fe1430
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 108 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
211 changes: 107 additions & 104 deletions core/src/main/groovy/com/leshazlewood/scms/core/DefaultProcessor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ class DefaultProcessor implements Processor {
}
}

private static Map<String,Renderer> asRendererMap(Collection<Renderer> c) {
private static Map<String, Renderer> asRendererMap(Collection<Renderer> c) {

Map<String,Renderer> m = [:]
for(Renderer r : c) {
Map<String, Renderer> m = [:]
for (Renderer r : c) {
if (r instanceof FileRenderer) {
m[r.inputFileExtension] = r
}
Expand All @@ -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 {
Expand Down Expand Up @@ -231,130 +227,140 @@ 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<String,Object> config = (Map<String,Object>)deepcopy(this.config as Map)
private void renderFile(File f) throws IOException {

Map<String,Object> model = [:]
String relPath = getRelativePath(sourceDir, f);

if (config.containsKey('model') && config.model instanceof Map) {
model = config.model as Map
} else {
config.model = model
}
Map<String, Object> config = (Map<String, Object>) 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<String, Object> 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<String, ?> 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<String, ?> 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) {
if (map == null) {
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)
Expand All @@ -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)
Expand All @@ -386,7 +392,6 @@ class DefaultProcessor implements Processor {
}



private boolean hasExtension(String path) {
return getExtension(path) != null
}
Expand All @@ -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;
}
Expand All @@ -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.
*/
Expand Down

0 comments on commit 3fe1430

Please sign in to comment.