generated from shuding/nextra-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 38
/
generate-seid-docs.sh
94 lines (76 loc) · 2.71 KB
/
generate-seid-docs.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
#!/bin/bash
# Path to the seid binary
SEID_PATH=~/go/bin/seid
# Function to extract subcommands from help text blob
extract_subcommands() {
local cmd=$1
$cmd --help | awk '/^ [a-zA-Z0-9_-]+/ {print $1}' | grep -v '^-'
}
# Function to generate help documentation for seid command and its subcommands
generate_docs() {
local for_cmd=$1
local output_dir=$2
local full_cmd=$3
# Determine the name for the output file and directory
if [[ "$for_cmd" == "$SEID_PATH" ]]; then
# If it's the initial seid command
local cmd_name="seid"
local sub_dir="$output_dir"
full_cmd="seid"
else
local cmd_name=$(echo "$for_cmd" | sed 's/.*seid //')
local sub_dir="$output_dir/$(echo "$cmd_name" | awk -F ' ' '{for (i=1; i<NF; i++) printf "%s/", $i}')"
cmd_name=$(echo "$cmd_name" | awk '{print $NF}')
full_cmd="$full_cmd $cmd_name"
fi
local output_file="$sub_dir$cmd_name.md"
# Create directory if it doesn't exist
mkdir -p "$sub_dir"
echo "Generating: $output_file"
# Add a header and the help command output wrapped in yaml code block for formatting
{
echo "### \`$full_cmd\`"
printf "\`\`\`ansi\n"
$for_cmd --help
printf "\n\`\`\`"
} > "$output_file"
# Replace the user directory path with ~/
sed -i '' "s|$(echo ~)|~|g" "$output_file"
# Extract subcommands and generate their help
local subcommands
subcommands=$(extract_subcommands "$for_cmd")
for subcmd in $subcommands; do
# Avoid recursion issues by skipping invalid or repeating subcommands like --help, seid, things with commas, and "To"
[[ "$subcmd" == "seid" || "$subcmd" == *","* || "$subcmd" == *"--"* || "$subcmd" == "To" ]] && continue
generate_docs "$for_cmd $subcmd" "$output_dir" "$full_cmd"
done
}
# Function to generate _meta.json file in the given directory
generate_meta() {
local dir=$1
local meta_file="$dir/_meta.json"
echo "Generating _meta.json for $dir"
# Create _meta.json with filenames as keys and values without the .md extension
echo "{" > "$meta_file"
for md_file in "$dir"/*.md; do
local filename=$(basename "$md_file" .md)
echo " \"$filename\": \"$filename\"," >> "$meta_file"
done
# Remove the trailing comma and close the JSON object
sed -i '' '$ s/,$//' "$meta_file"
echo "}" >> "$meta_file"
}
# Function to recursively generate _meta.json for all directories
generate_meta_for_all() {
local base_dir=$1
find "$base_dir" -type d | while read -r dir; do
generate_meta "$dir"
done
}
output_dir="./pages/seid"
mkdir -p "$output_dir"
# Generate help for the main seid command
generate_docs "$SEID_PATH" "$output_dir" "seid"
# Generate _meta.json for all directories
generate_meta_for_all "$output_dir"
echo "Seid docs written to $output_dir"