Repository navigation
Expand file tree
/
Copy pathrepos.sh
More file actions
233 lines (206 loc) · 6.05 KB
/
Copy pathrepos.sh
File metadata and controls
233 lines (206 loc) · 6.05 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
230
231
232
233
#!/usr/bin/env bash
# Shared helpers for reading structure/repos.json with jq.
set -euo pipefail
REPOS_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPOS_SCRIPT_DIR="${REPOS_SCRIPT_DIR:-$REPOS_LIB_DIR/..}"
REPOS_JSON="${REPOS_JSON:-$REPOS_SCRIPT_DIR/../structure/repos.json}"
FLEET_WORKSPACE="${FLEET_WORKSPACE:-${OPENPHYSICS_WORKSPACE:-$(cd "$REPOS_SCRIPT_DIR/../.." && pwd)}}"
FILTER_TYPE=""
FILTER_STATUS=""
FILTER_SIMULATION=""
FILTER_LINEAGE=""
FILTER_NAME=""
REQUIRE_LOCAL=0
repos_require_jq() {
if ! command -v jq >/dev/null 2>&1; then
echo "jq is required (install with: sudo apt install jq)" >&2
exit 1
fi
}
repos_catalog_path() {
local path="$REPOS_JSON"
if [[ -d "$(dirname "$path")" ]]; then
path="$(cd "$(dirname "$path")" && pwd)/$(basename "$path")"
fi
printf '%s\n' "$path"
}
repos_workspace_root() {
printf '%s\n' "$FLEET_WORKSPACE"
}
# The organization login, read from the catalog. This is the single place the
# org name enters the tooling; override with FLEET_ORG for dry runs against a
# fork. Keep callers using this rather than a literal so a rename is one edit.
repos_org() {
repos_require_jq
jq -r '.organization' "$(repos_catalog_path)"
}
# Base URL for Pages sites, e.g. https://<org>.github.io (no trailing slash).
repos_pages_base() {
repos_require_jq
jq -r '.pagesBase // "https://\(.organization | ascii_downcase).github.io"' "$(repos_catalog_path)"
}
repos_reset_filters() {
FILTER_TYPE=""
FILTER_STATUS=""
FILTER_SIMULATION=""
FILTER_LINEAGE=""
FILTER_NAME=""
REQUIRE_LOCAL=0
}
repos_jq_select_expr() {
local parts=()
if [[ -n "$FILTER_NAME" ]]; then
parts+=(".name == \"$FILTER_NAME\"")
fi
if [[ -n "$FILTER_TYPE" ]]; then
parts+=(".type == \"$FILTER_TYPE\"")
fi
if [[ -n "$FILTER_STATUS" ]]; then
parts+=(".status == \"$FILTER_STATUS\"")
fi
if [[ -n "$FILTER_SIMULATION" ]]; then
parts+=(".isSimulation == $FILTER_SIMULATION")
fi
if [[ -n "$FILTER_LINEAGE" ]]; then
parts+=(".lineage == \"$FILTER_LINEAGE\"")
fi
if [[ ${#parts[@]} -eq 0 ]]; then
printf '.'
return
fi
local expr="${parts[0]}"
local part
for part in "${parts[@]:1}"; do
expr="$expr and $part"
done
printf 'select(%s)' "$expr"
}
# Pages URL for a catalog row, resolved against the catalog's pagesBase:
# deployedUrl absent -> derive "<pagesBase>/<name>"
# deployedUrl null -> the repo is not deployed
# deployedUrl string -> explicit override
# No repo names appear here; "not deployed" is stated in the catalog, not guessed.
repos_jq_homepage_def='
def github_homepage($base):
if (has("deployedUrl") | not) then
"\($base)/\(.name)"
elif .deployedUrl == null then
null
else
(.deployedUrl | rtrimstr("/"))
end;
'
repos_jq_enrich_program() {
local select_expr
select_expr="$(repos_jq_select_expr)"
cat <<JQ
$repos_jq_homepage_def
(.pagesBase // "https://\(.organization | ascii_downcase).github.io") as \$base
| .repos[]
| $select_expr
| . + {
githubHomepage: github_homepage(\$base),
localPath: (\$workspace + "/" + .name)
}
JQ
}
repos_filtered_json_lines() {
repos_require_jq
jq -c --arg workspace "$(repos_workspace_root)" "$(repos_jq_enrich_program)" "$(repos_catalog_path)"
}
repos_add_local_exists() {
local workspace
workspace="$(repos_workspace_root)"
while IFS= read -r repo; do
local name path exists=false
name="$(jq -r '.name' <<<"$repo")"
path="$workspace/$name"
if [[ -d "$path" ]]; then
exists=true
fi
jq -c --argjson localExists "$exists" '. + {localExists: $localExists}' <<<"$repo"
done
}
repos_filtered_json() {
repos_filtered_json_lines | repos_add_local_exists | jq -s '.'
}
repos_names() {
repos_filtered_json_lines | jq -r '.name'
}
repos_simulation_names() {
local previous="$FILTER_SIMULATION"
FILTER_SIMULATION="true"
repos_names
FILTER_SIMULATION="$previous"
}
repos_get() {
local name="${1:?Repository name required}"
FILTER_NAME="$name"
local result
result="$(repos_filtered_json_lines | repos_add_local_exists | head -n 1)"
FILTER_NAME=""
if [[ -z "$result" ]]; then
echo "Repository not found: $name" >&2
return 1
fi
jq '.' <<<"$result"
}
repos_list_json() {
repos_filtered_json
}
repos_paths() {
local workspace
workspace="$(repos_workspace_root)"
while IFS= read -r name; do
local path="$workspace/$name"
if [[ "$REQUIRE_LOCAL" -eq 1 && ! -d "$path" ]]; then
continue
fi
printf '%s\n' "$path"
done < <(repos_names)
}
repos_summary() {
repos_require_jq
local catalog workspace
catalog="$(repos_catalog_path)"
workspace="$(repos_workspace_root)"
jq -r '
"organization: \(.organization)",
"schemaVersion: \(.schemaVersion // "unknown")",
"total: \(.repos | length)",
(.repos | group_by(.type) | map(" \(.[0].type): \(length)") | .[]),
"workspaceRoot: '"$workspace"'",
"catalog: '"$catalog"'"
' "$catalog"
}
repos_for_each() {
local exit_code=0
local command=("$@")
if [[ ${#command[@]} -eq 0 ]]; then
echo "for-each requires a command" >&2
return 2
fi
while IFS= read -r repo; do
local name homepage description repo_type status lineage display_name local_path local_exists
name="$(jq -r '.name' <<<"$repo")"
homepage="$(jq -r '.githubHomepage // ""' <<<"$repo")"
description="$(jq -r '.description // ""' <<<"$repo")"
repo_type="$(jq -r '.type // ""' <<<"$repo")"
status="$(jq -r '.status // ""' <<<"$repo")"
lineage="$(jq -r '.lineage // ""' <<<"$repo")"
display_name="$(jq -r '.displayName // .name' <<<"$repo")"
local_path="$(jq -r '.localPath' <<<"$repo")"
local_exists="$(jq -r '.localExists' <<<"$repo")"
REPO_NAME="$name" \
REPO_DISPLAY_NAME="$display_name" \
REPO_TYPE="$repo_type" \
REPO_STATUS="$status" \
REPO_LINEAGE="$lineage" \
REPO_DESCRIPTION="$description" \
REPO_HOMEPAGE="$homepage" \
REPO_PATH="$local_path" \
REPO_LOCAL_EXISTS="$([[ "$local_exists" == "true" ]] && echo 1 || echo 0)" \
"${command[@]}" || exit_code=$?
done < <(repos_filtered_json_lines | repos_add_local_exists)
return "$exit_code"
}