Debian Codegraph @

script/api-docs/generate

raw
1 #!/usr/bin/env bash
2 #
3 # Usage: generate repo_path output_path
4 #
5 # Example: generate https://github.com/libgit2/libgit2 path_to_output
6 # to clone the repository from GitHub and produce documentation;
7 # the repo_path can also be a local path
8
9 set -eo pipefail
10
11 source_path=$(mktemp -d)
12 verbose=
13 force=
14
15 for var in "$@"; do
16 if [ "${var}" == "--verbose" ]; then
17 verbose=true
18 elif [ "${var}" == "--force" ]; then
19 force=true
20 elif [ "${repo_path}" == "" ]; then
21 repo_path="${var}"
22 elif [ "${output_path}" == "" ]; then
23 output_path="${var}"
24 else
25 repo_path=""
26 output_path=""
27 fi
28 done
29
30 if [ "${repo_path}" = "" -o "${output_path}" = "" ]; then
31 echo "usage: $0 [--verbose] [--force] repo_path output_path" 1>&2
32 exit 1
33 fi
34
35 function do_checkout {
36 if [ "$1" = "" ]; then
37 echo "usage: $0 source_path" 1>&2
38 exit 1
39 fi
40
41 if [ "${verbose}" ]; then
42 echo ":: Checking out source trees..."
43 echo ""
44 fi
45
46 source_path=$1
47
48 mkdir -p "${source_path}"
49 git clone "${repo_path}" "${source_path}/main" --no-checkout
50 ( cd "${source_path}/main" && git sparse-checkout set --no-cone 'include/*' )
51 ( cd "${source_path}/main" && git read-tree origin/main )
52 ( cd "${source_path}/main" && git checkout -- include )
53
54 for tag in $(git --git-dir="${source_path}/main/.git" tag -l); do
55 git --git-dir="${source_path}/main/.git" worktree add -f "${source_path}/${tag}" "${tag}" --no-checkout
56 ( cd "${source_path}/${tag}" && git sparse-checkout set --no-cone 'include/*' )
57 ( cd "${source_path}/${tag}" && git read-tree HEAD )
58
59 if [ "${tag}" == "v0.1.0" ]; then
60 ( cd "${source_path}/${tag}" && git checkout -- src/git )
61 elif [ "${tag}" == "v0.2.0" -o "${tag}" == "v0.3.0" ]; then
62 ( cd "${source_path}/${tag}" && git checkout -- src/git2 )
63 else
64 ( cd "${source_path}/${tag}" && git checkout -- include )
65 fi
66 done
67 }
68
69 do_checkout ${source_path}
70
71 if [ "${verbose}" ]; then
72 echo ""
73 echo ":: Generating raw API documentation..."
74 echo ""
75 fi
76
77 for version in ${source_path}/*; do
78 version=$(echo "${version}" | sed -e "s/.*\///")
79 commit=$( cd "${source_path}/${version}" && git rev-parse HEAD )
80
81 if [ -f "${output_path}/api/${version}.json" ]; then
82 existing_commit=$(jq -r .info.commit < "${output_path}/api/${version}.json")
83
84 if [ "${existing_commit}" == "${commit}" -a ! "${force}" ]; then
85 if [ "${verbose}" ]; then
86 echo "Raw API documentation for ${version} exists; skipping..."
87 fi
88
89 continue
90 fi
91 fi
92
93 echo "Generating raw API documentation for ${version}..."
94 mkdir -p "${output_path}/api"
95 node ./api-generator.js "${source_path}/${version}" > "${output_path}/api/${version}.json"
96 done
97
98 if [ "${verbose}" ]; then
99 echo ""
100 echo ":: Generating HTML documentation..."
101 echo ""
102 fi
103
104 search_options=""
105 docs_options=""
106 if [ "${verbose}" ]; then
107 search_options="${search_options} --verbose"
108 docs_options="${docs_options} --verbose"
109 fi
110 if [ "${force}" ]; then
111 docs_options="${docs_options} --force"
112 fi
113
114 node ./search-generator.js ${search_options} "${output_path}/api" "${output_path}/search-index"
115 node ./docs-generator.js ${docs_options} --jekyll-layout default "${output_path}/api" "${output_path}/reference"