Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewrite tools/format.py in deno
Note: findFiles and findFilesWalk are borrowed from the previous attempt of @pseudo-su (#1434). Thanks!
- Loading branch information
Showing
21 changed files
with
209 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env deno --allow-run | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import * as deno from "deno"; | ||
import { join } from "../js/deps/https/deno.land/x/std/path/index.ts"; | ||
import { findFiles } from "./util.ts"; | ||
|
||
const clangFormat = join("third_party", "depot_tools", "clang-format"); | ||
const gn = join("third_party", "depot_tools", "gn"); | ||
const prettier = join( | ||
"third_party", | ||
"node_modules", | ||
"prettier", | ||
"bin-prettier.js" | ||
); | ||
const yapf = join("third_party", "python_packages", "bin", "yapf"); | ||
const rustfmt = join("third_party", "rustfmt", deno.platform.os, "rustfmt"); | ||
const rustfmtConfig = join("tools", "rustfmt.toml"); | ||
|
||
const run = (...args: string[]) => { | ||
if (deno.platform.os === "win") { | ||
args = ["cmd.exe", "/c", ...args]; | ||
} | ||
return deno.run({ args, stdout: "null", stderr: "piped" }).status(); | ||
}; | ||
|
||
(async () => { | ||
console.log("clang_format"); | ||
await run( | ||
clangFormat, | ||
"-i", | ||
"-style", | ||
"Google", | ||
...findFiles(["libdeno"], [".cc", ".h"]) | ||
); | ||
|
||
console.log("gn format"); | ||
for (const fn of [ | ||
"BUILD.gn", | ||
".gn", | ||
...findFiles(["build_extra", "libdeno"], [".gn", ".gni"]) | ||
]) { | ||
await run(gn, "format", fn); | ||
} | ||
|
||
console.log("yapf"); | ||
await run( | ||
"python", | ||
yapf, | ||
"-i", | ||
...findFiles(["tools", "build_extra"], [".py"], { | ||
skip: [join("tools", "clang")] | ||
}) | ||
); | ||
|
||
console.log("prettier"); | ||
await run( | ||
"node", | ||
prettier, | ||
"--write", | ||
"--loglevel=error", | ||
"rollup.config.js", | ||
...findFiles(["."], [".json", ".md"], { depth: 1 }), | ||
...findFiles( | ||
[".github", "js", "tests", "tools", "website"], | ||
[".js", ".json", ".ts", ".md"], | ||
{ skip: [join("tools", "clang"), join("js", "deps")] } | ||
) | ||
); | ||
|
||
console.log("rustfmt"); | ||
await run( | ||
rustfmt, | ||
"--config-path", | ||
rustfmtConfig, | ||
"build.rs", | ||
...findFiles(["src"], [".rs"]) | ||
); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("bar"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("foo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("bar"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("foo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("bar"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("foo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("bar"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("foo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { lstatSync, readDirSync } from "deno"; | ||
|
||
export interface FindOptions { | ||
skip?: string[]; | ||
depth?: number; | ||
} | ||
|
||
/** | ||
* Finds files of the give extensions under the given paths recursively. | ||
* @param dirs directories | ||
* @param exts extensions | ||
* @param skip patterns to ignore | ||
* @param depth depth to find | ||
*/ | ||
export function findFiles( | ||
dirs: string[], | ||
exts: string[], | ||
{ skip = [], depth = 20 }: FindOptions = {} | ||
) { | ||
return findFilesWalk(dirs, depth).filter( | ||
path => | ||
exts.some(ext => path.endsWith(ext)) && | ||
skip.every(pattern => !path.includes(pattern)) | ||
); | ||
} | ||
|
||
function findFilesWalk(paths: string[], depth: number) { | ||
if (depth < 0) { | ||
return []; | ||
} | ||
|
||
const foundPaths = paths.map(path => | ||
lstatSync(path).isDirectory() | ||
? findFilesWalk(readDirSync(path).map(f => f.path), depth - 1) | ||
: path | ||
); | ||
|
||
return [].concat(...foundPaths); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { test, assertEqual } from "../js/test_util.ts"; | ||
import { findFiles } from "./util.ts"; | ||
|
||
const testDir = "tools/testdata/find_files_testdata"; | ||
|
||
// Sorts and replace backslashes with slashes. | ||
const normalize = files => files.map(f => f.replace(/\\/g, "/")).sort(); | ||
|
||
test(function testFindFiles() { | ||
const files = findFiles([testDir], [".ts", ".md"]); | ||
assertEqual(normalize(files), [ | ||
`${testDir}/bar.md`, | ||
`${testDir}/bar.ts`, | ||
`${testDir}/foo.md`, | ||
`${testDir}/foo.ts`, | ||
`${testDir}/subdir0/bar.ts`, | ||
`${testDir}/subdir0/foo.ts`, | ||
`${testDir}/subdir0/subdir0/bar.ts`, | ||
`${testDir}/subdir0/subdir0/foo.ts`, | ||
`${testDir}/subdir1/bar.ts`, | ||
`${testDir}/subdir1/foo.ts` | ||
]); | ||
}); | ||
|
||
test(function testFindFilesDepth() { | ||
const files = findFiles([testDir], [".ts", ".md"], { depth: 1 }); | ||
assertEqual(normalize(files), [ | ||
`${testDir}/bar.md`, | ||
`${testDir}/bar.ts`, | ||
`${testDir}/foo.md`, | ||
`${testDir}/foo.ts` | ||
]); | ||
}); | ||
|
||
test(function testFindFilesSkip() { | ||
const files = findFiles([testDir], [".ts", ".md"], { | ||
skip: ["foo.md", "subdir1"] | ||
}); | ||
assertEqual(normalize(files), [ | ||
`${testDir}/bar.md`, | ||
`${testDir}/bar.ts`, | ||
`${testDir}/foo.ts`, | ||
`${testDir}/subdir0/bar.ts`, | ||
`${testDir}/subdir0/foo.ts`, | ||
`${testDir}/subdir0/subdir0/bar.ts`, | ||
`${testDir}/subdir0/subdir0/foo.ts` | ||
]); | ||
}); |