|
#!/bin/bash |
|
|
|
source "${BASH_SOURCE%/*}/../../scripts/common.sh" |
|
|
|
|
|
ROOT=$(realpath "$(dirname "${BASH_SOURCE[0]}")/../..") |
|
WEBSITE_HOME="https://openfactcheck.readthedocs.io/en" |
|
DOCS_HOME="docs" |
|
DOCS_LATEST="latest" |
|
DOCS_ROOT="$ROOT/$DOCS_HOME" |
|
DOCS_DEST="$ROOT/public" |
|
VERSION_FILE="$DOCS_ROOT/src/_static/versions.json" |
|
|
|
|
|
function format_version_entry { |
|
local version=$1 |
|
echo "{\"name\": \"$version\", \"version\": \"v$version\", \"url\": \"$WEBSITE_HOME/v$version/\"}" |
|
} |
|
|
|
|
|
function format_dev_version_entry { |
|
echo "{\"name\": \"dev\", \"version\": \"dev\", \"url\": \"$WEBSITE_HOME/dev/\"}" |
|
} |
|
|
|
|
|
function format_stable_version_entry { |
|
local version=$1 |
|
echo "{\"name\": \"$version (stable)\", \"version\": \"v$version\", \"url\": \"$WEBSITE_HOME/stable/\", \"preferred\": true}" |
|
} |
|
|
|
|
|
function get_versions { |
|
jq -r '.[] | .name' "$VERSION_FILE" |
|
} |
|
|
|
|
|
function generate_versions_file { |
|
local versions=$(get_versions) |
|
local entries=($(format_dev_version_entry)) |
|
|
|
for version in $versions; do |
|
entries+=($(format_version_entry "$version")) |
|
done |
|
|
|
echo "${entries[@]}" | jq -s '.' > "$DOCS_DEST/version.json" |
|
} |
|
|
|
|
|
dry_run=0 |
|
new_version="" |
|
|
|
|
|
function show_help { |
|
echo "Usage: $0 [-d|--dryrun] -n|--new-version <version>" |
|
echo " -d, --dryrun: Dry run; do not write any changes, just print the output." |
|
echo " -n, --new-version: Specify the new version to be added. This is a required argument." |
|
} |
|
|
|
|
|
while [[ $# -gt 0 ]]; do |
|
case "$1" in |
|
(-d|--dryrun) |
|
dry_run=1 |
|
shift |
|
;; |
|
(-n|--new-version) |
|
if [[ -n "$2" ]]; then |
|
new_version="$2" |
|
shift 2 |
|
else |
|
echo "Error: Argument for $1 is missing." |
|
show_help |
|
exit 1 |
|
fi |
|
;; |
|
(-h|--help) |
|
show_help |
|
exit 0 |
|
;; |
|
(*) |
|
echo "Invalid option: $1" |
|
show_help |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
|
|
if [[ -z "$new_version" ]]; then |
|
echo "Error: -n|--new-version is required." |
|
show_help |
|
exit 1 |
|
fi |
|
|
|
|
|
if [[ -z "$new_version" ]]; then |
|
echo "Error: -n new_version is required." |
|
show_help |
|
exit 1 |
|
fi |
|
|
|
|
|
current_versions=$(get_versions) |
|
stable_version=$(echo "$current_versions" | tail -n +2 | head -n 1 | cut -d' ' -f1) |
|
echo "Last Stable version: $stable_version" |
|
|
|
|
|
old_versions=($(echo "$current_versions" | tail -n +3)) |
|
old_versions+=("$stable_version") |
|
old_versions=($(printf '%s\n' "${old_versions[@]}" | tac)) |
|
echo "Old versions: ${old_versions[*]}" |
|
|
|
|
|
if echo "${old_versions[@]}" | grep -q "$new_version"; then |
|
echo "Version $new_version already exists in versions.json" |
|
exit 1 |
|
fi |
|
|
|
entries=( $(format_dev_version_entry) $(format_stable_version_entry "$new_version") ) |
|
for version in ${old_versions[@]}; do |
|
entries+=( $(format_version_entry "$version") ) |
|
done |
|
|
|
if [ "$dry_run" -eq 1 ]; then |
|
echo "$(echo ${entries[@]} | jq -s '.')" |
|
else |
|
echo "Writing to versions.json" |
|
echo "$(echo ${entries[@]} | jq -s '.')" > "$VERSION_FILE" |
|
fi |