Add js files to license

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2020-12-17 11:45:13 +01:00
parent 89cf1cb33a
commit 91510d95f1
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
3 changed files with 111 additions and 41 deletions

View File

@ -8,7 +8,7 @@ module.exports = {
escapeHTML: true,
oc_userconfig: true,
dayNames: true,
firstDay: true
firstDay: true,
},
extends: ['@nextcloud'],
rules: {

View File

@ -1,5 +1,4 @@
<?php
/**
* @author Thomas Müller
*
@ -19,6 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
class Licenses {
protected $paths = [];
protected $mailMap = [];
@ -98,7 +98,7 @@ EOD;
$excludes = array_map(function ($item) use ($folder) {
return $folder . '/' . $item;
}, ['vendor', '3rdparty', '.git', 'l10n', 'templates', 'composer']);
}, ['vendor', '3rdparty', '.git', 'l10n', 'templates', 'composer', 'js', 'node_modules']);
$iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveCallbackFilterIterator($iterator, function ($item) use ($folder, $excludes) {
@ -111,7 +111,7 @@ EOD;
return true;
});
$iterator = new RecursiveIteratorIterator($iterator);
$iterator = new RegexIterator($iterator, '/^.+\.php$/i');
$iterator = new RegexIterator($iterator, '/^.+\.(js)$/i');
foreach ($iterator as $file) {
/** @var SplFileInfo $file */
@ -140,6 +140,8 @@ With help from many libraries and frameworks including:
}
public function handleFile($path, $gitRoot) {
$isPhp = preg_match('/^.+\.php$/i', $path);
$source = file_get_contents($path);
if ($this->isMITLicensed($source)) {
echo "MIT licensed file: $path" . PHP_EOL;
@ -155,18 +157,25 @@ With help from many libraries and frameworks including:
}
if ($copyrightNotices === '') {
$license = str_replace('@COPYRIGHT@', ' *', $license);
$creator = $this->getCreatorCopyright($path, $gitRoot);
$license = str_replace('@COPYRIGHT@', $creator, $license);
} else {
$license = str_replace('@COPYRIGHT@', $copyrightNotices, $license);
}
[$source, $isStrict] = $this->eatOldLicense($source);
if ($isStrict) {
$source = "<?php" . PHP_EOL . PHP_EOL . 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $license . PHP_EOL . $source;
if ($isPhp) {
if ($isStrict) {
$source = "<?php" . PHP_EOL . PHP_EOL . 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $license . PHP_EOL . $source;
} else {
$source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
}
} else {
$source = "<?php" . PHP_EOL . $license . PHP_EOL . $source;
$source = $license . PHP_EOL . PHP_EOL . $source;
}
file_put_contents($path,$source);
file_put_contents($path, $source);
echo "License updated: $path" . PHP_EOL;
}
@ -207,41 +216,55 @@ With help from many libraries and frameworks including:
private function eatOldLicense($source) {
$lines = explode(PHP_EOL, $source);
$isStrict = false;
$index = 0;
while (!empty($lines)) {
$line = $lines[0];
$line = $lines[$index];
if (trim($line) === '<?php') {
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
// Skipping if the line contains important js keywords
if (strpos($line, 'eslint-') !== false
|| strpos($line, 'globals') !== false
|| strpos($line, 'const') !== false
|| strpos($line, 'import') !== false) {
$index++;
continue;
}
if (strpos($line, '<?php declare(strict_types') !== false) {
$isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
if (strpos($line, 'declare (strict_types') !== false) {
$isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
if (strpos($line, 'declare(strict_types') !== false) {
$isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
if (strpos($line, '/**') !== false) {
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
if (strpos($line, '*/') !== false) {
array_shift($lines);
// If we reach the end of the copyright header (and it's not a one-line comment /* xxx */)
if (strpos($line, '*/') !== false && strpos($line, '/*') !== false) {
array_splice($lines, $index, 1);
break;
}
if (strpos($line, '*') !== false) {
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
if (trim($line) === '') {
array_shift($lines);
array_splice($lines, $index, 1);
continue;
}
break;
@ -251,14 +274,14 @@ With help from many libraries and frameworks including:
}
private function getCopyrightNotices($path, $file) {
$licenseHeaderEndsAtLine = (int)trim(shell_exec("grep -n '*/' $path | head -n 1 | cut -d ':' -f 1"));
$lineByLine = explode(PHP_EOL, $file, $licenseHeaderEndsAtLine + 1);
$licenseHeaderCopyrightAtLines = trim(shell_exec("grep -ni 'copyright' $path | cut -d ':' -f 1"));
$lineByLine = explode(PHP_EOL, $file);
$copyrightNotice = [];
$licensePart = array_slice($lineByLine, 0, $licenseHeaderEndsAtLine);
foreach ($licensePart as $line) {
if (strpos($line, '@copyright') !== false) {
$copyrightNotice[] = $line;
}
if (trim($licenseHeaderCopyrightAtLines !== '')) {
$copyrightNotice = array_map(function ($line) use ($lineByLine) {
return $lineByLine[(int)$line - 1];
}, explode(PHP_EOL, $licenseHeaderCopyrightAtLines));
}
return implode(PHP_EOL, $copyrightNotice);
@ -311,20 +334,7 @@ With help from many libraries and frameworks including:
}
}
private function getAuthors($file, $gitRoot) {
// only add authors that changed code and not the license header
$licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
$buildDir = getcwd();
if ($gitRoot) {
chdir($gitRoot);
$file = substr($file, strlen($gitRoot));
}
$out = shell_exec("git blame --line-porcelain -L $licenseHeaderEndsAtLine, $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /' | sort -f | uniq");
if ($gitRoot) {
chdir($buildDir);
}
$authors = explode(PHP_EOL, $out);
private function filterAuthors($authors = []) {
$authors = array_filter($authors, function ($author) {
return !in_array($author, [
'',
@ -334,6 +344,61 @@ With help from many libraries and frameworks including:
]);
});
// Strip out dependabot
$authors = array_filter($authors, function ($author) {
return strpos($author, 'dependabot') === false;
});
return $authors;
}
private function getCreatorCopyright($file, $gitRoot) {
$buildDir = getcwd();
if ($gitRoot) {
chdir($gitRoot);
$file = substr($file, strlen($gitRoot));
}
$year = trim(shell_exec('date +%Y -d "$(git log --format=%aD ../apps/files/lib/Controller/ViewController.php | tail -1)"'));
$blame = shell_exec("git blame --line-porcelain $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /'");
$authors = explode(PHP_EOL, $blame);
if ($gitRoot) {
chdir($buildDir);
}
$authors = $this->filterAuthors($authors);
if ($gitRoot) {
$authors = array_map([$this, 'checkCoreMailMap'], $authors);
$authors = array_unique($authors);
}
$creator = array_key_exists(0, $authors)
? $this->fixInvalidEmail($authors[0])
: '';
return " * @copyright Copyright (c) $year $creator";
}
private function getAuthors($file, $gitRoot) {
// only add authors that changed code and not the license header
$licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
$buildDir = getcwd();
if ($gitRoot) {
chdir($gitRoot);
$file = substr($file, strlen($gitRoot));
}
$out = shell_exec("git blame --line-porcelain -L $licenseHeaderEndsAtLine, $file | sed -n 's/^author //p;s/^author-mail //p' | sed 'N;s/\\n/ /' | sort -f | uniq");
if ($gitRoot) {
chdir($buildDir);
}
$authors = explode(PHP_EOL, $out);
$authors = $this->filterAuthors($authors);
if ($gitRoot) {
$authors = array_map([$this, 'checkCoreMailMap'], $authors);
$authors = array_unique($authors);
@ -386,6 +451,8 @@ if (isset($argv[1])) {
'../apps/admin_audit',
'../apps/cloud_federation_api',
'../apps/comments',
'../apps/contactsinteraction',
'../apps/dashboard',
'../apps/dav',
'../apps/encryption',
'../apps/federatedfilesharing',
@ -406,6 +473,9 @@ if (isset($argv[1])) {
'../apps/twofactor_backupcodes',
'../apps/updatenotification',
'../apps/user_ldap',
'../apps/user_status',
'../apps/weather_status',
'../apps/workflowengine',
'../build/integration/features/bootstrap',
'../core',
'../lib',

View File

@ -12,7 +12,7 @@
"dev": "NODE_ENV=development webpack --progress --config webpack.dev.js",
"watch": "NODE_ENV=development webpack --progress --watch --config webpack.dev.js",
"lint": "eslint '**/src/**/*.{vue,js}'",
"lint:fix": "eslint '**/*.{vue,js}' --fix",
"lint:fix": "eslint '**/src/**/*.{vue,js}' --fix",
"test": "mochapack --webpack-config core/webpack.test.js --require core/src/tests/setup.js \"core/src/tests/**/*.spec.js\"",
"test:watch": "mochapack -w --webpack-config core/webpack.test.js --require core/src/tests/setup.js \"core/src/tests/**/*.spec.js\""
},