-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·260 lines (220 loc) · 7.82 KB
/
build.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
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
#!/bin/bash
set -e
set -o pipefail
# Get the directory of the script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Directory for the site sources
TARGET_DIR="$SCRIPT_DIR/training-slicer-org"
# Settings for building the site
CONFIG_OPTS="_config.yml,$SCRIPT_DIR/_config_training.yml"
err() { echo -e >&2 "ERROR: $@\n"; }
die() { err "$@"; exit 1; }
# Help message
show_help() {
echo "Usage: $(basename "$0") [OPTIONS]"
echo ""
echo "Options:"
echo " --target-source-dir PATH Directory for preparing source files into (default: $TARGET_DIR)."
echo " --prepare Prepare the environment by cloning, setting up files and skipping cleanup."
echo " --install-ruby-deps Install Ruby dependencies (implies --prepare)."
echo " --slicer-org-checkout SHA Checkout slicer.org repository to the specified commit SHA (default: $SLICER_ORG_SHA)."
echo " --slicer-org-source-dir PATH Use an existing slicer.org source directory instead of cloning the repo."
echo " --build Build the site locally into the '_site' directory using Jekyll (implies --prepare)."
echo " --serve Build and serve the site locally using Jekyll (implies --prepare)."
echo " --skip-cleanup Skip removal of target directory (default: $TARGET_DIR)."
echo " --extra-config PATH1[,PATH2] Path of additional config files to associate with the --config Jekyll option."
echo " -h, --help Show this help message and exit."
}
# Argument default values
BUILD=false
SERVE=false
SLICER_ORG_SOURCE_DIR="" # Custom "slicer.org" source directory
CLEANUP=true # Flag to track if copied files should be removed on exit
INSTALL_RUBY_DEPS=false
PREPARE=false
EXTRA_CONFIG_OPTS=""
# Extract SHA associated with 'ref'
GITHUB_WORKFLOW_FILE="$SCRIPT_DIR/.github/workflows/build-website.yml"
if [[ ! -f "$GITHUB_WORKFLOW_FILE" ]]; then
die "GitHub Workflow '$GITHUB_WORKFLOW_FILE' does not exist"
fi
set +e
SLICER_ORG_SHA=$(grep -oP '(?<=slicer_org_sha: )[\da-f]+' "$GITHUB_WORKFLOW_FILE")
set -e
if [[ -z "$SLICER_ORG_SHA" ]]; then
die "No Slicer SHA found in $GITHUB_WORKFLOW_FILE"
fi
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--target-source-dir)
shift
TARGET_DIR="$(realpath "$1")" # Get absolute path to avoid issues
;;
--install-ruby-deps)
INSTALL_RUBY_DEPS=true
;;
--prepare)
PREPARE=true
;;
--slicer-org-checkout)
shift
SLICER_ORG_SHA="$1"
;;
--slicer-org-source-dir)
shift
SLICER_ORG_SOURCE_DIR="$(realpath "$1")" # Get absolute path to avoid issues
;;
--build)
BUILD=true
;;
--serve)
SERVE=true
;;
--skip-cleanup)
CLEANUP=false
;;
--extra-config)
shift
EXTRA_CONFIG_OPTS="$1"
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Unknown argument: $1"
show_help
exit 1
;;
esac
shift
done
if [[ "$INSTALL_RUBY_DEPS" == true || "$BUILD" == true || "$SERVE" == true ]]; then
PREPARE=true
fi
if $PREPARE; then
CLEANUP=false
fi
if [[ "$BUILD" == true && "$SERVE" == true ]]; then
die "Options --build and --serve are mutually exclusive."
fi
if [[ "$PREPARE" != true && "$INSTALL_RUBY_DEPS" != true && "$BUILD" != true && "$SERVE" != true ]]; then
die "At least one of these options should be specified: --prepare, --install-ruby-deps, --build or --serve."
fi
prepare_environment() {
echo "Preparing environment..."
github_short_sha=$(git -C "$SCRIPT_DIR" rev-parse --short HEAD)
echo "training.slicer.org version is $github_short_sha"
mkdir -p "$TARGET_DIR"
echo "github_short_sha: ${github_short_sha}" > $TARGET_DIR/_config_training_revision.yml
CONFIG_OPTS="$CONFIG_OPTS,_config_training_revision.yml"
# Determine if using a source directory or cloning the repository
if [[ -n "$SLICER_ORG_SOURCE_DIR" ]]; then
if [[ ! -d "$SLICER_ORG_SOURCE_DIR" ]]; then
die "Source directory $SLICER_ORG_SOURCE_DIR does not exist"
fi
else
source_dir="$SCRIPT_DIR/slicer-org"
cloned=false
# Clone the repository if it does not exist
if [[ ! -d "$source_dir" ]]; then
echo "Cloning slicer.org repository..."
git clone https://github.com/Slicer/slicer.org.git "$source_dir"
cloned=true
else
echo "Repository already exists. Skipping clone."
fi
# Ensure the repository directory exists before proceeding
if [[ ! -d "$source_dir" ]]; then
die "$source_dir directory does not exist"
fi
# Perform checkout if explicitly requested or if the repo was freshly cloned
if [[ -n "$SLICER_ORG_SHA" || "$cloned" == true ]]; then
echo "Checking out commit: ${SLICER_ORG_SHA:-HEAD}"
(cd "$source_dir" && git fetch origin && git checkout "${SLICER_ORG_SHA:-HEAD}")
fi
SLICER_ORG_SOURCE_DIR="$source_dir"
fi
}
prepare_environment
cleanup() {
if $CLEANUP; then
echo "Cleaning up..."
echo "Removing: $TARGET_DIR"
rm -rf "$TARGET_DIR"
else
echo "Skipping cleanup"
fi
}
# Register cleanup function to execute on exit
trap cleanup EXIT
# List of files to copy
FILES_TO_COPY=(
"$SCRIPT_DIR/training.markdown:$TARGET_DIR/training.markdown"
"$SCRIPT_DIR/training.markdown:$TARGET_DIR/index.markdown"
"$SCRIPT_DIR/_data/tutorials.yml:$TARGET_DIR/_data/tutorials.yml"
)
# Include all image assets
mkdir -p "$TARGET_DIR/assets/img"
for img in "$SCRIPT_DIR/assets/img/"*; do
FILES_TO_COPY+=("$img:$TARGET_DIR/assets/img/$(basename "$img")")
done
# Include all files from slicer.org source directory
echo "Using source directory: $SLICER_ORG_SOURCE_DIR"
while IFS= read -r -d '' file; do
relative_path="${file#$SLICER_ORG_SOURCE_DIR/}" # Compute correct relative path
dest="$TARGET_DIR/$relative_path" # Use TARGET_DIR for destination
mkdir -p "$(dirname "$dest")" # Ensure target directories exist
FILES_TO_COPY+=("$file:$dest")
done < <(find "$SLICER_ORG_SOURCE_DIR" -type f ! -path "$SLICER_ORG_SOURCE_DIR/.git/*" \
! -path "$SLICER_ORG_SOURCE_DIR/.jekyll-cache/*" \
! -path "$SLICER_ORG_SOURCE_DIR/.sass-cache/*" \
! -path "$SLICER_ORG_SOURCE_DIR/_site/*" \
! -name "index.markdown" \
! -name ".jekyll-metadata" -print0)
# Export array as a string
export FILES_TO_COPY_STRING="$(IFS=";"; echo "${FILES_TO_COPY[*]}")"
# Function to copy files only if modified
copy_files() {
IFS=";" read -ra FILES <<< "$FILES_TO_COPY_STRING"
for file_pair in "${FILES[@]}"; do
src="${file_pair%%:*}"
dest="${file_pair##*:}"
if [[ -f "$dest" && -e "$src" ]]; then
if cmp -s "$src" "$dest"; then
continue
fi
fi
echo "Copying: $src -> $dest"
cp "$src" "$dest"
done
}
copy_files
if $INSTALL_RUBY_DEPS; then
(cd $TARGET_DIR && bundle install)
fi
if $SERVE; then
# Export function
export -f copy_files
# If serving mode is enabled, check if `entr` is available
if command -v entr &> /dev/null; then
echo "Using entr to watch for file changes while serving..."
(printf "%s\n" "${FILES_TO_COPY[@]%%:*}" | entr bash -c "copy_files") & # Start entr in the background
fi
PORT=4000 # Also hard-coded in "$TARGET_DIR/_config_dev.yml"
# Serve the site if --serve is passed
echo "Starting Jekyll server on port $PORT..."
(cd "$TARGET_DIR" && \
bundle exec jekyll serve -d "$SCRIPT_DIR/_site" \
--config "$CONFIG_OPTS,$TARGET_DIR/_config_dev.yml,$EXTRA_CONFIG_OPTS" \
--watch --force_polling \
-H 0.0.0.0 -P "$PORT" \
--incremental)
elif $BUILD; then
# Build the site by default
echo "Building the site..."
(cd "$TARGET_DIR" && \
bundle exec jekyll build -d "$SCRIPT_DIR/_site" \
--config "$CONFIG_OPTS,$EXTRA_CONFIG_OPTS")
fi