1919
2020import nu .studer .java .util .OrderedProperties ;
2121import pl .project13 .core .log .LogInterface ;
22- import pl .project13 .core .util .BuildFileChangeListener ;
23- import pl .project13 .core .util .JsonManager ;
24- import pl .project13 .core .util .PropertyManager ;
25- import pl .project13 .core .util .XmlManager ;
26- import pl .project13 .core .util .YmlManager ;
22+ import pl .project13 .core .util .*;
2723
2824import javax .annotation .Nonnull ;
2925import java .io .*;
3026import java .nio .charset .Charset ;
31- import java .nio .file .Files ;
3227import java .util .Comparator ;
3328import java .util .Properties ;
3429
@@ -55,91 +50,37 @@ public void maybeGeneratePropertiesFile(
5550 Charset sourceCharset ,
5651 boolean escapeUnicode
5752 ) throws GitCommitIdExecutionException {
58- try {
59- final File gitPropsFile = craftPropertiesOutputFile (projectDir , propsFile );
60- boolean shouldGenerate = true ;
61-
62- if (gitPropsFile .exists ()) {
63- final Properties persistedProperties ;
64-
65- try {
66- switch (propertiesOutputFormat ) {
67- case JSON :
68- log .info (String .format ("Reading existing json file [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
69- persistedProperties = JsonManager .readJsonProperties (gitPropsFile , sourceCharset );
70- break ;
71- case PROPERTIES :
72- log .info (String .format ("Reading existing properties file [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
73- persistedProperties = PropertyManager .readProperties (gitPropsFile );
74- break ;
75- case XML :
76- log .info (String .format ("Reading existing xml file [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
77- persistedProperties = XmlManager .readXmlProperties (gitPropsFile , sourceCharset );
78- break ;
79- case YML :
80- log .info (String .format ("Reading existing yml file [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
81- persistedProperties = YmlManager .readYmlProperties (gitPropsFile , sourceCharset );
82- break ;
83- default :
84- throw new GitCommitIdExecutionException ("Not implemented:" + propertiesOutputFormat );
85- }
86-
87- final Properties propertiesCopy = (Properties ) localProperties .clone ();
88-
89- final String buildTimeProperty = prefixDot + GitCommitPropertyConstant .BUILD_TIME ;
90-
91- propertiesCopy .setProperty (buildTimeProperty , "" );
92- persistedProperties .setProperty (buildTimeProperty , "" );
93-
94- shouldGenerate = !propertiesCopy .equals (persistedProperties );
95- } catch (CannotReadFileException ex ) {
96- // Read has failed, regenerate file
97- log .info (String .format ("Cannot read properties file [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
98- shouldGenerate = true ;
99- }
53+ final File gitPropsFile = craftPropertiesOutputFile (projectDir , propsFile );
54+ boolean shouldGenerate = true ;
55+
56+ if (gitPropsFile .exists ()) {
57+ final Properties persistedProperties ;
58+ try {
59+ persistedProperties = GenericFileManager .readProperties (
60+ log , propertiesOutputFormat , gitPropsFile , sourceCharset , projectName );
61+ final Properties propertiesCopy = (Properties ) localProperties .clone ();
62+
63+ final String buildTimeProperty = prefixDot + GitCommitPropertyConstant .BUILD_TIME ;
64+
65+ propertiesCopy .setProperty (buildTimeProperty , "" );
66+ persistedProperties .setProperty (buildTimeProperty , "" );
67+
68+ shouldGenerate = !propertiesCopy .equals (persistedProperties );
69+ } catch (GitCommitIdExecutionException e ) {
70+ log .info (e .getMessage ());
71+ shouldGenerate = true ;
10072 }
73+ }
74+
75+ if (shouldGenerate ) {
76+ GenericFileManager .dumpProperties (
77+ log , propertiesOutputFormat , gitPropsFile , sourceCharset , escapeUnicode , projectName , localProperties );
10178
102- if (shouldGenerate ) {
103- Files .createDirectories (gitPropsFile .getParentFile ().toPath ());
104- try (OutputStream outputStream = new FileOutputStream (gitPropsFile )) {
105- OrderedProperties sortedLocalProperties = PropertiesFileGenerator .createOrderedProperties ();
106- localProperties .forEach ((key , value ) -> sortedLocalProperties .setProperty ((String ) key , (String ) value ));
107- switch (propertiesOutputFormat ) {
108- case JSON :
109- log .info (String .format ("Writing json file to [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
110- JsonManager .dumpJson (outputStream , sortedLocalProperties , sourceCharset );
111- break ;
112- case PROPERTIES :
113- log .info (String .format ("Writing properties file to [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
114- // using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
115- PropertyManager .dumpProperties (outputStream , sortedLocalProperties , escapeUnicode );
116- break ;
117- case XML :
118- log .info (String .format ("Writing xml file to [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
119- // using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
120- XmlManager .dumpXml (outputStream , sortedLocalProperties , sourceCharset );
121- break ;
122- case YML :
123- log .info (String .format ("Writing yml file to [%s] (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
124- // using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
125- YmlManager .dumpYml (outputStream , sortedLocalProperties , sourceCharset );
126- break ;
127- default :
128- throw new GitCommitIdExecutionException ("Not implemented:" + propertiesOutputFormat );
129- }
130- } catch (final IOException ex ) {
131- throw new RuntimeException ("Cannot create custom git properties file: " + gitPropsFile , ex );
132- }
133-
134- if (buildFileChangeListener != null ) {
135- buildFileChangeListener .changed (gitPropsFile );
136- }
137-
138- } else {
139- log .info (String .format ("Properties file [%s] is up-to-date (for module %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
79+ if (buildFileChangeListener != null ) {
80+ buildFileChangeListener .changed (gitPropsFile );
14081 }
141- } catch ( IOException e ) {
142- throw new GitCommitIdExecutionException ( e );
82+ } else {
83+ log . info ( String . format ( "Properties file [%s] is up-to-date (for module %s)..." , gitPropsFile . getAbsolutePath (), projectName ) );
14384 }
14485 }
14586
0 commit comments