|
| 1 | +#!/usr/bin/env groovy |
| 2 | +package com.itextpdf.copyright |
| 3 | + |
| 4 | +import groovy.cli.commons.CliBuilder |
| 5 | +import groovy.ant.FileNameFinder |
| 6 | + |
| 7 | +void processScript(String... args) { |
| 8 | + def time = System.currentTimeMillis() |
| 9 | + def date = Calendar.getInstance() |
| 10 | + |
| 11 | + def scriptDir = "" |
| 12 | + def inclList = "" |
| 13 | + |
| 14 | + def cli = new CliBuilder() |
| 15 | + cli.h(type: Boolean, 'help') |
| 16 | + cli.i(type: String, longOpt: 'includePattern', 'pattern in Ant\'s fileset format') |
| 17 | + cli.d(type: String, longOpt: 'dir', 'base dir for search (LICENSE.md should be present in destination folder)') |
| 18 | + def options = cli.parse(args) |
| 19 | + |
| 20 | + if (options == null) { |
| 21 | + throw new ScriptException("", 101) |
| 22 | + } |
| 23 | + |
| 24 | + if ( options.h ) { |
| 25 | + cli.usage() |
| 26 | + throw new ScriptException("", 0) |
| 27 | + } |
| 28 | + |
| 29 | + if ( options.i ) { |
| 30 | + inclListFile = options.i |
| 31 | + println "Including files in list '$inclListFile'" |
| 32 | + new File(inclListFile).eachLine { line -> |
| 33 | + inclList = inclList + " " + line |
| 34 | + } |
| 35 | + } else { |
| 36 | + println "Including all files" |
| 37 | + inclList = '**/*.java **/*.cs **/*.nuspec' |
| 38 | + } |
| 39 | + |
| 40 | + if ( options.arguments()[0] ) { |
| 41 | + scriptDir = options.arguments()[0].trim() |
| 42 | + } else if (options.d) { |
| 43 | + scriptDir = options.d.trim() |
| 44 | + } else { |
| 45 | + scriptDir = new File("").getAbsolutePath() |
| 46 | + } |
| 47 | + println "Starting groovy script @ $scriptDir" |
| 48 | + |
| 49 | + def licenseFile = new File("${scriptDir}/LICENSE.md") |
| 50 | + println "licenseFile is ${licenseFile.canonicalPath}" |
| 51 | + |
| 52 | + copyrightYear = "Copyright (c) 1998-" + date.get(Calendar.YEAR) + " Apryse Group NV" |
| 53 | + def sb = new StringBuilder() |
| 54 | + sb << "/*\n" |
| 55 | + sb << "This file is part of the iText (R) project.\n" |
| 56 | + sb << "" + copyrightYear |
| 57 | + sb << "\n" |
| 58 | + sb << "Authors: Apryse Software.\n\n" |
| 59 | + sb << licenseFile.text |
| 60 | + sb << " \n*/\n" |
| 61 | + copyrightText = sb.toString() |
| 62 | + def producerLine = /private static final String producerLine = iTextProductName \+ " " \+ release \+ " \\u00a92000-.*?Apryse Group NV";/ |
| 63 | + |
| 64 | + def sourceFolder = new File(scriptDir) |
| 65 | + if ( ! sourceFolder.directory ) { |
| 66 | + println "${sourceFolder.name} is not an existing folder" |
| 67 | + println "Aborting port attempt" |
| 68 | + return |
| 69 | + } else { |
| 70 | + println "Source folder is ${sourceFolder.canonicalPath}" |
| 71 | + } |
| 72 | + |
| 73 | + def other_copyrights = [ |
| 74 | + "Adobe" : /Copyright .* Adobe Systems Incorporated/, |
| 75 | + "Apache" : /Copyright .* Apache Software Foundation/, |
| 76 | + "AL2.0" : /Apache License, Version 2.0/, |
| 77 | + "ZXing" : /Copyright .* ZXing authors/, |
| 78 | + "Sun" : /Copyright .* Sun Microsystems, Inc./, |
| 79 | + "Fontbox" : /Copyright .* www.fontbox.org/, |
| 80 | + "SGI" : /Copyright .* Silicon Graphics, Inc./, |
| 81 | + "Clipper" : /Copyright .* Angus Johnson/, |
| 82 | + "Oracle" : /Copyright .* Oracle/, |
| 83 | + "Zlib" : /Copyright .* ymnk, JCraft,Inc./, |
| 84 | + "zlib" : /Copyright .* Lapo Luchini/, |
| 85 | + "sboxes" : /Copyright: .* Dr B. R Gladman/, |
| 86 | + "iharder" : /@author Robert Harder/, |
| 87 | + "Google" : /Copyright .* Google Inc./, |
| 88 | + "MIT" : /Distributed under MIT license/ |
| 89 | + ] |
| 90 | + |
| 91 | +// needed when run on Windows |
| 92 | + if ( File.separator.equals("\\") ) { |
| 93 | + inclList = inclList.replace("/", "\\\\") |
| 94 | + } |
| 95 | + |
| 96 | + fileList = new FileNameFinder().getFileNames(scriptDir, inclList) |
| 97 | + fileList.each {fileStr -> |
| 98 | + File file = new File(fileStr) |
| 99 | + skip = false |
| 100 | + |
| 101 | + def content = new StringBuilder() |
| 102 | + def changed = false |
| 103 | + |
| 104 | + if ( file.name.endsWith("package-info.java") ) { |
| 105 | + skip = true |
| 106 | + } |
| 107 | + |
| 108 | + if ( file.canonicalPath.contains("itext.kernel/bouncycastle") ) { |
| 109 | + skip = true |
| 110 | + } |
| 111 | + |
| 112 | + other_copyrights.each { author, copyright -> |
| 113 | + if ( file.text.find(copyright) ) { |
| 114 | + skip = true |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if ( !skip ) { |
| 119 | + copyright = /Copyright .* Apryse/ |
| 120 | + if ( file.text.find(copyright) ) { |
| 121 | + file.eachLine('UTF-8') { line -> |
| 122 | + if ( line.find(copyright) && !line.find(date.get(Calendar.YEAR) + "") ) { |
| 123 | + println "[YEAR] ${line} - ${file.canonicalPath}" |
| 124 | + if ( file.name.endsWith("AssemblyInfo.cs") ) { |
| 125 | + content << "[assembly: AssemblyCopyright(\"" + copyrightYear + "\")]" |
| 126 | + } else if ( file.name.endsWith(".nuspec") ) { |
| 127 | + content << "" + "<copyright>${copyrightYear}</copyright>" |
| 128 | + } else { |
| 129 | + content << "" + copyrightYear |
| 130 | + } |
| 131 | + changed = true |
| 132 | + } else { |
| 133 | + content << line |
| 134 | + } |
| 135 | + content << "\n" |
| 136 | + } |
| 137 | + } else { |
| 138 | + println "[MISSING] Copyright added to ${file.canonicalPath}" |
| 139 | + content << copyrightText |
| 140 | + content << file.getText('UTF-8') |
| 141 | + changed = true |
| 142 | + } |
| 143 | + if ( changed ) { |
| 144 | + file.write(content.toString(), "UTF8") |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + copyrightTo = "COPYRIGHT_TO = .*?;" |
| 149 | + if (file.name.contains("ProductData")) { |
| 150 | + String contents = file.getText('UTF-8') |
| 151 | + contents = contents.replaceAll( copyrightTo , "COPYRIGHT_TO = " + date.get(Calendar.YEAR) + ";") |
| 152 | + file.write(contents, "UTF-8") |
| 153 | + } |
| 154 | + |
| 155 | + // the following block of code makes sense only for 7.1. Drop these lines when 7.1 is no longer supported |
| 156 | + if ( file.absolutePath.endsWith("kernel" + File.separator + "Version.java") ) { |
| 157 | + String contents = file.getText('UTF-8') |
| 158 | + contents = contents.replaceAll( producerLine, 'private static final String producerLine = iTextProductName + " " + release + " \\\\u00a92000-' + date.get(Calendar.YEAR) + ' iText Group NV";' ) |
| 159 | + file.write(contents, "UTF-8") |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + println "\nCopyright check took " + ( ( System.currentTimeMillis() - time ) / 1000 ) + " seconds." |
| 164 | +} |
| 165 | + |
| 166 | +class ScriptException extends Exception { |
| 167 | + int exitCode |
| 168 | + |
| 169 | + ScriptException(String message, int exitCode) { |
| 170 | + super(message) |
| 171 | + this.exitCode = exitCode |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +try { |
| 176 | + processScript(args) |
| 177 | +} catch (ScriptException e) { |
| 178 | + System.exit(e.exitCode) |
| 179 | +} |
0 commit comments