Skip to content

Use a caching writer to avoid overwriting identical files #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() ) );
Copy link
Member

@michael-o michael-o Jan 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. JSON is by definition UTF-8 and nothing else. I think this breaks it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've raised #185


generator.useDefaultPrettyPrinter();

Expand Down