cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

febd955c07fde47be2effbaec7934ee9e0a8866c

unsigned

author: Christian Cleberg <hello@cleberg.net> · 2025-06-04T00:46:11Z

feat: convert build script to python
 build.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 build.sh |  56 --------------------------
 2 files changed, 136 insertions(+), 56 deletions(-)

diff --git a/build.py b/build.py
new file mode 100644
index 0000000..c0d14de
--- /dev/null
+++ b/build.py
@@ -0,0 +1,136 @@
+#!/usr/bin/env python3
+import os
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+
+def prompt(prompt_text):
+    try:
+        return input(prompt_text).strip()
+    except EOFError:
+        return ''
+
+def remove_build_directory(build_dir):
+    if build_dir.exists():
+        print(f"Removing previous build directory: {build_dir}/")
+        shutil.rmtree(build_dir)
+    build_dir.mkdir(parents=True, exist_ok=True)
+
+def minify_css(src_css, dest_css):
+    print(f"Minifying CSS: {src_css} → {dest_css}")
+    result = subprocess.run(
+        ["minify", "-o", str(dest_css), str(src_css)],
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        text=True,
+    )
+    if result.returncode != 0:
+        print("Error during CSS minification:")
+        print(result.stderr, file=sys.stderr)
+        sys.exit(1)
+
+def run_emacs_publish(dev_mode=False):
+    if dev_mode:
+        print("Running Emacs publish script (development)...")
+        result = subprocess.run(
+            ["emacs", "--script", "publish.el"],
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE,
+            text=True,
+        )
+    else:
+        print("Running Emacs publish script (production)...")
+        result = subprocess.run(
+            ["emacs", "--script", "publish.el"],
+            stdout=subprocess.DEVNULL,
+            stderr=subprocess.DEVNULL,
+            text=True,
+        )
+
+    if result.returncode != 0:
+        print("Error running publish.el:")
+        print(result.stderr, file=sys.stderr)
+        sys.exit(1)
+
+def deploy_to_server(build_dir, server):
+    remote_path = f"{server}:/var/www/cleberg.net/"
+    print(f"Deploying .build/ → {remote_path}")
+    result = subprocess.run(
+        ["rsync", "-r", "--delete-before", f"{build_dir}/", remote_path],
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        text=True,
+    )
+    if result.returncode != 0:
+        print("Error during rsync deployment:")
+        print(result.stderr, file=sys.stderr)
+        sys.exit(1)
+
+def start_dev_server(build_dir):
+    print(f"Starting development HTTP server from {build_dir}/ on port 8000")
+    os.chdir(build_dir)
+    # This will run until interrupted (Ctrl+C)
+    try:
+        subprocess.run(
+            [sys.executable, "-m", "http.server", "8000"],
+            check=True
+        )
+    except KeyboardInterrupt:
+        print("\nDevelopment server stopped.")
+    except subprocess.CalledProcessError as e:
+        print(f"Error starting development server: {e}", file=sys.stderr)
+        sys.exit(1)
+
+def main():
+    build_dir = Path(".build")
+    theme_dir = Path("theme/static")
+    css_src = theme_dir / "styles.css"
+    css_min = theme_dir / "styles.min.css"
+
+    answer = prompt("Did you update the 'Recent Blog Posts' section? [yn] ")
+    if not answer.lower().startswith("y"):
+        print("Please update the 'Recent Blog Posts' section before publishing!")
+        sys.exit(0)
+
+    env = os.environ.get("ENV", "").lower()
+    if env == "prod":
+        print("Environment: Production")
+        method = prompt("Publishing on remote or LAN? [r|l] ").lower()
+        if method == "r":
+            ubuntu_server = "ubuntu-remote"
+        elif method == "l":
+            ubuntu_server = "ubuntu"
+        else:
+            print("Invalid input. Assuming LAN (ubuntu)")
+            ubuntu_server = "ubuntu"
+
+        # Remove previous build
+        remove_build_directory(build_dir)
+
+        # Minify CSS
+        minify_css(css_src, css_min)
+
+        # Run publishing script (silenced output)
+        run_emacs_publish(dev_mode=False)
+
+        # Deploy changes
+        deploy_to_server(build_dir, ubuntu_server)
+
+    else:
+        print("Environment: Development")
+
+        # Remove previous build
+        remove_build_directory(build_dir)
+
+        # Minify CSS
+        minify_css(css_src, css_min)
+
+        # Run publishing script (with console output)
+        run_emacs_publish(dev_mode=True)
+
+        # Launch development web server
+        start_dev_server(build_dir)
+
+if __name__ == "__main__":
+    main()
\ No newline at end of file
diff --git a/build.sh b/build.sh
deleted file mode 100755
index 66887b3..0000000
--- a/build.sh
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/bin/bash
-
-# Ensure the latest post is included on the home page
-printf "Did you update the 'Recent Blog Posts' section? [yn] "
-read answer
-
-# Only continue if latest post is included on the home page
-if [[ "$answer" =~ ^[Yy]$ ]]; then
-    # Check if the environment flag is set to PROD
-    if [[ "$ENV" == "prod" ]]; then
-        echo "Environment: Production"
-
-        # Check if publishing via LAN or remotely
-        printf "Publishing on remote or LAN? [r|l] "
-
-        # Update ubuntu_server variable based on answer
-        read method
-        if [[ "$method" =~ ^[Rr]$ ]]; then
-            ubuntu_server="ubuntu-remote"
-        elif [[ "$method" =~ ^[Ll]$ ]]; then
-            ubuntu_server="ubuntu"
-        else
-            echo "Invalid input. Assuming LAN (ubuntu)"
-            ubuntu_server="ubuntu"
-        fi
-
-        # Remove previous build
-        rm -rf .build/*
-
-        # Minify CSS
-        minify -o theme/static/styles.min.css theme/static/styles.css
-
-        # Run publishing script
-        emacs --script publish.el &>/dev/null
-
-        # Deploy changes
-        rsync -r --delete-before .build/* $ubuntu_server:/var/www/cleberg.net/
-    else
-        echo "Environment: Development"
-
-        # Remove previous build
-        rm -rf .build/*
-
-        # Minify CSS
-        minify -o theme/static/styles.min.css theme/static/styles.css
-
-        # Run publishing script
-        emacs --script publish.el
-
-        # Launch development web server
-        cd .build/
-        python3 -m http.server
-    fi
-else
-    echo "Please update the 'Recent Blog Posts' section before publishing!"
-fi