CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/extensions/gen-sample-extension-docs.sh
Views: 3956
1
## Hooks
2
function extension_metadata_ready__499_display_docs_generation_start_info() {
3
display_alert "Generating hook documentation and sample extension"
4
}
5
6
function extension_metadata_ready__docs_markdown() {
7
generate_markdown_docs_to_stdout >"${DEST}/"${LOG_SUBPATH}"/hooks.auto.docs.md"
8
}
9
10
function extension_metadata_ready__docs_sample_extension() {
11
mkdir -p "${SRC}/userpatches/extensions"
12
generate_sample_extension_to_stdout >"${SRC}/userpatches/extensions/sample-extension.sh"
13
}
14
15
## Internal functions
16
17
### Common stuff
18
function read_common_data() {
19
export HOOK_POINT_CALLS_COUNT=$(wc -l <"${EXTENSION_MANAGER_TMP_DIR}/hook_point_calls.txt")
20
export HOOK_POINT_CALLS_UNIQUE_COUNT=$(sort <"${EXTENSION_MANAGER_TMP_DIR}/hook_point_calls.txt" | uniq | wc -l)
21
export HOOK_POINTS_WITH_MULTIPLE_CALLS=""
22
23
# Read the hook_points (main, official names) from the hook point ordering file.
24
export ALL_HOOK_POINT_CALLS=$(xargs echo -n <"${EXTENSION_MANAGER_TMP_DIR}/hook_point_calls.txt")
25
}
26
27
function loop_over_hook_points_and_call() {
28
local callback="$1"
29
HOOK_POINT_COUNTER=0
30
for one_hook_point in ${ALL_HOOK_POINT_CALLS}; do
31
export HOOK_POINT_COUNTER=$((HOOK_POINT_COUNTER + 1))
32
export HOOK_POINT="${one_hook_point}"
33
export MARKDOWN_HEAD="$(head -1 "${EXTENSION_MANAGER_TMP_DIR}/${one_hook_point}.orig.md")"
34
export MARKDOWN_BODY="$(tail -n +2 "${EXTENSION_MANAGER_TMP_DIR}/${one_hook_point}.orig.md")"
35
export COMPATIBILITY_NAMES="$(xargs echo -n <"${EXTENSION_MANAGER_TMP_DIR}/${one_hook_point}.compat")"
36
${callback}
37
done
38
}
39
40
## Markdown stuff
41
function generate_markdown_docs_to_stdout() {
42
read_common_data
43
cat <<MASTER_HEADER
44
# Armbian build system extensibility documentation
45
- This documentation is auto-generated.
46
MASTER_HEADER
47
48
[[ $HOOK_POINT_CALLS_COUNT -gt $HOOK_POINT_CALLS_UNIQUE_COUNT ]] && {
49
# Some hook points were called multiple times, determine which.
50
HOOK_POINTS_WITH_MULTIPLE_CALLS=$(comm -13 <(sort <"${EXTENSION_MANAGER_TMP_DIR}/hook_point_calls.txt" | uniq) <(sort <"${EXTENSION_MANAGER_TMP_DIR}/hook_point_calls.txt") | sort | uniq | xargs echo -n)
51
52
cat <<MULTIPLE_CALLS_WARNING
53
- *Important:* The following hook points where called multiple times during the documentation generation. This can be indicative of a bug in the build system. Please check the sources for the invocation of the following hooks: \`${HOOK_POINTS_WITH_MULTIPLE_CALLS}\`.
54
MULTIPLE_CALLS_WARNING
55
56
}
57
58
cat <<PRE_HOOKS_HEADER
59
## Hooks
60
- Hooks are listed in the order they are called.
61
PRE_HOOKS_HEADER
62
63
loop_over_hook_points_and_call "generate_markdown_one_hook_point_to_stdout"
64
65
cat <<MASTER_FOOTER
66
------------------------------------------------------------------------------------------
67
MASTER_FOOTER
68
69
}
70
71
function generate_markdown_one_hook_point_to_stdout() {
72
# Hook name in 3rd level title, first line of description in a blockquote.
73
# The rest in a normal block.
74
cat <<HOOK_DOCS
75
### \`${one_hook_point}\`
76
> ${MARKDOWN_HEAD}
77
78
${MARKDOWN_BODY}
79
80
HOOK_DOCS
81
82
[[ "${COMPATIBILITY_NAMES}" != "" ]] && {
83
echo -e "\n\nAlso known as (for backwards compatibility only):"
84
for old_name in ${COMPATIBILITY_NAMES}; do
85
echo "- \`${old_name}\`"
86
done
87
}
88
89
echo ""
90
}
91
92
## Bash sample extension stuff
93
generate_sample_extension_to_stdout() {
94
read_common_data
95
cat <<HEADER
96
# Sample Armbian build system extension with all extension methods.
97
# This file is auto-generated from and by the build system itself.
98
# Please, always use the latest version of this file as a starting point for your own extensions.
99
# Generation date: $(date)
100
# Read more about the build system at https://docs.armbian.com/Developer-Guide_Build-Preparation/
101
102
HEADER
103
104
loop_over_hook_points_and_call "generate_bash_sample_for_hook_point"
105
}
106
107
generate_bash_sample_for_hook_point() {
108
# Include the markdown documentation as a comment.
109
# Right now clean it up naively (remove backticks, mostly) but we could pipe through stuff to get better plaintext. (pandoc is a 155mb binary FYI)
110
local COMMENT_HEAD="#### $(echo "${MARKDOWN_HEAD}" | tr '`' '"')"
111
# shellcheck disable=SC2001
112
local COMMENT_BODY="$(echo "${MARKDOWN_BODY}" | tr '`' '"' | sed -e 's/^/### /')"
113
114
local bonus=""
115
[[ "${HOOK_POINT_COUNTER}" == "1" ]] && bonus="$(echo -e "\n\texport PROGRESS_DISPLAY=verysilent # Example: export a variable. This one silences the built.")"
116
117
cat <<SAMPLE_BASH_CODE
118
${COMMENT_HEAD}
119
${COMMENT_BODY}
120
function ${HOOK_POINT}__be_more_awesome() {
121
# @TODO: Please rename this function to reflect what it does, but preserve the "${HOOK_POINT}__" prefix.
122
display_alert "Being awesome at \${HOOK_POINT}" "\${EXTENSION}" "info"${bonus}
123
}
124
125
SAMPLE_BASH_CODE
126
}
127
128