use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class InputReaderMatcherTest method testVariableAssignment_propertyName.
/**
* Tests variable assignment resolution of PROPERTY type at the example of the component version
*
* @throws Exception test fails
*/
@Test
public void testVariableAssignment_propertyName() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "one-component.yaml"), TestConstants.UTF_8);
List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
String templateName = "testVariableAssignment_propertyName.txt";
TemplateTo template = findTemplate(cobigen, inputObjects.get(0), templateName);
File targetFolder = this.tmpFolder.newFolder();
GenerationReportTo report = cobigen.generate(inputObjects.get(0), template, targetFolder.toPath());
assertThat(report).isSuccessful();
assertThat(targetFolder.toPath().resolve("testVariableAssignment_propertyName.txt").toFile()).exists().hasContent("Table");
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class InputReaderMatcherTest method testBasicElementMatcher_twoComponents.
/**
* Tests the correct basic retrieval of ComponentDef inputs
*
* @throws Exception test fails
*/
@Test
public void testBasicElementMatcher_twoComponents() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "two-components.yaml"), TestConstants.UTF_8);
assertThat(openApiFile).isNotNull();
List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
assertThat(inputObjects).isNotNull().hasSize(2);
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class InputReaderMatcherTest method testVariableAssignment_noAttributeFound.
/**
* Tests the case when <b>no</b> ATTRIBUTE was found on the OpenAPI input file for one entity. Therefore an empty
* string should be assigned.<br>
* <br>
* The input test file contains two entities, one has an attribute and the other one does not. We are testing here
* that the first entity gets his attribute and the second entity gets an empty string
*
* @throws Exception test fails
*/
@Test
public void testVariableAssignment_noAttributeFound() throws Exception {
CobiGen cobigen = CobiGenFactory.create(Paths.get(testdataRoot, "templates").toUri());
Object openApiFile = cobigen.read(Paths.get(testdataRoot, "two-components-no-attribute.yaml"), TestConstants.UTF_8);
// Previous version: List<Object> inputObjects = cobigen.getInputObjects(openApiFile,
// TestConstants.UTF_8);
List<Object> inputObjects = cobigen.resolveContainers(openApiFile);
String templateName = "testVariableAssignment_attribute.txt";
TemplateTo template = findTemplate(cobigen, inputObjects.get(0), templateName);
File targetFolder = this.tmpFolder.newFolder();
GenerationReportTo report = cobigen.generate(inputObjects.get(0), template, targetFolder.toPath());
assertThat(report).isSuccessful();
assertThat(targetFolder.toPath().resolve("testVariableAssignment_attribute.txt").toFile()).exists().hasContent("testingAttributeTable");
template = findTemplate(cobigen, inputObjects.get(1), templateName);
targetFolder = this.tmpFolder.newFolder();
report = cobigen.generate(inputObjects.get(1), template, targetFolder.toPath());
assertThat(report.hasWarnings());
assertThat(report).isSuccessful();
assertThat(targetFolder.toPath().resolve("testVariableAssignment_attribute.txt").toFile()).exists().hasContent("");
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class GenerateCommand method generate.
/**
* Generates new templates or increments using the inputFile from the inputProject.
*
* @param <T> type of generable artifacts to generate
* @param inputFile input file to be selected by the user
* @param input parsed by CobiGen read method
* @param inputProject input project where the input file is located. We need this in order to build the classpath of
* the input file
* @param generableArtifacts the list of increments or templates that the user is going to use for generation
* @param cg Initialized CobiGen instance
* @param c class type, specifies whether Templates or Increments should be preprocessed
*/
public <T extends GenerableArtifact> void generate(Path inputFile, Object input, Path inputProject, List<T> generableArtifacts, CobiGen cg, Class<T> c) {
boolean isIncrements = c.getSimpleName().equals(IncrementTo.class.getSimpleName());
if (this.outputRootPath == null) {
// If user did not specify the output path of the generated files, we can use
// the current project folder
setOutputRootPath(inputProject);
}
GenerationReportTo report = null;
LOG.info("Generating {} for input '{}, this can take a while...", isIncrements ? "increments" : "templates", inputFile);
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());
if (!generatedJavaFiles.isEmpty()) {
try {
ParsingUtils.formatJavaSources(generatedJavaFiles);
} catch (FormatterException e) {
LOG.warn("Generation was successful but we were not able to format your code. Maybe you will see strange formatting.");
}
}
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class GeneratorWrapperFactory method createGenerator.
/**
* Creates a generator dependent on the input of the selection
*
* @param selection current {@link IStructuredSelection} treated as input for generation
* @param monitor tracking progress
* @return a specific {@link CobiGenWrapper} instance
* @throws GeneratorCreationException if any exception occurred during converting the inputs or creating the generator
* @throws GeneratorProjectNotExistentException if the generator configuration project does not exist
* @throws InvalidInputException if the selection includes non supported input types or is composed in a non supported
* combination of inputs.
*/
public static CobiGenWrapper createGenerator(ISelection selection, IProgressMonitor monitor) throws GeneratorCreationException, GeneratorProjectNotExistentException, InvalidInputException {
List<Object> extractedInputs = extractValidEclipseInputs(selection);
if (extractedInputs.size() > 0) {
monitor.subTask("Initialize CobiGen instance");
CobiGen cobigen = initializeGenerator();
monitor.subTask("Reading inputs...");
monitor.worked(10);
Object firstElement = extractedInputs.get(0);
if (firstElement instanceof IJavaElement) {
LOG.info("Create new CobiGen instance for java inputs...");
return new JavaInputGeneratorWrapper(cobigen, ((IJavaElement) firstElement).getJavaProject().getProject(), JavaInputConverter.convertInput(extractedInputs, cobigen), monitor);
} else if (firstElement instanceof IResource) {
LOG.info("Create new CobiGen instance for file inputs...");
return new FileInputGeneratorWrapper(cobigen, ((IResource) firstElement).getProject(), FileInputConverter.convertInput(cobigen, extractedInputs), monitor);
}
}
return null;
}
Aggregations