-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
generate
executable file
·109 lines (83 loc) · 2.51 KB
/
generate
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
#!/usr/bin/env bash
# Usage: generate [--commit] MODELS
#
# Runs amazonka-gen on the specified MODELS - which are directory names of the
# services definitions in https://github.com/boto/botocore/tree/develop/botocore/data/<MODEL>.
#
# Generates the output per MODEL in a temporary directory, formats .cabal and
# .hs files, removes the related amazonka-*/gen directories, and finally
# replaces the amazonka-*/gen contents with the updated sources.
#
# If --commit is the first argument a commit will be created per service directory.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# Output a message to stderr.
say() {
echo >&2 "$@"
}
commit="false"
case "${1-}" in
--commit)
if [[ "$(git diff --stat)" != "" ]]; then
say "Unable to use --commit when the git working tree is dirty."
exit 1
fi
commit="true"
shift
;;
esac
# Format new sources in tmp and replace the corresponding amazonka-*/gen
# directory in the source tree if successful.
format_and_replace() {
local -r dir="$1"
say "Formatting $dir"
find "$dir" -type f -name '*.cabal' -print0 |
xargs -0 cabal-fmt --inplace --indent=2
find "$dir" -type f \( -name '*.hs' -o -name '*.hs-boot' \) \
-exec ormolu --mode=inplace \
--ghc-opt='-XPatternSynonyms' \
--ghc-opt='-XTypeApplications' \
{} \+
# Delete corresponding amazonka-*/gen dirs from the source tree.
rm -rf "./lib/services/$(basename "$dir")/gen"
# Copy formatted sources from tmp to the source tree.
cp -R "$dir" lib/services
}
# Export the functions for use in GNU parallel.
export -f say
export -f format_and_replace
botocore="${BOTOCORE:?}/botocore/data"
models=()
if [ $# -eq 0 ]; then
for model in configs/services/*.json; do
models+=("$(basename "$model" .json)")
done
else
models=("$@")
fi
tmp="$(mktemp -d -p "$PWD")"
trap 'rm -rf "$tmp"' EXIT
say "Using botocore $botocore"
say "Using temporary directory $tmp"
say "Generating models ${models[*]}"
args=(
--out="$tmp"
--retry="${botocore}/_retry.json"
)
for model in "${models[@]}"; do
args+=("${botocore}/${model}")
done
# Generate new sources in tmp.
nix run '.#gen' -- "${args[@]}"
# Run the format and copy/replace steps in parallel.
parallel --halt-on-error 2 format_and_replace ::: "$tmp"/amazonka-*
set +e
# If --commit was passed as the first argument, created a commit per service.
if [ "$commit" = "true" ]; then
for service in lib/services/amazonka-*; do
if git add "$service" >/dev/null 2>&1; then
git commit --no-verify -m "$(basename "$service"): regenerating service"
fi
done
fi
say "Done."