Skip to content

Commit 232173f

Browse files
committed
feat(core): google java format
1 parent a9cc153 commit 232173f

File tree

5 files changed

+100
-17
lines changed

5 files changed

+100
-17
lines changed

polygenesis-craftsman/src/test/java/com/invoiceful/genesis/backend/InvoicefulBackendCreator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.invoiceful.genesis.backend;
2222

23+
import com.invoiceful.genesis.contexts.access.ContextAccess;
2324
import com.invoiceful.genesis.contexts.invoicing.ContextInvoicing;
2425
import com.oregor.trinity.scaffolder.java.core.ContextDescription;
2526
import com.oregor.trinity.scaffolder.java.core.ProjectDescription;
@@ -70,12 +71,11 @@ private static void scaffoldJava() {
7071
private static void generateJava() {
7172
Project project =
7273
ProjectBuilder.of("invoiceful")
73-
// .addContext(
74-
// ContextAccess.get(
75-
// new PackageName(String.format("%s.%s", JAVA_ROOT_PACKAGE,
76-
// "access")),
77-
// contextGenerator("access", "access", "acs_", "access"),
78-
// deducers("access")))
74+
.addContext(
75+
ContextAccess.get(
76+
new PackageName(String.format("%s.%s", JAVA_ROOT_PACKAGE, "access")),
77+
contextGenerator("access", "access", "acs_", "access"),
78+
deducers("access")))
7979
.addContext(
8080
ContextInvoicing.get(
8181
new PackageName(String.format("%s.%s", JAVA_ROOT_PACKAGE, "invoicing")),

polygenesis-exporters/polygenesis-exporter-file/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
<artifactId>polygenesis-core</artifactId>
2020
</dependency>
2121

22+
<!--GOOGLE JAVA FORMAT-->
23+
<dependency>
24+
<groupId>com.google.googlejavaformat</groupId>
25+
<artifactId>google-java-format</artifactId>
26+
<version>${google-java-format.version}</version>
27+
</dependency>
28+
2229
<!--TEST-->
2330
<dependency>
2431
<groupId>io.polygenesis</groupId>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-
2+
* ==========================LICENSE_START=================================
3+
* PolyGenesis Platform
4+
* ========================================================================
5+
* Copyright (C) 2015 - 2019 Christos Tsakostas, OREGOR LTD
6+
* ========================================================================
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ===========================LICENSE_END==================================
19+
*/
20+
21+
package io.polygenesis.core;
22+
23+
import com.google.googlejavaformat.java.Formatter;
24+
import com.google.googlejavaformat.java.FormatterException;
25+
import com.google.googlejavaformat.java.JavaFormatterOptions;
26+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
27+
28+
/**
29+
* The type Abstract exporter.
30+
*
31+
* @author Christos Tsakostas
32+
*/
33+
public abstract class AbstractExporter {
34+
35+
// ===============================================================================================
36+
// STATIC
37+
// ===============================================================================================
38+
39+
private static Formatter formatter =
40+
new Formatter(JavaFormatterOptions.builder().style(Style.GOOGLE).build());
41+
42+
// ===============================================================================================
43+
// PROTECTED FUNCTIONALITY
44+
// ===============================================================================================
45+
46+
/**
47+
* Format string.
48+
*
49+
* @param sourceString the source string
50+
* @param fileName the file name
51+
* @return the string
52+
*/
53+
protected String format(String sourceString, String fileName) {
54+
if (!fileName.endsWith(".java")) {
55+
return sourceString;
56+
}
57+
58+
try {
59+
return formatter.formatSourceAndFixImports(sourceString);
60+
} catch (FormatterException e) {
61+
throw new IllegalStateException(e.getMessage(), e);
62+
}
63+
}
64+
}

polygenesis-exporters/polygenesis-exporter-file/src/main/java/io/polygenesis/core/ActiveFileExporter.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
import io.polygenesis.commons.assertion.Assertion;
2424
import io.polygenesis.commons.path.PathService;
2525
import java.io.ByteArrayOutputStream;
26-
import java.io.FileOutputStream;
2726
import java.io.IOException;
27+
import java.nio.charset.Charset;
28+
import java.nio.file.Files;
2829
import java.nio.file.Paths;
2930

3031
/**
3132
* The type Active file exporter.
3233
*
3334
* @author Christos Tsakostas
3435
*/
35-
public class ActiveFileExporter implements Exporter {
36+
public class ActiveFileExporter extends AbstractExporter implements Exporter {
37+
38+
// ===============================================================================================
39+
// OVERRIDES
40+
// ===============================================================================================
3641

3742
@Override
3843
public void export(ByteArrayOutputStream byteArrayOutputStream, ExportInfo exportInfo) {
@@ -43,16 +48,13 @@ public void export(ByteArrayOutputStream byteArrayOutputStream, ExportInfo expor
4348

4449
PathService.ensurePath(exportInfo.getGenerationPath());
4550

46-
FileOutputStream fileOutputStream;
47-
try {
48-
fileOutputStream =
49-
new FileOutputStream(
50-
Paths.get(exportInfo.getGenerationPath().toString(), exportInfo.getFileName())
51-
.toFile());
52-
53-
byteArrayOutputStream.writeTo(fileOutputStream);
51+
String formattedContent =
52+
format(byteArrayOutputStream.toString(Charset.defaultCharset()), exportInfo.getFileName());
5453

55-
fileOutputStream.close();
54+
try {
55+
Files.write(
56+
Paths.get(exportInfo.getGenerationPath().toString(), exportInfo.getFileName()),
57+
formattedContent.getBytes());
5658
} catch (IOException e) {
5759
throw new IllegalStateException(e.getMessage(), e);
5860
}

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
<!--INFLECTOR-->
129129
<evo-inflector.version>1.2.2</evo-inflector.version>
130130

131+
<!--GOOGLE JAVA FORMAT-->
132+
<google-java-format.version>1.7</google-java-format.version>
133+
131134
<!--TRINITY-->
132135
<trinity-scaffolder-java.version>0.0.7</trinity-scaffolder-java.version>
133136

@@ -192,6 +195,13 @@
192195
<version>${evo-inflector.version}</version>
193196
</dependency>
194197

198+
<!--GOOGLE JAVA FORMAT-->
199+
<dependency>
200+
<groupId>com.google.googlejavaformat</groupId>
201+
<artifactId>google-java-format</artifactId>
202+
<version>${google-java-format.version}</version>
203+
</dependency>
204+
195205
<!--TRINITY-->
196206
<dependency>
197207
<groupId>com.oregor.trinity.scaffolder.java</groupId>

0 commit comments

Comments
 (0)