Skip to content

Commit 78bd24f

Browse files
committed
added cmd line arg piping from shell script, backup, reset, help, class, method, debug - lots of working cmd line goodies for those that want to adventure into creating koans
1 parent 86ceaf5 commit 78bd24f

18 files changed

Lines changed: 65 additions & 68 deletions

File tree

‎koans-lib/src/com/sandwich/koan/cmdline/CommandLineArgumentBuilder.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ public Map<ArgumentType, CommandLineArgument> build() {
1717
if(args != null){
1818
for(int index = 0; index < args.length;){
1919
String stringArg = args[index];
20+
if(stringArg == null || stringArg.trim().length() == 0){
21+
index++;
22+
continue;
23+
}
2024
// is incremented in test of ternary
21-
String stringArgPlusOne = args.length <= ++index ? null : args[index];
25+
String stringArgPlusOne = args.length <= ++index ? null : args[index];
26+
stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne.trim().length() == 0
27+
? null : stringArgPlusOne;
2228
ArgumentType argumentType = ArgumentType.TYPES_BY_STRING.get(stringArg);
2329
ArgumentType argumentTypePlusOne = ArgumentType.TYPES_BY_STRING.get(stringArgPlusOne);
2430
if(argumentType != null){ // matches an anticipated argument string
@@ -46,8 +52,6 @@ else if(stringArgPlusOne != null){
4652
guessAtMethodAndClass(commandLineArguments, stringArg);
4753
// do not increment again, bump stringArgPlusOne into stringArg from prior increment
4854
continue;
49-
}else{
50-
warnIgnored(stringArgPlusOne);
5155
}
5256
}
5357
}
@@ -79,8 +83,4 @@ private static void guessAtMethodAndClass(
7983
}
8084
}
8185
}
82-
83-
private static void warnIgnored(String a1) {
84-
System.out.println(a1+" was ignored");
85-
}
8686
}

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Commit.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Compile.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎koans-lib/src/com/sandwich/koan/cmdline/behavior/Deploy.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎koans-lib/src/com/sandwich/koan/constant/ArgumentType.java‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,35 @@
77
import java.util.Map;
88

99
import com.sandwich.koan.cmdline.behavior.ArgumentBehavior;
10+
import com.sandwich.koan.cmdline.behavior.Backup;
1011
import com.sandwich.koan.cmdline.behavior.ClassArg;
11-
import com.sandwich.koan.cmdline.behavior.Commit;
12-
import com.sandwich.koan.cmdline.behavior.Deploy;
12+
import com.sandwich.koan.cmdline.behavior.Debug;
1313
import com.sandwich.koan.cmdline.behavior.Help;
1414
import com.sandwich.koan.cmdline.behavior.MethodArg;
15+
import com.sandwich.koan.cmdline.behavior.Reset;
1516
import com.sandwich.koan.cmdline.behavior.Test;
1617
import com.sandwich.koan.runner.RunKoans;
1718

1819
public enum ArgumentType implements ArgumentBehavior {
1920

20-
// COMPILE("-c", new Compile(), "Compiles the application."), // requires tools jar - wait till needed to require
21+
HELP("Help. Displays stuff to, er, help you.",
22+
new Help(), "-help", "help", "h", "?"),
23+
RESET("Restore all the koans in the src/ folder to their original (or last backed up) state.",
24+
new Reset(), "-reset"),
25+
BACKUP("Backup all the koans in the src/ for easy restoration later (useful for developing koans).",
26+
new Backup(), "-backup"),
27+
// nothing using this currently - but probably a few things that could - say, a debug presenter?
28+
DEBUG("Enable debug state in the app.",
29+
new Debug(), "-debug"),
2130
TEST("Run tests. System returns number of failing testcases - not to exceed 255 to retain DOS compatibility for BAT files.",
2231
new Test(), "-test"),
2332
// important class MUST come before method - due to how Enum implements comparable and order
2433
// dependent logic later @ see ArgumentTypeTest.testClassPrecedesMethod
2534
CLASS_ARG("Switch is optional, app tries to find a class definition for any unrecognized string - which becomes a method argument if class is not found. If classcast succeeds - the class will become the only koansuite to run. Permits users/developers to focus on one suite at a time.",
2635
new ClassArg(), "-class"),
2736
METHOD_ARG("Switch is optional, results from failing to find a class definition by an unrecognized string if switch is omitted.",
28-
new MethodArg(), "-method"),
29-
DEPLOY("Runs tests, if all pass - packs a jar for the koans folder, otherwise present stack trace from first failed test.",
30-
new Deploy(), "-deploy"),
31-
COMMIT("Runs tests, if all pass - pushes all changes in lib, koans, and tests to branch (sring value following switch). If tests fail, present stack trace from first failed test.",
32-
new Commit(), "-commit"),
33-
HELP("Help. Displays stuff to, er, help you.",
34-
new Help(), "-help", "help", "h", "?"),
35-
RUN_KOANS("Defaul targent. No switch - this runs if no switch is defined, or if a valid class is found as an argument.",
37+
new MethodArg(), "-method"),
38+
RUN_KOANS("Default target. No switch - this runs if no switch is defined, or if a valid class is found as an argument.",
3639
new RunKoans(), "");
3740

3841
private final List<String> args;
@@ -54,6 +57,9 @@ public String description(){
5457
Map<String, ArgumentType> types = new HashMap<String, ArgumentType>();
5558
for(ArgumentType type : ArgumentType.values()){
5659
for(String arg : type.args){
60+
if(types.containsKey(arg)){
61+
throw new IllegalArgumentException("command line arg: "+arg+" is duplicated.");
62+
}
5763
types.put(arg, type);
5864
}
5965
}

‎koans-lib/src/com/sandwich/koan/constant/KoanConstants.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public abstract class KoanConstants {
77

88
private KoanConstants(){}
99

10+
public static boolean DEBUG = false;
11+
// public static boolean DEBUG = true;
1012
public static final boolean ENABLE_ENCOURAGEMENT= false;
1113

1214
public static final String __ = "REPLACE ME";
@@ -17,6 +19,7 @@ private KoanConstants(){}
1719

1820
public static final String EOL = System.getProperty("line.separator");
1921
public static final String EOLS = "[\n\r"+EOL+"]";
22+
public static final String FILESYSTEM_SEPARATOR= System.getProperty("file.separator");
2023
public static final String PERIOD = ".";
2124
public static final String EXPECTATION_LEFT_ARG= "has expectation as wrong argument!";
2225
public static final String EXPECTED_LEFT = "expected:<";

‎koans-lib/src/com/sandwich/koan/path/PathToEnlightenment.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static void removeAllKoanMethodsExcept(String koanName) {
3838
Path koans = getPathToEnlightment();
3939
// if more than 1 pkg, or more than 1 suite - something is likely broken before this point
4040
Map<Object, List<KoanMethod>> lessonsBySuiteMap = koans.iterator().next().getValue();
41+
boolean logWarning = false;
4142
if(koans.size() != 1 ||
4243
lessonsBySuiteMap.size() !=1){
43-
Logger.getAnonymousLogger().warning("not just one koansuite remains, " +
44-
"check koan suite name argument - not likely that filtering by method will work.");
44+
logWarning = true;
4545
}
4646
koanName = koanName.trim();
4747
for(Entry<Object, List<KoanMethod>> methodsBySuite : lessonsBySuiteMap.entrySet()){
@@ -56,10 +56,18 @@ public static void removeAllKoanMethodsExcept(String koanName) {
5656
// }
5757
}
5858
if(keeper == null){
59-
throw new NoSuchMethodError(koanName);
59+
// default is to assume method if not a class or recognized arg
60+
// no sense throwing exception, might be user error
61+
System.err.println(koanName+" was not a recognized parameter.");
62+
System.exit(-3);
6063
}
6164
methodsBySuite.setValue(Arrays.asList(keeper));
6265
}
66+
// defer the warning until after potential user error is in console and app is exited
67+
if(!logWarning){
68+
Logger.getAnonymousLogger().warning("not just one koansuite remains, " +
69+
"check koan suite name argument - not likely that filtering by method will work.");
70+
}
6371
}
6472

6573
static void stagePathToEnlightenment(String pkg, String koanSuite){

‎koans-lib/src/com/sandwich/koan/runner/AppLauncher.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sandwich.koan.runner;
22

33
import com.sandwich.koan.cmdline.CommandLineArgumentBuilder;
4+
import com.sandwich.koan.constant.KoanConstants;
45

56
public class AppLauncher implements Runnable {
67

@@ -12,6 +13,14 @@ public AppLauncher(String...args){
1213

1314
public static void main(final String... args) throws Throwable {
1415
new AppLauncher(args).run();
16+
if(KoanConstants.DEBUG){
17+
StringBuilder argsBuilder = new StringBuilder();
18+
int argNumber = 0;
19+
for(String arg : args){
20+
argsBuilder.append("Argument number "+String.valueOf(++argNumber)+": '"+arg+"'");
21+
}
22+
System.out.println(argsBuilder.toString());
23+
}
1524
}
1625

1726
public void run() {

‎koans-lib/src/com/sandwich/koan/runner/KoanSuiteRunner.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.Map.Entry;
1011

1112
import com.sandwich.koan.cmdline.CommandLineArgument;
1213
import com.sandwich.koan.constant.ArgumentType;
14+
import com.sandwich.koan.constant.KoanConstants;
1315

1416
public class KoanSuiteRunner {
1517

@@ -31,8 +33,7 @@ public void run() throws ClassNotFoundException, IOException,
3133
Map<ArgumentType, CommandLineArgument> commandLineArguments =
3234
new HashMap<ArgumentType, CommandLineArgument>(this.commandLineArguments); //clone entries
3335
ifNecessaryPlantDefaultArgumentToRunKoans(commandLineArguments);
34-
List<CommandLineArgument> sortedArguments = new ArrayList<CommandLineArgument>(
35-
commandLineArguments.values());
36+
List<CommandLineArgument> sortedArguments = new ArrayList<CommandLineArgument>(commandLineArguments.values());
3637
Collections.sort(sortedArguments);
3738
for(CommandLineArgument argument : sortedArguments){
3839
argument.run();
@@ -43,6 +44,13 @@ void ifNecessaryPlantDefaultArgumentToRunKoans(Map<ArgumentType, CommandLineArgu
4344
if(commandLineArguments.isEmpty() ||
4445
(!commandLineArguments.containsKey(ArgumentType.RUN_KOANS)
4546
&& commandLineArguments.containsKey(ArgumentType.CLASS_ARG))){
47+
if(KoanConstants.DEBUG){
48+
System.out.println("Planting default run target.");
49+
for(Entry<ArgumentType, CommandLineArgument> argEntry : commandLineArguments.entrySet()){
50+
System.out.println("Key: '"+argEntry.getKey()+"'");
51+
System.out.println("Value: '"+argEntry.getValue()+"'");
52+
}
53+
}
4654
commandLineArguments.put(ArgumentType.RUN_KOANS, new CommandLineArgument(
4755
ArgumentType.RUN_KOANS, null));
4856
}

‎koans-tests/test/com/sandwich/koan/constant/ArgumentTypeTest.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
import com.sandwich.koan.path.CommandLineTestCase;
1313

1414
public class ArgumentTypeTest extends CommandLineTestCase {
15-
16-
@Test
17-
public void testArgumentTypes_behaviorNoExceptions() throws Exception {
18-
for(ArgumentType type : ArgumentType.values()){
19-
type.run("");
20-
}
21-
}
2215

2316
@Test
2417
public void testClassPrecedesMethod() throws Exception {

0 commit comments

Comments
 (0)