package com.adaptrex.tools.command;
import java.io.IOException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import com.adaptrex.tools.environment.IEnvironmentService;
import com.adaptrex.tools.log.ILogService;
import com.adaptrex.tools.webapp.IWebappService;
import com.adaptrex.tools.webapp.Page;
import com.adaptrex.tools.webapp.Webapp;
import com.google.inject.Inject;
import freemarker.template.TemplateException;
public class Compile extends Command {
private static final String WEBAPP_USAGE = "adaptrex compile webapp [-p <webapp-path>]";
private static final String PAGE_USAGE = "adaptrex compile page [-p <webapp-path>] -P <page-path>";
@Inject
public Compile(ICommandService commandService, ILogService logService,
IEnvironmentService environmentService, IWebappService webappService) throws ParseException,
IOException {
super(commandService, logService, environmentService, webappService);
}
public void run() throws ParseException, IOException, TemplateException {
String target = commandService.getTarget();
if (target.equals("webapp")) compileWebapp();
else if (target.equals("page")) compilePage();
}
private void compilePage() throws ParseException, IOException {
setPageOptions();
cl = commandService.parseCommands(PAGE_USAGE);
commandService.requireOptions(PAGE_USAGE, "P");
Webapp webapp = commandService.getWebapp();
if (webapp == null) return;
String pagePath = cl.getOptionValue("P");
Page page = webappService.getPage(webapp, pagePath);
webappService.compilePage(webapp, page);
commandService.printLog();
}
private void compileWebapp() throws ParseException, IOException {
setWebappOptions();
cl = commandService.parseCommands(WEBAPP_USAGE);
Webapp webapp = commandService.getWebapp();
if (webapp == null) return;
for (String pageKey : webapp.getPages().keySet()) {
webappService.compilePage(webapp, webapp.getPages().get(pageKey));
commandService.printLog();
}
}
private void setPageOptions() {
Option p = new Option("p", "webapp-path", true, "Path of the webapp to generate a new page for");
p.setArgName("webapp-path");
p.setOptionalArg(true);
options.addOption(p);
Option pg = new Option("P", "page-path", true, "Path of the page to generate");
pg.setArgName("page-path");
options.addOption(pg);
}
private void setWebappOptions() {
Option p = new Option("p", "webapp-path", true, "Path of the webapp to generate a new page for");
p.setArgName("webapp-path");
p.setOptionalArg(true);
options.addOption(p);
}
}