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

Skip existing copyright lines #323

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
@Parameter(property = "license.skipExistingHeaders", defaultValue = "false")
public boolean skipExistingHeaders = false;

/**
* Skip the formatting of files in which the header matches other than text on the copyright line.
*/
@Parameter(property = "license.skipExistingCopyrightLine", defaultValue = "false")
public boolean skipExistingCopyrightLine = false;

/**
* When enforcing licenses on dependencies, exclude all but these scopes.
*/
Expand Down Expand Up @@ -642,12 +648,12 @@ public Properties load(final Document document) {
callback.onUnknownFile(document, h);
} else if (document.is(h)) {
debug("Skipping header file: %s", document.getFilePath());
} else if (document.hasHeader(h, strictCheck)) {
} else if (document.hasHeader(h, strictCheck, skipExistingCopyrightLine)) {
callback.onExistingHeader(document, h);
} else {
boolean headerFound = false;
for (final Header validHeader : validHeaders) {
headerFound = document.hasHeader(validHeader, strictCheck);
headerFound = document.hasHeader(validHeader, strictCheck, skipExistingCopyrightLine);
if (headerFound) {
callback.onExistingHeader(document, h);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,55 @@ public boolean isNotSupported() {
}

public boolean hasHeader(Header header, boolean strictCheck) {
return hasHeader(header, strictCheck, false);
}

public boolean hasHeader(Header header, boolean strictCheck, boolean skipExistingCopyrightLine) {
if (!strictCheck) {
try {
String fileHeader = readFirstLines(file, header.getLineCount() + 10, encoding);
String fileHeaderOneLine = remove(fileHeader, headerDefinition.getFirstLine().trim(), headerDefinition.getEndLine().trim(), headerDefinition.getBeforeEachLine().trim(), "\n", "\r", "\t", " ");
String headerOnOnelIne = mergeProperties(header.asOneLineString());
if (skipExistingCopyrightLine) {
// extract the copyright line from the file and put it in the header
String existingCopyrightLine = getCopyrightLine(fileHeader);
String headerCopyrightLine = getCopyrightLine(header.asString());
if (existingCopyrightLine != null) {
headerOnOnelIne = headerOnOnelIne.replace(headerCopyrightLine, existingCopyrightLine);
}
}
return fileHeaderOneLine.contains(remove(headerOnOnelIne, headerDefinition.getFirstLine().trim(), headerDefinition.getEndLine().trim(), headerDefinition.getBeforeEachLine().trim()));
} catch (IOException e) {
throw new IllegalStateException("Cannot read file " + getFilePath() + ". Cause: " + e.getMessage(), e);
}
}
try {
if (skipExistingCopyrightLine) {
return header.isMatchForTextKeepingCopyrightLine(this, headerDefinition, true, encoding);
}
return header.isMatchForText(this, headerDefinition, true, encoding);
} catch (IOException e) {
throw new IllegalStateException("Cannot read file " + getFilePath() + ". Cause: " + e.getMessage(), e);
}
}

public static String getCopyrightLine(String header) {
int index = header.toLowerCase().indexOf("copyright");
if (index < 0) {
return null;
}
String c = header.substring(index);
index = c.indexOf('\r');
if (index > 0) {
c = c.substring(0, index);
}
index = c.indexOf('\n');
if (index > 0) {
c = c.substring(0, index);
}
return c.trim();
}

public void updateHeader(Header header) {
String headerStr = header.applyDefinitionAndSections(parser.getHeaderDefinition(), parser.getFileContent().isUnix());
parser.getFileContent().insert(parser.getBeginPosition(), mergeProperties(headerStr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ public boolean isMatchForText(Document d, HeaderDefinition headerDefinition, boo
return isMatchForText(expected, fileHeader, headerDefinition, unix);
}

public boolean isMatchForTextKeepingCopyrightLine(Document d, HeaderDefinition headerDefinition, boolean unix, String encoding) throws IOException {
String fileHeader = readFirstLines(d.getFile(), getLineCount() + 10, encoding).replaceAll(" *\r?\n", "\n");
String existingCopyrightLine = Document.getCopyrightLine(fileHeader);
if (existingCopyrightLine == null) {
return isMatchForText(d, headerDefinition, unix, encoding);
}
String headerCopyrightLine = Document.getCopyrightLine(headerContent);
String expected = buildForDefinition(headerDefinition, unix);
expected = d.mergeProperties(expected).replace(headerCopyrightLine, existingCopyrightLine);
return isMatchForText(expected, fileHeader, headerDefinition, unix);
}

public String applyDefinitionAndSections(HeaderDefinition headerDefinition, boolean unix) {

String expected = buildForDefinition(headerDefinition, unix);
Expand Down
Loading