1
1
mirror of https://github.com/nmattia/snack.git synced 2024-11-10 22:30:34 +03:00

Merge pull request #2 from zimbatm/snack-exe-massage

Snack exe massage
This commit is contained in:
Nicolas Mattia 2018-05-26 13:23:57 +02:00 committed by GitHub
commit 3a91490cd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 45 deletions

81
bin/snack Executable file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env bash
set -euo pipefail
## Defaults
NIX_BUILD=nix-build
SNACK_NIX=snack.nix
COMMAND=
## Functions
log_error() {
echo "ERROR: $*" >&2
}
show_usage() {
cat <<USAGE
Usage: snack [options] <command>
Snack is a Haskell build tool
Options:
-f | --snack-nix <PATH>: sets the path ot the "snack.nix" file. Default: "./snack.nix"
-h | --help: Shows this help
Commands:
run: builds and executes
build: builds
ghci: builds and loads in ghci
USAGE
}
## Main
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f | --snack-nix)
SNACK_NIX="$2"
shift
shift
;;
-h | --help)
show_usage
exit 0
;;
run | build | ghci)
COMMAND="$1"
shift # past argument
;;
*) # unknown option
echo "unknown option: $1"
exit 1
;;
esac
done
if [[ -z "$COMMAND" ]]; then
log_error "missing <command>\n"
show_usage
exit 1
fi
echo "snack nix: $SNACK_NIX"
echo "command: $COMMAND"
case $COMMAND in
build)
"$NIX_BUILD" --no-out-link -A build "$SNACK_NIX"
;;
ghci)
res=$("$NIX_BUILD" --no-out-link -A ghci "$SNACK_NIX")
"$res/bin/ghci"
;;
run)
res=$("$NIX_BUILD" --no-out-link -A build "$SNACK_NIX")
echo "running $res"
"$res/out"
;;
esac

View File

@ -1,49 +1,11 @@
{ pkgs ? import ./nix {} }: { pkgs ? import ./nix {} }:
{ snack-lib = import ./snack/default.nix { inherit pkgs; }; { snack-lib = import ./snack/default.nix { inherit pkgs; };
snack-exe = pkgs.writeScriptBin "snack" snack-exe = pkgs.writeScriptBin
'' "snack"
set -euo pipefail (builtins.replaceStrings
["NIX_BUILD=nix-build"]
SNACK_NIX=snack.nix ["NIX_BUILD=${pkgs.nix}/bin/nix-build"]
(builtins.readFile ./bin/snack));
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--snack-nix)
SNACK_NIX="$2"
shift
shift
;;
run|build|ghci)
COMMAND="$1"
shift # past argument
;;
*) # unknown option
echo "unknown option: $1"
exit 1
;;
esac
done
echo "snack nix: $SNACK_NIX"
echo "command: $COMMAND"
case $COMMAND in
build)
${pkgs.nix}/bin/nix-build --no-out-link -A build $SNACK_NIX
;;
ghci)
res=$(${pkgs.nix}/bin/nix-build --no-out-link -A ghci $SNACK_NIX)
$res/bin/ghci
;;
run)
res=$(${pkgs.nix}/bin/nix-build --no-out-link -A build $SNACK_NIX)
echo "running $res"
$res/out
;;
esac
'';
} }

4
script/snack-fmt Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env nix-shell
#!nix-shell -p shfmt -i bash
cd "$(dirname "$0")/.."
shfmt -i 2 -w bin/snack

View File

@ -2,23 +2,50 @@
#!nix-shell -i bash #!nix-shell -i bash
#!nix-shell -I nixpkgs=./nix #!nix-shell -I nixpkgs=./nix
#!nix-shell -p snack.snack-exe #!nix-shell -p snack.snack-exe
#!nix-shell -p shfmt
#!nix-shell --pure #!nix-shell --pure
# vim: ft=sh sw=2 et # vim: ft=sh sw=2 et
set -eux set -euo pipefail
## Functions
banner() {
echo "--- $*"
}
fail() {
echo "ERROR: $*"
exit 1
}
## Main
cd "$(dirname "$0")/.."
banner "Test packages"
pushd tests/packages pushd tests/packages
snack build snack build
popd popd
banner "Test Template Haskell"
pushd tests/template-haskell pushd tests/template-haskell
snack build snack build
popd popd
banner "Test Template Haskell 2"
pushd tests/template-haskell-2 pushd tests/template-haskell-2
snack build -f code/snack.nix snack build -f code/snack.nix
popd popd
banner "Test Template Haskell 3"
pushd tests/template-haskell-3 pushd tests/template-haskell-3
snack build snack build
popd popd
banner "Test stack-exe formatting"
list=$(shfmt -i 2 -l bin/snack)
if [[ -n "$list" ]]; then
fail "Please apply script/snack-fmt to format bin/snack"
fi
echo OK