package com.adaptrex.tools;
import static com.adaptrex.tools.Tools.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import com.adaptrex.tools.command.AdaptrexCommandMissingOptionException;
import com.adaptrex.tools.command.AdaptrexInvalidOptionException;
import com.adaptrex.tools.command.Compile;
import com.adaptrex.tools.command.Configure;
import com.adaptrex.tools.command.Delete;
import com.adaptrex.tools.command.Generate;
import com.adaptrex.tools.command.ICommandService;
import com.adaptrex.tools.command.Import;
import com.adaptrex.tools.command.Install;
import com.adaptrex.tools.command.Show;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class ToolsApp {
/*
* Entry point into Adaptrex Tools
*/
public static void main(String[] args) throws ParseException, IOException {
System.out.println(HR + CR + "Adaptrex Tools" + CR +
"adaptrex " + StringUtils.join(args, " ") + CR
);
String verb, target;
if (args.length == 0) {
verb = "-";
target = "-";
} else if (args.length == 1) {
verb = args[0];
target = "-";
} else {
verb = args[0];
target = args[1];
}
/*
* Get guiced and get the environment service
*/
Injector injector = Guice.createInjector(new ToolsModule());
ICommandService commandService = injector.getInstance(ICommandService.class);
/*
* Get our options. We've got a problem with scripts breaking apart arguments
* that include spaces... so we manually rebuild it here
*/
String[] realArgs = args.length > 1 ? Arrays.copyOfRange(args, 2, args.length) : null;
String repackagedArgs = " ";
for (String arg : realArgs) repackagedArgs += arg + " ";
List<String> optionArgs = new ArrayList<String>();
for (String splitArg : repackagedArgs.split(" -")) {
splitArg = splitArg.trim();
if (!splitArg.isEmpty()) optionArgs.add("-" + splitArg.trim());
}
/*
* Initialize our command service
*/
boolean goodEnv = commandService.initialize(verb, target,
optionArgs.toArray(new String[optionArgs.size()]));
if (!goodEnv) {
System.out.println(CR + "This is the first time you are running Adaptrex Tools." + CR + CR +
"Before you can run the tools, you need to run \"adaptrex config environment\"" + CR +
"and configure the path to your weblib folder and the path to Sencha Cmd."
);
return;
}
try {
if (verb.equals("compile")) {
if (testSentence("compile", target, Arrays.asList("webapp","page")))
injector.getInstance(Compile.class).run();
} else if (verb.equals("generate")) {
if (testSentence("generate", target, Arrays.asList("webapp","page")))
injector.getInstance(Generate.class).run();
} else if (verb.equals("configure")) {
if (testSentence("configure", target, Arrays.asList("environment","webapp")))
injector.getInstance(Configure.class).run();
} else if (verb.equals("import")) {
if (testSentence("import", target, Arrays.asList("webapp","page")))
injector.getInstance(Import.class).run();
} else if (verb.equals("delete")) {
if (testSentence("delete", target, Arrays.asList("webapp","page")))
injector.getInstance(Delete.class).run();
} else if (verb.equals("install")) {
if (testSentence("install", target, Arrays.asList("adaptrex-js","extjs", "sencha-touch")))
injector.getInstance(Install.class).run();
} else if (verb.equals("show")) {
if (testSentence("show", target, Arrays.asList("environment","webapp")))
injector.getInstance(Show.class).run();
} else if (verb.equals("help")) {
printHelp();
} else {
System.out.println("Invalid command" + CR);
printHelp();
}
System.out.println("");
} catch (AdaptrexInvalidOptionException e) {
e.printHelp();
} catch (AdaptrexCommandMissingOptionException e) {
e.printError();
} catch (Exception e) {
System.out.println(CR + "Uncaught Error" + CR + HR);
e.printStackTrace();
}
}
/*
* Print help
*/
private static void printHelp() {
System.out.println(
TAB + "Valid Commands" + CR +
TAB + "--------------" + CR +
TAB + "configure : compile an entire 'webapp' or single 'page'" + CR +
TAB + "generate : generate a new 'webapp' or 'page'" + CR +
TAB + "compile : compile an entire 'webapp' or single 'page'" + CR +
TAB + "import : import an existing 'webapp' or 'page'" + CR +
TAB + "delete : deletes an entire 'webapp' or single 'page'" + CR +
TAB + "install : installs or upgrades 'adaptrexjs', 'extjs' or 'sencha-touch'" + CR +
TAB + "show : show information about the Adaptrex 'environment' or specific 'webapp'"
);
}
/*
* Make sure the target is valid for the verb
*/
private static boolean testSentence(String verb, String target, List<String> validTargets) {
if (validTargets.contains(target)) return true;
System.out.println("Invalid target for '" + verb + "'" + CR + CR +
TAB + "Valid Targets" + CR +
TAB + "-------------" + CR +
TAB + StringUtils.join(validTargets, CR + TAB));
return false;
}
}