|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.gradle; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.Charset; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.TreeMap; |
| 29 | + |
| 30 | +import org.gradle.api.DefaultTask; |
| 31 | +import org.gradle.api.tasks.InputFile; |
| 32 | +import org.gradle.api.tasks.OutputFile; |
| 33 | +import org.gradle.api.tasks.TaskAction; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * A task to create a notice file which includes dependencies' notices. |
| 38 | + */ |
| 39 | +public class NoticeTask extends DefaultTask { |
| 40 | + |
| 41 | + private static final Charset UTF8 = Charset.forName("UTF-8"); |
| 42 | + @InputFile |
| 43 | + private File inputFile = getProject().getRootProject().file("NOTICE.txt"); |
| 44 | + |
| 45 | + @OutputFile |
| 46 | + private File outputFile = new File(getProject().getBuildDir(), "notices/" + getName() + "/NOTICE.txt"); |
| 47 | + |
| 48 | + /** |
| 49 | + * Directories to include notices from |
| 50 | + */ |
| 51 | + private List<File> licensesDirs = new ArrayList<>(); |
| 52 | + |
| 53 | + public NoticeTask() { |
| 54 | + setDescription("Create a notice file from dependencies"); |
| 55 | + // Default licenses directory is ${projectDir}/licenses (if it exists) |
| 56 | + File licensesDir = new File(getProject().getProjectDir(), "licenses"); |
| 57 | + if (licensesDir.exists()) { |
| 58 | + licensesDirs.add(licensesDir); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add notices from the specified directory. |
| 64 | + */ |
| 65 | + public void setLicensesDir(File licensesDir) { |
| 66 | + licensesDirs.add(licensesDir); |
| 67 | + } |
| 68 | + |
| 69 | + public void setInputFile(File inputFile) { |
| 70 | + this.inputFile = inputFile; |
| 71 | + } |
| 72 | + |
| 73 | + @TaskAction |
| 74 | + public void generateNotice() throws IOException { |
| 75 | + StringBuilder output = new StringBuilder(); |
| 76 | + output.append(read(inputFile)); |
| 77 | + output.append("\n\n"); |
| 78 | + // This is a map rather than a set so that the sort order is the 3rd |
| 79 | + // party component names, unaffected by the full path to the various files |
| 80 | + Map<String, File> seen = new TreeMap<>(); |
| 81 | + for (File licensesDir : licensesDirs) { |
| 82 | + File[] files = licensesDir.listFiles((file) -> file.getName().matches(".*-NOTICE\\.txt")); |
| 83 | + if (files == null) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + for (File file : files) { |
| 87 | + String name = file.getName().substring(0, file.getName().length() - "-NOTICE.txt".length()); |
| 88 | + if (seen.containsKey(name)) { |
| 89 | + File prevFile = seen.get(name); |
| 90 | + if (!read(prevFile).equals(read(file))) { |
| 91 | + throw new RuntimeException("Two different notices exist for dependency '" + |
| 92 | + name + "': " + prevFile + " and " + file); |
| 93 | + } |
| 94 | + } else { |
| 95 | + seen.put(name, file); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + for (Map.Entry<String, File> entry : seen.entrySet()) { |
| 100 | + String name = entry.getKey(); |
| 101 | + File file = entry.getValue(); |
| 102 | + appendFile(file, name, "NOTICE", output); |
| 103 | + appendFile(new File(file.getParent(), name + "-LICENSE.txt"), name, "LICENSE", output); |
| 104 | + } |
| 105 | + outputFile.getParentFile().mkdirs(); |
| 106 | + outputFile.createNewFile(); |
| 107 | + write(outputFile, output.toString()); |
| 108 | + } |
| 109 | + |
| 110 | + private String read(File file) throws IOException { |
| 111 | + return new String(Files.readAllBytes(file.toPath()), UTF8); |
| 112 | + } |
| 113 | + |
| 114 | + private void write(File file, String content) throws IOException { |
| 115 | + Files.write(file.toPath(), content.getBytes(UTF8)); |
| 116 | + } |
| 117 | + |
| 118 | + private void appendFile(File file, String name, String type, StringBuilder output) throws IOException { |
| 119 | + String text = read(file); |
| 120 | + if (text.trim().isEmpty()) { |
| 121 | + return; |
| 122 | + } |
| 123 | + output.append("================================================================================\n"); |
| 124 | + output.append(name).append(" ").append(type).append("\n"); |
| 125 | + output.append("================================================================================\n"); |
| 126 | + output.append(text).append("\n\n"); |
| 127 | + } |
| 128 | +} |
0 commit comments