-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·210 lines (184 loc) · 5.39 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·210 lines (184 loc) · 5.39 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
#!/usr/bin/env bash
#
# Copyright 2026 LiveKit
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./cpp-tools/install.sh [COMPONENT...] [OPTIONS]
Install shared clang configuration symlinks and, when requested, the
pre-commit hook. With no components, installs both configuration symlinks.
Components:
clang-format
Install the root-level .clang-format symlink.
clang-tidy
Install the root-level .clang-tidy symlink.
precommit-hook
Install the auto-formatting Git pre-commit hook.
Options:
--repo-root PATH
Consumer repository root. Defaults to the cpp-tools superproject,
then the Git repository containing the current working directory.
--force
Replace existing files or symlinks at the destination paths.
-h, --help
Show this help and exit.
EOF
}
install_precommit_hook() {
local consumer_root="$1"
local hook_path="${consumer_root}/.git/hooks/pre-commit"
cat >"${hook_path}" <<'HOOK'
#!/bin/sh
# Auto-format staged C/C++ files using LiveKit C++ tooling.
files=$(git diff --cached --name-only --diff-filter=ACMR \
-- "*.c" "*.cc" "*.cpp" "*.cxx" "*.h" "*.hpp" "*.hxx")
[ -z "${files}" ] && exit 0
if [ -x "./cpp-tools/clang-format.sh" ]; then
repo_root=$(git rev-parse --show-toplevel)
echo "${files}" | xargs ./cpp-tools/clang-format.sh --repo-root "${repo_root}" --fix
else
echo "ERROR: no LiveKit clang-format wrapper found." >&2
exit 1
fi
echo "${files}" | xargs git add
HOOK
chmod +x "${hook_path}"
echo "Installed pre-commit hook at ${hook_path}"
}
tools_root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
repo_root=""
force=0
components_specified=0
install_clang_format=0
install_clang_tidy=0
install_precommit=0
while (($#)); do
case "$1" in
clang-format)
components_specified=1
install_clang_format=1
shift
;;
clang-tidy)
components_specified=1
install_clang_tidy=1
shift
;;
precommit-hook)
components_specified=1
install_precommit=1
shift
;;
--repo-root)
if (($# < 2)); then
echo "ERROR: --repo-root requires a path." >&2
exit 2
fi
repo_root="$2"
shift 2
;;
--force)
force=1
shift
;;
-h|--help)
usage
exit 0
;;
-*)
echo "ERROR: unknown option: $1" >&2
usage >&2
exit 2
;;
*)
echo "ERROR: unknown component: $1" >&2
usage >&2
exit 2
;;
esac
done
if ((components_specified == 0)); then
install_clang_format=1
install_clang_tidy=1
fi
if [[ -z "${repo_root}" ]]; then
repo_root="$(git -C "${tools_root}" rev-parse --show-superproject-working-tree 2>/dev/null || true)"
fi
if [[ -z "${repo_root}" ]]; then
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
fi
if [[ -z "${repo_root}" ]]; then
echo "ERROR: could not determine the consumer repository root." >&2
echo "Run this script from the consumer repository or pass --repo-root." >&2
exit 1
fi
repo_root="$(cd "${repo_root}" && pwd -P)"
if [[ "${repo_root}" == "${tools_root}" ]]; then
echo "ERROR: refusing to install links into the cpp-tools repository itself." >&2
echo "Run from a consuming repository or pass --repo-root." >&2
exit 1
fi
case "${tools_root}" in
"${repo_root}"/*)
tools_relative="${tools_root#"${repo_root}"/}"
;;
*)
echo "ERROR: cpp-tools must be located inside the consumer repository." >&2
echo "cpp-tools: ${tools_root}" >&2
echo "consumer: ${repo_root}" >&2
exit 1
;;
esac
configs=()
if ((install_clang_format == 1)); then
configs+=(.clang-format)
fi
if ((install_clang_tidy == 1)); then
configs+=(.clang-tidy)
fi
# Validate every destination before changing any of them.
for config in "${configs[@]}"; do
source_path="${tools_root}/${config}"
destination="${repo_root}/${config}"
link_target="${tools_relative}/${config}"
if [[ ! -f "${source_path}" ]]; then
echo "ERROR: shared config not found: ${source_path}" >&2
exit 1
fi
if [[ -L "${destination}" ]] && [[ "$(readlink "${destination}")" == "${link_target}" ]]; then
continue
fi
if [[ -e "${destination}" || -L "${destination}" ]] && ((force == 0)); then
echo "ERROR: ${destination} already exists." >&2
echo "Re-run with --force to replace existing config files or symlinks." >&2
exit 1
fi
done
for config in "${configs[@]}"; do
destination="${repo_root}/${config}"
link_target="${tools_relative}/${config}"
if [[ -L "${destination}" ]] && [[ "$(readlink "${destination}")" == "${link_target}" ]]; then
echo "Already installed: ${destination} -> ${link_target}"
continue
fi
if [[ -e "${destination}" || -L "${destination}" ]]; then
rm -f "${destination}"
fi
ln -s "${link_target}" "${destination}"
echo "Installed: ${destination} -> ${link_target}"
done
if ((install_precommit == 1)); then
install_precommit_hook "${repo_root}"
fi