forked from brendanhay/amazonka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate
executable file
·96 lines (73 loc) · 2.48 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
#!/usr/bin/env bash
# Usage: generate 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.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# Output a message to stderr.
say() {
echo >&2 "$@"
}
# 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' -print0 |
xargs -0 ormolu --mode=inplace \
--ghc-opt='-XPatternSynonyms' \
--ghc-opt='-XTypeApplications'
# Delete corresponding amazonka-*/gen dirs from the source tree.
rm -rf "./$(basename "$dir")/gen"
# Copy formatted sources from tmp to the source tree.
cp -R "$dir" .
}
# Export the functions for use in GNU parallel.
export -f say
export -f format_and_replace
# Build the generator, obtain the botocore repository, and ensure buildozer exists.
bazel build //gen @botocore//... @com_github_bazelbuild_buildtools//buildozer
botocore="$(bazel info output_base)/external/botocore"
version="$(sed -n '0,/^version: */s///p' amazonka/amazonka.cabal)"
services="config/services"
models=()
if [ $# -eq 0 ]; then
for model in "$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 amazonka version $version."
say "Generating models ${models[*]}."
say "Using temporary directory $tmp."
generator=(
--out="$tmp"
--library-version="${version}"
--client-version="${version}"
--services="$services"
--annexes="config/annexes"
--templates="config/templates"
--assets="config/assets"
--retry="${botocore}/_retry.json"
)
for model in "${models[@]}"; do
generator+=("${botocore}/${model}")
done
# Generate new sources in tmp.
./bazel-bin/gen/gen +RTS -qg0 -RTS "${generator[@]}"
# Run the format and copy/replace steps in parallel.
parallel --halt-on-error 2 format_and_replace ::: "$tmp"/amazonka-*
# Generate BUILD files for every package.
./scripts/update-gazelle
say "Done."