Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1434 added repeatable prompt for output path of OpenApi generation #1445

Merged
merged 8 commits into from
Dec 27, 2021
2 changes: 1 addition & 1 deletion cobigen-cli/cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.5.1</version>
<version>4.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,13 +56,13 @@ public class GenerateCommand extends CommandCommons {
* User input file
*/
@Parameters(index = "0", arity = "1..*", split = ",", description = MessagesConstants.INPUT_FILE_DESCRIPTION)
List<File> inputFiles = null;
List<Path> inputFiles = null;

/**
* User output project
*/
@Option(names = { "--out", "-o" }, arity = "0..1", description = MessagesConstants.OUTPUT_ROOT_PATH_DESCRIPTION)
File outputRootPath = null;
Path outputRootPath = null;

/**
* This option provides the use of multiple available increments
Expand Down Expand Up @@ -107,16 +107,14 @@ public Integer doAction() throws Exception {
if (this.increments == null && this.templates != null) {
Tuple<List<Object>, List<TemplateTo>> inputsAndArtifacts = preprocess(cg, TemplateTo.class);
for (int i = 0; i < inputsAndArtifacts.getA().size(); i++) {
generate(this.inputFiles.get(i).toPath(), inputsAndArtifacts.getA().get(i),
MavenUtil.getProjectRoot(this.inputFiles.get(i).toPath(), false), inputsAndArtifacts.getB(), cg,
TemplateTo.class);
generate(this.inputFiles.get(i), inputsAndArtifacts.getA().get(i),
MavenUtil.getProjectRoot(this.inputFiles.get(i), false), inputsAndArtifacts.getB(), cg, TemplateTo.class);
}
} else {
Tuple<List<Object>, List<IncrementTo>> inputsAndArtifacts = preprocess(cg, IncrementTo.class);
for (int i = 0; i < inputsAndArtifacts.getA().size(); i++) {
generate(this.inputFiles.get(i).toPath(), inputsAndArtifacts.getA().get(i),
MavenUtil.getProjectRoot(this.inputFiles.get(i).toPath(), false), inputsAndArtifacts.getB(), cg,
IncrementTo.class);
generate(this.inputFiles.get(i), inputsAndArtifacts.getA().get(i),
MavenUtil.getProjectRoot(this.inputFiles.get(i), false), inputsAndArtifacts.getB(), cg, IncrementTo.class);
}
}
return 0;
Expand All @@ -140,14 +138,38 @@ private <T extends GenerableArtifact> Tuple<List<Object>, List<T>> preprocess(Co
boolean firstIteration = true;
List<T> finalTos = new ArrayList<>();
List<Object> generationInputs = new ArrayList<>();
for (File inputFile : this.inputFiles) {
for (Path inputFile : this.inputFiles) {

String extension = inputFile.getName().toLowerCase();
String extension = inputFile.getFileName().toString().toLowerCase();
boolean isJavaInput = extension.endsWith(".java");
boolean isOpenApiInput = extension.endsWith(".yaml") || extension.endsWith(".yml");

// checks for OpenApi input file, output root path and project root being detectable
if (isOpenApiInput && this.outputRootPath == null && MavenUtil.getProjectRoot(inputFile, false) == null) {

LOG.info(
"No output directory was found. Please specify an output path or just press enter to generate your files to this directory: {}",
inputFile.toAbsolutePath().getParent().toString());

String userInput = getUserInput();
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved

Path preprocessedFile = Paths.get(userInput);

if (!userInput.isEmpty()) {
while (!ValidationUtils.isOutputRootPathValid(preprocessedFile)) {
Path userFolder = Paths.get(getUserInput());
preprocessedFile = preprocessInputFile(userFolder);
}
this.outputRootPath = preprocessedFile;
LOG.info("Your output directory was successfully set to: {}",
this.outputRootPath.toAbsolutePath().toString());
} else {
setOutputRootPath(inputFile.toAbsolutePath().getParent());
}
}

try {
Object input = cg.read(inputFile.toPath(), StandardCharsets.UTF_8);
Object input = cg.read(inputFile, StandardCharsets.UTF_8);
List<T> matching = (List<T>) (isIncrements ? cg.getMatchingIncrements(input) : cg.getMatchingTemplates(input));

if (matching.isEmpty()) {
Expand Down Expand Up @@ -214,22 +236,22 @@ private List<TemplateTo> toTemplateTo(List<? extends GenerableArtifact> matching
public boolean areArgumentsValid() {

int index = 0;
for (File inputFile : this.inputFiles) {
for (Path inputFile : this.inputFiles) {
inputFile = preprocessInputFile(inputFile);
// Input file can be: C:\folder\input.java
if (inputFile.exists() == false) {
if (Files.exists(inputFile) == false) {
LOG.debug("We could not find input file: {}. But we will keep trying, maybe you are using relative paths",
inputFile.getAbsolutePath());
inputFile.toAbsolutePath());

// Input file can be: folder\input.java. We should use current working directory
if (ParsingUtils.parseRelativePath(this.inputFiles, inputFile, index) == false) {
LOG.error("Your <inputFile> '{}' has not been found", inputFile.toString());
return false;
}
}
if (inputFile.isDirectory()) {
if (Files.isDirectory(inputFile)) {
LOG.error("Your input file: {} is a directory. CobiGen cannot understand that. Please use files.",
inputFile.getAbsolutePath());
inputFile.toAbsolutePath());
return false;
}
}
Expand Down Expand Up @@ -267,9 +289,8 @@ public <T extends GenerableArtifact> void generate(Path inputFile, Object input,
GenerationReportTo report = null;
LOG.info("Generating {} for input '{}, this can take a while...", isIncrements ? "increments" : "templates",
inputFile);
report = cg.generate(input, generableArtifacts, Paths.get(this.outputRootPath.getAbsolutePath()), false,
(task, progress) -> {
});
report = cg.generate(input, generableArtifacts, this.outputRootPath.toAbsolutePath(), false, (task, progress) -> {
});
ValidationUtils.checkGenerationReport(report);
Set<Path> generatedJavaFiles = report.getGeneratedFiles().stream().filter(e -> e.getFileName().endsWith(".java"))
.collect(Collectors.toSet());
Expand All @@ -294,7 +315,7 @@ private void setOutputRootPath(Path inputProject) {
"As you did not specify where the code will be generated, we will use the project of your current Input file.");
LOG.debug("Generating to: {}", inputProject);

this.outputRootPath = inputProject.toFile();
this.outputRootPath = inputProject.toAbsolutePath();
}

/**
Expand Down Expand Up @@ -529,15 +550,15 @@ public static String getUserInput() {
* @param inputFile the input file
* @return input file with processed path
*/
public static File preprocessInputFile(File inputFile) {
public static Path preprocessInputFile(Path inputFile) {

String path = inputFile.getPath();
String path = inputFile.toString();
String pattern = "[\\\"|\\'](.+)[\\\"|\\']";
boolean matches = path.matches(pattern);
if (matches) {
path = path.replace("\"", "");
path = path.replace("\'", "");
return new File(path);
return Paths.get(path);
}
jan-vcapgemini marked this conversation as resolved.
Show resolved Hide resolved
return inputFile;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

package com.devonfw.cobigen.cli.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
Expand Down Expand Up @@ -39,13 +38,13 @@ public class ParsingUtils {
* @return true only if the parsed file exists, false otherwise
*
*/
public static boolean parseRelativePath(List<File> inputFiles, File inputFile, int index) {
public static boolean parseRelativePath(List<Path> inputFiles, Path inputFile, int index) {

try {
Path inputFilePath = Paths.get(System.getProperty("user.dir"), inputFile.toString());

if (inputFilePath.toFile().exists()) {
inputFiles.set(index, inputFilePath.toFile());
inputFiles.set(index, inputFilePath);
return true;
}
} catch (InvalidPathException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.devonfw.cobigen.cli.utils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.InputMismatchException;

import org.slf4j.Logger;
Expand Down Expand Up @@ -53,10 +55,10 @@ public boolean validateFile(File inputFile) {
*
* @return true if it is a valid output root path
*/
public static boolean isOutputRootPathValid(File outputRootPath) {
public static boolean isOutputRootPathValid(Path outputRootPath) {

// As outputRootPath is an optional parameter, it means that it can be null
if (outputRootPath == null || outputRootPath.exists()) {
if (outputRootPath == null || Files.exists(outputRootPath)) {
return true;
} else {
LOG.error("Your <outputRootPath> '{}' does not exist, please use a valid path.", outputRootPath);
Expand Down Expand Up @@ -100,10 +102,10 @@ public static void checkGenerationReport(GenerationReportTo report) {
* @param isJavaInput true when input file is Java
* @param isOpenApiInput true when input file is OpenAPI
*/
public static void throwNoTriggersMatched(File inputFile, boolean isJavaInput, boolean isOpenApiInput) {
public static void throwNoTriggersMatched(Path inputFile, boolean isJavaInput, boolean isOpenApiInput) {

LOG.error("Your input file '{}' is not valid as input for any generation purpose. It does not match any trigger.",
inputFile.getName());
inputFile.getFileName());
if (isJavaInput) {
LOG.error("Check that your Java input file is following devon4j naming convention. "
+ "Explained on https://devonfw.com/website/pages/docs/devon4j.asciidoc_coding-conventions.html");
Expand Down