Skip to content

Commit

Permalink
include, exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcalpin committed Jul 8, 2010
1 parent e3aca70 commit 6b58244
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
Binary file modified lib/play-scaffold.jar
Binary file not shown.
85 changes: 79 additions & 6 deletions src/play/modules/scaffold/Generate.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import play.Play; import play.Play;
import play.modules.scaffold.entity.Entity; import play.modules.scaffold.entity.Entity;
import play.modules.scaffold.entity.ModelType; import play.modules.scaffold.entity.ModelType;
import play.modules.scaffold.entity.ScaffoldGenerator; import play.modules.scaffold.entity.ScaffoldingGenerator;


/** /**
* Processes the scaffold:gen command. This command generates the scaffolding * Processes the scaffold:gen command. This command generates the scaffolding
Expand All @@ -36,22 +36,52 @@
*/ */
public class Generate public class Generate
{ {
private static final String EXCLUDE = "--exclude=";
private static final String INCLUDE = "--include=";
private static final String OVERWRITE = "--overwrite";

private static final String INVALID_MODELPATTERN = "Invalid pattern, provide a text string with optional '*' wildcard";

public static void main(String[] args) throws Exception public static void main(String[] args) throws Exception
{ {
// initialize Play! // initialize Play!
File root = new File(System.getProperty("application.path")); File root = new File(System.getProperty("application.path"));
Play.init(root, System.getProperty("play.id", "")); Play.init(root, System.getProperty("play.id", ""));
Thread.currentThread().setContextClassLoader(Play.classloader); Thread.currentThread().setContextClassLoader(Play.classloader);


// default options
boolean forceOverwrite = false; boolean forceOverwrite = false;
String includeRegEx = null;
String excludeRegEx = null;

// interpret command line arguments
for (String arg : args) for (String arg : args)
{ {
String lowerArg = arg.toLowerCase();
if (arg.startsWith("--")) if (arg.startsWith("--"))
{ {
if (arg.equalsIgnoreCase("--overwrite")) if (lowerArg.equals(OVERWRITE))
{ {
Logger.info("--overwrite: We will force overwrite target files");
forceOverwrite = true; forceOverwrite = true;
Logger.info("--overwrite: We will force overwrite target files");
} else if (lowerArg.startsWith(INCLUDE)) {
includeRegEx = arg.substring(INCLUDE.length());
if (includeRegEx.isEmpty())
{
Logger.warn(INCLUDE + ": " + INVALID_MODELPATTERN);
System.exit(-1);
}
Logger.info("--include: Including files that match: %s", includeRegEx);
} else if (lowerArg.startsWith(EXCLUDE)) {
excludeRegEx = arg.substring(EXCLUDE.length());
if (excludeRegEx.isEmpty())
{
Logger.warn(EXCLUDE + ": " + INVALID_MODELPATTERN);
System.exit(-1);
}
Logger.info("--exclude: Skipping files that match: %s", excludeRegEx);
} else {
Logger.warn("Invalid argument: %s", arg);
} }
} }
} }
Expand All @@ -60,7 +90,7 @@ public static void main(String[] args) throws Exception
// Currently, we only support classes that extend the // Currently, we only support classes that extend the
// play.db.jpa.Model or siena.Model classes. // play.db.jpa.Model or siena.Model classes.
List<Class> classes = Play.classloader.getAllClasses(); List<Class> classes = Play.classloader.getAllClasses();
ScaffoldGenerator generator = new ScaffoldGenerator(); ScaffoldingGenerator generator = new ScaffoldingGenerator();
generator.setForceOverwrite(forceOverwrite); generator.setForceOverwrite(forceOverwrite);
for (Class clazz : classes) for (Class clazz : classes)
{ {
Expand All @@ -69,10 +99,53 @@ public static void main(String[] args) throws Exception
// and views. // and views.
if (ModelType.forClass(clazz) != null) if (ModelType.forClass(clazz) != null)
{ {
Entity entity = new Entity(clazz); String simpleName = clazz.getSimpleName();
generator.addEntity(entity); boolean includeEntity = false;
// by default, include all entities if no --include= value is
// specified
if (includeRegEx == null)
{
includeEntity = true;
}
// if an --include= value is specified, include only the models
// that match
if (includeRegEx != null && match(simpleName, includeRegEx))
{
includeEntity = true;
}
// always exclude models that match the --exclude= parameter
if (excludeRegEx != null && match(simpleName, excludeRegEx))
{
includeEntity = false;
}
if (includeEntity)
{
Entity entity = new Entity(clazz);
generator.addEntity(entity);
} else {
Logger.info("Skipping %s", simpleName);
}
} }
} }
generator.generate(); generator.generate();
} }

// Does simple matching: you can add an asterisk to match "any"
// text. Matching is case insensitive.
public static boolean match(String text, String pattern)
{
String normalizedText = text.toLowerCase();
String normalizedPattern = pattern.toLowerCase();
String [] subsections = normalizedPattern.split("\\*");
for (String subsection : subsections)
{
int idx = normalizedText.indexOf(subsection);
if(idx == -1)
{
return false;
}
normalizedText = normalizedText.substring(idx + subsection.length());
}
return true;
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* *
* @author Lawrence McAlpin * @author Lawrence McAlpin
*/ */
public class ScaffoldGenerator public class ScaffoldingGenerator
{ {
private static final String CREATE_HTML = "create.html"; private static final String CREATE_HTML = "create.html";
private static final String SHOW_HTML = "show.html"; private static final String SHOW_HTML = "show.html";
Expand All @@ -55,7 +55,7 @@ public class ScaffoldGenerator
private List<Entity> entities = new ArrayList<Entity>(); private List<Entity> entities = new ArrayList<Entity>();
private boolean forceOverwrite; private boolean forceOverwrite;


public ScaffoldGenerator() public ScaffoldingGenerator()
{ {


} }
Expand Down
26 changes: 26 additions & 0 deletions test/play/modules/scaffold/GenerateTest.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,26 @@
package play.modules.scaffold;

import org.junit.Test;
import static org.junit.Assert.*;

public class GenerateTest
{
@Test
public void testMatchWildcard()
{
assertTrue(Generate.match("StartsWith", "Starts*"));
assertTrue(Generate.match("EndsWith", "*With"));
assertTrue(Generate.match("EndsWith", "*sW*"));
assertTrue(Generate.match("EndsWith", "E*sW*h"));
assertFalse(Generate.match("EndsWith", "E*sW*hh"));
assertFalse(Generate.match("EndsWith", "EE*sW*h"));
assertFalse(Generate.match("EndsWith", "E*ssW*h"));
}

@Test
public void testMatchIsCaseInsensitive()
{
assertTrue(Generate.match("StartsWith", "startswith"));
assertTrue(Generate.match("EndsWith", "enDsWitH"));
}
}

0 comments on commit 6b58244

Please sign in to comment.