Skip to content

Commit

Permalink
print help on error task usage (#76)
Browse files Browse the repository at this point in the history
* print help on error task usage

* add unit test

* retrigger checks
  • Loading branch information
juancgalvis authored Jun 30, 2020
1 parent b19b205 commit b3b263c
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To use the plugin you need Gradle version 5 or later, to start add the following

```groovy
plugins {
id "co.com.bancolombia.cleanArchitecture" version "1.6.4"
id "co.com.bancolombia.cleanArchitecture" version "1.6.5"
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
package=co.com.bancolombia
systemProp.version=1.6.4
systemProp.version=1.6.5
2 changes: 1 addition & 1 deletion src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Constants {
public static final String SECRETS_VERSION = "2.1.0";
public static final String RCOMMONS_ASYNC_COMMONS_STARTER_VERSION = "0.4.7";
public static final String RCOMMONS_OBJECT_MAPPER_VERSION = "0.1.0";
public static final String PLUGIN_VERSION = "1.6.4";
public static final String PLUGIN_VERSION = "1.6.5";

public enum BooleanOption {
TRUE, FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package co.com.bancolombia.task;

import co.com.bancolombia.factory.ModuleBuilder;
import org.gradle.api.DefaultTask;
import org.gradle.api.Task;
import org.gradle.api.internal.tasks.options.OptionReader;
import org.gradle.api.logging.Logger;
import org.gradle.configuration.TaskDetailPrinter;
import org.gradle.execution.TaskSelector;
import org.gradle.internal.logging.text.StyledTextOutput;
import org.gradle.internal.logging.text.StyledTextOutputFactory;

import javax.inject.Inject;

public class CleanArchitectureDefaultTask extends DefaultTask {
protected final ModuleBuilder builder = new ModuleBuilder(getProject());
protected final Logger logger = getProject().getLogger();

protected void printHelp() {
StyledTextOutput output = this.getTextOutputFactory().create(CleanArchitectureDefaultTask.class);
final Task task = this;
TaskSelector.TaskSelection selection = new TaskSelector.TaskSelection(getPath(), getName(),
collection -> collection.add(task));
OptionReader optionReader = this.getOptionReader();
TaskDetailPrinter taskDetailPrinter = new TaskDetailPrinter(getName(), selection, optionReader);
taskDetailPrinter.print(output);
}

@Inject
protected StyledTextOutputFactory getTextOutputFactory() {
throw new UnsupportedOperationException();
}

@Inject
protected OptionReader getOptionReader() {
throw new UnsupportedOperationException();
}
}
7 changes: 2 additions & 5 deletions src/main/java/co/com/bancolombia/task/DeleteModuleTask.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package co.com.bancolombia.task;

import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
Expand All @@ -11,9 +9,7 @@
import java.util.ArrayList;
import java.util.List;

public class DeleteModuleTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());

public class DeleteModuleTask extends CleanArchitectureDefaultTask {
private String module;

@Option(option = "module", description = "Set module name to delete")
Expand All @@ -29,6 +25,7 @@ public List<String> getModules() {
@TaskAction
public void deleteModule() throws IOException {
if (module == null || !getProject().getChildProjects().containsKey(module)) {
printHelp();
throw new IllegalArgumentException("No valid module name is set, usage: gradle deleteModule --module "
+ Utils.formatTaskOptions(getModules()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import co.com.bancolombia.Constants.BooleanOption;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.adapters.ModuleFactoryDrivenAdapter;
import co.com.bancolombia.factory.adapters.ModuleFactoryDrivenAdapter.DrivenAdapterType;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
Expand All @@ -18,10 +15,7 @@
import java.util.Arrays;
import java.util.List;

public class GenerateDrivenAdapterTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();

public class GenerateDrivenAdapterTask extends CleanArchitectureDefaultTask {
private DrivenAdapterType type;
private String name;
private BooleanOption secret = BooleanOption.FALSE;
Expand Down Expand Up @@ -54,6 +48,7 @@ public List<BooleanOption> getSecretOptions() {
@TaskAction
public void generateDrivenAdapterTask() throws IOException, CleanException {
if (type == null) {
printHelp();
throw new IllegalArgumentException("No Driven Adapter type is set, usage: gradle generateDrivenAdapter " +
"--type " + Utils.formatTaskOptions(getTypes()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package co.com.bancolombia.task;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.entrypoints.ModuleFactoryEntryPoint;
import co.com.bancolombia.factory.entrypoints.ModuleFactoryEntryPoint.EntryPointType;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
Expand All @@ -17,10 +14,7 @@
import java.util.Arrays;
import java.util.List;

public class GenerateEntryPointTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();

public class GenerateEntryPointTask extends CleanArchitectureDefaultTask {
private EntryPointType type;
private String name;

Expand All @@ -42,6 +36,7 @@ public List<EntryPointType> getTypes() {
@TaskAction
public void generateEntryPointTask() throws IOException, CleanException {
if (type == null) {
printHelp();
throw new IllegalArgumentException("No Entry Point is set, usage: gradle generateEntryPoint --type "
+ Utils.formatTaskOptions(getTypes()));
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/co/com/bancolombia/task/GenerateModelTask.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package co.com.bancolombia.task;

import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import java.io.IOException;

public class GenerateModelTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();

public class GenerateModelTask extends CleanArchitectureDefaultTask {
private String name = "";

@Option(option = "name", description = "Set the model name")
Expand All @@ -24,6 +18,7 @@ public void setName(String modelName) {
@TaskAction
public void generateModelTask() throws IOException, ParamNotFoundException {
if (name.isEmpty()) {
printHelp();
throw new IllegalArgumentException("No model name, usage: gradle generateModel --name [name]");
}
name = Utils.capitalize(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package co.com.bancolombia.task;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.pipelines.ModuleFactoryPipeline;
import co.com.bancolombia.factory.pipelines.ModuleFactoryPipeline.PipelineType;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
Expand All @@ -17,10 +14,7 @@
import java.util.Arrays;
import java.util.List;

public class GeneratePipelineTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();

public class GeneratePipelineTask extends CleanArchitectureDefaultTask {
private PipelineType type;

@Option(option = "type", description = "Set type of pipeline to be generated")
Expand All @@ -36,6 +30,7 @@ public List<PipelineType> getTypes() {
@TaskAction
public void generatePipelineTask() throws IOException, CleanException {
if (type == null) {
printHelp();
throw new IllegalArgumentException("No Pipeline type was set, usage: gradle generatePipeline --type "
+ Utils.formatTaskOptions(getTypes()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package co.com.bancolombia.task;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.gradle.api.tasks.options.OptionValues;
Expand All @@ -14,10 +11,7 @@
import java.util.Arrays;
import java.util.List;

public class GenerateStructureTask extends DefaultTask {
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();

public class GenerateStructureTask extends CleanArchitectureDefaultTask {
private String packageName = "co.com.bancolombia";
private ProjectType type = ProjectType.IMPERATIVE;
private CoveragePlugin coverage = CoveragePlugin.JACOCO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package co.com.bancolombia.task;

import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.utils.Utils;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import java.io.IOException;

public class GenerateUseCaseTask extends DefaultTask {
public class GenerateUseCaseTask extends CleanArchitectureDefaultTask {
private static final String USECASE_CLASS_NAME = "UseCase";
private final ModuleBuilder builder = new ModuleBuilder(getProject());
private final Logger logger = getProject().getLogger();
private String name = "";

@Option(option = "name", description = "Set UseCase name")
Expand All @@ -24,6 +19,7 @@ public void setName(String useCaseName) {
@TaskAction
public void generateUseCaseTask() throws IOException, ParamNotFoundException {
if (name.isEmpty()) {
printHelp();
throw new IllegalArgumentException(
"No use case name, usage: gradle generateUseCase --name [name]");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package co.com.bancolombia.task;

import org.gradle.api.Project;
import org.gradle.api.internal.tasks.options.OptionDescriptor;
import org.gradle.api.internal.tasks.options.OptionReader;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.List;

import static org.junit.Assert.*;

public class CleanArchitectureDefaultTaskTest {
private Project project;

@Before
public void setup() {
project = ProjectBuilder.builder()
.withName("cleanArchitecture")
.withProjectDir(new File("build/unitTest"))
.build();

project.getTasks().create("dm", DeleteModuleTask.class);
project.getTasks().create("cadt", CleanArchitectureDefaultTask.class);
}

@Test
public void shouldGetTaskDescriptor() {
// Arrange
CleanArchitectureDefaultTask task = (CleanArchitectureDefaultTask) project.getTasks().getByName("cadt");
// Act
OptionReader reader = task.getOptionReader();
// Assert
assertTrue(reader.getOptions(task).isEmpty());
}

@Test
public void shouldGetTaskDescriptorWithOptions() {
// Arrange
CleanArchitectureDefaultTask task = (CleanArchitectureDefaultTask) project.getTasks().getByName("dm");
// Act
OptionReader reader = task.getOptionReader();
// Assert
List<OptionDescriptor> list = reader.getOptions(task);
assertEquals(1, list.size());
assertEquals("module", list.get(0).getName());
}

@Test
public void shouldGetTextOutputFactory() {
// Arrange
CleanArchitectureDefaultTask task = (CleanArchitectureDefaultTask) project.getTasks().getByName("dm");
// Act
StyledTextOutputFactory factory = task.getTextOutputFactory();
// Assert
assertNotNull(factory);
}

@Test
public void shouldPrintHelp() {
// Arrange
CleanArchitectureDefaultTask task = (CleanArchitectureDefaultTask) project.getTasks().getByName("dm");
// Act
task.printHelp();
// Assert
assertNotNull(task.getTextOutputFactory());
}
}

0 comments on commit b3b263c

Please sign in to comment.