|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +DOCS_REPO=$1 |
| 4 | +DOCS_VERSION=$2 |
| 5 | + |
| 6 | +RED='\033[0;31m' |
| 7 | +NC='\033[0m' # No Color |
| 8 | +function echoerr() { |
| 9 | + printf '%s%s%s\n' "$RED" "$@" "$NC" 1>&2; |
| 10 | +} |
| 11 | + |
| 12 | +function copy() { |
| 13 | + printf 'cp %s\t→\t%s\n' "$1" "$2" |
| 14 | + cp "$1" "$2" |
| 15 | +} |
| 16 | + |
| 17 | +INFINITY=9223372036854775807 |
| 18 | +COUNT_INDENTS=0 |
| 19 | +function count_indents_on_line() { |
| 20 | + local file=$1 |
| 21 | + local lineno=$2 |
| 22 | + local line |
| 23 | + line=$(sed -n "${lineno}p" "$file") |
| 24 | + local trim="${line#*[^[:blank:]]}" |
| 25 | + COUNT_INDENTS="$(( ${#line} - ${#trim} - 1 ))" |
| 26 | + |
| 27 | + # empty lines count as "infinitely indented" |
| 28 | + if [ "$COUNT_INDENTS" -lt 0 ] |
| 29 | + then |
| 30 | + COUNT_INDENTS="$INFINITY" |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +function insert_pdk_nav_file_into_docs_nav_file() { |
| 35 | + local pdk_nav_file=$1 |
| 36 | + local docs_nav_file=$2 |
| 37 | + |
| 38 | + # find first_line and last line of the current pdk index in docs_nav_file |
| 39 | + # first line contains "Plugin Development Kit" |
| 40 | + local first_line |
| 41 | + first_line=$(sed -n "/text: Plugin Development Kit/=" "$docs_nav_file") |
| 42 | + |
| 43 | + # find last_line by looking at the indentation of all subsequent lines, looking at the indentation |
| 44 | + # last line is the last one with more indentation that first_line |
| 45 | + local max_line |
| 46 | + max_line=$(wc -l < "$docs_nav_file" | bc) |
| 47 | + count_indents_on_line "$docs_nav_file" "$first_line" |
| 48 | + local initial_indent="$COUNT_INDENTS" |
| 49 | + local current_indent="$(( initial_indent + 1 ))" |
| 50 | + local current_line="$first_line" |
| 51 | + while [ "$current_indent" -gt "$initial_indent" ] && [ "$current_line" -lt "$max_line" ] |
| 52 | + do |
| 53 | + current_line="$((current_line + 1))" |
| 54 | + count_indents_on_line "$docs_nav_file" "$current_line" |
| 55 | + current_indent="$COUNT_INDENTS" |
| 56 | + done |
| 57 | + local last_line="$(( current_line - 1 ))" |
| 58 | + |
| 59 | + # insert_line is where we want to put the new pdk docs after deleting the old ones (one less than first) |
| 60 | + local insert_line="$(( first_line - 1 ))" |
| 61 | + |
| 62 | + # delete existing pdk index, insert new pdk nav file instead |
| 63 | + sed -i '' -e "${first_line},${last_line}d" -e "${insert_line}r${pdk_nav_file}" "$docs_nav_file" |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +echo "Generating docs ..." |
| 68 | + |
1 | 69 | rm -rf ./autodoc/output
|
2 | 70 | ./autodoc/admin-api/generate.lua && \
|
3 | 71 | ./autodoc/cli/generate.lua && \
|
4 | 72 | ./autodoc/conf/generate.lua && \
|
5 | 73 | ./autodoc/pdk/generate.lua
|
| 74 | + |
| 75 | +if [ -z "$DOCS_REPO" ] || [ -z "$DOCS_VERSION" ] |
| 76 | +then |
| 77 | + echo |
| 78 | + echo "No docs repo & version parameters found. For example, this:" |
| 79 | + echo |
| 80 | + echo " script/autodocs ../docs.konghq.com 2.1.x" |
| 81 | + echo |
| 82 | + echo "would install the files in the docs repo, located in ../docs.konghq.com," |
| 83 | + echo "and in the 2.1.x version" |
| 84 | + echo |
| 85 | + echo "Since no repo or version was specified, I won't attempt to copy the files, and exit successfully now" |
| 86 | + echo |
| 87 | + |
| 88 | + exit 0 |
| 89 | +fi |
| 90 | + |
| 91 | +if [ -d "$DOCS_REPO" ] |
| 92 | +then |
| 93 | + echo |
| 94 | + echo "docs repo: $DOCS_REPO" |
| 95 | + echo "docs version: $DOCS_VERSION" |
| 96 | + |
| 97 | +else |
| 98 | + echoerr |
| 99 | + echoerr "Could not find the docs repo in $DOCS_REPO . Please clone it there so I can copy the autodocs files there" |
| 100 | + echoerr "For example with:" |
| 101 | + echoerr "cd .." |
| 102 | + echoerr "git clone https://github.com/kong/docs.konghq.com" |
| 103 | + echoerr "cd docs.konghq.com" |
| 104 | + echoerr "git checkout -b docs/autodocs" |
| 105 | + echoerr |
| 106 | + |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | + |
| 111 | +if [ ! -d "$DOCS_REPO/autodoc-nav" ] |
| 112 | +then |
| 113 | + echoerr |
| 114 | + echoerr "The folder $DOCS_REPO/autodoc-nav does not exist. Please create it in order to copy the autodocs there" |
| 115 | + echoerr "Usually this is done by copying and renaming a previous version of the docs" |
| 116 | + |
| 117 | + exit 2 |
| 118 | +fi |
| 119 | + |
| 120 | +DOCS_APP="$DOCS_REPO/app/gateway-oss/$DOCS_VERSION" |
| 121 | + |
| 122 | +if [ ! -d "$DOCS_APP" ] |
| 123 | +then |
| 124 | + echoerr |
| 125 | + echoerr "The app doc folder for the chosen version ($DOCS_APP) does not exist. Please create it in order to copy the autodocs there" |
| 126 | + echoerr "Usually this is done by copying and renaming a previous version of the docs" |
| 127 | + echoerr "The file $DOCS_REPO/app/_data/kong_versions.yml might need to be updated with the new version as well" |
| 128 | + echoerr |
| 129 | + |
| 130 | + exit 3 |
| 131 | +fi |
| 132 | + |
| 133 | +DOCS_NAV="$DOCS_REPO/app/_data/docs_nav_ce_$DOCS_VERSION.yml" |
| 134 | + |
| 135 | +if [ ! -f "$DOCS_NAV" ] |
| 136 | +then |
| 137 | + echoerr |
| 138 | + echoerr "The doc file $DOCS_NAV does not exist. Please create it in order to copy the autodocs there" |
| 139 | + echoerr "Usually this is done by copying and renaming a previous version of the docs. Example:" |
| 140 | + echoerr |
| 141 | + echoerr "cp $DOCS_REPO/app/_data/docs_nav_**REPLACE_THIS**.yml $DOCS_NAV" |
| 142 | + echoerr |
| 143 | + |
| 144 | + exit 4 |
| 145 | +fi |
| 146 | + |
| 147 | +copy autodoc/output/admin-api/admin-api.md "$DOCS_APP/admin-api.md" |
| 148 | +copy autodoc/output/admin-api/db-less-admin-api.md "$DOCS_APP/db-less-admin-api.md" |
| 149 | +copy autodoc/output/nav/docs_nav.yml.admin-api.in "$DOCS_REPO/autodoc-nav/docs_nav_$DOCS_VERSION.yml.head.in" |
| 150 | + |
| 151 | +copy autodoc/output/configuration.md "$DOCS_APP/configuration.md" |
| 152 | +copy autodoc/output/cli.md "$DOCS_APP/cli.md" |
| 153 | + |
| 154 | +rm -rf "$DOCS_APP/pdk/*" |
| 155 | +mkdir -p "$DOCS_APP/pdk" |
| 156 | +copy autodoc/output/pdk/* "$DOCS_APP/pdk/" |
| 157 | + |
| 158 | +echo "Patching $DOCS_NAV with autodoc/output/pdk_nav.yml ..." |
| 159 | +insert_pdk_nav_file_into_docs_nav_file ./autodoc/output/_pdk_nav.yml "$DOCS_NAV" |
0 commit comments