-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
.repositories-update-contributing.sh
executable file
·125 lines (108 loc) · 3.86 KB
/
.repositories-update-contributing.sh
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
#!/bin/sh
# Copyright (c) 2020-2023 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# ./.repositories-update-contributing.sh
# no parameter = execute it with docker command
# ./.repositories-update-contributing.sh -w
# execute in native mode (need to have mikefarah/yq installed )
set -e
set -u
YQ_IMAGE="docker.io/mikefarah/yq:3.3.2"
DIR=$(cd "$(dirname "$0")"; pwd)
init() {
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
BOLD='\033[1m'
}
LAUNCH_IN_DOCKER=true
CHECK_MODE=false
analyze_args() {
if [ $# -eq 0 ]; then
return
fi
if [ "$1" = "IN_DOCKER" ] || [ "$1" = "-w" ]; then
LAUNCH_IN_DOCKER=false
shift
fi
if [ $# -eq 0 ]; then
return
fi
if [ "$1" = "check" ] ; then
CHECK_MODE=true
fi
}
markdown_link() {
name=$1
url=$2
if [ "${url}" != "" ]; then
echo "[${name}](${url})"
else
echo ""
fi
}
compute() {
repo_names=$(yq r .repositories.yaml "repositories.*.name")
echo "Repository | Component | Description | Devfile | Documentation"
echo "--- | --- | --- | --- | ---"
for repo_name in ${repo_names}; do
url=$(yq r .repositories.yaml "repositories.name==${repo_name}.url")
description=$(yq r .repositories.yaml "repositories.name==${repo_name}.description")
devfile=$(yq r .repositories.yaml "repositories.name==${repo_name}.devfile")
doc=$(yq r .repositories.yaml "repositories.name==${repo_name}.doc")
echo "$(markdown_link "${repo_name}" "${url}") | | ${description} | $(markdown_link "devfile" "${devfile}") | $(markdown_link "doc" "${doc}")"
# components ?
components_names=$(yq r .repositories.yaml "repositories.(name==${repo_name}).components.*.name")
for component_name in ${components_names}; do
component_url=$(yq r .repositories.yaml "repositories.(name==${repo_name}).components.(name==${component_name}).url")
component_description=$(yq r .repositories.yaml "repositories.(name==${repo_name}).components.(name==${component_name}).description")
echo "---| $(markdown_link "${component_name}" "${component_url}") | ${component_description} | | |"
done
done
}
update_contributing() {
CONTRIBUTING_FILE=${DIR}/CONTRIBUTING.md
TARGET_FILE=${DIR}/CONTRIBUTING.md
if [ ${CHECK_MODE} = true ]; then
TARGET_FILE=${DIR}/target/CONTRIBUTING.md
fi
content_to_include=$(compute)
# get begin/end lines
begin_line=$(grep -n '<!-- begin repository list -->' "${CONTRIBUTING_FILE}" | cut -d ":" -f 1 | head -n 1)
end_line=$(grep -n '<!-- end repository list -->' "${CONTRIBUTING_FILE}" | cut -d ":" -f 1 | head -n 1)
# update content
mkdir -p "${DIR}/target"
cp "${CONTRIBUTING_FILE}" "${DIR}/target/contributing.original"
(head -n "${begin_line}" "${CONTRIBUTING_FILE}" && echo "${content_to_include}" && tail -n "+${end_line}" "${CONTRIBUTING_FILE}") > "${DIR}/.tmp-contributing"
mv "${DIR}/.tmp-contributing" "${TARGET_FILE}"
}
run_in_docker() {
printf "%bRunning%b $*\n" "${BOLD}" "${NC}"
GIT_ROOT_DIRECTORY=$(git rev-parse --show-toplevel)
if docker run --rm -v "${GIT_ROOT_DIRECTORY}":/workdir --entrypoint=/bin/sh "${YQ_IMAGE}" "/workdir/$0" "IN_DOCKER" "$@"
then
printf "Script execution %b[OK]%b\n" "${GREEN}" "${NC}"
else
printf "%bFail to run the script%b\n" "${RED}" "${NC}"
exit 1
fi
}
init "$@"
analyze_args "$@"
if [ ${LAUNCH_IN_DOCKER} = true ]; then
run_in_docker "$@"
else
update_contributing
fi
if [ ${CHECK_MODE} = true ]; then
if ! diff "${DIR}/target/contributing.original" "${DIR}/target/CONTRIBUTING.md"; then
printf "%bError: %bCONTRIBUTING.md should be updated as repositories.yaml file has been updated\n" "${RED}" "${NC}"
printf "%s\n" "Run the command: ./.repositories-update-contributing.sh"
exit 1
fi
fi