Search in sources :

Example 1 with EntityLoader

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);
}
Also used : Arrays(java.util.Arrays) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Lists(org.eclipse.collections.api.factory.Lists) Parameter(org.apache.maven.plugins.annotations.Parameter) MutableList(org.eclipse.collections.api.list.MutableList) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Mojo(org.apache.maven.plugins.annotations.Mojo) RichIterable(org.eclipse.collections.api.RichIterable) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) PlanTransformer(org.finos.legend.engine.plan.generation.transformers.PlanTransformer) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StreamWriteFeature(com.fasterxml.jackson.core.StreamWriteFeature) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StreamReadFeature(com.fasterxml.jackson.core.StreamReadFeature) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Collectors(java.util.stream.Collectors) Root_meta_pure_router_extension_RouterExtension(org.finos.legend.pure.generated.Root_meta_pure_router_extension_RouterExtension) File(java.io.File) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) PureProtocolObjectMapperFactory(org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Comparator(java.util.Comparator) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PlanGeneratorExtension(org.finos.legend.engine.plan.generation.extension.PlanGeneratorExtension) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service)

Example 2 with EntityLoader

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);
}
Also used : PluginExecution(org.apache.maven.model.PluginExecution) Arrays(java.util.Arrays) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) MojoRule(org.apache.maven.plugin.testing.MojoRule) URLClassLoader(java.net.URLClassLoader) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) MavenProject(org.apache.maven.project.MavenProject) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) EntitySerializers(org.finos.legend.sdlc.serialization.EntitySerializers) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Plugin(org.apache.maven.model.Plugin) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) List(java.util.List) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) Writer(java.io.Writer) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Model(org.apache.maven.model.Model) MavenProject(org.apache.maven.project.MavenProject) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) File(java.io.File) Test(org.junit.Test)

Example 3 with EntityLoader

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);
}
Also used : EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) ClassGraph(io.github.classgraph.ClassGraph) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 4 with EntityLoader

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();
}
Also used : EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) ClassGraph(io.github.classgraph.ClassGraph) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder) BeforeClass(org.junit.BeforeClass)

Example 5 with EntityLoader

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);
    }
}
Also used : FileGenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) FileGenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.fileGeneration.FileGenerationSpecification) GenerationSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.generationSpecification.GenerationSpecification) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) List(java.util.List) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)

Aggregations

EntityLoader (org.finos.legend.sdlc.serialization.EntityLoader)20 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)13 Test (org.junit.Test)10 File (java.io.File)8 List (java.util.List)7 Path (java.nio.file.Path)6 Collectors (java.util.stream.Collectors)6 MavenProject (org.apache.maven.project.MavenProject)6 PureModelContextData (org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData)6 PureModelBuilder (org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder)6 IOException (java.io.IOException)5 Arrays (java.util.Arrays)5 Set (java.util.Set)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 PureModel (org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel)5 PackageableElement (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement)5 EntityToPureConverter (org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter)5 URL (java.net.URL)4 Service (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service)4 Files (java.nio.file.Files)3