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));
}
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);
}
}
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)));
}
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);
}
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));
}
Aggregations