Search in sources :

Example 1 with EntitySerializer

use of org.finos.legend.sdlc.serialization.EntitySerializer in project legend-sdlc by finos.

the class ModelGenerationMojo method serializeEntities.

private void serializeEntities(List<Entity> entities) throws IOException {
    long serializeStart = System.nanoTime();
    getLog().info(String.format("Serializing %,d entities to %s", entities.size(), this.outputDirectory));
    Path outputDirPath = this.outputDirectory.toPath();
    Path entitiesDir = outputDirPath.resolve("entities");
    Pattern pkgSepPattern = Pattern.compile("::", Pattern.LITERAL);
    String replacement = Matcher.quoteReplacement(outputDirPath.getFileSystem().getSeparator());
    EntitySerializer entitySerializer = EntitySerializers.getDefaultJsonSerializer();
    for (Entity entity : entities) {
        Path entityFilePath = entitiesDir.resolve(pkgSepPattern.matcher(entity.getPath()).replaceAll(replacement) + "." + entitySerializer.getDefaultFileExtension());
        Files.createDirectories(entityFilePath.getParent());
        try (OutputStream stream = Files.newOutputStream(entityFilePath)) {
            entitySerializer.serialize(entity, stream);
        }
    }
    getLog().info(String.format("Done serializing %,d entities to %s (%.9fs)", entities.size(), this.outputDirectory, (System.nanoTime() - serializeStart) / 1_000_000_000.0));
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) OutputStream(java.io.OutputStream) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Example 2 with EntitySerializer

use of org.finos.legend.sdlc.serialization.EntitySerializer in project legend-sdlc by finos.

the class EntityMojo method getDefaultSerializationSpecs.

private List<SerializationSpec> getDefaultSerializationSpecs(Map<String, EntitySerializer> entitySerializers) throws MojoExecutionException {
    Path srcMain = this.baseDir.toPath().resolve("src").resolve("main");
    if (!Files.isDirectory(srcMain)) {
        getLog().info("using default source directories: []");
        return Collections.emptyList();
    }
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(srcMain, Files::isDirectory)) {
        List<SerializationSpec> serializationSpecs = Lists.mutable.empty();
        for (Path dir : dirStream) {
            EntitySerializer serializer = entitySerializers.get(dir.getFileName().toString());
            if (serializer != null) {
                serializationSpecs.add(new SerializationSpec(dir, serializer));
            }
        }
        getLog().info("using default source directories: " + serializationSpecs);
        return serializationSpecs;
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder("Error accessing source directories");
        String eMessage = e.getMessage();
        if (eMessage != null) {
            builder.append(": ").append(eMessage);
        }
        throw new MojoExecutionException(builder.toString(), e);
    }
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Files(java.nio.file.Files) NoSuchFileException(java.nio.file.NoSuchFileException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Example 3 with EntitySerializer

use of org.finos.legend.sdlc.serialization.EntitySerializer in project legend-sdlc by finos.

the class EntityMojo method execute.

@Override
public void execute() throws MojoExecutionException {
    long start = System.nanoTime();
    getLog().info("Starting entity processing");
    getLog().info("source directories: " + this.sourceDirectories);
    getLog().info("output directory: " + this.outputDirectory);
    getLog().info("base directory: " + this.baseDir);
    EntitySerializer outputSerializer = EntitySerializers.getDefaultJsonSerializer();
    List<SerializationSpec> serializationSpecs = getSerializationSpecs();
    int totalCount = 0;
    for (SerializationSpec serializationSpec : serializationSpecs) {
        long sourceStart = System.nanoTime();
        getLog().info("Reserializing entities from " + serializationSpec.directory + " using serializer \"" + serializationSpec.serializer.getName() + "\" to " + this.outputDirectory);
        EntityReserializer reserializer = EntityReserializer.newReserializer(serializationSpec.serializer, outputSerializer);
        Predicate<Path> filter = (serializationSpec.fileExtensions == null) ? null : EntityReserializer.getExtensionsFilter(serializationSpec.fileExtensions);
        try {
            List<String> paths = reserializer.reserializeDirectoryTree(serializationSpec.directory, filter, this.outputDirectory.toPath());
            long sourceEnd = System.nanoTime();
            getLog().info(String.format("Finished reserializing %,d entities from %s using serializer \"%s\" to %s (%.9fs)", paths.size(), serializationSpec.directory, serializationSpec.serializer.getName(), this.outputDirectory, nanoDuration(sourceStart, sourceEnd)));
            if (getLog().isDebugEnabled()) {
                getLog().debug(Iterate.makeString(paths, "Reserialized: ", ", ", ""));
            }
            totalCount += paths.size();
        } catch (Exception e) {
            long sourceEnd = System.nanoTime();
            getLog().info(String.format("Error reserializing entities from %s using serializer \"%s\" to %s (%.9fs)", serializationSpec.directory, serializationSpec.serializer.getName(), this.outputDirectory, nanoDuration(sourceStart, sourceEnd)), e);
            StringBuilder builder = new StringBuilder("Error reserializing entities from ").append(serializationSpec.directory).append(" using serializer \"").append(serializationSpec.serializer.getName()).append('"').append(" to ").append(this.outputDirectory);
            String eMessage = e.getMessage();
            if (eMessage != null) {
                builder.append(": ").append(eMessage);
            }
            throw new MojoExecutionException(builder.toString(), e);
        }
    }
    long end = System.nanoTime();
    getLog().info(String.format("Finished processing %,d entities (%.9fs)", totalCount, nanoDuration(start, end)));
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) NoSuchFileException(java.nio.file.NoSuchFileException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Example 4 with EntitySerializer

use of org.finos.legend.sdlc.serialization.EntitySerializer in project legend-sdlc by finos.

the class EntityMojo method getSerializationSpec.

private SerializationSpec getSerializationSpec(SourceDirectory sourceDirectory, Map<String, EntitySerializer> entitySerializers) throws MojoExecutionException {
    Path dir = resolveSourceDirectoryPath(sourceDirectory);
    String serializerName = sourceDirectory.serializer;
    if ((serializerName == null) || serializerName.isEmpty()) {
        serializerName = sourceDirectory.directory.getName();
    }
    EntitySerializer serializer = entitySerializers.get(serializerName);
    if (serializer == null) {
        throw new MojoExecutionException("Unknown entity serializer: " + serializerName);
    }
    return new SerializationSpec(dir, serializer, sourceDirectory.extensions);
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Example 5 with EntitySerializer

use of org.finos.legend.sdlc.serialization.EntitySerializer in project legend-sdlc by finos.

the class VersionQualifiedPackageMojo method serializeEntities.

private void serializeEntities(List<Entity> entities) throws IOException {
    long serializeStart = System.nanoTime();
    getLog().info(String.format("Serializing %,d entities to %s", entities.size(), this.outputDirectory));
    Path outputDirPath = this.outputDirectory.toPath();
    Path entitiesDir = outputDirPath.resolve("entities");
    Pattern pkgSepPattern = Pattern.compile("::", Pattern.LITERAL);
    String replacement = Matcher.quoteReplacement(outputDirPath.getFileSystem().getSeparator());
    EntitySerializer entitySerializer = EntitySerializers.getDefaultJsonSerializer();
    for (Entity entity : entities) {
        Path entityFilePath = entitiesDir.resolve(pkgSepPattern.matcher(entity.getPath()).replaceAll(replacement) + "." + entitySerializer.getDefaultFileExtension());
        Files.createDirectories(entityFilePath.getParent());
        try (OutputStream stream = Files.newOutputStream(entityFilePath)) {
            entitySerializer.serialize(entity, stream);
        }
    }
    getLog().info(String.format("Done serializing %,d entities to %s (%.9fs)", entities.size(), this.outputDirectory, (System.nanoTime() - serializeStart) / 1_000_000_000.0));
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) OutputStream(java.io.OutputStream) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Aggregations

Path (java.nio.file.Path)5 EntitySerializer (org.finos.legend.sdlc.serialization.EntitySerializer)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 OutputStream (java.io.OutputStream)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Pattern (java.util.regex.Pattern)2 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)2 Files (java.nio.file.Files)1