Skip to content

Commit

Permalink
Use a caching writer to avoid overwriting identical files (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Aug 21, 2021
1 parent 7440d71 commit 0d7c5b7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/

import java.io.File;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -287,4 +290,11 @@ protected BuildContext getBuildContext()
{
return buildContext;
}

protected Writer newWriter( Path path )
{
Charset charset = getEncoding() != null ? Charset.forName( getEncoding() ) : Charset.defaultCharset();
return new CachingWriter( getBuildContext(), path, charset );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.codehaus.modello.plugin;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import org.sonatype.plexus.build.incremental.BuildContext;

public class CachingWriter extends StringWriter
{
private final BuildContext buildContext;
private final Path path;
private final Charset charset;

public CachingWriter( BuildContext buildContext, Path path, Charset charset )
{
this.buildContext = buildContext;
this.path = Objects.requireNonNull( path );
this.charset = Objects.requireNonNull( charset );
}

@Override
public void close() throws IOException
{
String str = getBuffer().toString();
if ( Files.exists( path ) )
{
String old = readString( path, charset );
if ( str.equals( old ) )
{
return;
}
}
writeString( path, str, charset );
if ( buildContext != null )
{
buildContext.refresh( path.toFile() );
}
}

private static String readString( Path path, Charset charset ) throws IOException
{
byte[] ba = Files.readAllBytes( path );
return new String( ba, charset );
}

private static void writeString( Path path, String str, Charset charset ) throws IOException
{
byte[] ba = str.getBytes( charset );
Files.write( path, ba );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -47,6 +48,7 @@
import org.codehaus.modello.model.ModelInterface;
import org.codehaus.modello.model.ModelType;
import org.codehaus.modello.plugin.AbstractModelloGenerator;
import org.codehaus.modello.plugin.CachingWriter;
import org.codehaus.modello.plugin.java.javasource.JClass;
import org.codehaus.modello.plugin.java.javasource.JComment;
import org.codehaus.modello.plugin.java.javasource.JInterface;
Expand All @@ -57,7 +59,6 @@
import org.codehaus.modello.plugin.java.metadata.JavaModelMetadata;
import org.codehaus.modello.plugin.model.ModelClassMetadata;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.WriterFactory;

/**
* AbstractJavaModelloGenerator - similar in scope to {@link AbstractModelloGenerator} but with features that
Expand Down Expand Up @@ -102,12 +103,7 @@ protected JSourceWriter newJSourceWriter( String packageName, String className )
f.getParentFile().mkdirs();
}

OutputStream os = getBuildContext().newFileOutputStream( f );

Writer writer = ( getEncoding() == null ) ? WriterFactory.newPlatformWriter( os )
: WriterFactory.newWriter( os, getEncoding() );

return new JSourceWriter( writer );
return new JSourceWriter( newWriter( f.toPath() ) );
}

private JComment getHeaderComment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void generateJsonSchema( Properties parameters )
.enable( JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature() )
.enable( JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature() )
.disable( JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS.mappedFeature() )
.createGenerator( schemaFile, JsonEncoding.UTF8 );
.createGenerator( newWriter( schemaFile.toPath() ) );

generator.useDefaultPrettyPrinter();

Expand Down

0 comments on commit 0d7c5b7

Please sign in to comment.