Browse Source

Add js files to license

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
tags/v22.0.0beta1
John Molakvoæ (skjnldsv) 3 years ago
parent
commit
91510d95f1
No account linked to committer's email address
3 changed files with 106 additions and 36 deletions
  1. 1
    1
      .eslintrc.js
  2. 104
    34
      build/license.php
  3. 1
    1
      package.json

+ 1
- 1
.eslintrc.js View File

escapeHTML: true, escapeHTML: true,
oc_userconfig: true, oc_userconfig: true,
dayNames: true, dayNames: true,
firstDay: true
firstDay: true,
}, },
extends: ['@nextcloud'], extends: ['@nextcloud'],
rules: { rules: {

+ 104
- 34
build/license.php View File

<?php <?php

/** /**
* @author Thomas Müller * @author Thomas Müller
* *
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
* *
*/ */

class Licenses { class Licenses {
protected $paths = []; protected $paths = [];
protected $mailMap = []; protected $mailMap = [];


$excludes = array_map(function ($item) use ($folder) { $excludes = array_map(function ($item) use ($folder) {
return $folder . '/' . $item; 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 RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveCallbackFilterIterator($iterator, function ($item) use ($folder, $excludes) { $iterator = new RecursiveCallbackFilterIterator($iterator, function ($item) use ($folder, $excludes) {
return true; return true;
}); });
$iterator = new RecursiveIteratorIterator($iterator); $iterator = new RecursiveIteratorIterator($iterator);
$iterator = new RegexIterator($iterator, '/^.+\.php$/i');
$iterator = new RegexIterator($iterator, '/^.+\.(js)$/i');


foreach ($iterator as $file) { foreach ($iterator as $file) {
/** @var SplFileInfo $file */ /** @var SplFileInfo $file */
} }


public function handleFile($path, $gitRoot) { public function handleFile($path, $gitRoot) {
$isPhp = preg_match('/^.+\.php$/i', $path);

$source = file_get_contents($path); $source = file_get_contents($path);
if ($this->isMITLicensed($source)) { if ($this->isMITLicensed($source)) {
echo "MIT licensed file: $path" . PHP_EOL; echo "MIT licensed file: $path" . PHP_EOL;
} }


if ($copyrightNotices === '') { if ($copyrightNotices === '') {
$license = str_replace('@COPYRIGHT@', ' *', $license);
$creator = $this->getCreatorCopyright($path, $gitRoot);
$license = str_replace('@COPYRIGHT@', $creator, $license);
} else { } else {
$license = str_replace('@COPYRIGHT@', $copyrightNotices, $license); $license = str_replace('@COPYRIGHT@', $copyrightNotices, $license);
} }


[$source, $isStrict] = $this->eatOldLicense($source); [$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 { } 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; echo "License updated: $path" . PHP_EOL;
} }


private function eatOldLicense($source) { private function eatOldLicense($source) {
$lines = explode(PHP_EOL, $source); $lines = explode(PHP_EOL, $source);
$isStrict = false; $isStrict = false;

$index = 0;
while (!empty($lines)) { while (!empty($lines)) {
$line = $lines[0];
$line = $lines[$index];

if (trim($line) === '<?php') { 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; continue;
} }
if (strpos($line, '<?php declare(strict_types') !== false) { if (strpos($line, '<?php declare(strict_types') !== false) {
$isStrict = true; $isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue; continue;
} }
if (strpos($line, 'declare (strict_types') !== false) { if (strpos($line, 'declare (strict_types') !== false) {
$isStrict = true; $isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue; continue;
} }
if (strpos($line, 'declare(strict_types') !== false) { if (strpos($line, 'declare(strict_types') !== false) {
$isStrict = true; $isStrict = true;
array_shift($lines);
array_splice($lines, $index, 1);
continue; continue;
} }
if (strpos($line, '/**') !== false) { if (strpos($line, '/**') !== false) {
array_shift($lines);
array_splice($lines, $index, 1);
continue; 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; break;
} }
if (strpos($line, '*') !== false) { if (strpos($line, '*') !== false) {
array_shift($lines);
array_splice($lines, $index, 1);
continue; continue;
} }
if (trim($line) === '') { if (trim($line) === '') {
array_shift($lines);
array_splice($lines, $index, 1);
continue; continue;
} }
break; break;
} }


private function getCopyrightNotices($path, $file) { 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 = []; $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); return implode(PHP_EOL, $copyrightNotice);
} }
} }


private function filterAuthors($authors = []) {
$authors = array_filter($authors, function ($author) {
return !in_array($author, [
'',
'Not Committed Yet <not.committed.yet>',
'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
]);
});

// 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) { private function getAuthors($file, $gitRoot) {
// only add authors that changed code and not the license header // 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")); $licenseHeaderEndsAtLine = trim(shell_exec("grep -n '*/' $file | head -n 1 | cut -d ':' -f 1"));
$buildDir = getcwd(); $buildDir = getcwd();

if ($gitRoot) { if ($gitRoot) {
chdir($gitRoot); chdir($gitRoot);
$file = substr($file, strlen($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"); $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) { if ($gitRoot) {
chdir($buildDir); chdir($buildDir);
} }
$authors = explode(PHP_EOL, $out);


$authors = array_filter($authors, function ($author) {
return !in_array($author, [
'',
'Not Committed Yet <not.committed.yet>',
'Jenkins for ownCloud <owncloud-bot@tmit.eu>',
'Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>',
]);
});
$authors = explode(PHP_EOL, $out);
$authors = $this->filterAuthors($authors);


if ($gitRoot) { if ($gitRoot) {
$authors = array_map([$this, 'checkCoreMailMap'], $authors); $authors = array_map([$this, 'checkCoreMailMap'], $authors);
'../apps/admin_audit', '../apps/admin_audit',
'../apps/cloud_federation_api', '../apps/cloud_federation_api',
'../apps/comments', '../apps/comments',
'../apps/contactsinteraction',
'../apps/dashboard',
'../apps/dav', '../apps/dav',
'../apps/encryption', '../apps/encryption',
'../apps/federatedfilesharing', '../apps/federatedfilesharing',
'../apps/twofactor_backupcodes', '../apps/twofactor_backupcodes',
'../apps/updatenotification', '../apps/updatenotification',
'../apps/user_ldap', '../apps/user_ldap',
'../apps/user_status',
'../apps/weather_status',
'../apps/workflowengine',
'../build/integration/features/bootstrap', '../build/integration/features/bootstrap',
'../core', '../core',
'../lib', '../lib',

+ 1
- 1
package.json View File

"dev": "NODE_ENV=development webpack --progress --config webpack.dev.js", "dev": "NODE_ENV=development webpack --progress --config webpack.dev.js",
"watch": "NODE_ENV=development webpack --progress --watch --config webpack.dev.js", "watch": "NODE_ENV=development webpack --progress --watch --config webpack.dev.js",
"lint": "eslint '**/src/**/*.{vue,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": "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\"" "test:watch": "mochapack -w --webpack-config core/webpack.test.js --require core/src/tests/setup.js \"core/src/tests/**/*.spec.js\""
}, },

Loading…
Cancel
Save