2015-09-19 09:23:53 +00:00
|
|
|
#!/usr/bin/env bash
|
2018-12-12 23:23:50 +00:00
|
|
|
# Make sure the current directory is the location of this script to simplify matters
|
|
|
|
cd "$(dirname $(readlink -f $0))";
|
|
|
|
################
|
|
|
|
### Settings ###
|
|
|
|
################
|
|
|
|
|
|
|
|
# The name of this project
|
|
|
|
project_name="Pepperminty Wiki";
|
|
|
|
|
|
|
|
# The path to the lantern build engine git submodule
|
|
|
|
lantern_path="./lantern-build-engine";
|
|
|
|
|
|
|
|
###
|
|
|
|
# Custom Settings
|
|
|
|
###
|
|
|
|
|
|
|
|
# Put any custom settings here.
|
|
|
|
|
|
|
|
# The file to store the development server's PID in.
|
|
|
|
server_pid_file="/tmp/pepperminty-wiki-dev-server.pid";
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Check out the lantern git submodule if needed
|
2019-12-24 17:34:19 +00:00
|
|
|
if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi
|
2018-12-12 23:23:50 +00:00
|
|
|
|
|
|
|
source "${lantern_path}/lantern.sh";
|
|
|
|
|
|
|
|
if [[ "$#" -lt 1 ]]; then
|
|
|
|
echo -e "${FBLE}${project_name}${RS} build script";
|
|
|
|
echo -e " by Starbeamrainbowlabs";
|
|
|
|
echo -e "${LC}Powered by the lantern build engine, v${version}${RS}";
|
|
|
|
echo -e "";
|
|
|
|
echo -e "${CSECTION}Usage${RS}";
|
|
|
|
echo -e " ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ...";
|
|
|
|
echo -e "";
|
|
|
|
echo -e "${CSECTION}Available actions${RS}";
|
|
|
|
echo -e " ${CACTION}setup${RS} - Perform initial setup, check the environment (skip if only building Pepperminty Wiki itself)";
|
|
|
|
echo -e " ${CACTION}build${RS} - Build Pepperminty Wiki";
|
2019-09-29 14:42:22 +00:00
|
|
|
echo -e " ${CACTION}themes${RS} - Rebuild the theme index";
|
2018-12-12 23:23:50 +00:00
|
|
|
echo -e " ${CACTION}docs${RS} - Build the documentation";
|
|
|
|
echo -e " ${CACTION}docs-livereload${RS} - Start the documentation livereload server";
|
|
|
|
echo -e " ${CACTION}start-server${RS} - Start a development server";
|
|
|
|
echo -e " ${CACTION}stop-server${RS} - Stop the development server";
|
2020-07-10 19:16:17 +00:00
|
|
|
echo -e " ${CACTION}sign${RS} - Sign the current build with SHA256 & GPG";
|
2020-07-07 20:42:53 +00:00
|
|
|
echo -e " ${CACTION}clean${RS} - Delete all build outputs (WARNING: THIS WILL DELETE ALL WIKI DATA)";
|
2018-12-12 23:23:50 +00:00
|
|
|
echo -e "";
|
|
|
|
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
2019-12-24 17:32:45 +00:00
|
|
|
# Tests to see if a PHP module is installed.
|
|
|
|
# $1 - The name of the module to check for
|
|
|
|
# $2 - The mode of operation. Values: optional, required
|
|
|
|
# $3 - The error message to show
|
|
|
|
test_php_module() {
|
|
|
|
module_name="${1}";
|
|
|
|
mode="${2}";
|
|
|
|
reason="${3}";
|
|
|
|
|
|
|
|
subtask_begin "Checking for ${module_name} PHP module";
|
|
|
|
php -m | grep -q "${module_name}";
|
|
|
|
exit_code="${?}";
|
|
|
|
if [[ "${mode}" = "optional" ]] && [[ "${exit_code}" -ne 0 ]]; then
|
|
|
|
echo "${FYEL}${HC}Warning: The PHP module ${module} was not found. It is needed to ${reason}.${RS}";
|
|
|
|
fi
|
|
|
|
subtask_end "${exit_code}";
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:23:50 +00:00
|
|
|
###############################################################################
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_setup() {
|
2018-12-12 23:23:50 +00:00
|
|
|
task_begin "Checking Environment";
|
|
|
|
|
|
|
|
check_command git true;
|
|
|
|
check_command npm true;
|
2019-12-24 17:32:45 +00:00
|
|
|
check_command php true;
|
|
|
|
test_php_module "mbstring" "required" "handle utf-8 characters correctly";
|
|
|
|
test_php_module "imagick" "optional" "generate image previews";
|
|
|
|
test_php_module "fileinfo" "optional" "properly check the mime type of uploaded files";
|
|
|
|
test_php_module "zip" "optional" "compressing exports";
|
|
|
|
test_php_module "intl" "required" "transliteration in the search engine and when sending emails when utf-8 is disabled";
|
|
|
|
test_php_module "sqlite" "optional" "store the inverted search index";
|
2019-08-25 17:12:08 +00:00
|
|
|
check_command jq true optional;
|
|
|
|
[[ "$?" -eq 0 ]] || echo -e "${FYEL}${HC}Warning: jq is required to update the theme index.${RS}";
|
|
|
|
check_command firefox true optional;
|
2019-08-25 20:38:28 +00:00
|
|
|
[[ "$?" -eq 0 ]] || echo -e "${FYEL}${HC}Warning: firefox is required to generate theme previews.${RS}";
|
|
|
|
check_command convert true optional;
|
|
|
|
[[ "$?" -eq 0 ]] || echo -e "${FYEL}${HC}Warning: The convert imagemagick command is required to generate theme previews.${RS}";
|
|
|
|
check_command nproc true optional;
|
|
|
|
[[ "$?" -eq 0 ]] || echo -e "${FYEL}${HC}Warning: nproc is required to generate theme previews.${RS}";
|
2018-12-12 23:23:50 +00:00
|
|
|
|
|
|
|
task_end $?;
|
|
|
|
|
|
|
|
task_begin "Initialising submodules";
|
|
|
|
git submodule update --init;
|
|
|
|
task_end $?;
|
|
|
|
|
|
|
|
task_begin "Installing packages";
|
|
|
|
npm install;
|
|
|
|
task_end $?;
|
|
|
|
|
|
|
|
task_begin "Creating build folders";
|
|
|
|
mkdir -p build/_tmp;
|
|
|
|
echo "This folder contains build tools automatically downloaded." >build/_tmp/README.txt;
|
|
|
|
task_end $?;
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_build() {
|
2018-12-12 23:23:50 +00:00
|
|
|
if [ -f "./build/index.php" ]; then
|
|
|
|
task_begin "Deleting old build result";
|
|
|
|
rm build/index.php;
|
|
|
|
task_end "$?";
|
|
|
|
fi
|
|
|
|
|
|
|
|
task_begin "Building";
|
|
|
|
php build.php
|
|
|
|
task_end $?;
|
|
|
|
}
|
|
|
|
|
2019-08-25 17:12:08 +00:00
|
|
|
task_themes() {
|
|
|
|
if [[ ! -f "${server_pid_file}" ]]; then
|
2019-08-25 20:38:28 +00:00
|
|
|
NO_BROWSER=true tasks_run start-server;
|
2019-08-25 17:12:08 +00:00
|
|
|
fi
|
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
stage_begin "Updating theme index";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
|
|
|
|
task_begin "Preparing";
|
|
|
|
|
|
|
|
[ -f "themes/themeindex.json" ] && cp "themes/themeindex.json" "themes/themeindex.json.old";
|
|
|
|
|
|
|
|
# Temporary firefox profile directory
|
2019-08-25 17:12:08 +00:00
|
|
|
tmp_profile="$(mktemp -d /tmp/peppermint-firefox-profile-XXXXXXX)";
|
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
# Temporary file for theme index items
|
|
|
|
tmp_themeindex_parts="$(mktemp /tmp/peppermint-themeindex-items-XXXXXXX)";
|
|
|
|
|
|
|
|
task_end $?;
|
|
|
|
|
|
|
|
preview_regen=false;
|
|
|
|
|
|
|
|
while read -r filename; do
|
|
|
|
task_begin "Processing ${filename}";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
|
|
|
hash="$(sha256sum "${filename}" | cut -d' ' -f1)";
|
|
|
|
read -r -d "" awk_script <<'AWK'
|
|
|
|
BEGIN {
|
|
|
|
items[0] = "\"hash\": \"" prop_hash "\"";
|
|
|
|
count=1;
|
|
|
|
}
|
|
|
|
/\s+\*\s+@/ {
|
|
|
|
atrule=$2;
|
|
|
|
gsub(/@/, "", atrule);
|
|
|
|
gsub(/\s*\*\s*@[a-z\_]+\s+/, "", $0);
|
|
|
|
items[count] = "\"" atrule "\": \"" $0 "\"";
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
END {
|
|
|
|
result="{";
|
|
|
|
for(i = 0; i < count; i++) {
|
|
|
|
result = result items[i];
|
|
|
|
if(i < count - 1) result = result ",";
|
|
|
|
}
|
|
|
|
print(result "}");
|
|
|
|
}
|
|
|
|
AWK
|
|
|
|
# TODO: Consider mapping it out as TSV, then using JQ to generate the object
|
2019-08-25 20:38:28 +00:00
|
|
|
subtask_begin "Generating index entry";
|
|
|
|
awk -v prop_hash="${hash}" "${awk_script}" <"${filename}" >>"${tmp_themeindex_parts}";
|
|
|
|
subtask_end "$?";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Capture the screenshot
|
2019-08-25 20:38:28 +00:00
|
|
|
if [ -f "themes/themeindex.json.old" ]; then
|
|
|
|
theme_id="$(awk '/@id/ { print $3 }' <"${filename}")";
|
|
|
|
old_hash="$(jq --raw-output --arg theme_id "${theme_id}" '.[] | select(.id == $theme_id).hash' <"themes/themeindex.json")";
|
|
|
|
|
|
|
|
# If the hash is the same as last time, don't bother to retake the screenshot
|
|
|
|
if [[ "${hash}" = "${hash_old}" ]]; then
|
|
|
|
continue;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
preview_regen=true;
|
|
|
|
|
2019-08-26 12:16:13 +00:00
|
|
|
screenshot_loc_full="$(dirname "${filename}")/preview_large.png";
|
2019-08-25 17:12:08 +00:00
|
|
|
screenshot_loc_small="$(dirname "${filename}")/preview_small.png";
|
|
|
|
|
|
|
|
# Set the theme
|
2019-08-25 20:38:28 +00:00
|
|
|
cp "build/peppermint.json" "build/peppermint.json.bak";
|
2019-08-25 17:12:08 +00:00
|
|
|
tmp_file="$(mktemp /tmp/peppermint-json-XXXXXXX)";
|
|
|
|
jq --arg theme_css "$(cat "${filename}")" '.css = $theme_css' <"build/peppermint.json" >"${tmp_file}";
|
|
|
|
mv "${tmp_file}" "build/peppermint.json";
|
|
|
|
|
|
|
|
# Capture the full-res screenshot
|
2019-08-25 20:38:28 +00:00
|
|
|
execute firefox --new-instance --headless --profile "${tmp_profile}" --window-size 1920,1080 --screenshot "${screenshot_loc_full}" "http://[::1]:35623/index.php";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
# Resize to get the smaller preview
|
|
|
|
execute convert "${screenshot_loc_full}" -resize 512x512 "${screenshot_loc_small}";
|
|
|
|
|
|
|
|
mv "build/peppermint.json.bak" "build/peppermint.json";
|
|
|
|
|
|
|
|
task_end "$?";
|
|
|
|
done < <(find themes -type f -name "theme.css");
|
2019-08-25 17:12:08 +00:00
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
task_begin "Optimising new previews";
|
|
|
|
find "themes/" -iname "*.png" -print0 | xargs -0 -P"$(nproc)" -n1 optipng -preserve;
|
|
|
|
task_end "$?";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
task_begin "Generating theme index";
|
|
|
|
jq --tab --slurp . <"${tmp_themeindex_parts}" >"themes/themeindex.json"
|
|
|
|
task_end "$?";
|
|
|
|
|
|
|
|
# Clean up
|
|
|
|
task_begin "Cleaning up";
|
|
|
|
[[ -d "${tmp_profile}" ]] && rm -r "${tmp_profile}";
|
|
|
|
[[ -f "themes/themeindex.json.old" ]] && rm "themes/themeindex.json.old";
|
|
|
|
task_end 0;
|
|
|
|
|
|
|
|
stage_end 0;
|
2019-08-25 17:12:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_docs() {
|
2018-12-12 23:23:50 +00:00
|
|
|
task_begin "Building HTTP API Docs";
|
|
|
|
node_modules/apidoc/bin/apidoc -o './docs/RestApi/' --config apidoc.json -f '.*\.php' -e 'index.php|ModuleApi'
|
|
|
|
exit_code="$?";
|
|
|
|
rm -rf doc/;
|
|
|
|
task_end "${exit_code}";
|
|
|
|
|
|
|
|
task_begin "Building PHP Module API Docs";
|
|
|
|
if [ ! -f "./build/_tmp/phpdoc" ]; then
|
|
|
|
subtask_begin "Downloading PHPDoc";
|
2019-08-08 17:13:13 +00:00
|
|
|
# Create the temporary directory if it doesn't exist yet
|
|
|
|
[ -d "./build/_tmp" ] || mkdir -p "./build/_tmp/";
|
2019-08-25 17:12:08 +00:00
|
|
|
|
2018-12-12 23:23:50 +00:00
|
|
|
curl -sSL https://phpdoc.org/phpDocumentor.phar -o ./build/_tmp/phpdoc
|
|
|
|
subtask_end $?;
|
|
|
|
fi
|
|
|
|
|
2019-08-08 17:16:56 +00:00
|
|
|
php ./build/_tmp/phpdoc run \
|
2018-12-12 23:23:50 +00:00
|
|
|
--directory . \
|
|
|
|
--target docs/ModuleApi\
|
2018-12-12 23:31:24 +00:00
|
|
|
--cache-folder build/_tmp/ModuleApiCache \
|
2018-12-12 23:37:35 +00:00
|
|
|
--ignore build/,Parsedown*,*.html \
|
2018-12-12 23:23:50 +00:00
|
|
|
--title "Pepperminty Wiki Module API" \
|
|
|
|
--visibility public;
|
|
|
|
task_end $?;
|
|
|
|
|
|
|
|
task_begin "Building Main Documentation";
|
2019-05-12 11:05:44 +00:00
|
|
|
node_modules/.bin/nightdocs -c nightdocs.toml
|
2018-12-12 23:23:50 +00:00
|
|
|
task_end $?;
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_docs-livereload() {
|
2019-05-12 11:05:44 +00:00
|
|
|
task_begin "Listening for changes to docs";
|
|
|
|
while :; do
|
|
|
|
inotifywait -qr -e modify --format '%:e %f' ./docs/ nightdocs.toml;
|
|
|
|
node_modules/.bin/nightdocs -c nightdocs.toml;
|
|
|
|
done
|
2018-12-12 23:23:50 +00:00
|
|
|
task_end $?;
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_start-server() {
|
2019-09-29 14:42:22 +00:00
|
|
|
task_begin "Starting server";
|
2018-12-12 23:23:50 +00:00
|
|
|
if [ -f "${server_pid_file}" ]; then
|
2019-03-02 21:13:41 +00:00
|
|
|
task_end 1 "${FRED}${HC}Error: A development server appears to be running already. Try running the 'stop-server' task before starting it again.${RS}";
|
2018-12-12 23:23:50 +00:00
|
|
|
fi
|
|
|
|
php -S [::]:35623 -t build/ &
|
2019-09-29 14:42:22 +00:00
|
|
|
local exit_code=$?; local pid=$!;
|
2018-12-12 23:23:50 +00:00
|
|
|
echo "${pid}" >"${server_pid_file}";
|
2019-09-29 14:42:22 +00:00
|
|
|
|
2019-03-02 21:13:41 +00:00
|
|
|
task_end "${exit_code}" "";
|
2018-12-12 23:23:50 +00:00
|
|
|
|
2019-09-29 14:42:22 +00:00
|
|
|
task_begin "Starting theme server";
|
|
|
|
php -S [::]:35624 -t themes/ &
|
|
|
|
exit_code=$?; pid=$!;
|
|
|
|
echo "${pid}" >"${server_pid_file}.themes";
|
|
|
|
task_end "${exit_code}";
|
|
|
|
|
2019-08-25 20:38:28 +00:00
|
|
|
if [[ -z "${NO_BROWSER}" ]]; then
|
|
|
|
task_begin "Opening Browser";
|
2020-10-25 22:50:03 +00:00
|
|
|
# sensible-browser isn't opening the right browser :-/
|
|
|
|
xdg-open "http://[::]:35623";
|
2019-08-25 20:38:28 +00:00
|
|
|
task_end $?;
|
|
|
|
fi
|
2018-12-12 23:23:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:14:52 +00:00
|
|
|
task_stop-server() {
|
2019-09-29 14:42:22 +00:00
|
|
|
task_begin "Stopping server";
|
2018-12-12 23:23:50 +00:00
|
|
|
kill "$(cat "${server_pid_file}")";
|
|
|
|
rm "${server_pid_file}";
|
|
|
|
task_end $?;
|
2019-09-29 14:42:22 +00:00
|
|
|
|
|
|
|
task_begin "Stopping theme server";
|
|
|
|
kill "$(cat "${server_pid_file}.themes")";
|
|
|
|
rm "${server_pid_file}.themes";
|
|
|
|
task_end "$?";
|
2018-12-12 23:23:50 +00:00
|
|
|
}
|
2020-07-07 20:42:53 +00:00
|
|
|
|
2020-07-10 19:16:17 +00:00
|
|
|
task_sign() {
|
|
|
|
task_begin "Preparing to sign release";
|
|
|
|
if [[ ! -f "build/index.php" ]]; then
|
|
|
|
task_end 1 "Error: build/index.php doesn't exist";
|
|
|
|
fi
|
|
|
|
|
|
|
|
temp_dir="$(mktemp --tmpdir -d "pepperminty-wiki-XXXXXXX")";
|
|
|
|
cp "build/index.php" "${temp_dir}";
|
|
|
|
task_end "$?";
|
|
|
|
|
|
|
|
task_begin "Signing release";
|
|
|
|
pushd "${temp_dir}" || { echo "Error: Failed to cd to temporary directory"; exit 1; };
|
|
|
|
# Generate hashes
|
|
|
|
find . -type f -not -name "*.SHA256" -print0 | xargs -0 -n1 -I{} -P"$(nproc)" sha256sum -b "{}" >HASHES.SHA256;
|
|
|
|
# Generate GPG signature
|
|
|
|
gpg --sign --detach-sign --armor HASHES.SHA256;
|
|
|
|
popd || { echo ""; exit 1; };
|
|
|
|
task_end "$?";
|
|
|
|
|
|
|
|
task_begin "Finalising";
|
|
|
|
cp "${temp_dir}/HASHES.SHA256" "./build";
|
|
|
|
cp "${temp_dir}/HASHES.SHA256.asc" "./build";
|
|
|
|
echo -e "Written output files to ${HC}$(display_url "${PWD}/build/HASHES.SHA256" "HASHES.SHA256") ${RS}and ${HC}$(display_url "${PWD}/build/HASHES.SHA256.asc" "HASHES.SHA256.asc").${RS}";
|
|
|
|
rm -r "${temp_dir}";
|
|
|
|
task_end "$?" "Failed to finalise!";
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:42:53 +00:00
|
|
|
task_clean() {
|
|
|
|
task_begin "Clearing out build outputs";
|
|
|
|
|
|
|
|
if [[ "${PEPPERMINT_REALLY_CLEAN}" != "yes" ]]; then
|
|
|
|
echo -e "Are you SURE you want to continue? This will delete ALL your wiki data!";
|
|
|
|
echo -e "Set the environment variable PEPPERMINT_REALLY_CLEAN to 'yes' (without quotes) to actually do the deletion.\n\n";
|
|
|
|
task_end 1 "Aborted.";
|
|
|
|
exit 1; # Just in case
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "[PEPPERMINT_REALLY_CLEAN] I sure hope you know what you're doing.";
|
|
|
|
|
|
|
|
rm -rf build module_index.json themes/themeindex.json node_modules __nightdocs ;
|
|
|
|
find themes -type f -name "preview_large.png" -delete;
|
|
|
|
find themes -type f -name "preview_small.png" -delete;
|
|
|
|
|
|
|
|
task_end "$?" "Failed to completely clear out all build outputs";
|
|
|
|
}
|
2018-12-12 23:23:50 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
tasks_run $@;
|