From 35a03b364fb757e04bf1b55097e6a319dd05b3d3 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Sun, 1 Aug 2021 00:35:56 -0700 Subject: [PATCH] add regenerate script --- README.md | 2 +- regenerate | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sh/document | 45 ++++++++++++++++++++------------- 3 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 regenerate diff --git a/README.md b/README.md index 3decf3c..ff8d583 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ quick install for random boxes: cd && curl -L https://github.com/notwa/rc/archive/master.tar.gz | tar zx && mv rc-master rc && rc/install ``` -**NOTE:** everything below this line is overwritten and regenerated by `sh/document`. +**NOTE:** everything below this line is overwritten and automatically [regenerated.](/regenerate) diff --git a/regenerate b/regenerate new file mode 100644 index 0000000..dc1901f --- /dev/null +++ b/regenerate @@ -0,0 +1,72 @@ +#!/usr/bin/env dash + +NAME="$0" + +note() { + local IFS=" " + printf "%s\n" "$*" +} + +warn() { + note "$@" >&2 +} + +die() { + warn "$NAME:" "$@" + #cleanup + exit 1 +} + +cleanup() { + : + #[ ! -e temp.tar.gz ] || rm temp.tar.gz || die failed + #[ ! -d temp ] || rm -r temp || die failed +} + +backup() { + if [ -e "$1" ]; then + backup "${1}~" || die 'failed to backup file' "$1" + fi + mv "$1" "${1}~" || die 'failed to backup file' "$1" +} + +rc="$(readlink -f "$(dirname "$NAME")" )" +[ -d "$rc" ] || die 'failed to determine rc directory' +cd "$rc" || die 'failed to change directory' + +if ! which git >/dev/null 2>&1; then + # git unavailable. just document everything as it is. + + [ "$1" != commit ] || die 'git not found' + dash ./sh/document || die 'failed to generate documentation' + +else + # git available. document the most recent commit, without pending changes. + + [ ! -e temp.tar.gz ] || backup temp.tar.gz || die 'failed to backup existing temp archive' + git archive --prefix=temp/ HEAD -o temp.tar.gz || die 'failed to create git archive' + + [ ! -e temp ] || backup temp || die 'failed to backup existing temp directory' + tar -zxf temp.tar.gz || die 'failed to unarchive temp archive' + + #( cd temp; HOME="$rc/temp" dash ./install ) + ln temp/home/zshrc temp/.zshrc || die 'failed to copy files' + ln temp/home/-shrc temp/.-shrc || die 'failed to copy files' + ln temp/home/bashrc temp/.bashrc || die 'failed to copy files' + + # note that we use the latest version instead of the one from the archive. + cp -p README.md temp/README.md || die 'failed to copy existing readme' + # note that we use the latest version instead of the one from the archive. + dash ./sh/document temp || die 'failed to generate documentation' + + #[ ! -e README.md ] || backup README.md || die 'failed to backup existing readme' + mv temp/README.md~ README.md~ || die 'failed to move generated readme' + + rm temp.tar.gz || die 'failed to clean up temp archive' + rm -r temp || die 'failed to clean up temp directory' + + if [ "$1" = commit ]; then + mv README.md~ README.md || die 'failed to overwrite readme' + git commit README.md -m 'regenerate readme' || die 'failed to commit' + fi +fi diff --git a/sh/document b/sh/document index 5277733..6edc9bc 100644 --- a/sh/document +++ b/sh/document @@ -69,18 +69,18 @@ document2() { [ "$f" != "${f#sh/}" ] && url="/sh/${f#sh/}#L$c" || url="/home/${f#.}#L$c" if [ "$n" = "$e" ]; then # function name matches the filename. - printf '\n%s [%s](%s)\n\n' "$d" "$n" "$url" >> rc/README.md~ || return 5 + printf '\n%s [%s](%s)\n\n' "$d" "$n" "$url" || return 5 else # this file contains some other function, so include the filename. - printf '\n%s [%s](%s)\n\n' "$d" "$n (${f#.})" "$url" >> rc/README.md~ || return 5 - #printf '\n%s %s\n\n* defined in [%s](%s)\n\n' "%d" "$n" "$f" "$url" >> rc/README.md~ || return 5 + printf '\n%s [%s](%s)\n\n' "$d" "$n (${f#.})" "$url" || return 5 + #printf '\n%s %s\n\n* defined in [%s](%s)\n\n' "%d" "$n" "$f" "$url" || return 5 fi fi if [ "$s" != ' ' ]; then # don't bother unless it was set to something if [ -z "$n" -o -n "$s" ]; then # might only be a name, check that # just pass the remaining comment through: - printf '%s\n' "$s" >> rc/README.md~ || return 5 + printf '%s\n' "$s" || return 5 fi fi done < "$f" || return 4 @@ -88,37 +88,48 @@ document2() { document1() { # NOTE: in the future, it'd be nice to support arbitary files through arguments. - [ $# -le 0 ] || return 1 + + local in_='rc/README.md' + local out='rc/README.md~' + if [ $# -eq 0 ]; then + cd || return 2 + [ -d rc ] || return 3 # sanity check + elif [ $# -eq 1 ]; then + in_='README.md' + out='README.md~' + cd "$1" || return 2 + else + return 1 + fi # sanity check: - cd || return 2 - [ -d rc ] && [ -d sh ] && [ -f .zshrc ] && [ -f .-shrc ] && [ -f .bashrc ] || return 3 + [ -d sh ] && [ -f .zshrc ] && [ -f .-shrc ] && [ -f .bashrc ] || return 3 # create new output file (with a tilde as not to overwrite just yet): - : > rc/README.md~ || return 4 + : > "$out" || return 4 local line - if [ -f rc/README.md ]; then + if [ -f "$in_" ]; then # copy existing lines up to (and including) the "DOCUMENT" marker: while IFS= read -r line; do - printf '%s\n' "$line" >> rc/README.md~ || return 5 + printf '%s\n' "$line" >> "$out" || return 5 case "$line" in *DOCUMENT*) break;; esac - done < rc/README.md || return 4 + done < "$in_" || return 4 fi # first section: - printf '\n## %s\n' 'shell functions' >> rc/README.md~ || return 5 + printf '\n## %s\n' 'shell functions' >> "$out" || return 5 for f in sh/*; do [ -e "$f" ] || continue # make sure glob went through - document2 "$f" || return $? + document2 "$f" >> "$out" || return $? done # second section: - printf '\n## %s\n' 'miscellaneous' >> rc/README.md~ || return 5 - document2 .zshrc || return $? - document2 .bashrc || return $? - document2 .-shrc || return $? + printf '\n## %s\n' 'miscellaneous' >> "$out" || return 5 + document2 .zshrc >> "$out" || return $? + document2 .bashrc >> "$out" || return $? + document2 .-shrc >> "$out" || return $? } document() { ### @-