Skip to content

Commit

Permalink
Adding flag to enable conditional write of writeIntermediateModel
Browse files Browse the repository at this point in the history
  • Loading branch information
RanVaknin committed Feb 21, 2025
1 parent b8398bc commit 649e235
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public class GenerationMojo extends AbstractMojo {
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
private String outputDirectory;

@Parameter(defaultValue = "false")
@Parameter(property = "writeIntermediateModel", defaultValue = "false")
private boolean writeIntermediateModel;


@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

Expand Down Expand Up @@ -117,7 +118,6 @@ private void generateCode(C2jModels models) {
.resourcesDirectory(resourcesDirectory.toFile().getAbsolutePath())
.testsDirectory(testsDirectory.toFile().getAbsolutePath())
.intermediateModelFileNamePrefix(intermediateModelFileNamePrefix(models))
.targetDirectory(outputDirectory)
.build()
.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class CodeGenerator {
* The prefix for the file name that contains the intermediate model.
*/
private final String fileNamePrefix;
private final String targetDirectory;

static {
// Make sure ClassName is statically initialized before we do anything in parallel.
Expand All @@ -57,7 +56,6 @@ public CodeGenerator(Builder builder) {
this.resourcesDirectory = builder.resourcesDirectory != null ? builder.resourcesDirectory
: builder.sourcesDirectory;
this.fileNamePrefix = builder.fileNamePrefix;
this.targetDirectory = builder.targetDirectory;
}

public static File getModelDirectory(String outputDirectory) {
Expand All @@ -84,10 +82,6 @@ public void execute() {
if (fileNamePrefix != null) {
writeIntermediateModel(intermediateModel);
}

File targetDir = new File(targetDirectory);
writeToTargetDirectory(intermediateModel, targetDir);

emitCode(intermediateModel);

} catch (Exception e) {
Expand All @@ -97,29 +91,6 @@ public void execute() {
}
}

private void writeToTargetDirectory(IntermediateModel model, File targetDir) throws IOException {
PrintWriter writer = null;
try {
if (!targetDir.exists() && !targetDir.mkdirs()) {
throw new RuntimeException("Failed to create " + targetDir.getAbsolutePath());
}

File outputFile = new File(targetDir, "intermediate-model.json");

if (!outputFile.exists() && !outputFile.createNewFile()) {
throw new RuntimeException("Error creating file " + outputFile.getAbsolutePath());
}

writer = new PrintWriter(outputFile, "UTF-8");
Jackson.writeWithObjectMapper(model, writer);
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}

private void writeIntermediateModel(IntermediateModel model) throws IOException {
File modelDir = getModelDirectory(sourcesDirectory);
PrintWriter writer = null;
Expand Down Expand Up @@ -167,7 +138,6 @@ public static final class Builder {
private String resourcesDirectory;
private String testsDirectory;
private String fileNamePrefix;
private String targetDirectory;

private Builder() {
}
Expand Down Expand Up @@ -196,12 +166,6 @@ public Builder intermediateModelFileNamePrefix(String fileNamePrefix) {
this.fileNamePrefix = fileNamePrefix;
return this;
}

public Builder targetDirectory(String targetDirectory) {
this.targetDirectory = targetDirectory;
return this;
}

/**
* @return An immutable {@link CodeGenerator} object.
*/
Expand Down

0 comments on commit 649e235

Please sign in to comment.