-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathMain.java
More file actions
124 lines (106 loc) · 3.09 KB
/
Copy pathMain.java
File metadata and controls
124 lines (106 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.jbake.launcher;
import java.io.File;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.ServiceLoader;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.jbake.app.ConfigUtil;
import org.jbake.app.FileUtil;
import org.jbake.app.Oven;
import org.jbake.plugins.JBakePlugin;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
/**
* Launcher for JBake.
*
* @author Jonathan Bullock <jonbullock@gmail.com>
*
*/
public class Main {
// public static final String VERSION = "v2.2";
private final String USAGE_PREFIX = "Usage: jbake";
/**
* Runs the app with the given arguments.
*
* @param String[] args
*/
public static void main(String[] args) {
Iterator<JBakePlugin> plugins = ServiceLoader.load(JBakePlugin.class).iterator();
while (plugins.hasNext()) {
JBakePlugin plugin = plugins.next();
plugin.init();
}
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);
CompositeConfiguration config = null;
try {
config = ConfigUtil.load(res.getSource());
} catch (ConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("JBake " + config.getString("version") + " (" + config.getString("build.timestamp") + ") [http://jbake.org]");
System.out.println();
if (res.isHelpNeeded()) {
printUsage(parser);
}
if (res.isRunServer()) {
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);
}
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.setUsageWidth(100);
parser.printUsage(System.out);
System.exit(0);
}
private void runServer(String path, String port) {
JettyServer.run(path, port);
System.exit(0);
}
private void initStructure(CompositeConfiguration config) {
Init init = new Init(config);
try {
File codeFolder = FileUtil.getRunningLocation();
init.run(new File("."), codeFolder);
System.out.println("Base folder structure successfully created.");
System.exit(0);
} catch (Exception e) {
System.err.println("Failed to initalise structure!");
e.printStackTrace();
System.exit(1);
}
}
}