Skip to content

Commit

Permalink
preliminary work for supporting subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcalpin committed Feb 20, 2012
1 parent ae3ed86 commit 003f982
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Mon Nov 30 19:35:26 CET 2009
#Mon Feb 20 16:02:51 EST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
Expand Down
2 changes: 1 addition & 1 deletion app/views/scaffold/controllers/controller.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers;
package controllers${subpackage};

import java.util.List;
import ${entity.package}.${entity.name};
Expand Down
2 changes: 1 addition & 1 deletion app/views/scaffold/views/Application/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<h2>Controllers</h2>

#{list items:entities, as:'entity'}
<a href="${play.modules.scaffold.utils.Tags.openActionTag}${entity.controllerName}.index}">${entity.controllerName}</a><br/>
<a href="${play.modules.scaffold.utils.Tags.openActionTag}${entity.subpackageAndControllerName}.index}">${entity.controllerName}</a><br/>
#{/list}
Binary file modified lib/play-scaffold.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions src/play/modules/scaffold/Generate.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public class Generate {
private static final String EXCLUDE = "--exclude";
private static final String INCLUDE = "--include";
private static final String OVERWRITE = "--overwrite";
// TODO: --flatten used to be default; we intend to change this so that future
// versions of scaffold use the subpackages the model is in when generating the controllers
// and views. This work is currently unfinished...
private static final String FLATTEN_PATHS = "--flatten";
private static final String NO_FLATTEN_PATHS = "--no-flatten";
private static final String WITH_LAYOUT = "--with-layout";
private static final String WITH_LOGIN = "--with-login";
private static final String ALL = "--all";
Expand Down Expand Up @@ -92,6 +90,8 @@ public void parseArguments(String[] args) {
excludeRegEx = validateFilePatternArgs(EXCLUDE, arg.split("=")[1]);
} else if (lowerArg.equalsIgnoreCase(FLATTEN_PATHS)) {
flattenPaths = true;
} else if (lowerArg.equalsIgnoreCase(NO_FLATTEN_PATHS)) {
flattenPaths = false;
} else if (lowerArg.equalsIgnoreCase(WITH_LAYOUT)) {
includeLayout = true;
} else if (lowerArg.equalsIgnoreCase(WITH_LOGIN)) {
Expand Down
30 changes: 29 additions & 1 deletion src/play/modules/scaffold/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*/
public class Entity {
private String packageName;
private String subpackage;
private String name;
private String controllerName;
private Class<?> modelType;
Expand All @@ -54,11 +55,15 @@ public class Entity {
// form elements that we render in the view
private List<FormElement> formElements;
private ViewScaffoldingStrategy scaffoldingStrategy;

// models package name
private static final String MODELS_PKG_NAME = "models";

public Entity(Class<?> clazz) {
this.modelType = clazz;
this.name = clazz.getSimpleName();
this.packageName = Classes.getPackageName(clazz);
this.subpackage = getSubpackage();
this.name = clazz.getSimpleName();

Scaffolding scaffolding = clazz.getAnnotation(Scaffolding.class);
String controllerOverride = null;
Expand Down Expand Up @@ -129,14 +134,37 @@ public String getControllerName() {
return controllerName;
}

public String getSubpackageAndControllerName() {
if (subpackage.isEmpty())
return controllerName;
return subpackage + "." + controllerName;
}

public String getName() {
return name;
}

public String getSubpackageAndName() {
if (subpackage.isEmpty())
return name;
return subpackage + "." + name;
}

public String getPackageName() {
return packageName;
}

public String getSubpackage() {
if (subpackage != null)
return subpackage;
if (packageName.startsWith(MODELS_PKG_NAME + ".")) {
subpackage = packageName.substring(MODELS_PKG_NAME.length() + 1);
} else {
subpackage = "";
}
return subpackage;
}

public PersistenceStrategy getPersistenceStrategy() {
return persistenceStrategy;
}
Expand Down
21 changes: 8 additions & 13 deletions src/play/modules/scaffold/generator/ScaffoldingGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ private void generate(TargetFileType type, String sourceFolderName, String sourc

private void generateForEntity(Entity entity, TargetFileType type, String templateFolderPath,
String templateFileName, String targetName) {
/*
if (!isFlattenPaths() && entity.getSubpackage() != null) {
String subpackage = entity.getSubpackage();
subpackage = subpackage.replaceAll("\\.", "/");
targetName = subpackage + "/" + targetName;
if (!subpackage.isEmpty()) {
subpackage = subpackage.replaceAll("\\.", "/");
targetName = subpackage + "/" + targetName;
}
}
*/
String[] paths = getPaths(type, templateFolderPath, templateFileName, targetName);
Map<String, Object> templateArgs = new HashMap<String, Object>();
templateArgs.put("entity", entity);
//templateArgs.put("subpackage", isFlattenPaths() ? "" : entity.getSubpackage() != null ? "." + entity.getSubpackage() : "");
templateArgs.put("subpackage", isFlattenPaths() ? "" : entity.getSubpackage().isEmpty() ? "" : "." + entity.getSubpackage());
invokeTemplate(type, paths[0], paths[1], templateArgs);
}

Expand All @@ -229,13 +229,8 @@ private String[] getPaths(TargetFileType type, String templateFolderPath, String
String additionalPath = type.getPath();
if (additionalPath.length() > 0)
targetPath = targetPath + additionalPath + '/';
ensureDirectoryExists(targetPath);
if (targetFileName.contains("/")) {
String[] fileSplit = targetFileName.split("/");
if (fileSplit.length >= 2) {
ensureDirectoryExists(targetPath + '/' + Strings.join(Arrays.copyOfRange(fileSplit, 0, fileSplit.length-1), '/'));
}
}
String pathToTargetFile = targetFileName.contains("/") ? targetFileName.substring(0, targetFileName.lastIndexOf('/')) : "";
ensureDirectoryExists(targetPath + pathToTargetFile);
String targetFile = targetPath + targetFileName + type.getTargetSuffix();
return new String[] { templateFile, targetFile };
}
Expand Down Expand Up @@ -319,7 +314,7 @@ private void generateViewsForEntity(Entity entity) {
private void ensureDirectoryExists(String templatePath) {
File viewPathDirectory = new File(templatePath);
if (!viewPathDirectory.exists()) {
viewPathDirectory.mkdir();
viewPathDirectory.mkdirs();
}
}

Expand Down

0 comments on commit 003f982

Please sign in to comment.