From beb627c482128c4604ae4c9906d4fbadf12f9296 Mon Sep 17 00:00:00 2001 From: Masataro Asai Date: Sun, 27 Oct 2019 18:01:02 -0400 Subject: [PATCH] a simple approach using shell commands --- plugins/gh-pages.lisp | 32 +++++++++++++++++++++ plugins/publish-gh-pages.sh | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 plugins/publish-gh-pages.sh diff --git a/plugins/gh-pages.lisp b/plugins/gh-pages.lisp index 4c61ffd..576bcbe 100644 --- a/plugins/gh-pages.lisp +++ b/plugins/gh-pages.lisp @@ -1,6 +1,38 @@ +(eval-when (:compile-toplevel :load-toplevel) + (ql:quickload 'puri :silent t)) (defpackage :coleslaw-gh-pages (:use :cl) + (:import-from :coleslaw + #:*config* + #:domain + #:deploy + #:staging-dir + #:deploy-dir) (:export #:enable)) (in-package :coleslaw-gh-pages) +(defvar *options* nil) + +(defmethod deploy (staging) + (uiop:run-program (list* (namestring + (merge-pathnames "plugins/publish-gh-pages.sh" + coleslaw-conf:*basedir*)) + (namestring + (merge-pathnames (staging-dir *config*))) + (namestring + (merge-pathnames (deploy-dir *config*))) + *options*) + :output t + :error-output t)) + +(defun enable (&key url (branch "gh-pages") (remote "origin") cname) + (check-type url string) + (check-type remote string) + (check-type branch string) + (if (eq t cname) + (progn + (setf cname (puri:uri-host (puri:parse-uri (domain *config*)))) + (check-type cname string) + (setf *options* (list url branch remote cname))) + (setf *options* (list url branch remote)))) diff --git a/plugins/publish-gh-pages.sh b/plugins/publish-gh-pages.sh new file mode 100755 index 0000000..528fbf0 --- /dev/null +++ b/plugins/publish-gh-pages.sh @@ -0,0 +1,55 @@ +#!/bin/bash -x + +set -e + +staging=$1 +deploy=$2 +url=$3 +branch=$4 +remote=$5 +cname=$6 + +if [[ -d $deploy && ! -d $deploy/.git ]] +then + echo "Target directory $deploy exists and is not a git repository. Aborting" >&2 + exit 1 +fi + +if [[ ! -d $deploy ]] +then + git clone --no-checkout --origin $remote $url $deploy +fi + +cd $deploy + +# safe and most reliable way to check if the branch exist +if git show-ref --verify --quiet refs/heads/$branch +then + # if the branch exists locally + git checkout $branch +elif git show-ref --verify --quiet refs/remotes/$remote/$branch +then + # if the branch does not exist locally but exist in the specified remote --- + # Note, git checkout $branch will search the branch with the same name with + # ALL remotes, and set it as the tracking branch if there is a single such + # remote, but does not allow the user to necessarily specify which. + git checkout -b $branch --track $remote/$branch +else + # if there is no matching branch, make an orphan branch + git checkout --orphan $branch +fi + +rsync -avz --delete --exclude .git/ --copy-links $staging $deploy + +if [[ ! -z "$cname" ]] +then + echo $cname > CNAME +fi + +git add -A # add all changes in the worktree +git add $(git ls-files -o ) # add all untracked files in the worktree + +git commit -m "Deployed on $(date)" + +git push $remote $branch +