Skip to content

Commit

Permalink
#1303 Merge branch 'dev_cli'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 17, 2020
2 parents 2d28735 + 397b255 commit 6b2bb62
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 533 deletions.
2 changes: 1 addition & 1 deletion cobigen-cli/class-loader-agent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>class-loader-agent</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
<name>Class loader agent</name>
<description>Class loader agent for CobiGen CLI</description>

Expand Down
21 changes: 9 additions & 12 deletions cobigen-cli/cli/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cli</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
<name>cobigen-cli</name>
<description>Command Line Interface for CobiGen</description>

Expand All @@ -24,17 +24,22 @@
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-api</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-test</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.0.0-beta-1b</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
Expand All @@ -58,13 +63,6 @@
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
Expand Down Expand Up @@ -182,7 +180,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-javaagent:${project.build.directory}/classes/class-loader-agent.jar</argLine>
<!-- Needed for JDK 9+ -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
Expand All @@ -15,11 +16,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.api.util.CobiGenPathUtil;
import com.devonfw.cobigen.cli.CobiGenCLI;
import com.devonfw.cobigen.cli.constants.MessagesConstants;
import com.devonfw.cobigen.cli.logger.CLILogger;
import com.devonfw.cobigen.cli.utils.CobiGenUtils;
import com.devonfw.cobigen.cli.utils.ConfigurationUtils;
import com.devonfw.cobigen.impl.util.ConfigurationUtil;
import com.devonfw.cobigen.impl.util.TemplatesJarUtil;

import ch.qos.logback.classic.Level;
import picocli.CommandLine.Command;
Expand All @@ -45,13 +49,6 @@ public class AdaptTemplatesCommand implements Callable<Integer> {
@Option(names = { "--verbose", "-v" }, negatable = true, description = MessagesConstants.VERBOSE_OPTION_DESCRIPTION)
private boolean verbose;

/**
* If this option is provided, we will unpack the templates jar at the given location
*/
@Option(names = { "--custom-location", "-cl" }, arity = "0..1",
description = MessagesConstants.CUSTOM_LOCATION_OPTION_DESCRIPTION)
private Path customTemplatesLocation = null;

/**
* Logger to output useful information to the user
*/
Expand All @@ -67,15 +64,15 @@ public class AdaptTemplatesCommand implements Callable<Integer> {
* if no destination path could be set
*/
public static void processJar(Path destinationPath) throws IOException {

Path sourcesJarPath = cobigenUtils.getTemplatesJar(true);
Path classesJarPath = cobigenUtils.getTemplatesJar(false);

if (destinationPath == null) {
throw new IOException("Cobigen folder path not found!");
}

Path templatesFolderPath = destinationPath.resolve(ConfigurationUtils.COBIGEN_TEMPLATES);
File destinationFile = destinationPath.toFile();
Path sourcesJarPath = TemplatesJarUtil.getJarFile(true, destinationFile).toPath();
Path classesJarPath = TemplatesJarUtil.getJarFile(false, destinationFile).toPath();

Path templatesFolderPath = destinationPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);

if (templatesFolderPath == null) {
throw new IOException("Cobigen templates folder path not found!");
Expand All @@ -86,7 +83,7 @@ public static void processJar(Path destinationPath) throws IOException {
extractArchive(sourcesJarPath, templatesFolderPath);
deleteDirectoryRecursively(templatesFolderPath.resolve("META-INF"));

extractArchive(classesJarPath, templatesFolderPath.resolve(ConfigurationUtils.COBIGEN_UTILITY_CLASSES_FOLDER));
extractArchive(classesJarPath, templatesFolderPath.resolve("target/classes"));
Files.deleteIfExists(templatesFolderPath.resolve("pom.xml"));
// If we are unzipping a sources jar, we need to get the pom.xml from the normal jar
Files.copy(templatesFolderPath.resolve("target/classes/pom.xml"), templatesFolderPath.resolve("pom.xml"));
Expand Down Expand Up @@ -154,33 +151,20 @@ public Integer call() throws Exception {
CLILogger.setLevel(Level.DEBUG);
}

Path cobigenTemplatesDirectory = null;
if (customTemplatesLocation != null) {
URI templatesLocationUri = ConfigurationUtil.findTemplatesLocation();

if (templatesLocationUri == null) {
LOG.info(
"CobiGen is attempting to download the latest CobiGen_Templates.jar and will extract it to a target directory of your choice. please wait...");
ConfigurationUtils.createConfigFile(customTemplatesLocation);
// sets custom templates directory path from configuration file property
cobigenTemplatesDirectory = ConfigurationUtils.getCustomTemplatesLocation();
LOG.info("Creating custom templates folder at custom location @ {}", cobigenTemplatesDirectory);
} else {
LOG.info(
"CobiGen is attempting to download the latest CobiGen_Templates.jar and will extract it to a default target directory. please wait...");
// sets default templates directory path from CLI location
cobigenTemplatesDirectory = ConfigurationUtils.getCobigenCliRootPath();
ConfigurationUtils.createConfigFile(cobigenTemplatesDirectory);
LOG.info("Creating custom templates folder next to the CLI @ {}", cobigenTemplatesDirectory);
}

if (Files.exists(cobigenTemplatesDirectory)) {
processJar(cobigenTemplatesDirectory);
} else {
LOG.error("Could not find target directory to extract templates @ {}", cobigenTemplatesDirectory);
return 1;
"CobiGen is attempting to download the latest CobiGen_Templates.jar and will extract it to cobigen home directory {}. please wait...",
ConfigurationConstants.COBIGEN_HOME_FOLDER);
File templatesDirectory = CobiGenPathUtil.getTemplatesFolderPath().toFile();
TemplatesJarUtil.downloadLatestDevon4jTemplates(true, templatesDirectory);
TemplatesJarUtil.downloadLatestDevon4jTemplates(false, templatesDirectory);
processJar(templatesDirectory.toPath());
LOG.info("Successfully downloaded and extracted templates to @ {}",
templatesDirectory.toPath().resolve(ConfigurationConstants.COBIGEN_TEMPLATES));
}

LOG.info("Successfully created custom templates folder @ {}",
cobigenTemplatesDirectory.resolve(ConfigurationUtils.COBIGEN_TEMPLATES));
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import static java.util.stream.Collectors.toMap;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,9 +35,9 @@
import com.devonfw.cobigen.cli.constants.MessagesConstants;
import com.devonfw.cobigen.cli.logger.CLILogger;
import com.devonfw.cobigen.cli.utils.CobiGenUtils;
import com.devonfw.cobigen.cli.utils.ConfigurationUtils;
import com.devonfw.cobigen.cli.utils.ParsingUtils;
import com.devonfw.cobigen.cli.utils.ValidationUtils;
import com.devonfw.cobigen.impl.util.ConfigurationUtil;
import com.google.googlejavaformat.java.FormatterException;

import ch.qos.logback.classic.Level;
Expand Down Expand Up @@ -125,13 +127,14 @@ public Integer call() throws Exception {
LOG.debug("Input files and output root path confirmed to be valid.");
CobiGen cg = cobigenUtils.initializeCobiGen();

Path templateFolder = ConfigurationUtils.getCobigenTemplatesFolderPath();
URI templatesLocationUri = ConfigurationUtil.findTemplatesLocation();
Path templateFolder = Paths.get(templatesLocationUri);

ClassLoader inputClassLoader;

if (templateFolder != null) {
inputClassLoader = URLClassLoader.newInstance(new URL[] { templateFolder.toUri().toURL() },
getClass().getClassLoader());
if (templatesLocationUri != null) {
inputClassLoader =
URLClassLoader.newInstance(new URL[] { templatesLocationUri.toURL() }, getClass().getClassLoader());
} else {
inputClassLoader = getClass().getClassLoader();
}
Expand Down Expand Up @@ -252,7 +255,7 @@ public Boolean areArgumentsValid() {

int index = 0;
for (File inputFile : inputFiles) {
inputFile = ConfigurationUtils.preprocessInputFile(inputFile);
inputFile = preprocessInputFile(inputFile);
// Input file can be: C:\folder\input.java
if (inputFile.exists() == false) {
LOG.debug(
Expand All @@ -273,7 +276,7 @@ public Boolean areArgumentsValid() {
}

if (outputRootPath != null) {
outputRootPath = ConfigurationUtils.preprocessInputFile(outputRootPath);
outputRootPath = preprocessInputFile(outputRootPath);
}
return ValidationUtils.isOutputRootPathValid(outputRootPath);

Expand Down Expand Up @@ -303,7 +306,7 @@ public void generate(File inputFile, File inputProject, List<? extends Generable
ClassLoader classLoader, Class<?> c, Path templateFolder) {

Boolean isIncrements = c.getSimpleName().equals(IncrementTo.class.getSimpleName());
inputFile = ConfigurationUtils.preprocessInputFile(inputFile);
inputFile = preprocessInputFile(inputFile);
try {
Object input;
String extension = FileUtils.getExtension(inputFile.getName());
Expand Down Expand Up @@ -436,12 +439,12 @@ private List<? extends GenerableArtifact> generableArtifactSelection(ArrayList<S
LOG.info("(" + selectedArtifactNumber + ") " + artifactDescription);
} catch (IndexOutOfBoundsException e) {
LOG.error("The {} number you have specified is out of bounds!", artifactType);
System.exit(1);
throw (e);
} catch (NumberFormatException e) {
LOG.error(
"Error parsing your input. You need to specify {}s using numbers separated by comma (2,5,6).",
artifactType);
System.exit(1);
throw (e);
}
}

Expand Down Expand Up @@ -476,7 +479,7 @@ private List<? extends GenerableArtifact> generableArtifactSelection(ArrayList<S
LOG.info(
"No increment with that name has been found, Please provide correct increment name and try again ! Thank you");

System.exit(1);
throw new InputMismatchException("Wrong increment name");
}

userSelection = artifactStringSelection(userSelection, possibleArtifacts, artifactType);
Expand Down Expand Up @@ -541,11 +544,11 @@ private List<GenerableArtifact> artifactStringSelection(List<GenerableArtifact>
} catch (NumberFormatException e) {
LOG.error("Error parsing your input. You need to specify {}s using numbers separated by comma (2,5,6).",
artifactType);
System.exit(1);
throw (e);

} catch (ArrayIndexOutOfBoundsException e) {
LOG.error("Error parsing your input. Please give a valid number from the list above.");
System.exit(1);
throw (e);
}
}
return userSelection;
Expand Down Expand Up @@ -635,4 +638,22 @@ public static String getUserInput() {
return userInput;
}

/**
* Processes the input file's path. Strips the quotes from the file path if they are given.
* @param inputFile
* the input file
* @return input file with processed path
*/
public static File preprocessInputFile(File inputFile) {
String path = inputFile.getPath();
String pattern = "[\\\"|\\'](.+)[\\\"|\\']";
boolean matches = path.matches(pattern);
if (matches) {
path = path.replace("\"", "");
path = path.replace("\'", "");
return new File(path);
}
return inputFile;
}

}
Loading

0 comments on commit 6b2bb62

Please sign in to comment.