Clean up the discarded shell scripts

This commit is contained in:
Cotes Chung 2020-11-19 20:39:02 +08:00
parent 10bc44367a
commit 8fa1f3b497
10 changed files with 40 additions and 676 deletions

View file

@ -29,20 +29,6 @@ jobs:
with:
ruby-version: '2.6.x'
- name: Install tools (for Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CC86BB64
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq -y
- name: Install tools (for macOS)
if: matrix.os == 'macos-latest'
run: |
brew install coreutils
brew install yq
- name: Checkout
uses: actions/checkout@v2
with:
@ -73,7 +59,7 @@ jobs:
- name: Build Site
run: |
bash tools/build.sh
JEKYLL_ENV=production bundle exec jekyll b
- name: Test Site
run: |

View file

@ -21,13 +21,6 @@ jobs:
with:
ruby-version: 2.6.x
- name: Install tools
run: |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CC86BB64
sudo add-apt-repository ppa:rmescandon/yq
sudo apt update
sudo apt install yq -y
- name: Checkout
uses: actions/checkout@v2
with:
@ -58,19 +51,19 @@ jobs:
- name: Check baseurl
run: |
baseurl="$(grep '^baseurl:' _config.yml | yq r - baseurl)"
baseurl="$(grep '^baseurl:' _config.yml | sed "s/.*: *//;s/['\"]//g;s/#.*//")"
if [[ -n $baseurl ]]; then
echo "SPEC_TEST=_site_no_baseurl" >> $GITHUB_ENV
fi
- name: Build Site
run: |
bash tools/build.sh
JEKYLL_ENV=production bundle exec jekyll b
if [[ -n $SPEC_TEST ]]; then
# Bypass the defects of htmlproofer
bash tools/build.sh -b "" -d "$SPEC_TEST"
fi
JEKYLL_ENV=production bundle exec jekyll b -b "" -d "$SPEC_TEST"
fi
- name: Test Site
run: |

View file

@ -12,11 +12,8 @@ install:
addons:
apt:
sources:
- sourceline: "ppa:rmescandon/yq"
packages:
- libcurl4-openssl-dev # required to avoid SSL error (for htmlproofer)
- yq
script:
- git -C "$HOME" clone "$BUILDER_REPO" --depth=1 -q

View file

@ -1,159 +0,0 @@
#!/usr/bin/env bash
#
# Create HTML pages for Categories and Tags in posts.
#
# Usage:
# Call from the '_posts' sibling directory.
#
# v2.2
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2020 Cotes Chung
# Published under MIT License
set -eu
TYPE_CATEGORY=0
TYPE_TAG=1
category_count=0
tag_count=0
_read_yaml() {
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
head -"$_endline" "$1"
}
read_categories() {
local _yaml="$(_read_yaml "$1")"
local _categories="$(echo "$_yaml" | yq r - "categories.*")"
local _category="$(echo "$_yaml" | yq r - "category")"
if [[ -n $_categories ]]; then
echo "$_categories"
elif [[ -n $_category ]]; then
echo "$_category"
fi
}
read_tags() {
local _yaml="$(_read_yaml "$1")"
local _tags="$(echo "$_yaml" | yq r - "tags.*")"
local _tag="$(echo "$_yaml" | yq r - "tag")"
if [[ -n $_tags ]]; then
echo "$_tags"
elif [[ -n $_tag ]]; then
echo "$_tag"
fi
}
init() {
if [[ -d categories ]]; then
rm -rf categories
fi
if [[ -d tags ]]; then
rm -rf tags
fi
if [[ ! -d _posts ]]; then
exit 0
fi
mkdir categories tags
}
create_category() {
if [[ -n $1 ]]; then
local _name=$1
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
if [[ ! -f $_filepath ]]; then
echo "---" > "$_filepath"
echo "layout: category" >> "$_filepath"
echo "title: $_name" >> "$_filepath"
echo "category: $_name" >> "$_filepath"
echo "---" >> "$_filepath"
((category_count = category_count + 1))
fi
fi
}
create_tag() {
if [[ -n $1 ]]; then
local _name=$1
local _filepath="tags/$(echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}').html"
if [[ ! -f $_filepath ]]; then
echo "---" > "$_filepath"
echo "layout: tag" >> "$_filepath"
echo "title: $_name" >> "$_filepath"
echo "tag: $_name" >> "$_filepath"
echo "---" >> "$_filepath"
((tag_count = tag_count + 1))
fi
fi
}
#########################################
# Create HTML pages for Categories/Tags.
# Arguments:
# $1 - an array string
# $2 - type specified option
#########################################
create_pages() {
if [[ -n $1 ]]; then
# split string to array
IFS_BAK=$IFS
IFS=$'\n'
local _string=$1
case $2 in
$TYPE_CATEGORY)
for i in $_string; do
create_category "$i"
done
;;
$TYPE_TAG)
for i in $_string; do
create_tag "$i"
done
;;
*) ;;
esac
IFS=$IFS_BAK
fi
}
main() {
init
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)); do
local _categories=$(read_categories "$_file")
local _tags=$(read_tags "$_file")
create_pages "$_categories" $TYPE_CATEGORY
create_pages "$_tags" $TYPE_TAG
done
if [[ $category_count -gt 0 ]]; then
echo "[INFO] Succeed! $category_count category-pages created."
fi
if [[ $tag_count -gt 0 ]]; then
echo "[INFO] Succeed! $tag_count tag-pages created."
fi
}
main

View file

@ -1,88 +0,0 @@
#!/bin/bash
#
# Find out the posts that have been modified and record
# its lastmod information to file '_data/updates.yml'
#
# Usage:
# Call from the '_posts' sibling directory.
#
# v2.2
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2020 Cotes Chung
# Published under MIT License
set -eu
POST_DIR=_posts
OUTPUT_DIR=_data
OUTPUT_FILE=updates.yml
_init() {
if [[ ! -d "$OUTPUT_DIR" ]]; then
mkdir "$OUTPUT_DIR"
fi
if [[ -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then
rm -f "$OUTPUT_DIR/$OUTPUT_FILE"
fi
if [[ ! -d $POST_DIR ]]; then
exit 0
fi
}
_has_changed() {
local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
_log_count=$((_log_count + 0))
if [[ $_log_count > 1 ]]; then
return 0 # true
fi
return 1 # false
}
###################################
# Storage the posts' lastmod.
#
# Args:
# - $1 the post's filename
# - $2 the post's filepath
# Output:
# the file '_data/updates.yml'
###################################
_dump() {
local _lasmod="$(git log -1 --pretty=%ad --date=iso "$2")"
if [[ ! -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then
touch "$OUTPUT_DIR/$OUTPUT_FILE"
fi
echo "-" >> "$OUTPUT_DIR/$OUTPUT_FILE"
echo " filename: '$1'" >> "$OUTPUT_DIR/$OUTPUT_FILE"
echo " lastmod: '$_lasmod'" >> "$OUTPUT_DIR/$OUTPUT_FILE"
}
main() {
_init
local _count=0
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)); do
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//')" # remove date and extension
if _has_changed "$_file"; then
_dump "$_filename" "$_file"
((_count = _count + 1))
fi
done
if [[ $_count > 0 ]]; then
echo "[INFO] Success to update lastmod for $_count post(s)."
fi
}
main

View file

@ -1,37 +0,0 @@
#!/bin/bash
# Files sync monitor
# v2.0
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2019 Cotes Chung
# MIT Licensed
# $1 -> the origin file with absolute path.
# $2 -> the origin sync directory
# $3 -> the destination sync directory
# Omit the system temp file
if [[ ! -f $1 ]]; then
exit 0
fi
src_dir="$(dirname "$(realpath "$1")")"
dir_prefix="$(realpath "$2")/"
related_dir="${src_dir:${#dir_prefix}}"
dest="$(realpath "$3")/${related_dir}"
if [[ ! -d $dest ]]; then
mkdir -p "$dest"
fi
if [[ -f $1 ]]; then
cp "$1" "$dest"
fi
if [[ $related_dir == "_posts" ]]; then
bash "$3"/_scripts/sh/create_pages.sh
bash "$3"/_scripts/sh/dump_lastmod.sh
fi

View file

@ -40,34 +40,6 @@ const include = [
'{{ item | relative_url }}',
{% endfor %}
/* The posts of first Home page and recent update list */
{% assign post_list = "" | split: "" %}
{% for post in site.posts limit: site.paginate %}
{% capture post_url %}{{ post.url | relative_url }}{% endcapture %}
{% assign post_list = post_list | push: post_url %}
{% endfor %}
{% include update-list.html %}
{% for item in update_list %}
{% assign url = item | split: "::" | last | url_encode | prepend: "/posts/" | append: "/" | relative_url %}
{% assign post_list = post_list | push: url %}
{% endfor %}
{% assign post_list = post_list | uniq %}
{% for url in post_list %}
'{{ url }}',
{% endfor %}
/* Trending tags */
{% include trending-tags.html %}
{% for tag in trending_tags %}
{% capture tag_url %}/tags/{{ tag | downcase | url_encode }}/{% endcapture %}
'{{ tag_url | relative_url }}',
{% endfor %}
/*--- Icons ---*/
{%- capture icon_url -%}

View file

@ -1,147 +0,0 @@
#!/bin/bash
#
# Build jekyll site and store site files in ./_site
# v2.0
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2019 Cotes Chung
# Published under MIT License
set -eu
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
CONTAINER="${WORK_DIR}/.container"
dest="${WORK_DIR}/_site"
cmd="JEKYLL_ENV=production bundle exec jekyll b"
docker=false
config=""
_help() {
echo "Usage:"
echo
echo " bash build.sh [options]"
echo
echo "Options:"
echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'"
echo " -h, --help Print the help information"
echo " -d, --destination <DIR> destination directory (defaults to ./_site)"
echo " --docker Build site within docker"
echo " --config <CONFIG_a[,CONFIG_b]> Specify config files"
}
_install_tools() {
# docker image `jekyll/jekyll` based on Alpine Linux
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
apk update
apk add yq
}
_init() {
cd "$WORK_DIR"
if [[ -f Gemfile.lock ]]; then
rm -f Gemfile.lock
fi
if [[ -d $CONTAINER ]]; then
rm -rf "$CONTAINER"
fi
if [[ -d $dest ]]; then
bundle exec jekyll clean
fi
local _temp="$(mktemp -d)"
cp -r ./* "$_temp"
cp -r ./.git "$_temp"
mv "$_temp" "$CONTAINER"
}
_build() {
cd "$CONTAINER"
echo "$ cd $(pwd)"
bash "_scripts/sh/create_pages.sh"
bash "_scripts/sh/dump_lastmod.sh"
cmd+=" -d $dest"
if [[ -n $config ]]; then
cmd+=" --config $config"
fi
echo "\$ $cmd"
eval "$cmd"
echo -e "\nBuild success, the site files have been placed in '${dest}'."
if [[ -d "${dest}/.git" ]]; then
if [[ -n $(git -C "$dest" status -s) ]]; then
git -C "$dest" add .
git -C "$dest" commit -m "[Automation] Update site files." -q
echo -e "\nPlease push the changes of $dest to remote master branch.\n"
fi
fi
cd .. && rm -rf "$CONTAINER"
}
_check_unset() {
if [[ -z ${1:+unset} ]]; then
_help
exit 1
fi
}
main() {
while [[ $# -gt 0 ]]; do
opt="$1"
case $opt in
-b | --baseurl)
local _baseurl="$2"
if [[ -z $_baseurl ]]; then
_baseurl='""'
fi
cmd+=" -b $_baseurl"
shift
shift
;;
-d | --destination)
_check_unset "$2"
dest="$(realpath "$2")"
shift
shift
;;
--docker)
docker=true
shift
;;
--config)
_check_unset "$2"
config="$(realpath "$2")"
shift
shift
;;
-h | --help)
_help
exit 0
;;
*) # unknown option
_help
exit 1
;;
esac
done
if $docker; then
_install_tools
fi
_init
_build
}
main "$@"

View file

@ -1,188 +0,0 @@
#!/bin/bash
# Run jekyll site at http://127.0.0.1:4000
#
# Requirement:
# Option '-r, --realtime' needs fswatch http://emcrisostomo.github.io/fswatch/
#
# v2.0
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2019 Cotes Chung
# Published under MIT License
set -eu
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
CONTAINER="${WORK_DIR}/.container"
SYNC_TOOL=_scripts/sh/sync_monitor.sh
cmd="bundle exec jekyll s"
JEKYLL_DOCKER_HOME="/srv/jekyll"
realtime=false
docker=false
_help() {
echo "Usage:"
echo
echo " bash run.sh [options]"
echo
echo "Options:"
echo " -H, --host <HOST> Host to bind to"
echo " -P, --port <PORT> Port to listen on"
echo " -b, --baseurl <URL> The site relative url that start with slash, e.g. '/project'"
echo " -h, --help Print the help information"
echo " -t, --trace Show the full backtrace when an error occurs"
echo " -r, --realtime Make the modified content updated in real time"
echo " --docker Run within docker"
}
_cleanup() {
rm -rf "$CONTAINER"
ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
}
_setup_docker() {
# docker image `jekyll/jekyll` based on Alpine Linux
echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
## CN Apline sources mirror
# sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
apk update
apk add yq
}
_init() {
cd "$WORK_DIR"
if [[ -f Gemfile.lock ]]; then
rm -f Gemfile.lock
fi
if [[ -d $CONTAINER ]]; then
rm -rf "$CONTAINER"
fi
mkdir "$CONTAINER"
cp -r ./* "$CONTAINER"
cp -r ./.git "$CONTAINER"
if $docker; then
local _image_user=$(stat -c "%U" "$JEKYLL_DOCKER_HOME"/.)
if [[ $_image_user != $(whoami) ]]; then
# under Docker for Linux
chown -R "$(stat -c "%U:%G" "$JEKYLL_DOCKER_HOME"/.)" "$CONTAINER"
fi
fi
trap _cleanup INT
}
_check_unset() {
if [[ -z ${1:+unset} ]]; then
_help
exit 1
fi
}
_check_command() {
if [[ -z $(command -v "$1") ]]; then
echo "Error: command '$1' not found !"
echo "Hint: Get '$1' on <$2>"
exit 1
fi
}
_run() {
cd "$CONTAINER"
bash _scripts/sh/create_pages.sh
bash _scripts/sh/dump_lastmod.sh
if $realtime; then
exclude_regex="\/\..*"
if [[ $OSTYPE == "darwin"* ]]; then
exclude_regex="/\..*" # darwin gcc treat regex '/' as character '/'
fi
fswatch -e "$exclude_regex" -0 -r \
--event Created --event Removed \
--event Updated --event Renamed \
--event MovedFrom --event MovedTo \
"$WORK_DIR" | xargs -0 -I {} bash "./${SYNC_TOOL}" {} "$WORK_DIR" . &
fi
if $docker; then
cmd+=" -H 0.0.0.0"
else
cmd+=" -l -o"
fi
echo "\$ $cmd"
eval "$cmd"
}
main() {
if $docker; then
_setup_docker
fi
_init
_run
}
while (($#)); do
opt="$1"
case $opt in
-H | --host)
_check_unset "$2"
cmd+=" -H $2"
shift # past argument
shift # past value
;;
-P | --port)
_check_unset "$2"
cmd+=" -P $2"
shift
shift
;;
-b | --baseurl)
_check_unset "$2"
if [[ $2 == \/* ]]; then
cmd+=" -b $2"
else
_help
exit 1
fi
shift
shift
;;
-t | --trace)
cmd+=" -t"
shift
;;
-r | --realtime)
_check_command fswatch "http://emcrisostomo.github.io/fswatch/"
realtime=true
shift
;;
--docker)
docker=true
shift
;;
-h | --help)
_help
exit 0
;;
*)
# unknown option
_help
exit 1
;;
esac
done
main

View file

@ -14,10 +14,45 @@
DEST=_site
URL_IGNORE=cdn.jsdelivr.net
_build=false
help() {
echo "Usage:"
echo
echo " bash ./tools/test.sh [options]"
echo
echo "Options:"
echo " --build Run jekyll build before testing."
echo " -h, --help Print this information."
}
if [[ -n $1 && -d $1 ]]; then
DEST=$1
fi
while (($#)); do
opt="$1"
case $opt in
--build)
_build=true
shift
;;
-h | --help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
esac
done
if $_build; then
JEKYLL_ENV=production bundle exec jekyll b
fi
bundle exec htmlproofer $DEST \
--disable-external \
--check-html \