-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
mvnix-update
executable file
·216 lines (183 loc) · 5.65 KB
/
mvnix-update
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
set -e
usage() {
# shellcheck disable=SC2015
test "$1" && echo -e Error: "$1"\\n || true
cat >&2 <<EOF
Usage: mvnix-update [OPTIONS] [<NIX-FILE>]
Options:
-E, --exp <expression> Pass in a Nix expression that represents the
derivation to be updated.
-s, --save-repo <path> Specify path to save maven repo to. This will
speed up consecutive runs, but might affect
lock file if POM is changed in between.
-n, --no-add Don't add dependencies to nix store
-v, --verbose Verbose output
-h, --help Print usage
Version: $MAVENIX_VERSION
EOF
exit 1
}
# Setup work dir and cleanup
WORK_DIR=$(mktemp -d --tmpdir mvnix-update.XXXXXX)
cleanup() { { chmod -R +w "$WORK_DIR"; rm -rf "$WORK_DIR"; } || true; }
trap 'trap - EXIT; cleanup; kill -- $$' EXIT
# Default values
tmp_repo="$WORK_DIR/m2-repo"
# Parse CLI arguments
while test "$1";do
case "$1" in
-E|--exp) shift;expression="$1";;
-s|--save-repo) shift;tmp_repo="$1";;
-n|--no-add) no_add=1;;
-v|--verbose) verbose=1;set -x;;
-h|--help) usage;;
-*) usage "no option \"$1\"";;
*) config="$1";;
esac
shift
done
die() { echo >&2 "$@"; exit 1;}
nix_flags=$(test "$verbose" && printf %s "--verbose" || true)
if [[ "$expression" && "$config" ]]; then
die "Expression and file arguments are mutually exclusive"
elif [[ "$config" ]]; then
expression="import \"$(realpath "$config")\" {}"
elif [[ ! "$expression" ]]; then
expression="import ./. {}"
fi
tmp_repo=$(realpath "$tmp_repo")
mkdir -p "$tmp_repo"
ns_() {
local ns_wd="$1";shift
HOME="/tmp/nowhere" nix-shell $nix_flags --show-trace --pure \
--run "cd \"$ns_wd\"; $*" -E "($expression).overrideAttrs (_: { build = false; })";
}
echo >&2 "
Getting mavenix meta data...
"
realiseStorePath() {
# Check if $1 is a store path (similar to lib.isStorePath) and realize only
# if it is.
if [[ $1 =~ ^/nix/store/ ]]; then
nix-store --realise "$1" >/dev/null
fi
}
mavenixMeta=$(nix-instantiate $nix_flags --eval --strict --json -E "($expression).mavenixMeta" | jq -rc .)
mq() { jq -rc "$1" <<<"$mavenixMeta"; }
test "$mavenixMeta" || die "Nix expression is not a mavenix derivation"
nix-build --no-out-link -E "{ inherit (($expression).mavenixMeta) emptyRepo settings infoFile; }"
src_path=$(mq .srcPath)
realiseStorePath "$src_path"
settings=$(mq .settings)
empty_repo=$(mq .emptyRepo)
if test -d "$empty_repo"; then
cp -rf -t "$tmp_repo" "$empty_repo"/* >/dev/null 2>&1 || true
chmod -R u+w "$tmp_repo" || true
fi
src_wd="$src_path"
if test ! -w "$src_wd"; then
src_wd="$WORK_DIR/src"
cp -r "$src_path" "$src_wd"
chmod -R u+w "$src_wd" || true #echo >&2 Failed to set chmod on temp repo dir.
fi
# shellcheck disable=SC2015
mvn_flags=$(test "$verbose" && printf %s "-e -X" || true)
# shellcheck disable=SC2086
mvn_() { ns_ "$src_wd" mvn $mvn_flags -B -nsu --settings "$settings" "$@"; }
mvn_out() { ns_ "$src_wd" mvn -B -nsu --settings "$settings" "$@"; }
echo >&2 "
Running mvn package...
"
mvn_ >&2 install -Dmaven.test.skip.exec=true -DskipTests -Dmaven.repo.local="$tmp_repo"
echo >&2 "
Getting project info...
"
pom=$(mvn_out help:effective-pom | grep -v '^\[\|^Effective \|^$' | xq -c .)
projects=$(jq -c '.projects.project // [.project]' <<<"$pom")
pq() { jq -rc "$1" <<<"$projects"; }
export -f pq
groupId=$(pq .[0].groupId)
artifactId=$(pq .[0].artifactId)
version=$(pq .[0].version)
modules=$(pq '
[ .[]
| {
name: (.artifactId + "-" + .version),
groupId, artifactId, version,
path: (.build.directory | sub("^'"$src_wd"'/"; "./") | sub("/target"; ""))
}
]
')
remotes_repos=$(pq '
[ [.[].repositories.repository, .[].pluginRepositories.pluginRepository]
| flatten | .[]
| {(.id):.url}
] | add
')
tmp_lock_file="$WORK_DIR/tmp-mavenix.lock"
lock_file="$(mq .infoFile)"
echo >&2 "
Resolving maven dependencies...
"
mvn_ >&2 dependency:go-offline -Dmaven.repo.local="$tmp_repo"
echo >&2 "
Creating lock file...
"
(
echo -n "{
\"name\": \"$artifactId-$version\",
\"groupId\": \"$groupId\",
\"artifactId\": \"$artifactId\",
\"version\": \"$version\",
\"submodules\": $modules,
\"deps\": [ $(mq .deps[] | sed 's/\(.\)$/\1,/')"
( cd "$tmp_repo"
remotes=$(find . -type f -name "*.repositories" | sed 's|^\./||' | sort)
sep=""
for remote in $remotes; do
dir=$(dirname "$remote")
files=$(find "$dir" -type f ! -name "*.repositories" ! -name "*.sha1" \
| grep -v '^#' "$remote" | sed "s|^|$dir/|" | sort)
for file_ in $files; do
file=$(echo "$file_" | cut -d '>' -f1)
# Maven 3.0.5 for 3.3.9 use $file instead of $file_real
# shellcheck disable=SC2116,SC2086
file_real=$(echo ${file//-SNAPSHOT./-[0-9]*.})
repo=$(echo "$file_" | cut -d '>' -f2 | sed 's/=$//')
test "$repo" || continue
echo -n "$sep
{\"path\":\"$file_real\",\"sha1\":\"$(grep -Eo '[0-9a-zA-Z]{40}' < "$file_real.sha1")\"}"
sep=","
test "$no_add" || nix-store --add-fixed sha1 "$file_real" >&2
done
done
echo -n "
],
\"metas\": ["
metafiles=$(find . -type f -name "maven-metadata-*.xml" | sed 's|^\./||' | sort)
sep=""
for file in $metafiles; do
repo=$(basename "$file" | sed 's/^maven-metadata-//;s/\.xml$//')
[[ "$repo" && "$repo" != "local" ]] || continue
echo -n "$sep{
\"path\": \"$(dirname "$file")\",
\"content\": \"$(sed ':a;N;$!ba;s/\n/\\n/g;s/\"/\\\"/g' "$file")\"
}"
sep=", "
done
)
echo -n "
],
\"remotes\": $remotes_repos
}
"
) > "$tmp_lock_file"
echo >&2 "
Sanity check...
"
jq -c . "$tmp_lock_file" > /dev/null
jq -S . "$tmp_lock_file" > "$lock_file"
echo >&2 "
Lock file created at: $lock_file
"