-
Notifications
You must be signed in to change notification settings - Fork 37
/
versions.sh
executable file
·211 lines (156 loc) · 8.01 KB
/
versions.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
#!/bin/bash
set -e
###################################################################################################
# This script is used to generate the versioned documentation for Spectro Cloud Docs.
# The script first loops through all version-* branches and runs the Docusaurus command `npm run docusaurus docs:version <version>`
# to generate the versioned documentation.
# The version folders and their content are moved to a temp directory specified by you through the first argument to the script.
# Once all the version folders are generated, the script moves the versioned folders from the temp directory to the root directory of the repository.
# Lastly, a nodeJS script (update_docusaurs_config.js) is invoked to update the docusarus.config.js file.
# The docusarus.config.js file is updated to remove the default Docusarus banner for old versions that states it's no longer maintained.
# The script also updates the versions.json file to include the new versions.
# Last note. You can use the versionsOverride.json file to customize the versions label, banner, and other properties.
###################################################################################################
tempdir=$1
baseDir="$PWD" # Get the parent directory of the current directory
# Check if the temp directory is specified, otherwise use /tmp
if [ -z "$tempdir" ]; then
tempdir="/tmp"
fi
echo "Temp directory: $tempdir"
echo "Base directory: $baseDir"
# List of version branches to exclude
exclude_branches=(version-3-4 version-4-0 version-4-1 version-4-2 version-4-3) # DO NOT ADD A COMMA BETWEEN THE BRANCHES. ADD A SPACE INSTEAD AND THE NEW VERSION STRING.
# exclude_branches=("version-3-4")
# Save the current branch name
current_branch=$(git branch --show-current)
# If current branch is empty, then use HEAD
if [ -z "$current_branch" ]; then
current_branch=$HEAD
fi
echo "Current branch: $current_branch"
echo "HEAD Branch: $HEAD"
# Fetch all branches from the remote
git fetch -p origin
branches=$(git branch -a | grep -E 'version-[0-9]+(-[0-9]+)*$')
# Loop through each branch to fetch it locally
for b in $branches; do
# Remove leading spaces and remote prefix (if any)
b=$(echo $b | sed 's/ *//;s/remotes\/origin\///')
# Fetch the remote branch to corresponding local branch
git fetch origin $b:$b
done
# Make sure we are in a clean state for repeatable runs.
make clean-versions
# Remove the existing versioned directories in the temp directory.
rm -rf $tempdir/staging_docs
rm -rf $tempdir/staging_api_docs
rm -rf $tempdir/staging_sidebars
rm -rf $tempdir/staging_api_docs_sidebars
rm -rf $tempdir/staging_partials
rm -rf $tempdir/temp_versions.json
rm -rf $tempdir/temp_api_versions.json
# Create the staging directory for all the versions
mkdir -p $tempdir/staging_docs
mkdir -p $tempdir/staging_api_docs
mkdir -p $tempdir/staging_sidebars
mkdir -p $tempdir/staging_api_docs_sidebars
mkdir -p $tempdir/staging_partials
touch $tempdir/temp_versions.json
touch $tempdir/temp_api_versions.json
echo '[]' > $tempdir/temp_versions.json # Initialize as an empty array if it doesn't exist
echo '[]' > $tempdir/temp_api_versions.json # Initialize as an empty array if it doesn't exist
echo "Entering the loop to generate the versioned documentation"
# Loop through all local branches
for item in $(git branch --format '%(refname:short)'); do
echo "Checking branch: $branch"
# Check if the branch is in the exclude list
if [[ " ${exclude_branches[@]} " =~ " ${item} " ]]; then
echo "Skipping excluded branch: $item"
continue
fi
# Check if the branch name starts with 'version-' followed by numbers
if [[ $item =~ ^version-[0-9]+(-[0-9]+)*$ ]]; then
echo "Found branch: $item"
# Extract the version and replace '-' with '.'
version=${item#version-}
version=${version//-/.}
# Append .x to the version
versionX="$version.x"
# Append .0 to the version
version="$version.0"
# Store in a variable
extracted_version=$version
extracted_versionX=$versionX
echo "Extracted version: $extracted_version"
# Add version to temp_versions.json and sort it
jq --arg ver "$extracted_version" '. |= [$ver] + . | sort_by(. | split(".") | map(tonumber)) | reverse' $tempdir/temp_versions.json > $tempdir/temp.json && mv $tempdir/temp.json $tempdir/temp_versions.json
jq --arg ver "$extracted_version" '. |= [$ver] + . | sort_by(. | split(".") | map(tonumber)) | reverse' $tempdir/temp_api_versions.json > $tempdir/temp_api.json && mv $tempdir/temp_api.json $tempdir/temp_api_versions.json
# Switch to the version branch
git checkout $item
# Pull the latest changes
git pull origin $item
# Generate the partials once we are on the version branch
make generate-partials
# Run the npm command
echo "Running: npm run docusaurus docs:version $extracted_versionX"
npm run docusaurus docs:version $extracted_versionX
# Generate the API docs
echo "Running: npm run generate-api-docs"
npm run generate-api-docs
echo "Running: npm run docusaurus docs:version:api $extracted_versionX"
npm run docusaurus docs:version:api $extracted_versionX
# Copy version docs content
cp -R versioned_docs/version-$extracted_versionX $tempdir/staging_docs/
cp -R versioned_sidebars/version-$extracted_versionX $tempdir/staging_sidebars/ || true
cp versioned_sidebars/version-$extracted_versionX-sidebars.json $tempdir/staging_sidebars/version-$extracted_versionX-sidebars.json
# Copy version API docs content
cp -R api_versioned_docs/version-$extracted_versionX $tempdir/staging_api_docs/
cp -R api_versioned_sidebars/version-$extracted_versionX $tempdir/staging_api_docs_sidebars/ || true
cp api_versioned_sidebars/version-$extracted_versionX-sidebars.json $tempdir/staging_api_docs_sidebars/version-$extracted_versionX-sidebars.json
# Copy the partials folder
cp -R _partials $tempdir/staging_partials/version-$extracted_versionX
rm -rf versioned_docs/
rm -rf versioned_sidebars/
rm -rf api_versioned_docs/
rm -rf api_versioned_sidebars/
rm versions.json
rm api_versions.json
# Remove API auto-generated files
make clean-api
# Switch back to the original branch
git checkout $current_branch
echo "Switched back to branch: $current_branch"
fi
done
# Rename the staging directory to the expected Docusarus versioned directory names
cp -R $tempdir/staging_docs $baseDir/versioned_docs
cp -R $tempdir/staging_sidebars $baseDir/versioned_sidebars
cp -R $tempdir/staging_api_docs $baseDir/api_versioned_docs
cp -R $tempdir/staging_api_docs_sidebars $baseDir/api_versioned_sidebars
cp -R $tempdir/staging_partials/. $baseDir/versioned_partials
# Remove the existing versions.json if it exists
[ -e versions.json ] && rm versions.json
[ -e api_versions.json ] && rm api_versions.json
# Replace the last number with 'x' to indicate it's a version branch
jq '.[] |= (split(".")[:-1] | join(".")) + ".x"' $tempdir/temp_versions.json > $tempdir/temp.json && mv $tempdir/temp.json $tempdir/temp_versions.json
jq '.[] |= (split(".")[:-1] | join(".")) + ".x"' $tempdir/temp_api_versions.json > $tempdir/temp_api.json && mv $tempdir/temp_api.json $tempdir/temp_api_versions.json
# Rename temp_versions.json to versions.json
mv $tempdir/temp_versions.json $baseDir/versions.json
mv $tempdir/temp_api_versions.json $baseDir/api_versions.json
echo "Updating docusarus.config.js through the node script."
node $baseDir/scripts/update_docusarus_config.js $tempdir $baseDir
if [ $? -ne 0 ]; then
echo "Error updating docusarus.config.js"
exit 1
fi
mv $tempdir/temp.docusaurus.config.js $baseDir/docusaurus.config.js
echo "Versioned documentation generated successfully"
echo "Create the robots.txt file"
# Invoke the generated_robots.sh script
$baseDir/scripts/generate_robots.sh $baseDir/versions.json $baseDir/static
if [ $? -ne 0 ]; then
echo "Error generating the robots.txt file"
exit 1
fi
echo "robots.txt file generated successfully"