forked from fossasia/yaydoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
executable file
·227 lines (183 loc) · 7.19 KB
/
generate.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/bash
source logging.sh
while getopts g:t:d:m:u:s:p:b:l: option
do
case "${option}"
in
g) GITURL=${OPTARG};;
t) export DOCTHEME=${OPTARG};;
d) export DEBUG=${OPTARG};;
m) EMAIL=${OPTARG};;
u) UNIQUEID=${OPTARG};;
s) export SUBPROJECT_URLS=${OPTARG};;
p) export SUBPROJECT_DOCPATHS=${OPTARG};;
b) TARGET_BRANCH=${OPTARG};;
l) export DOCPATH=${OPTARG};;
esac
done
function generateAPIDocs {
# $1 :- CLONED REPO DIR
# $2 :- RESULT DIR
cd ${1}
export SWAGGER_UI=""
export SWAGGER_SPEC_URL=""
export JAVADOC_PATH=""
# Setting environment variables for api docs
ENVVARS="$(python ${BASE}/modules/scripts/config/__main__.py)"
echo -e "\n${ENVVARS}\n" >> ${LOGFILE}
eval ${ENVVARS}
if [ -n "${SWAGGER_SPEC_URL}" ]; then
if [ "${SWAGGER_UI}" == "swagger" ]; then
STABLE_SWAGGER=$(curl -s https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | grep tarball_url | cut -d '"' -f 4) >/dev/null 2>&1
wget -O swagger.tar.gz ${STABLE_SWAGGER} >/dev/null 2>&1
mkdir swagger
tar -xf swagger.tar.gz --strip-components=1 -C swagger >/dev/null 2>&1
cp -r swagger/dist/. ${2}/apidocs
sed -i -e "s|http://petstore.swagger.io/v2/swagger.json|${SWAGGER_SPEC_URL}|g" ${2}/apidocs/index.html
fi
fi
if [[ -n "${JAVADOC_PATH}" ]]; then
javadoc -sourcepath ${1}/${JAVADOC_PATH} -encoding UTF-8 -d ${2}/javadoc -subpackages . >>${LOGFILE} 2>>${LOGFILE}
fi
}
URL_SPLIT=(${GITURL//// })
PLATFORM=${URL_SPLIT[1]}
export OWNER=${URL_SPLIT[2]}
export REPONAME=${URL_SPLIT[3]%.git}
# https://stackoverflow.com/a/15454916/4127836
INVENV=$(python -c 'import sys; print ("true" if hasattr(sys, "real_prefix") else "false")')
# Ensures that BASE has contents of yaydoc repository
# and change current directory to git repository.
BASE=$(pwd)
LOGFILE=${BASE}/temp/${EMAIL}/generate_${UNIQUEID}.txt
mkdir -p temp/${EMAIL} && cd $_
print_log "Cloning Repository...\n"
CLONE_URL=https://:@${PLATFORM}/${OWNER}/${REPONAME}
if [ -z "${TARGET_BRANCH}" ]; then
git clone "${CLONE_URL}" "${UNIQUEID}" >>${LOGFILE} 2>>${LOGFILE}
else
git clone -b "${TARGET_BRANCH}" "${CLONE_URL}" "${UNIQUEID}" >>${LOGFILE} 2>>${LOGFILE}
fi
if [ ${?} -ne 0 ]; then
print_danger "Failed to Clone. Either the repository or the specified branch in that repository does not exist.\n"
exit 4
fi
cd ${UNIQUEID}
print_log "Repository Cloned Successfully!\n"
if [ -z "${DOCPATH}" ]; then
print_danger "DOCPATH isn't specified. Default value of 'docs' or configuration from .yaydoc.yml is used"
fi
ROOT_DIR=$(pwd)
if [ -z "${DEPLOYMENT}" ]; then
# Create an isolated Python environment
if [ "${INVENV}" == "false" ]; then
print_log "Installing virtualenv\n"
pip install --user virtualenv >>${LOGFILE} 2>>${LOGFILE}
print_log "Installation successful\n"
print_log "Creating an isolated Python environment\n"
virtualenv --python=python ${HOME}/yaydocvenv >>${LOGFILE} 2>>${LOGFILE}
source ${HOME}/yaydocvenv/bin/activate
print_log "Python environment created successfully!\n"
fi
# Install packages required for documentation generation
print_log "Installing dependencies...\n"
pip install -r ${BASE}/requirements.txt >>${LOGFILE} 2>>${LOGFILE}
print_log "Installation successful\n"
fi
# Copying yaml file
cp .yaydoc.yml ${BASE}/temp/${EMAIL}/${UNIQUEID}.yaydoc.yml >>${LOGFILE} 2>>${LOGFILE}
# Setting environment variables
ENVVARS="$(python ${BASE}/modules/scripts/config/__main__.py)"
echo -e "\n${ENVVARS}\n" >> ${LOGFILE}
eval ${ENVVARS}
if [ ! -d ${ROOT_DIR}/${DOCPATH} ]; then
print_danger "The DOCPATH (${DOCPATH}) does not exist.\n"
exit 5
fi
# Setting up build directory
if [ "${DOCPATH}" != "." ]; then
cd ${DOCPATH}/../ 2>>${LOGFILE}
fi
mkdir yaydoctemp
BUILD_DIR=$(pwd)/yaydoctemp
cd ${ROOT_DIR}
cp -a ${BASE}/modules/scripts/ ${BUILD_DIR}/
cp -a ${BASE}/modules/templates/ ${BUILD_DIR}/
cd ${BUILD_DIR}
mkdir -p _themes
# Setting up documentation sources
print_log "Setting up documentation sources\n"
sphinx-quickstart -q -v "$VERSION" -a "$AUTHOR" -p "$PROJECTNAME" -t templates/ -d html_theme=${DOCTHEME} \
-d html_logo=${LOGO} -d root_dir=${ROOT_DIR} -d autoapi_python=${AUTOAPI_PYTHON} -d mock_modules=${MOCK_MODULES} \
-d owner=${OWNER} -d repo=${REPONAME} -d github_ribbon_enable=${GITHUB_RIBBON_ENABLE} \
-d github_ribbon_position=${GITHUB_RIBBON_POSITION} -d github_ribbon_color=${GITHUB_RIBBON_COLOR} \
-d theme_opts_keys=${DOCTHEME_OPTIONS_KEYS} -d theme_opts_values=${DOCTHEME_OPTIONS_VALUES} \
-d github_button_enable=${GITHUB_BUTTON_ENABLE} -d github_buttons=${GITHUB_BUTTONS} \
-d github_button_large=${GITHUB_BUTTON_LARGE} -d github_button_show_count=${GITHUB_BUTTON_SHOW_COUNT} \
-d enable_inline_math=${ENABLE_INLINE_MATH} \
>>${LOGFILE} 2>>${LOGFILE}
if [ $? -ne 0 ]; then
print_danger "Failed to initialize build process.\n"
exit 1
fi
print_log "Documentation setup successful!\n"
rm index.rst
cd ${ROOT_DIR}
# Extract markup files from source repository and extend pre-existing conf.py
if [ -f ${DOCPATH}/conf.py ]; then
echo >> ${BUILD_DIR}/conf.py
cat ${DOCPATH}/conf.py >> ${BUILD_DIR}/conf.py
fi
rsync -a --exclude=conf.py --exclude=yaydoctemp ${DOCPATH}/ ${BUILD_DIR}/ 2>>${LOGFILE}
cd ${BUILD_DIR}
if [ "${AUTOAPI_PYTHON:-false}" == "true" ]; then
# autodoc imports the module while building source files. To avoid
# ImportError, install any packages in requirements.txt of the project
# if available
if [ -f ${ROOT_DIR}/setup.py ]; then
pip install ${ROOT_DIR}/ >>${LOGFILE} 2>>${LOGFILE}
elif [ -f ${ROOT_DIR}/requirements.txt ]; then
pip install -r ${ROOT_DIR}/requirements.txt >>${LOGFILE} 2>>${LOGFILE}
fi
sphinx-apidoc -o source/ ${ROOT_DIR}/${AUTOAPI_PYTHON_PATH}/ >>${LOGFILE} 2>>${LOGFILE}
fi
if [ "${AUTOAPI_JAVA:-false}" == "true" ]; then
javasphinx-apidoc -o source/ ${ROOT_DIR}/${AUTOAPI_JAVA_PATH}/ >>${LOGFILE} 2>>${LOGFILE}
fi
if [ "${SUBPROJECT_URLS}" != "[]" ]; then
source ${BASE}/multiple_generate.sh
fi
if [ ! -f index.rst ] && [ ! -f index.md ]; then
print_danger "No index.rst found. Auto generating...\n"
GENINDEX_PARAM=${ROOT_DIR}
if [ "${DOCPATH}" == "." ]; then
GENINDEX_PARAM=${ROOT_DIR}/yaydoctemp
fi
python ${BASE}/modules/scripts/genindex.py -s "${SUBPROJECT_DIRS}" -d "${SUBPROJECT_DOCPATHS}" "${GENINDEX_PARAM}"
if [ "${DEBUG:-false}" == "true" ]; then
print_log "\n--------------------------------------\n"
cat index.rst | tee -a ${LOGFILE}
print_log "\n--------------------------------------\n"
fi
print_log "Auto generated index.rst\n"
fi
print_log "Starting Documentation Generation...\n"
make html >> ${LOGFILE} 2>>${LOGFILE}
if [ $? -ne 0 ]; then
print_danger "Failed to generate documentation.\n"
exit 2
fi
print_log "Documentation Generated Successfully!\n"
generateAPIDocs ${ROOT_DIR} ${BUILD_DIR}/_build/html
if [ "${SUBPROJECT_URLS}" != "[]" ]; then
source ${BASE}/multiple_api_docs.sh
fi
print_log "Setting up documentation for Download and Preview\n"
mv ${BUILD_DIR}/_build/html ${ROOT_DIR}/../${UNIQUEID}_preview && cd $_/../
rm -rf ${BASE}/temp/${EMAIL}/${UNIQUEID}
zip -r ${UNIQUEID}.zip ${UNIQUEID}_preview >>${LOGFILE} 2>>${LOGFILE}
if [ $? -ne 0 ]; then
print_danger "Failed setting up.\n"
exit 3
fi
print_log "Documentation setup successful!\n"