use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class XmlPluginIntegrationTest method testSimpleUmlEntityExtraction.
/**
* Tests simple extraction of entities out of XMI UML.
*
* @throws Exception test fails
*/
@Test
public void testSimpleUmlEntityExtraction() throws Exception {
// arrange
Path configFolder = new File(testFileRootPath + "uml-classdiag").toPath();
File xmlFile = configFolder.resolve("completeUmlXmi.xml").toFile();
CobiGen cobigen = CobiGenFactory.create(configFolder.toUri());
Object doc = cobigen.read(xmlFile.toPath(), UTF_8);
File targetFolder = this.tmpFolder.newFolder("testSimpleUmlEntityExtraction");
// act
List<TemplateTo> matchingTemplates = cobigen.getMatchingTemplates(doc);
List<TemplateTo> templateOfInterest = matchingTemplates.stream().filter(e -> e.getId().equals("${className}.txt")).collect(Collectors.toList());
assertThat(templateOfInterest).hasSize(1);
GenerationReportTo generate = cobigen.generate(doc, templateOfInterest, targetFolder.toPath());
// assert
assertThat(generate).isSuccessful();
File[] files = targetFolder.listFiles();
assertThat(files).extracting(e -> e.getName()).containsExactlyInAnyOrder("Student.txt", "User.txt", "Marks.txt", "Teacher.txt");
assertThat(targetFolder.toPath().resolve("Student.txt")).hasContent("public Student EAID_4509184A_D724_495f_AAEB_1ACE1AD90879");
assertThat(targetFolder.toPath().resolve("User.txt")).hasContent("public User EAID_C2E366C0_510F_4145_B650_110537B98360");
assertThat(targetFolder.toPath().resolve("Marks.txt")).hasContent("public Marks EAID_1D7DCE81_651D_40f2_A6E5_A522CF6E0C64");
assertThat(targetFolder.toPath().resolve("Teacher.txt")).hasContent("public Teacher EAID_6EA6FC61_FB9B_4e8e_98A1_30BD386AEA9A");
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class XPathGenerationTest method testXpathAccess.
/**
* Testing basic Xpath Access
*
* @throws Exception test fails
*/
@Test
public void testXpathAccess() throws Exception {
Path cobigenConfigFolder = new File("src/test/resources/testdata/integrationtest/uml-basic-test").toPath();
Path input = cobigenConfigFolder.resolve("uml.xml");
CobiGen cobigen = CobiGenFactory.create(cobigenConfigFolder.toUri());
Object compliantInput = cobigen.read(input, Charset.forName("UTF-8"));
List<TemplateTo> matchingTemplates = cobigen.getMatchingTemplates(compliantInput);
assertThat(matchingTemplates).isNotNull().hasSize(1);
File targetFolder = this.tmpFolder.newFolder("testXpathAccess");
GenerationReportTo report = cobigen.generate(compliantInput, matchingTemplates.get(0), targetFolder.toPath());
assertThat(report).isSuccessful();
assertThat(targetFolder.toPath().resolve("DocXPath.txt")).hasContent("Bill");
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class CobiGenWrapper method generate.
/**
* Generates the the list of templates based on the given {@link #inputs}.
*
* @param templates to be generated
* @param monitor to track the progress
* @return the {@link GenerationReportTo generation report of CobiGen}
* @throws Exception if anything during generation fails.
*/
public GenerationReportTo generate(List<TemplateTo> templates, IProgressMonitor monitor) throws Exception {
final IProject proj = getGenerationTargetProject();
if (proj != null) {
LOG.debug("Generating files...");
SubMonitor subMonitor = SubMonitor.convert(monitor, 105);
if (monitor.isCanceled()) {
throw new CancellationException("generation got Cancelled by the User");
}
monitor.setTaskName("Load templates...");
SubMonitor loadTemplates = subMonitor.split(2);
// set override flags individually for every template
for (TemplateTo template : templates) {
// wizard and does not declare any merge strategy), the complete file should be overwritten
if (template.getMergeStrategy() == null) {
template.setForceOverride(true);
}
}
loadTemplates.done();
if (monitor.isCanceled()) {
throw new CancellationException("Generation got cancelled by the user");
}
monitor.setTaskName("Determine destination paths...");
SubMonitor generateTargetUri = subMonitor.split(1);
URI generationTargetUri = getGenerationTargetProject().getLocationURI();
if (generationTargetUri == null) {
throw new CobiGenRuntimeException("The location URI of the generation target project " + getGenerationTargetProject().getName() + " could not be resolved. This might be " + "a temporary issue. If this problem persists, please state a bug report.");
}
generateTargetUri.done();
monitor.setTaskName("Generate files...");
GenerationReportTo report;
if (this.singleNonContainerInput) {
// if we only consider one input, we want to allow some customizations of the generation
LOG.debug("Generating with single non container input ...");
report = this.cobiGen.generate(this.inputs.get(0), templates, Paths.get(generationTargetUri), false, (String taskName, Integer progress) -> {
monitor.setTaskName(taskName);
monitor.worked(progress);
});
} else {
report = new GenerationReportTo();
for (Object input : this.inputs) {
report.aggregate(this.cobiGen.generate(input, templates, Paths.get(generationTargetUri), false, (taskname, progress) -> {
monitor.setTaskName(taskname);
monitor.worked(progress);
}));
}
}
monitor.setTaskName("Refresh workspace...");
proj.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
monitor.done();
if (report.isSuccessful()) {
LOG.info("Generation finished successfully.");
}
return report;
} else {
throw new CobiGenRuntimeException("No generation target project configured! This is a Bug!");
}
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class CobiGenUtils method extractArtificialPom.
/**
* Extracts an artificial POM which defines all the CobiGen plug-ins that are needed
*
* @return the extracted POM file
*/
public static File extractArtificialPom() {
Path cliHome = getCliHomePath();
File pomFile = cliHome.resolve(MavenConstants.POM).toFile();
if (!pomFile.exists()) {
try (InputStream resourcesIs1 = CobiGenUtils.class.getResourceAsStream("/" + MavenConstants.POM);
InputStream resourcesIs2 = CobiGenUtils.class.getClass().getResourceAsStream("/" + MavenConstants.POM)) {
if (resourcesIs1 != null) {
LOG.debug("Taking pom.xml from classpath");
Files.createDirectories(pomFile.toPath().getParent());
Files.copy(resourcesIs1, pomFile.getAbsoluteFile().toPath());
} else if (resourcesIs2 != null) {
LOG.debug("Taking pom.xml from system classpath");
Files.createDirectories(pomFile.toPath().getParent());
Files.copy(resourcesIs1, pomFile.getAbsoluteFile().toPath());
} else {
if (CobiGenUtils.class.getClassLoader() instanceof URLClassLoader) {
LOG.debug("Classloader URLs:");
Arrays.stream(((URLClassLoader) CobiGenUtils.class.getClassLoader()).getURLs()).forEach(url -> LOG.debug(" - {}", url));
}
if (CobiGenUtils.class.getClass().getClassLoader() instanceof URLClassLoader) {
LOG.debug("System Classloader URLs:");
Arrays.stream(((URLClassLoader) CobiGenUtils.class.getClassLoader()).getURLs()).forEach(url -> LOG.debug(" - {}", url));
}
throw new CobiGenRuntimeException("Unable to locate pom.xml on classpath");
}
} catch (IOException e1) {
throw new CobiGenRuntimeException("Failed to extract CobiGen plugins pom.", e1);
}
}
return pomFile;
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class GenerateCommand method doAction.
@Override
public Integer doAction() throws Exception {
if (!areArgumentsValid()) {
return 1;
}
LOG.debug("Input files and output root path confirmed to be valid.");
CobiGen cg = CobiGenUtils.initializeCobiGen(this.templatesProject);
resolveTemplateDependencies();
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), 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), inputsAndArtifacts.getA().get(i), MavenUtil.getProjectRoot(this.inputFiles.get(i), false), inputsAndArtifacts.getB(), cg, IncrementTo.class);
}
}
return 0;
}
Aggregations