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.framework.javascript.ICompileService;
import com.adaptrex.tools.framework.javascript.JavaScriptFramework;
import com.adaptrex.tools.log.ILogService;
import com.adaptrex.tools.log.Log;
import com.adaptrex.tools.webapp.IWebappService;
import com.adaptrex.tools.webapp.Webapp;
import com.google.inject.Inject;
public class Configure extends Command {
private static final String ENV_USAGE = "adaptrex configure environment [-w <webroot>] [-c <sencha-cmd>]";
private static final String WEBAPP_USAGE = "adaptrex configure webapp [-p <path>] " +
"[-n <name>] [-a <adaptrex-js>] [-e <extjs>] [-s <sencha-touch>] [-t <ext-theme>] " +
"[-T <sencha-touch-theme>]";
private ICompileService compileService;
@Inject
public Configure(ICommandService commandService, ILogService logService, ICompileService compileService,
IEnvironmentService environmentService, IWebappService webappService) throws ParseException,
IOException {
super(commandService, logService, environmentService, webappService);
this.compileService = compileService;
}
public void run() throws ParseException, IOException {
String target = commandService.getTarget();
if (target.equals("environment")) configureEnvironment();
else if (target.equals("webapp")) configureWebapp();
}
/*
* adaptrex configure environment
*/
private void configureEnvironment() throws ParseException, IOException {
setConfigureEnvironmentOptions();
cl = commandService.parseCommands(ENV_USAGE);
commandService.requireOneOption(ENV_USAGE, "w", "c");
boolean success = false;
/*
* Set weblib path
*/
if (cl.hasOption("w"))
success = environmentService.setWebrootPath(cl.getOptionValue("w"));
/*
* Set Sencha Cmd path
*/
if (cl.hasOption("c"))
success = environmentService.setSenchaCmdPath(cl.getOptionValue("c"));
commandService.printLog();
if (success) environmentService.saveEnvironment();
}
/*
* Configure webapp
*/
private void configureWebapp() throws ParseException, IOException {
setConfigureWebappOptions();
cl = commandService.parseCommands(WEBAPP_USAGE);
commandService.requireOneOption(WEBAPP_USAGE, "n", "a", "e", "s", "t", "T");
Webapp webapp = commandService.getWebapp();
if (webapp == null) return;
boolean doSave = false;
boolean bootstrapExt = false;
boolean bootstrapSenchaTouch = false;
/*
* Set Name
*/
if (cl.hasOption("n")) {
String sn = cl.getOptionValue("n");
if (webappService.setName(webapp, sn)) doSave = true;
}
/*
* Set Adaptrex
*/
if (cl.hasOption("a")) {
String adaptrex = cl.getOptionValue("a");
if (adaptrex == null) adaptrex = JavaScriptFramework.DEFAULT;
if (webappService.setAdaptrexJS(webapp, adaptrex)) {
doSave = true;
bootstrapExt = true;
bootstrapSenchaTouch = true;
}
}
/*
* Set Ext
*/
if (cl.hasOption("e")) {
String ext = cl.getOptionValue("e");
if (ext == null) ext = JavaScriptFramework.DEFAULT;
if (webappService.setExtJS(webapp, ext)) {
doSave = true;
bootstrapExt = true;
}
}
/*
* Set Sencha Touch
*/
if (cl.hasOption("s")) {
String sencha = cl.getOptionValue("s");
if (sencha == null) sencha = JavaScriptFramework.DEFAULT;
if (webappService.setSenchaTouch(webapp, sencha)) {
doSave = true;
bootstrapSenchaTouch = true;
}
}
if (doSave) {
webappService.saveWebapp(webapp);
commandService.printLog();
}
if (bootstrapExt) {
compileService.createBootstrap(webapp, JavaScriptFramework.EXTJS);
commandService.printLog();
}
if (bootstrapSenchaTouch) {
compileService.createBootstrap(webapp, JavaScriptFramework.SENCHA_TOUCH);
commandService.printLog();
}
/*
* Add themes
*/
if (cl.hasOption("t")) {
try {
boolean success = webapp.getExt().importTheme(webapp, cl.getOptionValue("t"));
if (success) {
commandService.log(Log.SUCCESS, "Theme Successfully Imported", null);
} else {
commandService.log(Log.ERROR, "Unspecified Error Importing Theme", null);
}
} catch (Exception e) {
commandService.log(Log.ERROR, "Error Importing Theme", e.getMessage());
}
commandService.printLog();
}
if (cl.hasOption("T")) {
try {
boolean success = webapp.getSenchaTouch().importTheme(webapp, cl.getOptionValue("T"));
if (success) {
commandService.log(Log.SUCCESS, "Theme Successfully Imported", null);
} else {
commandService.log(Log.ERROR, "Unspecified Error Importing Theme", null);
}
} catch (Exception e) {
commandService.log(Log.ERROR, "Error Importing Theme", e.getMessage());
}
commandService.printLog();
}
}
/*
* "adaptrex configure environment" options
*/
private void setConfigureEnvironmentOptions() {
Option wp = new Option("w", "webroot", true,
"The path to your webroot folder. This folder must contain your weblib folder.");
wp.setArgName("webroot");
Option wn = new Option("c", "sencha-cmd", true,
"The path to the Sencha Cmd folder to use with Adaptrex Tools");
wn.setArgName("sencha-cmd");
options.addOption(wp);
options.addOption(wn);
}
/*
* "adaptrex configure webapp" options
*/
private void setConfigureWebappOptions() {
commandService.addWebappOptions();
commandService.addFrameworkOptions();
Option sn = new Option("n", "set-name", true, "Set the name of the webapp");
sn.setArgName("set-name");
options.addOption(sn);
Option t = new Option("t", "ext-theme", true, "Copy an ExtJS theme to the webapp");
t.setArgName("ext-theme");
options.addOption(t);
Option st = new Option("T", "sencha-touch-theme", true, "Copy a Sencha Touch theme to the webapp");
st.setArgName("sencha-touch-theme");
options.addOption(st);
}
}