File size: 3,566 Bytes
790a3cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d54cef8
790a3cd
 
 
 
f78f1bb
790a3cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

source "${BASH_SOURCE%/*}/../../scripts/common.sh"

# Configurable variables
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"

# Formats a version entry
function format_version_entry {
    local version=$1
    echo "{\"name\": \"$version\", \"version\": \"v$version\", \"url\": \"$WEBSITE_HOME/v$version/\"}"
}

# Formats the development version entry
function format_dev_version_entry {
    echo "{\"name\": \"dev\", \"version\": \"dev\", \"url\": \"$WEBSITE_HOME/dev/\"}"
}

# Formats the stable version entry
function format_stable_version_entry {
    local version=$1
    echo "{\"name\": \"$version (stable)\", \"version\": \"v$version\", \"url\": \"$WEBSITE_HOME/stable/\", \"preferred\": true}"
}

# Retrieves versions from versions.json
function get_versions {
    jq -r '.[] | .name' "$VERSION_FILE"
}

# Generate the version.json 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"
}

# Initialize default values
dry_run=0
new_version=""

# Function to show help message
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."
}

# Manual parsing of command-line options
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

# Check if new version was specified
if [[ -z "$new_version" ]]; then
    echo "Error: -n|--new-version is required."
    show_help
    exit 1
fi

# Check if new version was specified
if [[ -z "$new_version" ]]; then
    echo "Error: -n new_version is required."
    show_help
    exit 1
fi

# Check the last stable version
current_versions=$(get_versions)
stable_version=$(echo "$current_versions" | tail -n +2 | head -n 1 | cut -d' ' -f1)
echo "Last Stable version: $stable_version"

# Check the old versions
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[*]}"

# Create new version entry
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