Skip to content

Commit

Permalink
Started work on #34
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Sep 22, 2013
1 parent 64905ef commit ccbf06f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
15 changes: 11 additions & 4 deletions src/main/java/org/jbake/launcher/LaunchOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ class LaunchOptions {
private File destination = null;

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

@Option(name = "-s", aliases = {"--server"}, usage="serves out destination folder on port 8080")
private boolean isRunServer;
private boolean runServer;

@Option(name = "-i", aliases = {"--init"}, usage="initialises required folder structure with default templates")
private boolean init;

File getSource() {
return source;
Expand All @@ -27,10 +30,14 @@ File getDestination() {
}

boolean isHelpNeeded() {
return isHelpNeeded;
return helpNeeded;
}

boolean isRunServer() {
return isRunServer;
return runServer;
}

boolean isInit() {
return init;
}
}
93 changes: 86 additions & 7 deletions src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.jbake.launcher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
Expand Down Expand Up @@ -55,25 +61,30 @@ private LaunchOptions parseArguments(String[] args) {
try {
parser.parseArgument(args);

Oven oven = new Oven();
CompositeConfiguration config = null;
try {
config = oven.loadConfig();
} catch (ConfigurationException e) {
e.printStackTrace();
}

if (res.isHelpNeeded()) {
printUsage(parser);
}

if (res.isRunServer()) {
Oven oven = new Oven();
CompositeConfiguration config = null;
try {
config = oven.loadConfig();
} catch (ConfigurationException e) {
e.printStackTrace();
}
if (res.getSource().getPath().equals(".")) {
// use the default destination folder
runServer(config.getString("destination.folder"), config.getString("server.port"));
} else {
runServer(res.getSource().getPath(), config.getString("server.port"));
}
}

if (res.isInit()) {
initStructure(config);
}
} catch (CmdLineException e) {
printUsage(parser);
}
Expand Down Expand Up @@ -117,4 +128,72 @@ private void runServer(String path, String port) {
}
System.exit(0);
}

private void initStructure(CompositeConfiguration config) {
File outputFolder = new File(".");
if (!outputFolder.canWrite()) {
System.err.println("Error: Folder is not writable!");
System.exit(1);
}
File[] contents = outputFolder.listFiles();
boolean safe = true;
if (contents != null) {
for (File content : contents) {
if (content.isDirectory()) {
if (content.getName().equalsIgnoreCase(config.getString("template.folder"))) {
safe = false;
}
if (content.getName().equalsIgnoreCase(config.getString("content.folder"))) {
safe = false;
}
if (content.getName().equalsIgnoreCase(config.getString("asset.folder"))) {
safe = false;
}
}
}
}

if (!safe) {
System.err.println("Error: Folder already contains structure!");
System.exit(2);
}

InputStream is = getClass().getResourceAsStream("/base.zip");
if (is != null) {
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry;
byte[] buffer = new byte[1024];

try {
while ((entry = zis.getNextEntry()) != null) {
File outputFile = new File(outputFolder.getCanonicalPath() + File.separatorChar + entry.getName());
File outputParent = new File(outputFile.getParent());
outputParent.mkdirs();

if (entry.isDirectory()) {
if (!outputFile.exists()) {
outputFile.mkdir();
}
} else {
FileOutputStream fos = new FileOutputStream(outputFile);

int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}

fos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.err.println("Error: Cannot locate base structure!");
System.exit(3);
}

System.out.println("Base folder structure successfully created.");
System.exit(0);
}
}
Binary file added src/main/resources/base.zip
Binary file not shown.

0 comments on commit ccbf06f

Please sign in to comment.