Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/mismatch/JBake into args4j-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Apr 24, 2013
2 parents 3c4ba20 + c854c55 commit 5b7ffb6
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JBake

[![Build Status](https://travis-ci.org/jonbullock/JBake.png?branch=master)](https://travis-ci.org/jonbullock/JBake)
![Build Status](https://travis-ci.org/mismatch/JBake.png?branch=master)

by **[Jonathan Bullock](http://jonathanbullock.com/)**

Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<version>2.0.23</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jbake/app/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public boolean accept(File pathname) {
}
};
}

public static boolean isExistingFolder(File f) {
return null != f && f.exists() && f.isDirectory();
}
}
63 changes: 50 additions & 13 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,76 @@ public class Oven {
*
* @param source The source folder
* @param destination The destination folder
* @throws ConfigurationException
* @throws Exception
*/
public Oven(File source, File destination) throws ConfigurationException {
public Oven(File source, File destination) throws Exception {
this.source = source;
this.destination = destination;

ensureSource();

loadConfig();

ensureDestination();
}

private void loadConfig() throws ConfigurationException {
this.config = new CompositeConfiguration();
File customConfig = new File(source.getPath()+File.separator+"custom.properties");
File customConfig = new File(source, "custom.properties");
if (customConfig.exists()) {
config.addConfiguration(new PropertiesConfiguration(customConfig));
}
config.addConfiguration(new PropertiesConfiguration("default.properties"));
}
}

private void ensureSource() throws Exception {
if (!FileUtil.isExistingFolder(source)) {
throw new Exception("Source folder MUST exist!");
}
if (!source.canRead()) {
throw new Exception("Source folder is not readable! Please, check");
}
}


private void ensureDestination() throws Exception {
if (null == destination) {
destination = new File(config.getString("destination.folder"));
}
if (!destination.exists()) {
destination.mkdirs();
}
if (!destination.canWrite()) {
throw new Exception("Destination folder is not writable! Please, check");
}
}

/**
* Checks source path contains required sub-folders (i.e. templates) and setups up variables for them.
*
* @throws Exception If template or contents folder don't exist
*/
public void setupPaths() throws Exception {
templatesPath = new File(source.getPath() + File.separator + config.getString("template.folder"));
if (!templatesPath.exists()) {
throw new Exception("Error: Required template folder cannot be found!");
}
contentsPath = new File(source.getPath() + File.separator + config.getString("content.folder"));
if (!contentsPath.exists()) {
throw new Exception("Error: Required content folder cannot be found!");
}
assetsPath = new File(source.getPath() + File.separator + config.getString("asset.folder"));
templatesPath = setupRequiredFolderFromConfig("template.folder");
contentsPath = setupRequiredFolderFromConfig("content.folder");
assetsPath = setupPathFromConfig("asset.folder");
if (!assetsPath.exists()) {
System.out.println("Warning: No asset folder was found!");
}
}

private File setupPathFromConfig(String key) {
return new File(source, config.getString(key));
}

private File setupRequiredFolderFromConfig(String key) throws Exception {
File path = setupPathFromConfig(key);
if (!FileUtil.isExistingFolder(path)) {
throw new Exception("Error: Required folder cannot be found! Config key is " + key);
}
return path;
}

/**
* All the good stuff happens in here...
*
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/jbake/launcher/LaunchOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jbake.launcher;

import java.io.File;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

class LaunchOptions {
@Argument(index = 0, usage = "source of your blog posts (with templates and assets)", metaVar = "source_folder")
private File source = new File(".");

@Argument(index = 1, usage = "destination folder for baked artifacts", metaVar = "destination_folder")
private File destination = null;

@Option(name = "-h", aliases = {"--help"}, usage="prints this message")
private boolean isHelpNeeded;


LaunchOptions() {}

File getSource() {
return source;
}

File getDestination() {
return destination;
}

boolean isHelpNeeded() {
return isHelpNeeded;
}
}
65 changes: 43 additions & 22 deletions src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jbake.launcher;

import java.io.File;
import java.io.StringWriter;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import org.jbake.app.Oven;

Expand All @@ -11,38 +15,55 @@
*
*/
public class Main {

public static final String VERSION = "v2.0";
private static final String USAGE = "Usage: java -jar jbake.jar <source path> <destination path>";

private final String USAGE_PREFIX = "Usage: java -jar jbake.jar";

/**
* Runs the app with the given arguments.
*
* @param String[] args
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out.println(USAGE);
} else {
if (args.length != 2) {
System.out.println(USAGE);
} else {
File source = new File(args[0]);
File destination = new File(args[1]);
if (!source.exists() || !destination.exists()) {
System.out.println(USAGE);
} else {
try {
Oven oven = new Oven(source, destination);
oven.setupPaths();
oven.bake();
} catch (Exception e) {
e.printStackTrace();
}

}
Main m = new Main();
m.run(m.parseArguments(args));
}

private void run(LaunchOptions options) {
try {
Oven oven = new Oven(options.getSource(), options.getDestination());
oven.setupPaths();
oven.bake();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}

private LaunchOptions parseArguments(String[] args) {
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);

try {
parser.parseArgument(args);

if (res.isHelpNeeded()) {
printUsage(parser);
}
} catch (CmdLineException e) {
printUsage(parser);
}

return res;
}

private void printUsage(CmdLineParser parser) {
StringWriter sw = new StringWriter();
sw.append(USAGE_PREFIX);
parser.printSingleLineUsage(sw, null);
System.out.println(sw.toString());
parser.printUsage(System.out);
System.exit(0);
}

}
4 changes: 3 additions & 1 deletion src/main/resources/default.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# path to destination folder by default
destination.folder=baked
# folder that contains all template files
template.folder=templates
# filename of index template file
Expand Down Expand Up @@ -31,4 +33,4 @@ tag.path=tags
# file extension for output content files
output.extension=.html
# draft content suffix
draft.suffix=-draft
draft.suffix=-draft

0 comments on commit 5b7ffb6

Please sign in to comment.