From 584239a1a0c97f9d57f2de76c04708127178bceb Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Tue, 14 Jul 2020 20:29:53 -0700 Subject: dev: move some commands to shell --- Biz/lint.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ default.nix | 2 +- deps | 2 -- lint | 72 ----------------------------------------------------------- nix/build.nix | 32 +++++++++++++++++++++++++- repl | 4 ---- shell.nix | 23 +------------------ 7 files changed, 105 insertions(+), 102 deletions(-) create mode 100755 Biz/lint.py delete mode 100755 deps delete mode 100755 lint delete mode 100755 repl diff --git a/Biz/lint.py b/Biz/lint.py new file mode 100755 index 0000000..d12431c --- /dev/null +++ b/Biz/lint.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +""" +all your lint are belong to us +""" +import os +import subprocess +import sys + + +def run(cmd, file): + "Exec a linter for a file." + global ERRORS # pylint: disable=global-statement + args = { + "ormolu": ["--mode", "check"], + "hlint": [], + "black": ["--quiet", "--check"], + "pylint": [], + } + # pylint: disable=subprocess-run-check + ret = subprocess.run([cmd, *args[cmd], file], stdout=subprocess.PIPE) + if ret.returncode != 0: + ERRORS += 1 # pylint: disable=undefined-variable + msg = ret.stdout.decode("utf-8").strip() + print(f"lint error: {cmd} on {file}") + if msg: + print(msg) + + +def changed_files(): + "Return a list of changed files according to git." + return ( + subprocess.check_output(["git", "diff", "--cached", "--name-only"]) + .decode("utf-8") + .strip() + ) + + +def group_files(files, extensions): + """Given a list of files and list of extensions, return a dict of: + {ext: [files]} + + """ + root = os.getenv("BIZ_ROOT") + ret = {k: [] for k in extensions} + for ext in extensions: + for file in files: + if file.endswith(ext): + ret[ext].append(os.path.join(root, file)) + return ret + + +if __name__ == "__main__": + ERRORS = 0 + if "-h" in sys.argv: + print(f"usage: {os.path.basename(__file__)} ") + print("if no files given, lint files staged in git") + sys.exit(0) + elif len(sys.argv) == 1: + FILES = group_files(changed_files(), [".hs", ".py"]) + else: + FILES = group_files(sys.argv[1:], [".hs", ".py"]) + for hs in FILES[".hs"]: + print(f"lint: {hs}") + run("ormolu", hs) + run("hlint", hs) + for py in FILES[".py"]: + print(f"lint: {py}") + run("black", py) + run("pylint", py) + run("black", __file__) + run("pylint", __file__) + sys.exit(ERRORS) diff --git a/default.nix b/default.nix index 32f9952..237a6da 100644 --- a/default.nix +++ b/default.nix @@ -87,7 +87,7 @@ in rec { Que.Server = build.ghc ./Que/Server.hs; Que.Website = build.ghc ./Que/Website.hs; # Development environment - repl = build.env; + env = build.env; # Fall through to any of our overlay packages inherit nixpkgs; } diff --git a/deps b/deps deleted file mode 100755 index 7f185b8..0000000 --- a/deps +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash -niv --sources-file $BIZ_ROOT/nix/sources.json "$@" diff --git a/lint b/lint deleted file mode 100755 index d12431c..0000000 --- a/lint +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python -""" -all your lint are belong to us -""" -import os -import subprocess -import sys - - -def run(cmd, file): - "Exec a linter for a file." - global ERRORS # pylint: disable=global-statement - args = { - "ormolu": ["--mode", "check"], - "hlint": [], - "black": ["--quiet", "--check"], - "pylint": [], - } - # pylint: disable=subprocess-run-check - ret = subprocess.run([cmd, *args[cmd], file], stdout=subprocess.PIPE) - if ret.returncode != 0: - ERRORS += 1 # pylint: disable=undefined-variable - msg = ret.stdout.decode("utf-8").strip() - print(f"lint error: {cmd} on {file}") - if msg: - print(msg) - - -def changed_files(): - "Return a list of changed files according to git." - return ( - subprocess.check_output(["git", "diff", "--cached", "--name-only"]) - .decode("utf-8") - .strip() - ) - - -def group_files(files, extensions): - """Given a list of files and list of extensions, return a dict of: - {ext: [files]} - - """ - root = os.getenv("BIZ_ROOT") - ret = {k: [] for k in extensions} - for ext in extensions: - for file in files: - if file.endswith(ext): - ret[ext].append(os.path.join(root, file)) - return ret - - -if __name__ == "__main__": - ERRORS = 0 - if "-h" in sys.argv: - print(f"usage: {os.path.basename(__file__)} ") - print("if no files given, lint files staged in git") - sys.exit(0) - elif len(sys.argv) == 1: - FILES = group_files(changed_files(), [".hs", ".py"]) - else: - FILES = group_files(sys.argv[1:], [".hs", ".py"]) - for hs in FILES[".hs"]: - print(f"lint: {hs}") - run("ormolu", hs) - run("hlint", hs) - for py in FILES[".py"]: - print(f"lint: {py}") - run("black", py) - run("pylint", py) - run("black", __file__) - run("pylint", __file__) - sys.exit(ERRORS) diff --git a/nix/build.nix b/nix/build.nix index dd16df6..7335159 100644 --- a/nix/build.nix +++ b/nix/build.nix @@ -93,7 +93,37 @@ in { installPhase = "exit 0"; } // { env = ghcjs; }; - env = ghc_ allDeps; + env = mkShell { + name = "bizdev"; + buildInputs = [ + (ghc_ allDeps) + # this says something about missing haskelline? + #(ghcjs_ allDeps) + + nixpkgs.niv.niv + nixpkgs.hlint + nixpkgs.ormolu + nixpkgs.python37Packages.black + nixpkgs.python37Packages.pylint + nixpkgs.wemux + ]; + shellHook = '' + echo "bizdev" | ${nixpkgs.figlet}/bin/figlet | ${nixpkgs.lolcat}/bin/lolcat + echo "(be sure to run 'nix-shell' to get the build functions)" + echo "-------------------------------------------------------" + echo "" + + function repl() { + ghci -i$BIZ_ROOT -ghci-script "$BIZ_ROOT/.ghci" + } + + function deps() { + niv --sources-file $BIZ_ROOT/nix/sources.json $@ + } + + alias lint=$BIZ_ROOT/Biz/lint.py + ''; + }; os = cfg: (nixos (args: lib.attrsets.recursiveUpdate cfg { boot.cleanTmpDir = true; diff --git a/repl b/repl deleted file mode 100755 index b09bb5a..0000000 --- a/repl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -set -ex -nix run -f $BIZ_ROOT/default.nix "repl" \ - -c ghci -i$BIZ_ROOT -ghci-script "$BIZ_ROOT/.ghci" diff --git a/shell.nix b/shell.nix index a83b9af..7e64e11 100644 --- a/shell.nix +++ b/shell.nix @@ -1,22 +1 @@ -{ - isDevelopmentShell ? true -}: - -let - nixpkgs = import ./nix/default.nix; - basepkgs = import ./Biz/packages.nix { inherit (nixpkgs) pkgs; }; - devpkgs = with nixpkgs; [ - hlint - #niv - ormolu - python37Packages.black - python37Packages.pylint - wemux - ]; -in nixpkgs.mkShell { - name = "bizdev"; - buildInputs = devpkgs; - shellHook = '' - echo "biz" | ${nixpkgs.figlet}/bin/figlet | ${nixpkgs.lolcat}/bin/lolcat - ''; -} +(import ./default.nix).env -- cgit v1.2.3