mirror of
https://github.com/nmattia/snack.git
synced 2024-11-10 22:30:34 +03:00
commit
3a91490cd1
81
bin/snack
Executable file
81
bin/snack
Executable 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
|
50
default.nix
50
default.nix
@ -1,49 +1,11 @@
|
||||
{ pkgs ? import ./nix {} }:
|
||||
|
||||
{ snack-lib = import ./snack/default.nix { inherit pkgs; };
|
||||
snack-exe = pkgs.writeScriptBin "snack"
|
||||
''
|
||||
set -euo pipefail
|
||||
|
||||
SNACK_NIX=snack.nix
|
||||
|
||||
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
|
||||
'';
|
||||
snack-exe = pkgs.writeScriptBin
|
||||
"snack"
|
||||
(builtins.replaceStrings
|
||||
["NIX_BUILD=nix-build"]
|
||||
["NIX_BUILD=${pkgs.nix}/bin/nix-build"]
|
||||
(builtins.readFile ./bin/snack));
|
||||
}
|
||||
|
||||
|
4
script/snack-fmt
Executable file
4
script/snack-fmt
Executable 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
|
29
script/test
29
script/test
@ -2,23 +2,50 @@
|
||||
#!nix-shell -i bash
|
||||
#!nix-shell -I nixpkgs=./nix
|
||||
#!nix-shell -p snack.snack-exe
|
||||
#!nix-shell -p shfmt
|
||||
#!nix-shell --pure
|
||||
# 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
|
||||
snack build
|
||||
popd
|
||||
|
||||
banner "Test Template Haskell"
|
||||
pushd tests/template-haskell
|
||||
snack build
|
||||
popd
|
||||
|
||||
banner "Test Template Haskell 2"
|
||||
pushd tests/template-haskell-2
|
||||
snack build -f code/snack.nix
|
||||
popd
|
||||
|
||||
banner "Test Template Haskell 3"
|
||||
pushd tests/template-haskell-3
|
||||
snack build
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user