-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_version.sh
More file actions
executable file
·267 lines (215 loc) · 7.62 KB
/
create_version.sh
File metadata and controls
executable file
·267 lines (215 loc) · 7.62 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
# =============================================================================
# Fess Documentation Version Creator
# =============================================================================
# This script creates a new version of the Fess documentation by:
# 1. Copying the previous version's documentation directories
# 2. Copying the previous version's image directories (for en and ja)
# 3. Replacing version numbers in all RST files
#
# Usage: ./create_version.sh <new_version>
# Example: ./create_version.sh 15.4
# =============================================================================
set -e
# Language directories to process
LANGUAGES=("ja" "en" "de" "fr" "es" "zh-cn" "ko")
# Languages that have image directories
LANGUAGES_WITH_IMAGES=("ja" "en")
# Script directory (where the script is located)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# =============================================================================
# Helper Functions
# =============================================================================
# Print colored output
print_info() {
echo "[INFO] $1"
}
print_success() {
echo "[SUCCESS] $1"
}
print_warning() {
echo "[WARNING] $1"
}
print_error() {
echo "[ERROR] $1" >&2
}
# Detect OS and return appropriate sed in-place flag
get_sed_inplace_flag() {
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS requires empty string after -i
echo "-i ''"
else
# Linux sed
echo "-i"
fi
}
# Run sed with proper in-place flag based on OS
run_sed_inplace() {
local pattern="$1"
local file="$2"
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "$pattern" "$file"
else
# Linux
sed -i "$pattern" "$file"
fi
}
# Find the latest version with a given major number
# Arguments: $1 = major version number, $2 = search directory
find_latest_minor_version() {
local major="$1"
local search_dir="$2"
local latest_minor=-1
# Find all directories matching the major version pattern
for dir in "$search_dir"/"$major".*; do
if [[ -d "$dir" ]]; then
# Extract the minor version number
local version=$(basename "$dir")
local minor=${version#$major.}
# Check if minor is a valid number
if [[ "$minor" =~ ^[0-9]+$ ]] && [[ "$minor" -gt "$latest_minor" ]]; then
latest_minor=$minor
fi
fi
done
if [[ "$latest_minor" -ge 0 ]]; then
echo "$major.$latest_minor"
else
echo ""
fi
}
# Calculate the previous version
# Arguments: $1 = new version (e.g., "15.4" or "16.0")
calculate_previous_version() {
local new_version="$1"
local major="${new_version%%.*}"
local minor="${new_version#*.}"
if [[ "$minor" -gt 0 ]]; then
# Simple case: decrement minor version
echo "$major.$((minor - 1))"
else
# Minor is 0, need to find the latest version of previous major
local prev_major=$((major - 1))
local latest=$(find_latest_minor_version "$prev_major" "$SCRIPT_DIR/ja")
if [[ -z "$latest" ]]; then
print_error "Could not find any version for major version $prev_major"
exit 1
fi
echo "$latest"
fi
}
# Validate version format (X.Y where X and Y are numbers)
validate_version_format() {
local version="$1"
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+$ ]]; then
print_error "Invalid version format: $version"
print_error "Version must be in format X.Y (e.g., 15.4)"
exit 1
fi
}
# =============================================================================
# Main Script
# =============================================================================
# Check command line arguments
if [[ $# -lt 1 ]]; then
print_error "Usage: $0 <new_version>"
print_error "Example: $0 15.4"
exit 1
fi
NEW_VERSION="$1"
# Validate version format
validate_version_format "$NEW_VERSION"
# Calculate previous version
PREV_VERSION=$(calculate_previous_version "$NEW_VERSION")
print_info "New version: $NEW_VERSION"
print_info "Previous version: $PREV_VERSION"
# =============================================================================
# Pre-flight Checks
# =============================================================================
print_info "Running pre-flight checks..."
# Check if any new version directory already exists
errors_found=0
for lang in "${LANGUAGES[@]}"; do
new_dir="$SCRIPT_DIR/$lang/$NEW_VERSION"
if [[ -d "$new_dir" ]]; then
print_error "Directory already exists: $new_dir"
errors_found=1
fi
done
for lang in "${LANGUAGES_WITH_IMAGES[@]}"; do
new_img_dir="$SCRIPT_DIR/resources/images/$lang/$NEW_VERSION"
if [[ -d "$new_img_dir" ]]; then
print_error "Image directory already exists: $new_img_dir"
errors_found=1
fi
done
if [[ $errors_found -eq 1 ]]; then
print_error "Pre-flight check failed. Please remove existing directories first."
exit 1
fi
print_success "Pre-flight checks passed"
# =============================================================================
# Process Documentation Directories
# =============================================================================
print_info "Processing documentation directories..."
processed_langs=()
skipped_langs=()
for lang in "${LANGUAGES[@]}"; do
prev_dir="$SCRIPT_DIR/$lang/$PREV_VERSION"
new_dir="$SCRIPT_DIR/$lang/$NEW_VERSION"
if [[ ! -d "$prev_dir" ]]; then
print_warning "Skipping $lang: Previous version directory not found: $prev_dir"
skipped_langs+=("$lang")
continue
fi
# Copy directory
print_info "Copying $prev_dir to $new_dir"
cp -r "$prev_dir" "$new_dir"
# Replace version numbers in RST files
print_info "Replacing version numbers in $new_dir/*.rst files"
find "$new_dir" -name "*.rst" -type f | while read -r rst_file; do
run_sed_inplace "s/$PREV_VERSION/$NEW_VERSION/g" "$rst_file"
done
processed_langs+=("$lang")
print_success "Processed: $lang"
done
# =============================================================================
# Process Image Directories
# =============================================================================
print_info "Processing image directories..."
processed_img_langs=()
skipped_img_langs=()
for lang in "${LANGUAGES_WITH_IMAGES[@]}"; do
prev_img_dir="$SCRIPT_DIR/resources/images/$lang/$PREV_VERSION"
new_img_dir="$SCRIPT_DIR/resources/images/$lang/$NEW_VERSION"
if [[ ! -d "$prev_img_dir" ]]; then
print_warning "Skipping images for $lang: Previous version directory not found: $prev_img_dir"
skipped_img_langs+=("$lang")
continue
fi
# Copy image directory
print_info "Copying $prev_img_dir to $new_img_dir"
cp -r "$prev_img_dir" "$new_img_dir"
processed_img_langs+=("$lang")
print_success "Processed images: $lang"
done
# =============================================================================
# Summary
# =============================================================================
echo ""
echo "=============================================="
echo "Summary"
echo "=============================================="
echo "New version: $NEW_VERSION"
echo "Previous version: $PREV_VERSION"
echo ""
echo "Documentation directories:"
echo " Processed: ${processed_langs[*]:-none}"
echo " Skipped: ${skipped_langs[*]:-none}"
echo ""
echo "Image directories:"
echo " Processed: ${processed_img_langs[*]:-none}"
echo " Skipped: ${skipped_img_langs[*]:-none}"
echo "=============================================="
print_success "Version $NEW_VERSION created successfully!"