forked from nix-community/mavenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvnix-init
executable file
·73 lines (61 loc) · 1.83 KB
/
mvnix-init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
set -e
usage() {
# shellcheck disable=SC2015
test "$1" && echo -e Error: "$1"\\n || true
cat >&2 <<EOF
Usage: mvnix-init [OPTIONS] [<NIX-FILE>]
Options:
-S, --src A Nix expression to use as 'src' for derivation
-o, --output <dir> Output directory for generated files
-s, --standalone Include 'mavenix.nix' file
-f, --force Overwrite output file if exists
-v, --verbose Verbose output
-h, --help Print usage
Version: $MAVENIX_VERSION
EOF
exit 1
}
tmpl() { sed "s|%%$1%%|$2|g"; }
# Default values
outputDir="."
# Parse CLI arguments
while test "$1";do
case "$1" in
-S|--src) shift;srcExpr="($1)";;
-o|--output) shift;outputDir="$1";;
-s|--standalone) standalone=1;;
-f|--force) forceOverwrite=1;;
-v|--verbose) set -x;;
-h|--help) usage;;
-*) usage "no option \"$1\"";;
*) config="$1";;
esac
shift
done
mkdir -p "$outputDir"
config="${config-$outputDir/default.nix}"
[[ -n "$forceOverwrite" || ! -e "$config" ]] || usage "\"$config\" already exists"
srcExpr="${srcExpr-./$(realpath --relative-to="$(dirname "$config")" ".")}"
copy() { cp "$1" "$2"; echo "$2"; }
echo >&2 "
Creating files:
"
if test "$standalone"; then
relEnv="./$(realpath --relative-to="$(dirname "$config")" "$outputDir/mavenix.nix")"
copy "$MAVENIX_SCRIPT" "$outputDir/mavenix.nix"
chmod u+w "$outputDir/mavenix.nix"
else
relEnv="fetchTarball { url = \"$MAVENIX_DOWNLOAD\"; sha256 = \"$(nix-prefetch-url "$MAVENIX_DOWNLOAD" --unpack 2>&-)\"; }"
fi
# shellcheck disable=SC2002
cat "$CONFIG_TEMPLATE" \
| tmpl src "$srcExpr" \
| tmpl env "$relEnv" \
> "$config"
echo "$config"
echo >&2 "
1. Configure by editing '$config'
2. Create a lock file by running 'mvnix-update \"$config\"'
3. Build your project 'nix-build \"$config\"'
"