Skip to content
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

Simple compare tool #13 #14

Merged
merged 5 commits into from
Sep 13, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [workflow codeql on branch main](.github/workflows/codeql-analysis.yml)
- badge link to the daogen-config xsd 1.0

### Changed

- fj-bom set to 1.3.6
- fj-core set to 8.2.7
- fj-doc set to 2.0.2

### Fixed

- typos to the daogen-config-1-0.xsd
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.fugerit.java.daogen.base.gen.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.function.SimpleValue;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.lang.helpers.StringUtils;

import lombok.Getter;
import lombok.Setter;

public class ExtractCustomCode {

private ExtractCustomCode() {}

public static String extractCustom( File file, String startTag, String endTag ) {
return SafeFunction.get( () -> extractCustom( FileIO.readString( file ) , startTag, endTag ) );
}

public static String extractCustom( CharSequence text, String startTag, String endTag ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
reader.lines().forEach( line -> {
if ( line.contains( startTag ) ) {
customCode.setValue( true );
} else if ( line.contains( endTag ) ) {
customCode.setValue( false );
} else if ( customCode.getValue().booleanValue() ) {
writer.println( line );
}
} );
return buffer.toString();
}
} );
}

public static String addCustomContent( CharSequence text, String startTag, String endTag, String customContent ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
reader.lines().forEach( line -> {
if ( line.contains( startTag ) ) {
customCode.setValue( true );
} else if ( line.contains( endTag ) ) {
writer.println( customContent ); // append custom content
customCode.setValue( false );
}
// all lines myst be written anyway
writer.println( line );
} );
return buffer.toString();
}
} );
}

private static String addWithCondition( CharSequence text, String customContent, Predicate<LineCursor> condition ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
List<String> lines = reader.lines().collect( Collectors.toList() );
LineCursor cursor = new LineCursor();
cursor.setLines( lines );
cursor.setIndex( 0 );
while ( cursor.getIndex()<lines.size() ) {
String currentLine = lines.get( cursor.getIndex() );
if ( condition.test( cursor ) ) {
writer.println( customContent );
}
writer.println( currentLine );
cursor.setIndex( cursor.getIndex()+1 );
}
return buffer.toString();
}
} );
}

public static String addAfterPackageClassEnd( CharSequence text, String customContent ) {
return addWithCondition(text, customContent, c -> {
boolean ok = false;
String previousLine = c.getPreviousLine();
if ( StringUtils.isNotEmpty( previousLine ) ) {
ok = previousLine.trim().startsWith( "package" );
}
return ok;
} );
}

public static String addBeforeClassEnd( CharSequence text, String customContent ) {
return addWithCondition(text, customContent, c -> c.isLast() && c.getCurrentLine().trim().equals( "}" ) );
}

}

class LineCursor {

@Getter @Setter private List<String> lines;

@Getter @Setter private int index;

public boolean isLast() {
return this.lines.size()-1 == this.index;
}

public String getPreviousLine() {
return this.index==0 ? null : this.lines.get( this.index-1 );
}

public String getCurrentLine() {
return this.lines.get( this.index );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package test.org.fugerit.java.daogen.util;

import java.io.File;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
import org.fugerit.java.daogen.base.gen.util.ExtractCustomCode;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestExtractCustomCode {

@Test
public void test001() {
SafeFunction.apply( () -> {
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java";
File file = new File( path );
File fileReal = new File( pathReal );
log.info( "try source : {}", file.getCanonicalPath() );
String content = FileIO.readString( file );
String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END );
Assert.assertNotNull( result );
log.info( "result : {}", result );
// try to correct
log.info( "try correct : {}", fileReal.getCanonicalPath() );
String realContent = FileIO.readString( fileReal );
// test real mode 1
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END, result );
Assert.assertNotNull( resultReal );
// test real mode 2
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
String resultRealAlt = ExtractCustomCode.addBeforeClassEnd( realContent , result );
log.info( "real result alt {}", resultRealAlt );
Assert.assertNotNull( resultRealAlt );
} );
}

@Test
public void test002() {
SafeFunction.apply( () -> {
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java";
File file = new File( path );
File fileReal = new File( pathReal );
log.info( "try source : {}", file.getCanonicalPath() );
String result = ExtractCustomCode.extractCustom( file , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END );
Assert.assertNotNull( result );
log.info( "result : {}", result );
// try to correct
log.info( "try correct : {}", fileReal.getCanonicalPath() );
String realContent = FileIO.readString( fileReal );
// test real mode 1
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END, result );
Assert.assertNotNull( resultReal );
// test real mode 2
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
String resultRealAlt = ExtractCustomCode.addAfterPackageClassEnd( realContent , result );
log.info( "real result alt {}", resultRealAlt );
Assert.assertNotNull( resultRealAlt );
} );
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.util.PropsIO;
import org.fugerit.java.daogen.base.config.DaogenFacade;
import org.fugerit.java.daogen.base.tool.handler.CompareHandler;
import org.fugerit.java.daogen.base.config.DaogenConfigDump;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,13 +35,17 @@ private DaoGenToolHandler() {}
public static final String ARG_ACTION = "action";
public static final String ARG_ACTION_DAOGEN = "daogen";
public static final String ARG_ACTION_DUMP = "dump";
public static final String ARG_ACTION_COMPARE = "compare";

public static void handle( Properties params ) {
String action = params.getProperty( ARG_ACTION );
if ( ARG_ACTION_DAOGEN.equalsIgnoreCase( action ) ) {
handleDaogen(params);
} else if ( ARG_ACTION_DUMP.equalsIgnoreCase( action ) ) {
handleDump(params);
} else if ( ARG_ACTION_COMPARE.equalsIgnoreCase( action ) ) {
CompareHandler handler = new CompareHandler();
handler.handleCompare(params);
} else {
throw new ConfigRuntimeException( "Reuired parameter : "+ARG_ACTION+" ("+ARG_ACTION_DAOGEN+"|"+ARG_ACTION_DUMP+")" );
}
Expand Down
Loading
Loading