use of org.finos.legend.sdlc.serialization.EntityLoader in project legend-sdlc by finos.
the class ServicesGenerationMojo method resolveServicesSpecification.
private static ResolvedServicesSpecification resolveServicesSpecification(ServicesSpecification servicesSpec) throws Exception {
Set<String> servicePaths = null;
if (servicesSpec.directories != null) {
try (EntityLoader directoriesLoader = EntityLoader.newEntityLoader(servicesSpec.directories)) {
EntityToPureConverter converter = new EntityToPureConverter();
servicePaths = directoriesLoader.getAllEntities().filter(e -> converter.fromEntityIfPossible(e).filter(s -> s instanceof Service).isPresent()).map(Entity::getPath).collect(Collectors.toSet());
}
}
if (servicesSpec.servicePaths != null) {
if (servicePaths == null) {
servicePaths = servicesSpec.servicePaths;
} else {
servicePaths.addAll(servicesSpec.servicePaths);
}
}
return new ResolvedServicesSpecification(servicePaths, servicesSpec.packages);
}
use of org.finos.legend.sdlc.serialization.EntityLoader in project legend-sdlc by finos.
the class TestServicesGenerationMojo method testNoServices.
@Test
public void testNoServices() throws Exception {
File entitiesDir = this.tempFolder.newFolder("testEntities");
try (EntityLoader testEntities = getTestEntities()) {
testEntities.getAllEntities().filter(p -> !SERVICE_CLASSIFIER.equals(p.getClassifierPath())).forEach(e -> writeEntityToDirectory(entitiesDir.toPath(), e));
}
File projectDir = buildSingleModuleProject("project", "org.finos.test", "test-project", "1.0.0", null, null, new File[] { entitiesDir }, null, null, null, "org.finos.test.test_project", null, null);
MavenProject mavenProject = this.mojoRule.readMavenProject(projectDir);
File outputDir = new File(mavenProject.getBuild().getOutputDirectory());
assertDirectoryEmpty(outputDir);
executeMojo(projectDir, entitiesDir);
assertDirectoryEmpty(outputDir);
}
use of org.finos.legend.sdlc.serialization.EntityLoader in project legend-sdlc by finos.
the class TestServiceExecutionClassGenerator method setUp.
@BeforeClass
public static void setUp() throws Exception {
CLASSPATH = new ClassGraph().getClasspath();
String resourceName = "org/finos/legend/sdlc/generation/service";
URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
if (url == null) {
throw new RuntimeException("Could not find resource: " + resourceName);
}
try (EntityLoader entityLoader = EntityLoader.newEntityLoader(Paths.get(url.toURI()))) {
PURE_MODEL_CONTEXT_DATA = PureModelContextDataBuilder.newBuilder().withEntities(entityLoader.getAllEntities()).build();
}
SERVICES = Iterate.groupByUniqueKey(PURE_MODEL_CONTEXT_DATA.getElementsOfType(Service.class), PackageableElement::getPath);
}
use of org.finos.legend.sdlc.serialization.EntityLoader in project legend-sdlc by finos.
the class TestServiceExecutionGenerator method setUp.
@BeforeClass
public static void setUp() throws Exception {
CLASSPATH = new ClassGraph().getClasspath();
String resourceName = "org/finos/legend/sdlc/generation/service";
URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
if (url == null) {
throw new RuntimeException("Could not find resource: " + resourceName);
}
try (EntityLoader entityLoader = EntityLoader.newEntityLoader(Paths.get(url.toURI()))) {
PureModelBuilder.PureModelWithContextData pureModelWithContextData = PureModelBuilder.newBuilder().withEntities(entityLoader.getAllEntities()).build();
PURE_MODEL_CONTEXT_DATA = pureModelWithContextData.getPureModelContextData();
PURE_MODEL = pureModelWithContextData.getPureModel();
}
SERVICES = Iterate.groupByUniqueKey(PURE_MODEL_CONTEXT_DATA.getElementsOfType(Service.class), PackageableElement::getPath);
OBJECT_MAPPER = new ObjectMapper();
}
use of org.finos.legend.sdlc.serialization.EntityLoader in project legend-sdlc by finos.
the class FileGenerationMojo method execute.
@Override
public void execute() throws MojoExecutionException {
long generateStart = System.nanoTime();
if (this.inclusions != null) {
getLog().info("include generation specification paths: " + this.inclusions.paths);
getLog().info("include generation specification packages: " + this.inclusions.packages);
getLog().info("include generation specification directories: " + Arrays.toString(this.inclusions.directories));
}
if (this.exclusions != null) {
getLog().info("exclude generation specification paths: " + this.exclusions.paths);
getLog().info("exclude generation specification packages: " + this.exclusions.packages);
getLog().info("exclude generation specification directories: " + Arrays.toString(this.exclusions.directories));
}
getLog().info("Output directory: " + this.outputDirectory);
// Load Model
long modelStart = System.nanoTime();
getLog().info("Start loading model");
PureModelBuilder pureModelBuilder = PureModelBuilder.newBuilder();
try (EntityLoader allEntities = EntityLoader.newEntityLoader(Thread.currentThread().getContextClassLoader())) {
pureModelBuilder.addEntitiesIfPossible(allEntities.getAllEntities());
int entityCount = pureModelBuilder.getElementCount();
getLog().info("Found " + entityCount + " entities");
if (entityCount == 0) {
long modelEnd = System.nanoTime();
getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
getLog().info("No elements found to generate");
return;
}
} catch (Exception e) {
throw new MojoExecutionException("Error loading entities from model", e);
}
getLog().info("Compiling model");
PureModelBuilder.PureModelWithContextData pureModelWithContextData = pureModelBuilder.build();
PureModelContextData pureModelContextData = pureModelWithContextData.getPureModelContextData();
PureModel pureModel = pureModelWithContextData.getPureModel();
long modelEnd = System.nanoTime();
getLog().info(String.format("Finished loading and compiling model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
Map<String, GenerationSpecification> generationSpecificationMap = LazyIterate.selectInstancesOf(pureModelContextData.getElements(), GenerationSpecification.class).groupByUniqueKey(PackageableElement::getPath, Maps.mutable.empty());
filterGenerationSpecsByIncludes(generationSpecificationMap);
filterGenerationSpecsByExcludes(generationSpecificationMap);
if (generationSpecificationMap.isEmpty()) {
getLog().info("No generation specification found, nothing to generate");
return;
}
if (generationSpecificationMap.size() > 1) {
throw new MojoExecutionException(Iterate.toSortedList(generationSpecificationMap.keySet()).makeString("Only 1 generation specification allowed, found " + generationSpecificationMap.size() + ": ", ", ", ""));
}
try {
// Start generating
GenerationSpecification generationSpecification = generationSpecificationMap.values().iterator().next();
getLog().info(String.format("Start generating file generations for generation specification '%s', %,d file generations found", generationSpecification.getPath(), generationSpecification.fileGenerations.size()));
FileGenerationFactory fileGenerationFactory = FileGenerationFactory.newFactory(generationSpecification, pureModelContextData, pureModel);
MutableMap<FileGenerationSpecification, List<GenerationOutput>> outputs = fileGenerationFactory.generateFiles();
serializeOutput(outputs);
getLog().info(String.format("Done (%.9fs)", (System.nanoTime() - generateStart) / 1_000_000_000.0));
} catch (Exception e) {
throw new MojoExecutionException("Error generating files: " + e.getMessage(), e);
}
}
Aggregations