Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support addition of license header to generated files #437

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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() {}
}
Original file line number Diff line number Diff line change
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;
}

protected 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"
+ "==============================================================";
}

protected String getLicenseHeader() {
return licenseText;
}

protected boolean isClassInModel(String fieldType, Model model) {
try {
return model.getClass(fieldType, generatedVersion) != null;
Expand Down
Original file line number Diff line number Diff line change
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;
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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));
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
}
} 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