forked from samcamwilliams/HyperBEAM
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathinstall-template
More file actions
executable file
·229 lines (208 loc) · 6.25 KB
/
Copy pathinstall-template
File metadata and controls
executable file
·229 lines (208 loc) · 6.25 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env sh
set -eu
usage() {
cat <<'EOF'
Usage:
install-template [--template-dir PATH]
install-template --local [PATH] [--template-dir PATH]
install-template --branch BRANCH [--repo URL] [--template-dir PATH]
install-template --commit COMMIT [--repo URL] [--template-dir PATH]
Installs the HyperBEAM Forge `rebar3 new device' template.
Options:
--local [PATH] Use a local HyperBEAM git checkout.
Defaults to the current working directory.
--branch NAME Use a branch from the HyperBEAM git repository.
Defaults to edge when no source option is given.
--commit SHA Use a commit from the HyperBEAM git repository.
--repo URL Git repository for --branch or --commit.
Defaults to https://github.com/permaweb/hyperbeam.git.
--template-dir PATH Rebar3 template directory.
Defaults to Rebar's user template directory.
-h, --help Show this help.
EOF
}
die() {
printf '%s\n' "$*" >&2
exit 1
}
quote() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}
git_url() {
case "$1" in
/*) printf 'file://%s' "$1" ;;
*) printf '%s' "$1" ;;
esac
}
local_ref() {
root="$1"
commit="$(git -C "$root" rev-parse HEAD)"
printf '{ref, "%s"}' "$(quote "$commit")"
}
script_dir() {
cd "$(dirname "$0")" && pwd
}
rebar3_config_dir() {
if [ "${XDG_CONFIG_HOME:-}" ]; then
printf '%s/rebar3' "$XDG_CONFIG_HOME"
else
printf '%s/.config/rebar3' "$HOME"
fi
}
rebar3_env_config_dir() {
case "$1" in
*/.config/rebar3) printf '%s' "$1" ;;
*) printf '%s/.config/rebar3' "$1" ;;
esac
}
default_template_dir() {
if [ "${REBAR_TEMPLATE_DIR:-}" ]; then
printf '%s' "$REBAR_TEMPLATE_DIR"
elif [ "${REBAR_GLOBAL_CONFIG_DIR:-}" ]; then
printf '%s/templates' "$(rebar3_env_config_dir "$REBAR_GLOBAL_CONFIG_DIR")"
elif [ "${REBAR_CACHE_DIR:-}" ]; then
printf '%s/.config/rebar3/templates' "$REBAR_CACHE_DIR"
else
printf '%s/templates' "$(rebar3_config_dir)"
fi
}
write_device_template() {
path="$1"
hb_dep="$2"
hb_plugin="$3"
prefix="$4"
cat > "$path" <<EOF
{description, "HyperBEAM device project"}.
{variables, [
{name, "my_device", "Device and OTP application name"},
{hb_dep,
"$(quote "$hb_dep")",
"HyperBEAM dependency term"},
{hb_plugin,
"$(quote "$hb_plugin")",
"HyperBEAM Forge plugin term"}
]}.
{template, "${prefix}_rebar.config", "{{name}}/rebar.config"}.
{template, "${prefix}_src/device.app.src", "{{name}}/src/{{name}}.app.src"}.
{template, "${prefix}_src/dev_device.erl.in", "{{name}}/src/dev_{{name}}.erl"}.
{template, "${prefix}_README.md", "{{name}}/README.md"}.
{template, "${prefix}_.gitignore", "{{name}}/.gitignore"}.
EOF
}
render_shared_rebar_config() {
path="$1"
shared_config="$2"
out="$3"
# reason: generated device repos cannot read files from this repo when their
# rebar.config loads, so render shared_rebar.config inline.
while IFS= read -r line; do
case "$line" in
*"{{hb_shared_rebar_config}}"*) cat "$shared_config" ;;
*) printf '%s\n' "$line" ;;
esac
done < "$path" > "$out"
mv "$out" "$path"
}
install_template() {
template_dir="$1"
hb_dep="$2"
hb_plugin="$3"
root="$(script_dir)"
src="$root/src/forge/plugin/priv"
shared_rebar_config="$root/shared_rebar.config"
support="$template_dir/hyperbeam-device"
tmp="$support.tmp.$$"
template="$template_dir/device.template"
template_tmp="$template.tmp.$$"
rm -rf "$tmp"
mkdir -p "$tmp"
cp -R "$src/." "$tmp"
rm -f "$tmp/device.template"
render_shared_rebar_config \
"$tmp/_rebar.config" \
"$shared_rebar_config" \
"$tmp/_rebar.config.rendered"
write_device_template \
"$template_tmp" \
"$hb_dep" \
"$hb_plugin" \
"hyperbeam-device/"
rm -rf "$support"
mv "$tmp" "$support"
mv "$template_tmp" "$template"
}
repo_url="https://github.com/permaweb/hyperbeam.git"
template_dir="$(default_template_dir)"
mode="branch"
value="edge"
while [ "$#" -gt 0 ]; do
case "$1" in
--local)
mode="local"
if [ "${2:-}" ] && [ "${2#--}" = "$2" ]; then
value="$2"
shift
else
value="$(pwd)"
fi
;;
--branch)
[ "$#" -ge 2 ] || die "--branch requires a value"
mode="branch"
value="$2"
shift
;;
--commit)
[ "$#" -ge 2 ] || die "--commit requires a value"
mode="commit"
value="$2"
shift
;;
--repo)
[ "$#" -ge 2 ] || die "--repo requires a value"
repo_url="$2"
shift
;;
--template-dir)
[ "$#" -ge 2 ] || die "--template-dir requires a value"
template_dir="$2"
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
shift
done
case "$mode" in
local)
root="$(cd "$value" && pwd)"
[ -d "$root/src/forge/plugin" ] ||
die "could not find $root/src/forge/plugin"
if ! git -C "$root" diff --quiet ||
! git -C "$root" diff --cached --quiet; then
printf 'warning: --local uses committed HEAD; '
printf 'uncommitted changes are not included\n'
fi
source_url="$(git_url "$root")"
source_ref="$(local_ref "$root")"
;;
branch)
source_url="$repo_url"
source_ref="{branch, \"$(quote "$value")\"}"
;;
commit)
source_url="$repo_url"
source_ref="{ref, \"$(quote "$value")\"}"
;;
esac
hb_dep="{git, \"$(quote "$source_url")\", $source_ref}"
hb_plugin="{git_subdir, \"$(quote "$source_url")\", $source_ref, \"src/forge\"}"
install_template "$template_dir" "$hb_dep" "$hb_plugin"
printf 'Installed HyperBEAM Forge template in %s\n' "$template_dir"
printf 'Generated projects will use %s at %s\n' "$source_url" "$source_ref"
printf 'Try: rebar3 new device name=my_device\n'