aboutsummaryrefslogtreecommitdiff
path: root/build.py
blob: c0d14dea63bfa4959e6c824188cf63b5cda3c3d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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()