Skip to content

Instantly share code, notes, and snippets.

@mschabhuettl
Last active April 2, 2024 03:11
Show Gist options
  • Save mschabhuettl/74092439f72035e9b44e5cb5d022edae to your computer and use it in GitHub Desktop.
Save mschabhuettl/74092439f72035e9b44e5cb5d022edae to your computer and use it in GitHub Desktop.
scripts/download_third_party_libraries.sh
#!/bin/bash
# Define the base directory for downloads relative to the script's location.
DOWNLOAD_DIR="../assets/thirdparty"
# Ensure DOWNLOAD_DIR is correctly set and not empty or root.
if [ -z "$DOWNLOAD_DIR" ] || [ "$DOWNLOAD_DIR" = "/" ]; then
echo "Error: DOWNLOAD_DIR is not set correctly."
exit 1
fi
# Associative arrays for storing libraries' information, versions, and additional resources.
declare -A libraries=(
["bootstrap-table:css"]="https://cdn.jsdelivr.net/npm/bootstrap-table@{version}/dist/bootstrap-table.min.css"
["chartjs:js"]="https://cdn.jsdelivr.net/npm/chart.js@{version}/dist/chart.min.js"
["d3:js"]="https://unpkg.com/d3@{version}/dist/d3.min.js"
["diff2html:css"]="https://cdn.jsdelivr.net/npm/diff2html@{version}/bundles/css/diff2html.min.css"
["echarts:js"]="https://cdn.jsdelivr.net/npm/echarts@{version}/dist/echarts.min.js"
["highlightjs:css"]="https://cdn.jsdelivr.net/npm/highlight.js@{version}/styles/default.min.css"
["imagesloaded:js"]="https://cdn.jsdelivr.net/npm/imagesloaded@{version}/imagesloaded.pkgd.min.js"
["img-comparison-slider:css"]="https://cdn.jsdelivr.net/npm/img-comparison-slider@{version}/dist/styles.min.css"
["jquery:js"]="https://cdn.jsdelivr.net/npm/jquery@{version}/dist/jquery.min.js"
["leaflet:css"]="https://cdn.jsdelivr.net/npm/leaflet@{version}/dist/leaflet.css"
["leaflet:js"]="https://cdn.jsdelivr.net/npm/leaflet@{version}/dist/leaflet.js"
["mathjax:js"]="https://cdn.jsdelivr.net/npm/mathjax@{version}/es5/tex-mml-chtml.js"
["masonry:js"]="https://cdn.jsdelivr.net/npm/masonry-layout@{version}/dist/masonry.pkgd.min.js"
["mdb:css"]="https://cdn.jsdelivr.net/npm/mdbootstrap@{version}/css/mdb.min.css"
["mdb:js"]="https://cdn.jsdelivr.net/npm/mdbootstrap@{version}/js/mdb.min.js"
["medium_zoom:js"]="https://cdn.jsdelivr.net/npm/medium-zoom@{version}/dist/medium-zoom.min.js"
["mermaid:js"]="https://cdn.jsdelivr.net/npm/mermaid@{version}/dist/mermaid.min.js"
["swiper:css"]="https://cdn.jsdelivr.net/npm/swiper@{version}/swiper-bundle.min.css"
["vega:js"]="https://cdn.jsdelivr.net/npm/vega@{version}/build/vega.min.js"
["vega-embed:js"]="https://cdn.jsdelivr.net/npm/vega-embed@{version}/build/vega-embed.min.js"
["vega-lite:js"]="https://cdn.jsdelivr.net/npm/vega-lite@{version}/build/vega-lite.min.js"
["highlightjs:css_github"]="https://cdn.jsdelivr.net/npm/highlight.js@{version}/styles/github.min.css"
["highlightjs:css_github_dark"]="https://cdn.jsdelivr.net/npm/highlight.js@{version}/styles/github-dark.min.css"
["diff2html:js"]="https://cdn.jsdelivr.net/npm/diff2html@{version}/bundles/js/diff2html-ui.min.js"
["echarts:js_theme_dark_fresh_cut"]="https://cdn.jsdelivr.net/npm/echarts@{version}/theme/dark-fresh-cut.js"
["img-comparison-slider:js"]="https://cdn.jsdelivr.net/npm/img-comparison-slider@{version}/dist/index.min.js"
["swiper:js"]="https://cdn.jsdelivr.net/npm/swiper@{version}/swiper-element-bundle.min.js"
["chartjs:js_umd"]="https://cdn.jsdelivr.net/npm/chart.js@{version}/dist/chart.umd.min.js"
)
declare -A versions=(
["bootstrap-table"]="1.22.1"
["chartjs"]="4.4.1"
["d3"]="7.8.5"
["diff2html"]="3.4.47"
["echarts"]="5.4.3"
["highlightjs"]="11.9.0"
["imagesloaded"]="5.0.0"
["img-comparison-slider"]="8.0.6"
["jquery"]="3.6.0"
["leaflet"]="1.9.4"
["mathjax"]="3.2.0"
["masonry"]="4.2.2"
["mdb"]="4.20.0"
["medium_zoom"]="1.1.0"
["mermaid"]="10.7.0"
["swiper"]="11.0.5"
["vega"]="5.27.0"
["vega-embed"]="6.24.0"
["vega-lite"]="5.16.3"
)
# Function to download library files.
download_libraries() {
for key in "${!libraries[@]}"; do
local base_library_name=${key%%:*} # Extracts the base library name.
local suffix=${key##*:} # Extracts the suffix (css or js).
local version=${versions[$base_library_name]} # Gets the version.
local url_template=${libraries[$key]}
local url="${url_template/\{version\}/$version}" # Replaces {version} placeholder.
local filename=$(basename "$url")
echo "Downloading $base_library_name ($suffix) version $version..."
mkdir -p "$DOWNLOAD_DIR/$base_library_name"
if wget -q "$url" -O "$DOWNLOAD_DIR/$base_library_name/$filename"; then
echo "Successfully downloaded $filename"
else
echo "Failed to download $base_library_name ($suffix)"
fi
done
}
# Function to append .map to URLs if applicable and download the map files.
download_map_files() {
for key in "${!libraries[@]}"; do
local base_library_name=${key%%:*} # Extracts the base library name.
local version=${versions[$base_library_name]} # Gets the version.
local url_template=${libraries[$key]}
local url="${url_template/\{version\}/$version}" # Replaces the {version} placeholder.
local library_dir="$DOWNLOAD_DIR/$base_library_name"
if [[ $url =~ \.js$ ]]; then
local js_map_url="${url}.map" # Assumes .js.map for simplicity
echo "Attempting to download JS map file for $key from $js_map_url..."
wget -q "$js_map_url" -P "$library_dir" && echo "Successfully downloaded $(basename "$js_map_url")"
elif [[ $url =~ \.css$ ]]; then
local css_map_url="${url}.map" # Assumes .css.map for CSS files
echo "Attempting to download CSS map file for $key from $css_map_url..."
wget -q "$css_map_url" -P "$library_dir" && echo "Successfully downloaded $(basename "$css_map_url")"
fi
done
}
# Function to download MathJax fonts.
download_mathjax_fonts() {
local base_url="https://cdn.jsdelivr.net/npm/mathjax@${versions[mathjax]}/es5/output/chtml/fonts/woff-v2/"
local font_dir="$DOWNLOAD_DIR/mathjax/output/chtml/fonts/woff-v2"
mkdir -p "$font_dir"
# Declare an array of MathJax font files to download.
declare -a fonts=(
"MathJax_AMS-Regular.woff"
"MathJax_Calligraphic-Bold.woff"
"MathJax_Calligraphic-Regular.woff"
"MathJax_Fraktur-Bold.woff"
"MathJax_Fraktur-Regular.woff"
"MathJax_Main-Bold.woff"
"MathJax_Main-Italic.woff"
"MathJax_Main-Regular.woff"
"MathJax_Math-BoldItalic.woff"
"MathJax_Math-Italic.woff"
"MathJax_Math-Regular.woff"
"MathJax_SansSerif-Bold.woff"
"MathJax_SansSerif-Italic.woff"
"MathJax_SansSerif-Regular.woff"
"MathJax_Script-Regular.woff"
"MathJax_Size1-Regular.woff"
"MathJax_Size2-Regular.woff"
"MathJax_Size3-Regular.woff"
"MathJax_Size4-Regular.woff"
"MathJax_Typewriter-Regular.woff"
"MathJax_Vector-Bold.woff"
"MathJax_Vector-Regular.woff"
"MathJax_Zero.woff"
)
for font in "${fonts[@]}"; do
echo "Downloading $font..."
if wget -q "${base_url}${font}" -O "${font_dir}/${font}"; then
echo "Successfully downloaded $font"
else
echo "Failed to download $font"
fi
done
}
# Function to download additional JS scripts with organized structure.
download_additional_scripts() {
echo "Downloading additional JS scripts..."
local polyfill_dir="$DOWNLOAD_DIR/js/polyfills"
mkdir -p "$polyfill_dir"
local polyfill_url="https://polyfill.io/v3/polyfill.min.js?features=es6"
echo "Downloading polyfill.min.js with ES6 features..."
if wget -q "$polyfill_url" -O "$polyfill_dir/polyfill.min.js"; then
echo "Successfully downloaded polyfill.min.js"
else
echo "Failed to download polyfill.min.js"
fi
}
# Main function to set up the environment and start the download process.
main() {
echo "Cleaning up the existing 'thirdparty' directory except for 'css' and 'fonts' subdirectories..."
find "$DOWNLOAD_DIR" -mindepth 1 -not -path "$DOWNLOAD_DIR/css/*" -not -path "$DOWNLOAD_DIR/css" -not -path "$DOWNLOAD_DIR/fonts/*" -not -path "$DOWNLOAD_DIR/fonts" -exec rm -rf {} +
echo "Setting up download directory at $DOWNLOAD_DIR..."
mkdir -p "$DOWNLOAD_DIR"
download_libraries
download_map_files
download_mathjax_fonts
download_additional_scripts
echo "All downloads completed."
}
# Execute the script.
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment