Files
website/website/build_css.py
2025-04-27 06:49:59 +02:00

104 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import subprocess
import shutil
import platform
import tempfile
import zipfile
import requests
from pathlib import Path
def download_tailwind_cli():
"""Download the standalone Tailwind CLI for the current platform"""
# Get current system info
system = platform.system().lower()
arch = platform.machine().lower()
# Map to tailwind downloadable platform
if system == "linux":
platform_name = "linux"
elif system == "darwin":
platform_name = "macos"
elif system == "windows":
platform_name = "windows"
else:
raise Exception(f"Unsupported platform: {system}")
# Map architecture
if "arm" in arch or "aarch" in arch:
arch_name = "arm64"
elif "x86_64" in arch or "amd64" in arch or "x64" in arch:
arch_name = "x64"
else:
# Default to x64 for other architectures
arch_name = "x64"
# URL for the binary
url = f"https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-{platform_name}-{arch_name}"
if system == "windows":
url += ".exe"
# Download location
bin_dir = Path("bin")
bin_dir.mkdir(exist_ok=True)
tailwind_bin = bin_dir / f"tailwindcss{'.exe' if system == 'windows' else ''}"
# Download if not exists
if not tailwind_bin.exists():
print(f"Downloading Tailwind CLI from {url}...")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(tailwind_bin, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
# Make executable on Unix
if system != "windows":
os.chmod(tailwind_bin, 0o755)
return str(tailwind_bin)
def build_css(watch=False):
"""Build CSS using Tailwind CLI"""
base_dir = Path(__file__).parent
input_file = base_dir / "static" / "css" / "src" / "input.css"
output_file = base_dir / "static" / "css" / "main.css"
tailwind_bin = download_tailwind_cli()
config_file = base_dir / "tailwind.config.js"
# Prepare command
cmd = [
tailwind_bin,
"-i", str(input_file),
"-o", str(output_file),
"-c", str(config_file)
]
if watch:
cmd.append("--watch")
print(f"Running: {' '.join(cmd)}")
# Run the command
if watch:
process = subprocess.Popen(cmd)
try:
process.wait()
except KeyboardInterrupt:
process.terminate()
print("\nBuild process terminated")
else:
subprocess.run(cmd, check=True)
print(f"CSS built successfully: {output_file}")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Build Tailwind CSS")
parser.add_argument("--watch", action="store_true", help="Watch for changes")
args = parser.parse_args()
build_css(watch=args.watch)