From 4f176847c6f93afc624dd36df96d07bc95e4b820 Mon Sep 17 00:00:00 2001 From: Geert Bevin Date: Thu, 11 May 2023 14:47:36 -0400 Subject: [PATCH] Renamed the 'create' command to 'create-rife2' and created a new 'create' command that selects the type of project. --- src/main/java/rife/bld/Cli.java | 19 +++- src/main/java/rife/bld/help/CreateHelp.java | 30 ++++++ .../java/rife/bld/help/CreateRife2Help.java | 2 +- .../rife/bld/operations/CreateOperation.java | 93 +++++++++++++++++++ 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 src/main/java/rife/bld/help/CreateHelp.java create mode 100644 src/main/java/rife/bld/operations/CreateOperation.java diff --git a/src/main/java/rife/bld/Cli.java b/src/main/java/rife/bld/Cli.java index 7a64984..c16c9f0 100644 --- a/src/main/java/rife/bld/Cli.java +++ b/src/main/java/rife/bld/Cli.java @@ -15,6 +15,7 @@ import rife.bld.operations.*; * @since 1.5 */ public class Cli extends BuildExecutor { + private final CreateOperation createOperation_ = new CreateOperation(); private final CreateBaseOperation createBaseOperation_ = new CreateBaseOperation(); private final CreateBlankOperation createBlankOperation_ = new CreateBlankOperation(); private final CreateLibOperation createLibOperation_ = new CreateLibOperation(); @@ -26,12 +27,12 @@ public class Cli extends BuildExecutor { * The standard {@code create} command. * * @throws Exception when an error occurred during the creation process - * @since 1.5 + * @since 1.7 */ - @BuildCommand(help = CreateRife2Help.class) + @BuildCommand(help = CreateHelp.class) public void create() throws Exception { - createRife2Operation_.executeOnce(() -> createRife2Operation_.fromArguments(arguments())); + createOperation_.fromArguments(arguments()).execute(); } /** @@ -70,6 +71,18 @@ public class Cli extends BuildExecutor { createLibOperation_.executeOnce(() -> createLibOperation_.fromArguments(arguments())); } + /** + * The standard {@code create-rife2} command. + * + * @throws Exception when an error occurred during the creation process + * @since 1.5 + */ + @BuildCommand(value = "create-rife2", help = CreateRife2Help.class) + public void createRife2() + throws Exception { + createRife2Operation_.executeOnce(() -> createRife2Operation_.fromArguments(arguments())); + } + /** * The standard {@code upgrade} command. * diff --git a/src/main/java/rife/bld/help/CreateHelp.java b/src/main/java/rife/bld/help/CreateHelp.java new file mode 100644 index 0000000..939f30a --- /dev/null +++ b/src/main/java/rife/bld/help/CreateHelp.java @@ -0,0 +1,30 @@ +/* + * Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com) + * Licensed under the Apache License, Version 2.0 (the "License") + */ +package rife.bld.help; + +import rife.bld.CommandHelp; +import rife.tools.StringUtils; + +/** + * Provides help for the create command. + * + * @author Geert Bevin (gbevin[remove] at uwyn dot com) + * @since 1.7 + */ +public class CreateHelp implements CommandHelp { + public String getSummary() { + return "Creates a new project"; + } + + public String getDescription(String topic) { + return StringUtils.replace(""" + Creates a new project. + + Usage : ${topic} + type The type of project to create (base, blank, lib, rife2) + package The package of the project to create + name The name of the project to create""", "${topic}", topic); + } +} diff --git a/src/main/java/rife/bld/help/CreateRife2Help.java b/src/main/java/rife/bld/help/CreateRife2Help.java index e9eb5b8..7c29014 100644 --- a/src/main/java/rife/bld/help/CreateRife2Help.java +++ b/src/main/java/rife/bld/help/CreateRife2Help.java @@ -8,7 +8,7 @@ import rife.bld.CommandHelp; import rife.tools.StringUtils; /** - * Provides help for the create command. + * Provides help for the create-rife2 command. * * @author Geert Bevin (gbevin[remove] at uwyn dot com) * @since 1.5 diff --git a/src/main/java/rife/bld/operations/CreateOperation.java b/src/main/java/rife/bld/operations/CreateOperation.java new file mode 100644 index 0000000..066cf57 --- /dev/null +++ b/src/main/java/rife/bld/operations/CreateOperation.java @@ -0,0 +1,93 @@ +/* + * Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com) + * Licensed under the Apache License, Version 2.0 (the "License") + */ +package rife.bld.operations; + +import rife.bld.operations.exceptions.OperationOptionException; + +import java.io.File; +import java.util.List; + +/** + * Creates a new project structure + * + * @author Geert Bevin (gbevin[remove] at uwyn dot com) + * @since 1.7 + */ +public class CreateOperation { + /** + * Configures a creation operation from command-line arguments. + * + * @param arguments the arguments that will be considered + * @return this operation instance + * @since 1.7 + */ + public AbstractCreateOperation fromArguments(List arguments) { + String type = null; + String package_name = null; + String project_name = null; + if (arguments.size() > 0) { + type = arguments.remove(0); + } + if (arguments.size() > 0) { + package_name = arguments.remove(0); + } + if (arguments.size() > 0) { + 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 (type == null || type.isEmpty()) { + System.out.println("Please enter a number for the project type:"); + System.out.println(" 1: base"); + System.out.println(" 2: blank"); + System.out.println(" 3: lib"); + System.out.println(" 4: rife2"); + var number = System.console().readLine(); + switch (Integer.parseInt(number)) { + case 1 -> type = "base"; + case 2 -> type = "blank"; + case 3 -> type = "lib"; + case 4 -> type = "rife2"; + } + } else { + System.out.println("Using project type: " + type); + } + if (type == null) { + throw new OperationOptionException("ERROR: Expecting the project type."); + } + + AbstractCreateOperation create_operation = null; + switch (type) { + case "base" -> create_operation = new CreateBaseOperation(); + case "blank" -> create_operation = new CreateBlankOperation(); + case "lib" -> create_operation = new CreateLibOperation(); + case "rife2" -> create_operation = new CreateRife2Operation(); + } + if (create_operation == null) { + throw new OperationOptionException("ERROR: Unsupported project type."); + } + + if (package_name == null || package_name.isEmpty()) { + System.out.println("Please enter a package name (for instance: com.example):"); + package_name = System.console().readLine(); + } 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):"); + project_name = System.console().readLine(); + } else { + System.out.println("Using project name: " + project_name); + } + + return create_operation.workDirectory(new File(System.getProperty("user.dir"))) + .packageName(package_name) + .projectName(project_name) + .downloadDependencies(true); + } +}