diff --git a/bake.rb b/bake.rb index 21db8e1..bc6b593 100644 --- a/bake.rb +++ b/bake.rb @@ -101,6 +101,49 @@ def new_link puts post.target_path end +# Build a link post and preview it on the mudge blog server without touching +# git: writes posts/YYYY/MM/slug.md, builds for http://mudge:8000 (the same +# target blog-server.service serves straight out of www/), prints the +# preview URL, then deletes the local file. Drives bin/preview-link. +def preview_link + payload = + begin + JSON.parse($stdin.read) + rescue JSON::ParserError => e + abort "Error: invalid JSON payload on stdin: #{e.message}" + end + + author = payload["author"] || Pressa::Config::SimpleToml.load_file("site.toml")["author"] + post = + begin + Pressa::LinkPost.build( + title: payload["title"], + link: payload["link"], + body: payload["body"], + tags: payload["tags"], + author: + ) + rescue Pressa::LinkPost::Error => e + abort "Error: #{e.message}" + end + + abort "Error: post already exists at #{post.target_path}" if File.exist?(post.target_path) + + FileUtils.mkdir_p(File.dirname(post.target_path)) + File.write(post.target_path, post.content) + + year_month = post.target_path[%r{^posts/(\d{4}/\d{2})/}, 1] + slug = File.basename(post.filename, ".md") + + begin + mudge + ensure + FileUtils.rm_f(post.target_path) + end + + puts "http://mudge:8000/posts/#{year_month}/#{slug}/" +end + # Create a new draft in public/drafts/. # @parameter title_parts [Array] Optional title words; defaults to Untitled. def new_draft(*title_parts) diff --git a/bin/preview-link b/bin/preview-link new file mode 100755 index 0000000..22f7a8a --- /dev/null +++ b/bin/preview-link @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# +# Build a link post from a JSON payload on stdin and preview it on the mudge +# blog server (http://mudge:8000, served by blog-server.service straight out +# of www/) without touching git at all: no pull, no commit, no push. The post +# file is deleted from posts/ again right after the preview build, so this +# never leaves a trace in the repo or in git history. +# +# ssh mudge '$HOME/samhuri.net/bin/preview-link' <<'JSON' +# {"title": "Magical Wristband", "link": "https://example.net/x", +# "body": "Optional commentary.", "tags": "gear, tech"} +# JSON +# +# Prints the preview URL on stdout, progress on stderr. Used by the +# dashboard's "Publish Beta" button. + +set -euo pipefail + +# Non-interactive SSH sessions get a bare PATH; make sure rbenv is reachable. +export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH" +if command -v rbenv >/dev/null 2>&1; then + eval "$(rbenv init - bash)" +fi + +# Run from the repo root regardless of where the script lives. +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO="${SAMHURI_REPO:-$(cd "$SCRIPT_DIR/.." && pwd)}" +cd "$REPO" + +payload="$(cat)" +if [ -z "${payload//[[:space:]]/}" ]; then + echo "Error: empty payload on stdin" >&2 + exit 1 +fi + +echo "==> Building preview" >&2 +# `bake mudge` logs its own build progress to stdout (not stderr), so only the +# final line -- the preview URL `preview_link` puts last -- is what we want. +printf '%s' "$payload" | bundle exec bake preview_link | tee /dev/stderr | tail -n 1