Skip to content

Commit 5a9acdd

Browse files
authored
Merge pull request #14 from fugerit-org/feature/issue_13_compare_tool
Simple compare tool #13
2 parents e45858f + 73f5a9c commit 5a9acdd

File tree

57 files changed

+6034
-81
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+6034
-81
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- [workflow codeql on branch main](.github/workflows/codeql-analysis.yml)
1313
- badge link to the daogen-config xsd 1.0
1414

15+
### Changed
16+
17+
- fj-bom set to 1.3.6
18+
- fj-core set to 8.2.7
19+
- fj-doc set to 2.0.2
20+
1521
### Fixed
1622

1723
- typos to the daogen-config-1-0.xsd
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.fugerit.java.daogen.base.gen.util;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.PrintWriter;
6+
import java.io.StringReader;
7+
import java.io.StringWriter;
8+
import java.util.List;
9+
import java.util.function.Predicate;
10+
import java.util.stream.Collectors;
11+
12+
import org.fugerit.java.core.function.SafeFunction;
13+
import org.fugerit.java.core.function.SimpleValue;
14+
import org.fugerit.java.core.io.FileIO;
15+
import org.fugerit.java.core.lang.helpers.StringUtils;
16+
17+
import lombok.Getter;
18+
import lombok.Setter;
19+
20+
public class ExtractCustomCode {
21+
22+
private ExtractCustomCode() {}
23+
24+
public static String extractCustom( File file, String startTag, String endTag ) {
25+
return SafeFunction.get( () -> extractCustom( FileIO.readString( file ) , startTag, endTag ) );
26+
}
27+
28+
public static String extractCustom( CharSequence text, String startTag, String endTag ) {
29+
return SafeFunction.get( () -> {
30+
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
31+
StringWriter buffer = new StringWriter();
32+
PrintWriter writer = new PrintWriter( buffer, true ) ) {
33+
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
34+
reader.lines().forEach( line -> {
35+
if ( line.contains( startTag ) ) {
36+
customCode.setValue( true );
37+
} else if ( line.contains( endTag ) ) {
38+
customCode.setValue( false );
39+
} else if ( customCode.getValue().booleanValue() ) {
40+
writer.println( line );
41+
}
42+
} );
43+
return buffer.toString();
44+
}
45+
} );
46+
}
47+
48+
public static String addCustomContent( CharSequence text, String startTag, String endTag, String customContent ) {
49+
return SafeFunction.get( () -> {
50+
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
51+
StringWriter buffer = new StringWriter();
52+
PrintWriter writer = new PrintWriter( buffer, true ) ) {
53+
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
54+
reader.lines().forEach( line -> {
55+
if ( line.contains( startTag ) ) {
56+
customCode.setValue( true );
57+
} else if ( line.contains( endTag ) ) {
58+
writer.println( customContent ); // append custom content
59+
customCode.setValue( false );
60+
}
61+
// all lines myst be written anyway
62+
writer.println( line );
63+
} );
64+
return buffer.toString();
65+
}
66+
} );
67+
}
68+
69+
private static String addWithCondition( CharSequence text, String customContent, Predicate<LineCursor> condition ) {
70+
return SafeFunction.get( () -> {
71+
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
72+
StringWriter buffer = new StringWriter();
73+
PrintWriter writer = new PrintWriter( buffer, true ) ) {
74+
List<String> lines = reader.lines().collect( Collectors.toList() );
75+
LineCursor cursor = new LineCursor();
76+
cursor.setLines( lines );
77+
cursor.setIndex( 0 );
78+
while ( cursor.getIndex()<lines.size() ) {
79+
String currentLine = lines.get( cursor.getIndex() );
80+
if ( condition.test( cursor ) ) {
81+
writer.println( customContent );
82+
}
83+
writer.println( currentLine );
84+
cursor.setIndex( cursor.getIndex()+1 );
85+
}
86+
return buffer.toString();
87+
}
88+
} );
89+
}
90+
91+
public static String addAfterPackageClassEnd( CharSequence text, String customContent ) {
92+
return addWithCondition(text, customContent, c -> {
93+
boolean ok = false;
94+
String previousLine = c.getPreviousLine();
95+
if ( StringUtils.isNotEmpty( previousLine ) ) {
96+
ok = previousLine.trim().startsWith( "package" );
97+
}
98+
return ok;
99+
} );
100+
}
101+
102+
public static String addBeforeClassEnd( CharSequence text, String customContent ) {
103+
return addWithCondition(text, customContent, c -> c.isLast() && c.getCurrentLine().trim().equals( "}" ) );
104+
}
105+
106+
}
107+
108+
class LineCursor {
109+
110+
@Getter @Setter private List<String> lines;
111+
112+
@Getter @Setter private int index;
113+
114+
public boolean isLast() {
115+
return this.lines.size()-1 == this.index;
116+
}
117+
118+
public String getPreviousLine() {
119+
return this.index==0 ? null : this.lines.get( this.index-1 );
120+
}
121+
122+
public String getCurrentLine() {
123+
return this.lines.get( this.index );
124+
}
125+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package test.org.fugerit.java.daogen.util;
2+
3+
import java.io.File;
4+
5+
import org.fugerit.java.core.function.SafeFunction;
6+
import org.fugerit.java.core.io.FileIO;
7+
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
8+
import org.fugerit.java.daogen.base.gen.util.ExtractCustomCode;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
@Slf4j
15+
public class TestExtractCustomCode {
16+
17+
@Test
18+
public void test001() {
19+
SafeFunction.apply( () -> {
20+
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
21+
String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java";
22+
File file = new File( path );
23+
File fileReal = new File( pathReal );
24+
log.info( "try source : {}", file.getCanonicalPath() );
25+
String content = FileIO.readString( file );
26+
String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END );
27+
Assert.assertNotNull( result );
28+
log.info( "result : {}", result );
29+
// try to correct
30+
log.info( "try correct : {}", fileReal.getCanonicalPath() );
31+
String realContent = FileIO.readString( fileReal );
32+
// test real mode 1
33+
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END, result );
34+
Assert.assertNotNull( resultReal );
35+
// test real mode 2
36+
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
37+
String resultRealAlt = ExtractCustomCode.addBeforeClassEnd( realContent , result );
38+
log.info( "real result alt {}", resultRealAlt );
39+
Assert.assertNotNull( resultRealAlt );
40+
} );
41+
}
42+
43+
@Test
44+
public void test002() {
45+
SafeFunction.apply( () -> {
46+
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
47+
String pathReal = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacade.java";
48+
File file = new File( path );
49+
File fileReal = new File( pathReal );
50+
log.info( "try source : {}", file.getCanonicalPath() );
51+
String result = ExtractCustomCode.extractCustom( file , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END );
52+
Assert.assertNotNull( result );
53+
log.info( "result : {}", result );
54+
// try to correct
55+
log.info( "try correct : {}", fileReal.getCanonicalPath() );
56+
String realContent = FileIO.readString( fileReal );
57+
// test real mode 1
58+
String resultReal = ExtractCustomCode.addCustomContent( realContent , SimpleJavaGenerator.CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_END, result );
59+
Assert.assertNotNull( resultReal );
60+
// test real mode 2
61+
log.info( "try correct alt : {}", fileReal.getCanonicalPath() );
62+
String resultRealAlt = ExtractCustomCode.addAfterPackageClassEnd( realContent , result );
63+
log.info( "real result alt {}", resultRealAlt );
64+
Assert.assertNotNull( resultRealAlt );
65+
} );
66+
}
67+
68+
}
69+
70+

fj-daogen-tool/src/main/java/org/fugerit/java/daogen/base/tool/DaoGenToolHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.fugerit.java.core.lang.helpers.StringUtils;
1515
import org.fugerit.java.core.util.PropsIO;
1616
import org.fugerit.java.daogen.base.config.DaogenFacade;
17+
import org.fugerit.java.daogen.base.tool.handler.CompareHandler;
1718
import org.fugerit.java.daogen.base.config.DaogenConfigDump;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
@@ -34,13 +35,17 @@ private DaoGenToolHandler() {}
3435
public static final String ARG_ACTION = "action";
3536
public static final String ARG_ACTION_DAOGEN = "daogen";
3637
public static final String ARG_ACTION_DUMP = "dump";
38+
public static final String ARG_ACTION_COMPARE = "compare";
3739

3840
public static void handle( Properties params ) {
3941
String action = params.getProperty( ARG_ACTION );
4042
if ( ARG_ACTION_DAOGEN.equalsIgnoreCase( action ) ) {
4143
handleDaogen(params);
4244
} else if ( ARG_ACTION_DUMP.equalsIgnoreCase( action ) ) {
4345
handleDump(params);
46+
} else if ( ARG_ACTION_COMPARE.equalsIgnoreCase( action ) ) {
47+
CompareHandler handler = new CompareHandler();
48+
handler.handleCompare(params);
4449
} else {
4550
throw new ConfigRuntimeException( "Reuired parameter : "+ARG_ACTION+" ("+ARG_ACTION_DAOGEN+"|"+ARG_ACTION_DUMP+")" );
4651
}

0 commit comments

Comments
 (0)