From 0b0d962eb9cf6e41f840a65d3718c4d0a12eeb54 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 12 Feb 2019 15:20:32 -0500 Subject: [PATCH] Revert "Rewrite tools/format.py in deno (#1528)" (#1752) tools/format.ts is making CI flaky and it's difficult to run right now. Reverting to tools/format.py This reverts commit f19622e7681b7753788137706e535f72c3ebb38e. --- .prettierignore | 3 + .travis.yml | 2 +- js/unit_tests.ts | 2 - tools/format.py | 50 +++++++++++ tools/format.ts | 86 ------------------- tools/test_format.py | 28 +----- tools/testdata/find_files_testdata/bar.md | 1 - tools/testdata/find_files_testdata/bar.ts | 1 - tools/testdata/find_files_testdata/bar.txt | 1 - tools/testdata/find_files_testdata/foo.md | 1 - tools/testdata/find_files_testdata/foo.ts | 1 - tools/testdata/find_files_testdata/foo.txt | 1 - .../find_files_testdata/subdir0/bar.ts | 1 - .../find_files_testdata/subdir0/foo.ts | 1 - .../subdir0/subdir0/bar.ts | 1 - .../subdir0/subdir0/foo.ts | 1 - .../find_files_testdata/subdir1/bar.ts | 1 - .../find_files_testdata/subdir1/foo.ts | 1 - tools/util.ts | 72 ---------------- tools/util_test.ts | 61 ------------- website/manual.md | 7 +- 21 files changed, 59 insertions(+), 264 deletions(-) create mode 100644 .prettierignore create mode 100755 tools/format.py delete mode 100755 tools/format.ts delete mode 100644 tools/testdata/find_files_testdata/bar.md delete mode 100644 tools/testdata/find_files_testdata/bar.ts delete mode 100644 tools/testdata/find_files_testdata/bar.txt delete mode 100644 tools/testdata/find_files_testdata/foo.md delete mode 100644 tools/testdata/find_files_testdata/foo.ts delete mode 100644 tools/testdata/find_files_testdata/foo.txt delete mode 100644 tools/testdata/find_files_testdata/subdir0/bar.ts delete mode 100644 tools/testdata/find_files_testdata/subdir0/foo.ts delete mode 100644 tools/testdata/find_files_testdata/subdir0/subdir0/bar.ts delete mode 100644 tools/testdata/find_files_testdata/subdir0/subdir0/foo.ts delete mode 100644 tools/testdata/find_files_testdata/subdir1/bar.ts delete mode 100644 tools/testdata/find_files_testdata/subdir1/foo.ts delete mode 100644 tools/util.ts delete mode 100644 tools/util_test.ts diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000000..9d83dd750e --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +js/flatbuffers.js +tests/error_syntax.js +tests/badly_formatted.js diff --git a/.travis.yml b/.travis.yml index a61e529613..b880932470 100644 --- a/.travis.yml +++ b/.travis.yml @@ -70,8 +70,8 @@ before_script: # Default script for release builds. script: - ./tools/lint.py -- ./tools/build.py -C target/release - ./tools/test_format.py +- ./tools/build.py -C target/release - DENO_BUILD_MODE=release ./tools/test.py jobs: diff --git a/js/unit_tests.ts b/js/unit_tests.ts index edad578931..a5fc836c50 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -45,8 +45,6 @@ import "./url_search_params_test.ts"; import "./write_file_test.ts"; import "./performance_test.ts"; -import "../tools/util_test.ts"; - import "../website/app_test.js"; import "deps/https/deno.land/x/std/testing/main.ts"; diff --git a/tools/format.py b/tools/format.py new file mode 100755 index 0000000000..fe053eaa32 --- /dev/null +++ b/tools/format.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# Copyright 2018 the Deno authors. All rights reserved. MIT license. +from glob import glob +import os +import sys +from third_party import fix_symlinks, google_env, python_env +from third_party import clang_format_path, third_party_path +from util import root_path, run, find_exts, platform + +fix_symlinks() + +prettier = os.path.join(third_party_path, "node_modules", "prettier", + "bin-prettier.js") +tools_path = os.path.join(root_path, "tools") +rustfmt_config = os.path.join(root_path, ".rustfmt.toml") + +os.chdir(root_path) + + +def qrun(cmd, env=None): + run(cmd, quiet=True, env=env) + + +print "clang_format" +qrun([clang_format_path, "-i", "-style", "Google"] + + find_exts(["libdeno"], [".cc", ".h"])) + +print "gn format" +for fn in ["BUILD.gn", ".gn"] + find_exts(["build_extra", "libdeno"], + [".gn", ".gni"]): + qrun(["third_party/depot_tools/gn", "format", fn], env=google_env()) + +print "yapf" +qrun( + [sys.executable, "third_party/python_packages/bin/yapf", "-i"] + find_exts( + ["tools", "build_extra"], [".py"], skip=["tools/clang"]), + env=python_env()) + +print "prettier" +qrun(["node", prettier, "--write", "--loglevel=error"] + ["rollup.config.js"] + + glob("*.json") + glob("*.md") + + find_exts([".github", "js", "tests", "tools", "website"], + [".js", ".json", ".ts", ".md"], + skip=["tools/clang", "js/deps"])) + +print "rustfmt" +qrun([ + "third_party/rustfmt/" + platform() + + "/rustfmt", "--config-path", rustfmt_config, "build.rs" +] + find_exts(["src"], [".rs"])) diff --git a/tools/format.ts b/tools/format.ts deleted file mode 100755 index 33241162b0..0000000000 --- a/tools/format.ts +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env deno --allow-read --allow-run -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { join } from "../js/deps/https/deno.land/x/std/fs/path.ts"; -import { findFiles, lookupDenoPath } from "./util.ts"; - -const clangFormat = join("third_party", "depot_tools", "clang-format"); -const gn = join("third_party", "depot_tools", "gn"); -const yapf = join("third_party", "python_packages", "bin", "yapf"); -const rustfmt = join("third_party", "rustfmt", Deno.platform.os, "rustfmt"); -const rustfmtConfig = ".rustfmt.toml"; - -const decoder = new TextDecoder(); - -async function run(...args: string[]): Promise { - if (Deno.platform.os === "win") { - args = ["cmd.exe", "/c", ...args]; - } - const p = Deno.run({ args, stdout: "piped", stderr: "piped" }); - const { code } = await p.status(); - if (code !== 0) { - console.log(decoder.decode(await Deno.readAll(p.stderr))); - console.log(decoder.decode(await Deno.readAll(p.stdout))); - Deno.exit(code); - } -} - -(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( - lookupDenoPath(), - "--allow-read", - "--allow-write", - "js/deps/https/deno.land/x/std/prettier/main.ts", - "rollup.config.js", - ...findFiles([".", "website"], [".json", ".md"], { depth: 1 }), - ...findFiles( - [".github", "js", "tests", "tools", "website"], - [".js", ".json", ".ts", ".md"], - { - skip: [ - join("tools", "clang"), - join("js", "deps"), - join("tests", "badly_formatted.js"), - join("tests", "error_syntax.js") - ] - } - ) - ); - - console.log("rustfmt"); - await run( - rustfmt, - "--config-path", - rustfmtConfig, - "build.rs", - ...findFiles(["src"], [".rs"]) - ); -})(); diff --git a/tools/test_format.py b/tools/test_format.py index 2e26f21990..a0d5ba08ec 100755 --- a/tools/test_format.py +++ b/tools/test_format.py @@ -1,40 +1,18 @@ #!/usr/bin/env python -# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -# This program fails if ./tools/format.ts changes any files. +# This program fails if ./tools/format.py changes any files. -import os import sys import util import sys import subprocess -from distutils.spawn import find_executable - - -def lookup_deno_path(): - deno_exe = "deno" + util.executable_suffix - release_deno = os.path.join(util.root_path, "target", "release", deno_exe) - debug_deno = os.path.join(util.root_path, "target", "debug", deno_exe) - - if os.path.exists(release_deno): - return release_deno - if os.path.exists(debug_deno): - return debug_deno - - return find_executable("deno") def main(): - deno_path = lookup_deno_path() - - if not deno_path: - print "No available deno executable." - sys.exit(1) - - util.run([deno_path, "--allow-read", "--allow-run", "tools/format.ts"]) + util.run([sys.executable, "tools/format.py"]) output = util.run_output( ["git", "status", "-uno", "--porcelain", "--ignore-submodules"]) if len(output) > 0: - print "Run tools/format.ts " + print "Run tools/format.py " print output sys.exit(1) diff --git a/tools/testdata/find_files_testdata/bar.md b/tools/testdata/find_files_testdata/bar.md deleted file mode 100644 index d93e603dcd..0000000000 --- a/tools/testdata/find_files_testdata/bar.md +++ /dev/null @@ -1 +0,0 @@ -# bar diff --git a/tools/testdata/find_files_testdata/bar.ts b/tools/testdata/find_files_testdata/bar.ts deleted file mode 100644 index 636c1fc277..0000000000 --- a/tools/testdata/find_files_testdata/bar.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("bar"); diff --git a/tools/testdata/find_files_testdata/bar.txt b/tools/testdata/find_files_testdata/bar.txt deleted file mode 100644 index 5716ca5987..0000000000 --- a/tools/testdata/find_files_testdata/bar.txt +++ /dev/null @@ -1 +0,0 @@ -bar diff --git a/tools/testdata/find_files_testdata/foo.md b/tools/testdata/find_files_testdata/foo.md deleted file mode 100644 index c2a839280e..0000000000 --- a/tools/testdata/find_files_testdata/foo.md +++ /dev/null @@ -1 +0,0 @@ -# foo diff --git a/tools/testdata/find_files_testdata/foo.ts b/tools/testdata/find_files_testdata/foo.ts deleted file mode 100644 index 85ce559e8f..0000000000 --- a/tools/testdata/find_files_testdata/foo.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("foo"); diff --git a/tools/testdata/find_files_testdata/foo.txt b/tools/testdata/find_files_testdata/foo.txt deleted file mode 100644 index 257cc5642c..0000000000 --- a/tools/testdata/find_files_testdata/foo.txt +++ /dev/null @@ -1 +0,0 @@ -foo diff --git a/tools/testdata/find_files_testdata/subdir0/bar.ts b/tools/testdata/find_files_testdata/subdir0/bar.ts deleted file mode 100644 index 636c1fc277..0000000000 --- a/tools/testdata/find_files_testdata/subdir0/bar.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("bar"); diff --git a/tools/testdata/find_files_testdata/subdir0/foo.ts b/tools/testdata/find_files_testdata/subdir0/foo.ts deleted file mode 100644 index 85ce559e8f..0000000000 --- a/tools/testdata/find_files_testdata/subdir0/foo.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("foo"); diff --git a/tools/testdata/find_files_testdata/subdir0/subdir0/bar.ts b/tools/testdata/find_files_testdata/subdir0/subdir0/bar.ts deleted file mode 100644 index 636c1fc277..0000000000 --- a/tools/testdata/find_files_testdata/subdir0/subdir0/bar.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("bar"); diff --git a/tools/testdata/find_files_testdata/subdir0/subdir0/foo.ts b/tools/testdata/find_files_testdata/subdir0/subdir0/foo.ts deleted file mode 100644 index 85ce559e8f..0000000000 --- a/tools/testdata/find_files_testdata/subdir0/subdir0/foo.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("foo"); diff --git a/tools/testdata/find_files_testdata/subdir1/bar.ts b/tools/testdata/find_files_testdata/subdir1/bar.ts deleted file mode 100644 index 636c1fc277..0000000000 --- a/tools/testdata/find_files_testdata/subdir1/bar.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("bar"); diff --git a/tools/testdata/find_files_testdata/subdir1/foo.ts b/tools/testdata/find_files_testdata/subdir1/foo.ts deleted file mode 100644 index 85ce559e8f..0000000000 --- a/tools/testdata/find_files_testdata/subdir1/foo.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("foo"); diff --git a/tools/util.ts b/tools/util.ts deleted file mode 100644 index 9d8ad0f1f7..0000000000 --- a/tools/util.ts +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { join } from "../js/deps/https/deno.land/x/std/fs/path/mod.ts"; - -const { platform, lstatSync, readDirSync } = 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); -} - -export const executableSuffix = platform.os === "win" ? ".exe" : ""; - -/** Returns true if the path exists. */ -export function existsSync(path: string): boolean { - try { - lstatSync(path); - } catch (e) { - return false; - } - return true; -} - -/** - * Looks up the available deno path with the priority - * of release -> debug -> global - */ -export function lookupDenoPath(): string { - const denoExe = "deno" + executableSuffix; - const releaseExe = join("target", "release", denoExe); - const debugExe = join("target", "debug", denoExe); - - if (existsSync(releaseExe)) { - return releaseExe; - } else if (existsSync(debugExe)) { - return debugExe; - } - - return denoExe; -} diff --git a/tools/util_test.ts b/tools/util_test.ts deleted file mode 100644 index 95def4aeb7..0000000000 --- a/tools/util_test.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { assert, testPerm, 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(); - -testPerm({ read: true }, 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` - ]); -}); - -testPerm({ read: true }, 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` - ]); -}); - -testPerm({ read: true }, 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` - ]); -}); - -testPerm({ read: false }, function testFindFilesPerm() { - let caughtError = false; - try { - const files = findFiles([testDir], [".ts", ".md"]); - } catch (e) { - caughtError = true; - assertEqual(e.kind, Deno.ErrorKind.PermissionDenied); - assertEqual(e.name, "PermissionDenied"); - } - assert(caughtError); -}); diff --git a/website/manual.md b/website/manual.md index 96f3b79713..dbc2f91144 100644 --- a/website/manual.md +++ b/website/manual.md @@ -137,8 +137,7 @@ cd deno ./tools/test.py # Format code. -# TODO: set PYTHONPATH in format.ts when run API has env option. -PYTHONPATH=third_party/python_packages deno ./tools/format.ts --allow-read --allow-run +./tools/format.py ``` #### Prerequisites @@ -612,9 +611,7 @@ Before submitting, please make sure the following is done: 1. There are tests that cover the changes. 2. Ensure `./tools/test.py` passes. -3. Format your code with - `PYTHONPATH=third_party/python_packages deno ./tools/format.ts --allow-read --allow-run`. - +3. Format your code with `tools/format.py` 4. Make sure `./tools/lint.py` passes. ### Changes to `third_party`