Skip to content

Commit

Permalink
Implements #52 : command line enhancements for project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Nov 16, 2024
1 parent 6c2a9ac commit 66f7d18
Show file tree
Hide file tree
Showing 21 changed files with 1,421 additions and 1,326 deletions.
8 changes: 4 additions & 4 deletions src/main/java/rife/bld/blueprints/AppProjectBlueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
* @since 1.9
*/
public class AppProjectBlueprint extends Project {
public AppProjectBlueprint(File work, String packageName, String projectName) {
this(work, packageName, projectName, new VersionNumber(0,0,1));
public AppProjectBlueprint(File work, String packageName, String projectName, String baseName) {
this(work, packageName, projectName, baseName, new VersionNumber(0,0,1));
}

public AppProjectBlueprint(File work, String packageName, String projectName, VersionNumber versionNumber) {
public AppProjectBlueprint(File work, String packageName, String projectName, String baseName, VersionNumber versionNumber) {
workDirectory = work;

pkg = packageName;
name = projectName;
mainClass = packageName + "." + StringUtils.capitalize(projectName) + "Main";
mainClass = packageName + "." + baseName + "Main";
version = versionNumber;

downloadSources = true;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/rife/bld/blueprints/BaseProjectBlueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
* @since 1.5.20
*/
public class BaseProjectBlueprint extends Project {
public BaseProjectBlueprint(File work, String packageName, String projectName) {
this(work, packageName, projectName, new VersionNumber(0,0,1));
public BaseProjectBlueprint(File work, String packageName, String projectName, String baseName) {
this(work, packageName, projectName, baseName, new VersionNumber(0,0,1));
}

public BaseProjectBlueprint(File work, String packageName, String projectName, VersionNumber versionNumber) {
public BaseProjectBlueprint(File work, String packageName, String projectName, String baseName, VersionNumber versionNumber) {
workDirectory = work;

pkg = packageName;
name = projectName;
mainClass = packageName + "." + StringUtils.capitalize(projectName) + "Main";
mainClass = packageName + "." + baseName + "Main";
version = versionNumber;

downloadSources = true;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/rife/bld/blueprints/LibProjectBlueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
* @since 1.6
*/
public class LibProjectBlueprint extends Project {
public LibProjectBlueprint(File work, String packageName, String projectName) {
this(work, packageName, projectName, new VersionNumber(0,0,1));
public LibProjectBlueprint(File work, String packageName, String projectName, String baseName) {
this(work, packageName, projectName, baseName, new VersionNumber(0,0,1));
}

public LibProjectBlueprint(File work, String packageName, String projectName, VersionNumber versionNumber) {
public LibProjectBlueprint(File work, String packageName, String projectName, String baseName, VersionNumber versionNumber) {
workDirectory = work;

pkg = packageName;
name = projectName;
mainClass = packageName + "." + StringUtils.capitalize(projectName) + "Lib";
mainClass = packageName + "." + baseName + "Lib";
version = versionNumber;

downloadSources = true;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/rife/bld/blueprints/Rife2ProjectBlueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
* @since 1.5
*/
public class Rife2ProjectBlueprint extends WebProject {
public Rife2ProjectBlueprint(File work, String packageName, String projectName) {
this(work, packageName, projectName, new VersionNumber(0,0,1));
public Rife2ProjectBlueprint(File work, String packageName, String projectName, String baseName) {
this(work, packageName, projectName, baseName, new VersionNumber(0,0,1));
}

public Rife2ProjectBlueprint(File work, String packageName, String projectName, VersionNumber versionNumber) {
public Rife2ProjectBlueprint(File work, String packageName, String projectName, String baseName, VersionNumber versionNumber) {
workDirectory = work;

pkg = packageName;
name = projectName;
mainClass = packageName + "." + StringUtils.capitalize(projectName) + "Site";
mainClass = packageName + "." + baseName + "Site";
uberJarMainClass = mainClass + "Uber";
version = versionNumber;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/rife/bld/help/CreateAppHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public String getSummary() {
public String getDescription(String topic) {
return StringUtils.replace("""
Creates a new Java application project.
Usage : ${topic} <package> <name>
Usage : ${topic} <package> <name> <base>
package The package of the project to create
name The name of the project to create""", "${topic}", topic);
name The name of the project to create
base The base name for generated project classes""", "${topic}", topic);
}
}
7 changes: 4 additions & 3 deletions src/main/java/rife/bld/help/CreateBaseHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public String getSummary() {
public String getDescription(String topic) {
return StringUtils.replace("""
Creates a new Java baseline project.
Usage : ${topic} <package> <name>
Usage : ${topic} <package> <name> <base>
package The package of the project to create
name The name of the project to create""", "${topic}", topic);
name The name of the project to create
base The base name for generated project classes""", "${topic}", topic);
}
}
7 changes: 4 additions & 3 deletions src/main/java/rife/bld/help/CreateHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public String getSummary() {
public String getDescription(String topic) {
return StringUtils.replace("""
Creates a new project from multiple choice.
Usage : ${topic} <type> <package> <name>
Usage : ${topic} <type> <package> <name> <base>
type The type of project to create (app, base, lib, rife2)
package The package of the project to create
name The name of the project to create""", "${topic}", topic);
name The name of the project to create
base The base name for generated project classes""", "${topic}", topic);
}
}
7 changes: 4 additions & 3 deletions src/main/java/rife/bld/help/CreateLibHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public String getSummary() {
public String getDescription(String topic) {
return StringUtils.replace("""
Creates a new Java library project.
Usage : ${topic} <package> <name>
Usage : ${topic} <package> <name> <base>
package The package of the project to create
name The name of the project to create""", "${topic}", topic);
name The name of the project to create
base The base name for generated project classes""", "${topic}", topic);
}
}
7 changes: 4 additions & 3 deletions src/main/java/rife/bld/help/CreateRife2Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public String getSummary() {
public String getDescription(String topic) {
return StringUtils.replace("""
Creates a new RIFE2 web application project.
Usage : ${topic} <package> <name>
Usage : ${topic} <package> <name> <base>
package The package of the project to create
name The name of the project to create""", "${topic}", topic);
name The name of the project to create
base The base name for generated project classes""", "${topic}", topic);
}
}
89 changes: 76 additions & 13 deletions src/main/java/rife/bld/operations/AbstractCreateOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
* @since 1.5
*/
public abstract class AbstractCreateOperation<T extends AbstractCreateOperation<T, P>, P extends Project> extends AbstractOperation<AbstractCreateOperation<T, P>> {
private String packageName_;
private String projectName_;
private String baseName_;

final String templateBase_;

File workDirectory_ = new File(System.getProperty("user.dir"));
String packageName_;
String projectName_;
boolean downloadDependencies_;

P project_;
Expand Down Expand Up @@ -98,10 +100,11 @@ protected void executeConfigure() {

// standard names
projectClassName_ = StringUtils.capitalize(project_.name());
projectBuildName_ = projectBuildClassName(projectClassName_);
projectMainName_ = projectMainClassName(projectClassName_);
projectMainUberName_ = projectMainUberClassName(projectClassName_);
projectTestName_ = projectTestClassName(projectClassName_);
var base_name = baseName();
projectBuildName_ = projectBuildClassName(base_name);
projectMainName_ = projectMainClassName(base_name);
projectMainUberName_ = projectMainUberClassName(base_name);
projectTestName_ = projectTestClassName(base_name);

// create the main project structure
ideaDirectory_ = new File(project_.workDirectory(), ".idea");
Expand Down Expand Up @@ -375,14 +378,18 @@ protected void executeDownloadDependencies() {
public T fromArguments(List<String> arguments) {
String package_name = null;
String project_name = null;
String base_name = null;
if (!arguments.isEmpty()) {
package_name = arguments.remove(0);
}
if (!arguments.isEmpty()) {
project_name = arguments.remove(0);
}
if ((package_name == null || project_name == null) && System.console() == null) {
throw new OperationOptionException("ERROR: Expecting the package and project names as the arguments.");
if (!arguments.isEmpty()) {
base_name = arguments.remove(0);
}
if ((package_name == null || project_name == null || base_name == null) && System.console() == null) {
throw new OperationOptionException("ERROR: Expecting the package, project and base names as the arguments.");
}

if (package_name == null || package_name.isEmpty()) {
Expand All @@ -391,16 +398,30 @@ public T fromArguments(List<String> arguments) {
} else {
System.out.println("Using package name: " + package_name);
}

if (project_name == null || project_name.isEmpty()) {
System.out.println("Please enter a project name (for instance: myapp):");
System.out.println("Please enter a project name (for instance: my-app):");
project_name = System.console().readLine();
} else {
System.out.println("Using project name: " + project_name);
}

if (base_name == null || base_name.isEmpty()) {
var default_base_name = generateBaseName(project_name);
System.out.println("Please enter the base name for generated project classes (default: " + default_base_name + "):");
base_name = System.console().readLine();
if (base_name == null || base_name.trim().isEmpty()) {
base_name = default_base_name;
System.out.println("Using base name: " + base_name);
}
} else {
System.out.println("Using base name: " + base_name);
}

return workDirectory(new File(System.getProperty("user.dir")))
.packageName(package_name)
.projectName(project_name)
.baseName(base_name)
.downloadDependencies(true);
}

Expand Down Expand Up @@ -445,7 +466,6 @@ public T packageName(String name) {
throw new OperationOptionException("ERROR: The package name is invalid.");
}

packageName_ = name;
return (T) this;
}

Expand All @@ -462,10 +482,26 @@ public T projectName(String name) {
throw new OperationOptionException("ERROR: The project name should not be blank.");
}

if (!ValidityChecks.checkJavaIdentifier(projectName_)) {
throw new OperationOptionException("ERROR: The project name is invalid.");
return (T) this;
}

/**
* Provides the base name for the project classes to generate.
*
* @param name the base name
* @return this operation instance
* @since 2.2
*/
public T baseName(String name) {
baseName_ = StringUtils.trim(name);
if (baseName_.isEmpty()) {
throw new OperationOptionException("ERROR: The base name should not be blank.");
}

if (!ValidityChecks.checkJavaIdentifier(baseName_)) {
throw new OperationOptionException("ERROR: The base name is invalid.");
}
projectName_ = name;

return (T) this;
}

Expand Down Expand Up @@ -513,6 +549,33 @@ public String projectName() {
return projectName_;
}

static String generateBaseName(String projectName) {
if (projectName != null) {
var base_name = projectName.trim();
base_name = StringUtils.filterAsIdentifier(base_name);
base_name = StringUtils.capitalize(base_name);
return base_name;
}

return null;
}

/**
* Retrieves the base name for the project classes to generate.
* <p>
* If no base name was provided, one will be generated from the project name.
*
* @return the base name
* @since 2.2
*/
public String baseName() {
if (baseName_ == null || baseName_.isEmpty()) {
return generateBaseName(projectName());
}

return baseName_;
}

/**
* Retrieves whether dependencies will be downloaded at project creation.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rife/bld/operations/CreateAppOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public CreateAppOperation() {
}

protected Project createProjectBlueprint() {
return new AppProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName());
return new AppProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName(), baseName());
}
}
2 changes: 1 addition & 1 deletion src/main/java/rife/bld/operations/CreateBaseOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public CreateBaseOperation() {
}

protected Project createProjectBlueprint() {
return new BaseProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName());
return new BaseProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName(), baseName());
}
}
2 changes: 1 addition & 1 deletion src/main/java/rife/bld/operations/CreateLibOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CreateLibOperation() {
}

protected Project createProjectBlueprint() {
return new LibProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName());
return new LibProjectBlueprint(new File(workDirectory(), projectName()), packageName(), projectName(), baseName());
}

protected String projectMainClassName(String projectClassName) {
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/rife/bld/operations/CreateOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CreateOperation {
String type = null;
String package_name = null;
String project_name = null;
String base_name = null;
if (!arguments.isEmpty()) {
type = arguments.remove(0);
}
Expand All @@ -42,8 +43,11 @@ public class CreateOperation {
if (!arguments.isEmpty()) {
project_name = arguments.remove(0);
}
if ((type == null || package_name == null || project_name == null) && System.console() == null) {
throw new OperationOptionException("ERROR: Expecting the type, package and project names as the arguments.");
if (!arguments.isEmpty()) {
base_name = arguments.remove(0);
}
if ((package_name == null || project_name == null || base_name == null) && System.console() == null) {
throw new OperationOptionException("ERROR: Expecting the package, project and base names as the arguments.");
}

if (type == null || type.isEmpty()) {
Expand Down Expand Up @@ -87,21 +91,34 @@ public class CreateOperation {
if (project_name == null || project_name.isEmpty()) {
String name_example;
if (LIB.equals(type)) {
name_example = "mylib";
name_example = "my-lib";
} else if (RIFE2.equals(type)) {
name_example = "mywebapp";
name_example = "my-webapp";
} else {
name_example = "myapp";
name_example = "my-app";
}
System.out.printf("Please enter a project name (for instance: %s):%n", name_example);
System.out.println("Please enter a project name (for instance: " + name_example + ")");
project_name = System.console().readLine();
} else {
System.out.println("Using project name: " + project_name);
}

if (base_name == null || base_name.isEmpty()) {
var default_base_name = AbstractCreateOperation.generateBaseName(project_name);
System.out.println("Please enter the base name for generated project classes (default: " + default_base_name + "):");
base_name = System.console().readLine();
if (base_name == null || base_name.trim().isEmpty()) {
base_name = default_base_name;
System.out.println("Using base name: " + base_name);
}
} else {
System.out.println("Using base name: " + base_name);
}

return create_operation.workDirectory(new File(System.getProperty("user.dir")))
.packageName(package_name)
.projectName(project_name)
.baseName(base_name)
.downloadDependencies(true);
}
}
Loading

0 comments on commit 66f7d18

Please sign in to comment.