Skip to content

Commit

Permalink
Support addition of license header to generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Apr 26, 2024
1 parent 676f104 commit 40be031
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Expand Up @@ -80,5 +80,12 @@ public class ModelloParameterConstants {
*/
public static final String XSD_ENFORCE_MANDATORY_ELEMENTS = "modello.xsd.enforce.mandatory.element";

/**
* The license text as string, to be added to generated files, if needed.
*
* @since 2.3.1
*/
public static final String LICENSE_TEXT = "modello.license.text";

private ModelloParameterConstants() {}
}
Expand Up @@ -66,6 +66,8 @@ public abstract class AbstractModelloGenerator implements ModelloGenerator {

private String encoding;

private String licenseText;

@Inject
private BuildContext buildContext;

Expand All @@ -85,6 +87,8 @@ protected void initialize(Model model, Properties parameters) throws ModelloExce
packageWithVersion = Boolean.valueOf(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));

encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);

licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
}

protected Model getModel() {
Expand All @@ -108,13 +112,27 @@ protected String getEncoding() {
}

protected String getHeader() {
String header = getLicenseHeader();
if (header == null) {
return getGeneratedHeader();
}
header += "\n";
header += getGeneratedHeader();
return header;
}

private String getGeneratedHeader() {
String version = getClass().getPackage().getImplementationVersion();
return "=================== DO NOT EDIT THIS FILE ====================\n"
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + ",\n"
+ "any modifications will be overwritten.\n"
+ "==============================================================";
}

private String getLicenseHeader() {
return licenseText;
}

protected boolean isClassInModel(String fieldType, Model model) {
try {
return model.getClass(fieldType, generatedVersion) != null;
Expand Down
Expand Up @@ -25,6 +25,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -95,6 +99,15 @@ public abstract class AbstractModelloGeneratorMojo extends AbstractMojo {
@Parameter
private List<String> packagedVersions = new ArrayList<String>();

/**
* The contents of license header, verbatim. It may be file path and if file exists, will be loaded up. Otherwise,
* the contents of this parameter is reused as-is.
*
* @since 2.3.1
*/
@Parameter
private String licenseText;

/**
* @since 1.0.1
*/
Expand Down Expand Up @@ -160,11 +173,29 @@ public void execute() throws MojoExecutionException {

parameters.setProperty(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(packageWithVersion));

if (packagedVersions.size() > 0) {
if (!packagedVersions.isEmpty()) {
parameters.setProperty(
ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
}

if (licenseText != null && !licenseText.trim().isEmpty()) {
String license = "";
try {
Path licenseFile = Paths.get(licenseText);
if (Files.isRegularFile(licenseFile)) {
license = String.join("\n", Files.readAllLines(licenseFile));
}
} catch (IOException e) {
throw new MojoExecutionException("Could not load up license text from " + licenseText, e);
} catch (InvalidPathException e) {
// ignore, is verbatim text probably
}
if (license.isEmpty()) {
license = licenseText;
}
parameters.setProperty(ModelloParameterConstants.LICENSE_TEXT, license);
}

customizeParameters(parameters);

// ----------------------------------------------------------------------
Expand Down

0 comments on commit 40be031

Please sign in to comment.