From 2559d32912d200b9b84f3ae69a241b54c7970e73 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 17 Apr 2021 21:07:38 +0200 Subject: [PATCH] nix(feat): Add argbash bash completion support --- nix/devtools.nix | 37 +++++++++------- nix/docker/default.nix | 2 +- nix/nixpkgs-upgrade.nix | 2 +- .../checked-shell-script.nix | 14 +++++- nix/release/default.nix | 14 ++++-- nix/style.nix | 18 ++++---- nix/tests.nix | 29 +++++++------ nix/withtools.nix | 22 ++++++---- shell.nix | 43 +++++++++++-------- 9 files changed, 113 insertions(+), 68 deletions(-) diff --git a/nix/devtools.nix b/nix/devtools.nix index 1c4489329..a0db42ce1 100644 --- a/nix/devtools.nix +++ b/nix/devtools.nix @@ -26,6 +26,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; + addCommandCompletion = true; redirectTixFiles = false; # will be done by sub-command inRootDir = true; } @@ -169,19 +170,25 @@ let '' ${hsieMinimalImports} graph-symbols | ${graphviz}/bin/dot -Tpng -o "$_arg_outfile" ''; -in -buildEnv { - name = "postgrest-devtools"; - paths = [ - watch.bin - pushCachix.bin - build.bin - run.bin - clean.bin - check.bin - dumpMinimalImports.bin - hsieMinimalImports.bin - hsieGraphModules.bin - hsieGraphSymbols.bin + + tools = [ + watch + pushCachix + build + run + clean + check + dumpMinimalImports + hsieMinimalImports + hsieGraphModules + hsieGraphSymbols ]; -} + + bashCompletion = builtins.map (tool: tool.bashCompletion) tools; + +in +buildEnv + { + name = "postgrest-devtools"; + paths = builtins.map (tool: tool.bin) tools; + } // { inherit bashCompletion; } diff --git a/nix/docker/default.nix b/nix/docker/default.nix index 28c28c779..baf827768 100644 --- a/nix/docker/default.nix +++ b/nix/docker/default.nix @@ -42,4 +42,4 @@ buildEnv { name = "postgrest-docker"; paths = [ load.bin ]; - } // { inherit image; } + } // { inherit image; bashCompletion = load.bashCompletion; } diff --git a/nix/nixpkgs-upgrade.nix b/nix/nixpkgs-upgrade.nix index e903dcc53..b49b97a66 100644 --- a/nix/nixpkgs-upgrade.nix +++ b/nix/nixpkgs-upgrade.nix @@ -44,4 +44,4 @@ let EOF ''; in -script.bin +script diff --git a/nix/overlays/checked-shell-script/checked-shell-script.nix b/nix/overlays/checked-shell-script/checked-shell-script.nix index 5a1ba4a4c..f7ab56d44 100644 --- a/nix/overlays/checked-shell-script/checked-shell-script.nix +++ b/nix/overlays/checked-shell-script/checked-shell-script.nix @@ -13,6 +13,7 @@ { name , docs , args ? [ ] +, addCommandCompletion ? false , inRootDir ? false , redirectTixFiles ? true , withEnv ? null @@ -46,6 +47,17 @@ let runCommand "${name}-parser" { } "${argbash}/bin/argbash -o $out ${argsTemplate}/${name}.m4"; + bashCompletion = + runCommand "${name}-completion" { } ( + '' + ${argbash}/bin/argbash --type completion --strip all ${argsTemplate}/${name}.m4 > $out + '' + + + lib.optionalString addCommandCompletion '' + sed 's/COMPREPLY.*compgen -o bashdefault .*$/_command/' -i $out + '' + ); + bin = writeTextFile { inherit name; @@ -111,4 +123,4 @@ let script = runCommand name { inherit bin name; } "ln -s $bin/bin/$name $out"; in -script // { inherit bin; } +script // { inherit bin bashCompletion; } diff --git a/nix/release/default.nix b/nix/release/default.nix index d9fd2c25e..77c348e8f 100644 --- a/nix/release/default.nix +++ b/nix/release/default.nix @@ -124,8 +124,14 @@ let [ "$responseCode" -eq 200 ] ''; + + tools = [ github dockerLogin dockerHub dockerHubDescription ]; + + bashCompletion = builtins.map (tool: tool.bashCompletion) tools; + in -buildEnv { - name = "postgrest-release"; - paths = [ github.bin dockerLogin.bin dockerHub.bin dockerHubDescription.bin ]; -} +buildEnv + { + name = "postgrest-release"; + paths = builtins.map (tool: tool.bin) tools; + } // { inherit bashCompletion; } diff --git a/nix/style.nix b/nix/style.nix index 74083ff26..fb4828c4e 100644 --- a/nix/style.nix +++ b/nix/style.nix @@ -55,12 +55,14 @@ let ${silver-searcher}/bin/ag -l --vimgrep -g '\.l?hs$' . \ | xargs ${hlint}/bin/hlint -X QuasiQuotes -X NoPatternSynonyms ''; + + tools = [ style styleCheck lint ]; + + bashCompletion = builtins.map (tool: tool.bashCompletion) tools; + in -buildEnv { - name = "postgrest-devtools"; - paths = [ - style.bin - styleCheck.bin - lint.bin - ]; -} +buildEnv + { + name = "postgrest-style"; + paths = builtins.map (tool: tool.bin) tools; + } // { inherit bashCompletion; } diff --git a/nix/tests.nix b/nix/tests.nix index f50ecce22..016442924 100644 --- a/nix/tests.nix +++ b/nix/tests.nix @@ -171,6 +171,18 @@ let sed -i 's|^module \(.*\):|module \1/|g' test/coverage.overlay ''; + tools = + [ + testSpec + testSpecIdempotence + testIO + dumpSchema + coverage + coverageDraftOverlay + ]; + + bashCompletion = builtins.map (tool: tool.bashCompletion) tools; + in # Create an environment that contains all the utility scripts for running tests # that we defined above. @@ -179,19 +191,12 @@ buildEnv name = "postgrest-tests"; - paths = - [ - testSpec.bin - testSpecIdempotence.bin - testIO.bin - dumpSchema.bin - coverage.bin - coverageDraftOverlay.bin - ]; - } + paths = builtins.map (tool: tool.bin) tools; + } // { + inherit bashCompletion; + # The memory tests have large dependencies (a profiled build of PostgREST) # and are run less often than the spec tests, so we don't include them in # the default test environment. We make them available through a separate attribute: - // { - memoryTests = testMemory.bin; + memoryTests = testMemory; } diff --git a/nix/withtools.nix b/nix/withtools.nix index c8e162175..bfcbb232c 100644 --- a/nix/withtools.nix +++ b/nix/withtools.nix @@ -1,7 +1,9 @@ -{ buildEnv +{ bashCompletion +, buildEnv , checkedShellScript , lib , postgresqlVersions +, writeTextFile }: let # Wrap the `test/with_tmp_db` script with the required dependencies from Nix. @@ -16,6 +18,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; + addCommandCompletion = true; inRootDir = true; redirectTixFiles = false; } @@ -66,6 +69,7 @@ let "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" ]; + addCommandCompletion = true; inRootDir = true; } (lib.concatStringsSep "\n\n" runners); @@ -73,18 +77,20 @@ let # Create a `postgrest-with-postgresql-` for each PostgreSQL version withVersions = builtins.map withTmpDb postgresqlVersions; + tools = [ withAll ] ++ withVersions; + + bashCompletion = builtins.map (tool: tool.bashCompletion) tools; + in buildEnv { name = - "postgrest-with"; + "postgrest-withtools"; + + paths = builtins.map (tool: tool.bin) tools; + } // { + inherit bashCompletion; - paths = - [ - withAll.bin - ] ++ (builtins.map (v: v.bin) withVersions); - } # make withTools.latest available for other nix files - // { latest = withTmpDb (builtins.head postgresqlVersions); } diff --git a/shell.nix b/shell.nix index 9ea7ccc2e..0016865b4 100644 --- a/shell.nix +++ b/shell.nix @@ -19,6 +19,16 @@ let lib = pkgs.lib; + + modules = + [ + postgrest.devtools + postgrest.style + postgrest.tests + postgrest.withTools + ] + ++ lib.optional release postgrest.release; + in lib.overrideDerivation postgrest.env ( base: { @@ -27,29 +37,26 @@ lib.overrideDerivation postgrest.env ( pkgs.cabal-install pkgs.cabal2nix pkgs.postgresql - postgrest.devtools postgrest.hsie.bin - postgrest.nixpkgsUpgrade - postgrest.style - postgrest.tests - postgrest.withTools + postgrest.nixpkgsUpgrade.bin ] - ++ lib.optional memoryTests postgrest.tests.memoryTests - ++ lib.optional docker postgrest.docker - ++ lib.optional release postgrest.release; + ++ modules + ++ lib.optional memoryTests postgrest.tests.memoryTests.bin + ++ lib.optional docker postgrest.docker; shellHook = '' source ${pkgs.bashCompletion}/etc/profile.d/bash_completion.sh - complete -F _command postgrest-watch - complete -F _command postgrest-with-all - complete -F _command postgrest-with-postgresql-13 - complete -F _command postgrest-with-postgresql-12 - complete -F _command postgrest-with-postgresql-11 - complete -F _command postgrest-with-postgresql-10 - complete -F _command postgrest-with-postgresql-9.6 - complete -F _command postgrest-with-postgresql-9.5 - source ${postgrest.hsie.bashCompletion} - ''; + + '' + + builtins.concatStringsSep "\n" ( + builtins.map (bashCompletion: "source ${bashCompletion}") ( + builtins.concatLists (builtins.map (module: module.bashCompletion) modules) + ++ [ postgrest.hsie.bashCompletion postgrest.nixpkgsUpgrade.bashCompletion ] + ++ lib.optional memoryTests postgrest.tests.memoryTests.bashCompletion + ++ lib.optional docker postgrest.docker.bashCompletion + + ) + ); } )