Skip to content

Commit

Permalink
Export targetDirectory so it can be used by internal release pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
RanVaknin committed Feb 19, 2025
1 parent fe68709 commit 4bb425e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ 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,6 +41,7 @@ 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 @@ -56,6 +57,7 @@ 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 Down Expand Up @@ -83,13 +85,38 @@ public void execute() {
writeIntermediateModel(intermediateModel);
}

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

emitCode(intermediateModel);

} catch (Exception e) {
log.error(() -> "Failed to generate code. ", e);
throw new RuntimeException(
"Failed to generate code. Exception message : " + e.getMessage(), e);
}
}

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();
}
}
}

Expand Down Expand Up @@ -140,6 +167,7 @@ public static final class Builder {
private String resourcesDirectory;
private String testsDirectory;
private String fileNamePrefix;
private String targetDirectory;

private Builder() {
}
Expand Down Expand Up @@ -169,6 +197,11 @@ public Builder intermediateModelFileNamePrefix(String 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 4bb425e

Please sign in to comment.