-
Notifications
You must be signed in to change notification settings - Fork 25.2k
This is for NoticeTask for issue #34459 #37032
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
Changes from 2 commits
4719951
21da41e
81ef89a
ae7f102
9de4048
1eee007
ec5c5e2
e58e252
d97350d
d672af5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.gradle; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import java.io.IOException; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* A task to create a notice file which includes dependencies' notices. | ||
*/ | ||
public class NoticeTask extends DefaultTask { | ||
|
||
@InputFile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The annotations have no effect on the private fields, they need to be placed on public getters. |
||
private File inputFile = getProject().getRootProject().file("NOTICE.txt"); | ||
@OutputFile | ||
private File outputFile = new File(getProject().getBuildDir(), "notices/" + getName() + "/NOTICE.txt"); | ||
|
||
/** Directories to include notices from */ | ||
private List<File> licensesDirs = new ArrayList<>(); | ||
|
||
public File getInputFile() { | ||
return inputFile; | ||
} | ||
|
||
public void setInputFile(File inputFile) { | ||
alpar-t marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.inputFile = inputFile; | ||
} | ||
|
||
public File getOutputFile() { | ||
return outputFile; | ||
} | ||
|
||
public void setOutputFile(File outputFile) { | ||
alpar-t marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.outputFile = outputFile; | ||
} | ||
|
||
public void licensesDir(File licensesDir) { | ||
licensesDirs.add(licensesDir); | ||
} | ||
|
||
public void addLicenseDir(File licenseDir){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need another method that does the exact thing. |
||
this.licensesDir(licenseDir); | ||
} | ||
public List<File> getLicensesDirs(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not annotated as such in the Groovy code, but it should be |
||
return Collections.unmodifiableList(this.licensesDirs); | ||
} | ||
|
||
// public static String getText(File file){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove commented code. |
||
// return getText(file, StandardCharsets.UTF_8); | ||
// } | ||
|
||
/** | ||
* Add notices from the specified directory. | ||
*/ | ||
|
||
public NoticeTask() { | ||
setDescription("Create a notice file from dependencies"); | ||
// Default licenses directory is ${projectDir}/licenses (if it exists) | ||
File licensesDir = new File(getProject().getProjectDir(), "licenses"); | ||
if (licensesDir.exists()) { | ||
licensesDirs.add(licensesDir); | ||
} | ||
|
||
} | ||
|
||
@TaskAction | ||
public void generateNotice() { | ||
final StringBuilder output = new StringBuilder(); | ||
|
||
try{ | ||
Files.lines(this.inputFile.toPath(), StandardCharsets.UTF_8) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One needs to close the stream when using Files.lines There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! I was unaware of the stream not closing this, I am very glad you pointed it out. |
||
.forEach(s -> { | ||
output.append(s.trim()); | ||
output.append("\n"); | ||
}); | ||
output.append("\n\n"); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a logging anti pattern as it will most likely cause the same stack trace to be printed twice. |
||
throw new RuntimeException(e); | ||
} | ||
|
||
// This is a map rather than a set so that the sort order is the 3rd | ||
// party component names, unaffected by the full path to the various files | ||
final Map<String, File> seen = new TreeMap<>(); | ||
licensesDirs.stream() | ||
.flatMap(file -> { | ||
try { | ||
return Files.walk(file.toPath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one needs to be closed too |
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
// throw new RuntimeException("failed to walk for " + file.toString()); | ||
return Stream.empty(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should never silence IOExceptions like this. |
||
} | ||
}) | ||
.filter(path -> path.toString().endsWith("-NOTICE.txt")) | ||
.map(Path::toFile) | ||
.forEach(licenseFile -> { | ||
// Here we remove the "-NOTICE.txt" so the name can be used later for the -LICENSE.txt file | ||
final String name = | ||
licenseFile.getName().substring(0,licenseFile.getName().length() - "-NOTICE.txt".length()); | ||
|
||
if (seen.containsKey(name)){ | ||
File prevLicenseFile = seen.get(name); | ||
if (getText(prevLicenseFile).equals(getText(licenseFile)) == false){ | ||
throw new RuntimeException("Two different notices exist for dependency '" + | ||
name + "': " + prevLicenseFile + " and " + licenseFile); | ||
} | ||
}else { | ||
seen.put(name,licenseFile); | ||
} | ||
}); | ||
|
||
|
||
for (Map.Entry<String,File> entry : seen.entrySet()){ | ||
final String name = entry.getKey(); | ||
final File file = entry.getValue(); | ||
final File licenseFile = new File(file.getParentFile(),name + "-LICENSE.txt"); | ||
|
||
appendFileToOutput(file, name, "NOTICE", output); | ||
appendFileToOutput(licenseFile,name, "LICENSE", output); | ||
} | ||
|
||
writeToFile(outputFile,output.toString(),StandardCharsets.UTF_8); | ||
} | ||
|
||
public static void appendFileToOutput(File file, final String name, final String type, StringBuilder output) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and other methods used in this class only should be private. |
||
String text = getText(file); | ||
|
||
if (text.trim().isEmpty()) { | ||
return; | ||
} | ||
|
||
output.append("================================================================================\n"); | ||
output.append(name + " " + type + "\n"); | ||
output.append("================================================================================\n"); | ||
output.append(text); | ||
output.append("\n\n"); | ||
} | ||
|
||
public static void writeToFile(File file, String output, Charset charset){ | ||
|
||
try(BufferedWriter out = Files.newBufferedWriter(file.toPath(),charset)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would prefer to use |
||
out.write(output); | ||
out.flush(); | ||
}catch (IOException exception){ | ||
exception.printStackTrace(); | ||
throw new RuntimeException("Unable to write to file " + file.getName()); | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran into issues with using the NIO Files methods using the UTF-8 charsets failing when they were reading the notice files and would run into a "java.nio.charset.MalformedInputException: Input length = 1" when running assemble command using Gradle. So I used the decoder here to cause it to replace the bad characters with the expected bad characters for UTF-8 so it can be clearly seen what characters were replaced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer not to allow leniency in general, so it would be preferable to throw a meaningful error showing the file(s) that don't have the correct encoding and then fix those up rather than live with inconsistent encodings or broken characters. @elasticmachine test this please |
||
|
||
public static String getText(File file){ | ||
final StringBuilder builder = new StringBuilder(); | ||
try(BufferedReader reader = new BufferedReader(new FileReader(file))){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would prefer to use |
||
char[] buffer = new char[8192]; | ||
int read; | ||
|
||
while ((read = reader.read(buffer)) != -1){ | ||
builder.append(buffer,0,read); | ||
} | ||
|
||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
throw new RuntimeException("Unable to read file " + file.toString()); | ||
} | ||
return builder.toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't allow * imports, this is why the build fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been replaced with individual imports.