From 3d423e114e46f206ad2c6bfa5dfcb22094c5d2f6 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 13 Jan 2023 13:42:15 -0500 Subject: [PATCH] chore: small cleanup of scripts in ./tools and run copyright checker in lint.js (#17393) --- tools/bench/mod.js | 2 + tools/bench/rebench.js | 2 + tools/bench/rebootstrap.js | 2 + tools/copyright_checker.js | 18 ++--- tools/flamebench.js | 146 ++++++++++++++++++------------------- tools/format.js | 52 +++---------- tools/lint.js | 50 ++++++------- tools/upload_wptfyi.js | 2 + 8 files changed, 121 insertions(+), 153 deletions(-) diff --git a/tools/bench/mod.js b/tools/bench/mod.js index 6858342813..9d90818a5b 100644 --- a/tools/bench/mod.js +++ b/tools/bench/mod.js @@ -1,2 +1,4 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + export * from "./rebench.js"; export * from "./rebootstrap.js"; diff --git a/tools/bench/rebench.js b/tools/bench/rebench.js index bbc5a3c2f4..7d77a79cd1 100644 --- a/tools/bench/rebench.js +++ b/tools/bench/rebench.js @@ -1,3 +1,5 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + export function benchSync(name, n, innerLoop) { const t1 = Date.now(); for (let i = 0; i < n; i++) { diff --git a/tools/bench/rebootstrap.js b/tools/bench/rebootstrap.js index 6090843d81..0e94285d59 100644 --- a/tools/bench/rebootstrap.js +++ b/tools/bench/rebootstrap.js @@ -1,3 +1,5 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + import { dirname, fromFileUrl, join } from "../../test_util/std/path/mod.ts"; import { expandGlobSync } from "../../test_util/std/fs/mod.ts"; diff --git a/tools/copyright_checker.js b/tools/copyright_checker.js index 706f59dbdf..c8ddcdc919 100644 --- a/tools/copyright_checker.js +++ b/tools/copyright_checker.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S deno run --unstable --allow-read --allow-run +#!/usr/bin/env -S deno run --unstable --allow-read=. --allow-run=git // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { getSources, ROOT_PATH } from "./util.js"; @@ -16,7 +16,7 @@ async function readFirstPartOfFile(filePath) { } } -async function checkCopyright() { +export async function checkCopyright() { const sourceFiles = await getSources(ROOT_PATH, [ // js and ts "*.js", @@ -28,7 +28,6 @@ async function checkCopyright() { ":!:cli/tsc/*typescript.js", ":!:cli/tsc/compiler.d.ts", ":!:test_util/wpt/**", - ":!:tools/**", // these files are starts with `#!/usr/bin/env` ":!:cli/tools/init/templates/**", // rust @@ -58,8 +57,9 @@ async function checkCopyright() { continue; } + // use .includes(...) because the first line might be a shebang if ( - !fileText.startsWith( + !fileText.includes( "// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.", ) ) { @@ -68,17 +68,11 @@ async function checkCopyright() { } } - console.log("\nTotal errors: " + totalCount); - if (totalCount > 0) { - Deno.exit(1); + throw new Error(`Copyright checker had ${totalCount} errors.`); } } -async function main() { - await Deno.chdir(ROOT_PATH); - +if (import.meta.main) { await checkCopyright(); } - -await main(); diff --git a/tools/flamebench.js b/tools/flamebench.js index e471d2891e..5d1fc161d5 100755 --- a/tools/flamebench.js +++ b/tools/flamebench.js @@ -2,6 +2,77 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { join, ROOT_PATH as ROOT } from "./util.js"; +const { 0: benchName, 1: benchFilter } = Deno.args; +// Print usage if no bench specified +if (!benchName) { + console.log("flamebench.js [bench_filter]"); + // Also show available benches + console.log("\nAvailable benches:"); + const benches = await availableBenches(); + console.log(benches.join("\n")); + return Deno.exit(1); +} + +// List available benches, hoping we don't have any benches called "ls" :D +if (benchName === "ls") { + const benches = await availableBenches(); + console.log(benches.join("\n")); + return; +} + +// Ensure flamegraph is installed +if (!await binExists("flamegraph")) { + console.log( + "flamegraph (https://github.com/flamegraph-rs/flamegraph) not found, please run:", + ); + console.log(); + console.log("cargo install flamegraph"); + return Deno.exit(1); +} + +// Build bench with frame pointers +await bashThrough( + `RUSTFLAGS='-C force-frame-pointers=y' cargo build --release --bench ${benchName}`, +); + +// Get the freshly built bench binary +const benchBin = await latestBenchBin(benchName); + +// Run flamegraph +const outputFile = join(ROOT, "flamebench.svg"); +await runFlamegraph(benchBin, benchFilter, outputFile); + +// Open flamegraph (in your browser / SVG viewer) +if (await binExists("open")) { + await bashThrough(`open ${outputFile}`); +} + +async function availableBenches() { + // TODO(AaronO): maybe reimplement with fs.walk + // it's important to prune the walked tree so this is fast (<50ms) + const prunedDirs = ["third_party", ".git", "target", "docs", "test_util"]; + const pruneExpr = prunedDirs.map((d) => `-path ${ROOT}/${d}`).join(" -o "); + return (await bashOut(` + find ${ROOT} -type d \ + \\( ${pruneExpr} \\) \ + -prune -false -o \ + -path "${ROOT}/*/benches/*" -type f -name "*.rs" \ + | xargs basename | cut -f1 -d. + `)).split("\n"); +} + +function latestBenchBin(name) { + return bashOut(`ls -t "${ROOT}/target/release/deps/${name}"* | head -n 1`); +} + +function runFlamegraph(benchBin, benchFilter, outputFile) { + return bashThrough( + `sudo -E flamegraph -o ${outputFile} ${benchBin} ${benchFilter ?? ""}`, + // Set $PROFILING env so benches can improve their flamegraphs + { env: { "PROFILING": "1" } }, + ); +} + async function bashOut(subcmd) { const { success, stdout } = await new Deno.Command("bash", { args: ["-c", subcmd], @@ -33,32 +104,6 @@ async function bashThrough(subcmd, opts = {}) { } } -async function availableBenches() { - // TODO(AaronO): maybe reimplement with fs.walk - // it's important to prune the walked tree so this is fast (<50ms) - const prunedDirs = ["third_party", ".git", "target", "docs", "test_util"]; - const pruneExpr = prunedDirs.map((d) => `-path ${ROOT}/${d}`).join(" -o "); - return (await bashOut(` - find ${ROOT} -type d \ - \\( ${pruneExpr} \\) \ - -prune -false -o \ - -path "${ROOT}/*/benches/*" -type f -name "*.rs" \ - | xargs basename | cut -f1 -d. - `)).split("\n"); -} - -function latestBenchBin(name) { - return bashOut(`ls -t "${ROOT}/target/release/deps/${name}"* | head -n 1`); -} - -function runFlamegraph(benchBin, benchFilter, outputFile) { - return bashThrough( - `sudo -E flamegraph -o ${outputFile} ${benchBin} ${benchFilter ?? ""}`, - // Set $PROFILING env so benches can improve their flamegraphs - { env: { "PROFILING": "1" } }, - ); -} - async function binExists(bin) { try { await bashOut(`which ${bin}`); @@ -67,52 +112,3 @@ async function binExists(bin) { return false; } } - -async function main() { - const { 0: benchName, 1: benchFilter } = Deno.args; - // Print usage if no bench specified - if (!benchName) { - console.log("flamebench.js [bench_filter]"); - // Also show available benches - console.log("\nAvailable benches:"); - const benches = await availableBenches(); - console.log(benches.join("\n")); - return Deno.exit(1); - } - - // List available benches, hoping we don't have any benches called "ls" :D - if (benchName === "ls") { - const benches = await availableBenches(); - console.log(benches.join("\n")); - return; - } - - // Ensure flamegraph is installed - if (!await binExists("flamegraph")) { - console.log( - "flamegraph (https://github.com/flamegraph-rs/flamegraph) not found, please run:", - ); - console.log(); - console.log("cargo install flamegraph"); - return Deno.exit(1); - } - - // Build bench with frame pointers - await bashThrough( - `RUSTFLAGS='-C force-frame-pointers=y' cargo build --release --bench ${benchName}`, - ); - - // Get the freshly built bench binary - const benchBin = await latestBenchBin(benchName); - - // Run flamegraph - const outputFile = join(ROOT, "flamebench.svg"); - await runFlamegraph(benchBin, benchFilter, outputFile); - - // Open flamegraph (in your browser / SVG viewer) - if (await binExists("open")) { - await bashThrough(`open ${outputFile}`); - } -} -// Run -await main(); diff --git a/tools/format.js b/tools/format.js index 223eb1b0d4..bd34b1cfd9 100755 --- a/tools/format.js +++ b/tools/format.js @@ -2,45 +2,15 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { getPrebuiltToolPath, join, ROOT_PATH } from "./util.js"; -async function dprint() { - const configFile = join(ROOT_PATH, ".dprint.json"); - const execPath = getPrebuiltToolPath("dprint"); - const cmd = new Deno.Command(execPath, { - args: ["fmt", "--config=" + configFile], - stdout: "inherit", - stderr: "inherit", - }); +const subcommand = Deno.args.includes("--check") ? "check" : "fmt"; +const configFile = join(ROOT_PATH, ".dprint.json"); +const execPath = getPrebuiltToolPath("dprint"); +const cmd = new Deno.Command(execPath, { + args: [subcommand, "--config=" + configFile], + cwd: ROOT_PATH, + stdout: "inherit", + stderr: "inherit", +}); - const { code } = await cmd.output(); - - if (code > 0) { - throw new Error("dprint failed"); - } -} - -async function main() { - await Deno.chdir(ROOT_PATH); - await dprint(); - - if (Deno.args.includes("--check")) { - const cmd = new Deno.Command("git", { - args: ["status", "-uno", "--porcelain", "--ignore-submodules"], - stderr: "inherit", - }); - - const { code, stdout } = await cmd.output(); - - if (code > 0) { - throw new Error("git status failed"); - } - const out = new TextDecoder().decode(stdout); - - if (out) { - console.log("run tools/format.js"); - console.log(out); - Deno.exit(1); - } - } -} - -await main(); +const { code } = await cmd.output(); +Deno.exit(code); diff --git a/tools/lint.js b/tools/lint.js index c460951f5d..caa7618960 100755 --- a/tools/lint.js +++ b/tools/lint.js @@ -7,6 +7,28 @@ import { join, ROOT_PATH, } from "./util.js"; +import { checkCopyright } from "./copyright_checker.js"; + +let didLint = false; + +if (Deno.args.includes("--js")) { + await dlint(); + await dlintPreferPrimordials(); + didLint = true; +} + +if (Deno.args.includes("--rs")) { + await clippy(); + didLint = true; +} + +if (!didLint) { + await dlint(); + await dlintPreferPrimordials(); + console.log("copyright checker"); + await checkCopyright(); + await clippy(); +} async function dlint() { const configFile = join(ROOT_PATH, ".dlint.json"); @@ -44,6 +66,7 @@ async function dlint() { const chunks = splitToChunks(sourceFiles, `${execPath} run`.length); for (const chunk of chunks) { const cmd = new Deno.Command(execPath, { + cwd: ROOT_PATH, args: ["run", "--config=" + configFile, ...chunk], stdout: "inherit", stderr: "inherit", @@ -77,6 +100,7 @@ async function dlintPreferPrimordials() { const chunks = splitToChunks(sourceFiles, `${execPath} run`.length); for (const chunk of chunks) { const cmd = new Deno.Command(execPath, { + cwd: ROOT_PATH, args: ["run", "--rule", "prefer-primordials", ...chunk], stdout: "inherit", stderr: "inherit", @@ -116,6 +140,7 @@ async function clippy() { } const cargoCmd = new Deno.Command("cargo", { + cwd: ROOT_PATH, args: [ ...cmd, "--", @@ -131,28 +156,3 @@ async function clippy() { throw new Error("clippy failed"); } } - -async function main() { - await Deno.chdir(ROOT_PATH); - - let didLint = false; - - if (Deno.args.includes("--js")) { - await dlint(); - await dlintPreferPrimordials(); - didLint = true; - } - - if (Deno.args.includes("--rs")) { - await clippy(); - didLint = true; - } - - if (!didLint) { - await dlint(); - await dlintPreferPrimordials(); - await clippy(); - } -} - -await main(); diff --git a/tools/upload_wptfyi.js b/tools/upload_wptfyi.js index 476c1981d7..d57347eb03 100644 --- a/tools/upload_wptfyi.js +++ b/tools/upload_wptfyi.js @@ -1,3 +1,5 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + // This script pushes new WPT results to wpt.fyi. When the `--ghstatus` flag is // passed, will automatically add a status check to the commit with a link to // the wpt.fyi page.