diff --git a/tools/copyright.js b/tools/copyright.js index 32208193..48d41111 100644 --- a/tools/copyright.js +++ b/tools/copyright.js @@ -10,8 +10,33 @@ const OS = require("os"); const SECTION_START = "AUTOGENERATED COPYRIGHT HEADER START"; const SECTION_END = "AUTOGENERATED COPYRIGHT HEADER END"; +const IGNORED = [ + ".git", + "cmake/clang", + "cmake/version", + "third-party", +] + +let abortAllWork = false; + +async function isIgnored(path) { + let rpath = PATH.relative(process.cwd(), path).replaceAll(PATH.sep, PATH.posix.sep); + for (let ignore of IGNORED) { + if (ignore instanceof RegExp) { + if (ignore.global) { + if (!rpath.matchAll(ignore).done) { + return true; + } + } else { + if (rpath.match(ignore) !== null) { + return true; + } + } + } else if (rpath.startsWith(ignore)) { + return true; + } + } -async function git_isIgnored(path) { return await new Promise((resolve, reject) => { try { let proc = CHILD_PROCESS.spawn("git", [ @@ -245,13 +270,24 @@ function makeHeader(file, copyright) { async function addCopyright(file) { try { + if (abortAllWork) { + return; + } // Async/Promises + // Copyright information. + let copyright = await generateCopyright(file); + let header = undefined; + try { + header = makeHeader(file, copyright); + } catch (ex) { + return; + } + console.log(`Updating file '${file}'...`); + + // File contents. let content = await FSPROMISES.readFile(file); let eol = (content.indexOf("\r\n") != -1 ? OS.EOL : "\n"); - - let copyright = await generateCopyright(file); - let header = makeHeader(file, copyright); let insert = Buffer.from(header.join(eol) + eol); // Find the starting point. @@ -264,7 +300,11 @@ async function addCopyright(file) { endHeader = content.indexOf(eol, endHeader); endHeader += Buffer.from(eol).byteLength; - let fd = await FSPROMISES.open(file, "w+"); + if (abortAllWork) { + return; + } + + let fd = await FSPROMISES.open(file, "w"); let fp = []; if ((startHeader >= 0) && (endHeader >= 0)) { let pos = 0; @@ -318,7 +358,10 @@ async function addCopyright(file) { } async function addCopyrights(path) { - if (await git_isIgnored(path)) { + if (abortAllWork) { + return; + } + if (await isIgnored(path)) { return; } @@ -326,16 +369,20 @@ async function addCopyrights(path) { let files = await FSPROMISES.readdir(path, { "withFileTypes": true }); for (let file of files) { + if (abortAllWork) { + break; + } + let fullname = PATH.join(path, file.name); - if (await git_isIgnored(fullname)) { + if (await isIgnored(fullname)) { console.log(`Ignoring path '${fullname}'...`); continue; } + if (file.isDirectory()) { - console.log(`Scanning path '${fullname}'...`); + //console.log(`Scanning path '${fullname}'...`); promises.push(addCopyrights(fullname)); } else { - console.log(`Updating file '${fullname}'...`); promises.push(addCopyright(fullname)); } } @@ -344,12 +391,36 @@ async function addCopyrights(path) { } (async function () { - let file = PROCESS.argv[2]; - let pathStat = await FSPROMISES.stat(file); + PROCESS.on("SIGINT", (ev) => { + abortAllWork = true; + console.log("Sanely aborting all pending work..."); + }) + + let path = PATH.resolve(PROCESS.argv[2]); + + { // Bootstrap to actually be in the directory where '.git' is. + let is_git_directory = false; + while (!is_git_directory) { + if (abortAllWork) { + return; + } + + let entries = await FSPROMISES.readdir(PROCESS.cwd()); + if (entries.includes(".git")) { + console.log(`Found .git at '${process.cwd()}'.`); + is_git_directory = true; + } else { + PROCESS.chdir(PATH.resolve(PATH.join(PROCESS.cwd(), ".."))); + } + } + path = PATH.normalize(PATH.relative(process.cwd(), path)); + } + + let pathStat = await FSPROMISES.stat(path); if (pathStat.isDirectory()) { - await addCopyrights(PATH.resolve(file)); + await addCopyrights(PATH.resolve(path)); } else { - await addCopyright(PATH.resolve(file)); + await addCopyright(PATH.resolve(path)); } console.log("Done"); })();