homebrew-fonts/.fontfoldercleanup/create_homebrew_formula.py
Matt Troutman 30e4dfb067
Add 4 Apple font families extracted from apple.com, regenerate all casks
New fonts: font-sf-pro-display, font-sf-pro-text, font-sf-pro-icons, font-apple-icons (171 files total).
2026-04-12 14:18:42 -05:00

117 lines
3.9 KiB
Python
Executable file

#!/usr/bin/env python3
"""Generate Homebrew Cask files for all fonts in font_files/."""
import re
from pathlib import Path
WEB_EXTENSIONS = (".woff", ".woff2", ".eot", ".svg")
class HomebrewCaskGenerator:
def __init__(self, fonts_dir):
self.fonts_dir = Path(fonts_dir)
self.casks_dir = self.fonts_dir.parent / "Casks"
self.casks_dir.mkdir(exist_ok=True)
def _collect_font_files(self, font_dir: Path) -> list[str]:
"""Return list of font file paths relative to extracted archive root for the font artifact."""
font_name = font_dir.name
files = []
ttf_dir = font_dir / "ttf"
for f in sorted(ttf_dir.iterdir()):
if f.is_file() and f.suffix.lower() in (".ttf", ".ttc"):
files.append(f"homebrew-fonts/font_files/{font_name}/ttf/{f.name}")
for otf in sorted((font_dir / "otf").glob("*.otf")):
files.append(f"homebrew-fonts/font_files/{font_name}/otf/{otf.name}")
return files
def _collect_all_info(self, font_dir: Path) -> dict:
"""Collect metadata about a font directory."""
font_name = font_dir.name
formula_name = font_name.replace("font-", "", 1)
font_files = self._collect_font_files(font_dir)
# Determine what types are present
has_ttf = any(f.endswith(".ttf") or f.endswith(".ttc") for f in font_files)
has_otf = any(f.endswith(".otf") for f in font_files)
web_dir = font_dir / "web"
has_web = any(
f.suffix.lower() in WEB_EXTENSIONS for f in web_dir.glob("*") if f.is_file()
)
extensions = []
if has_ttf:
extensions.append("TTF")
if has_otf:
extensions.append("OTF")
if has_web:
extensions.append("web")
ext_comment = " ".join(extensions) if extensions else "other_files only"
return {
"font_name": font_name,
"formula_name": formula_name,
"font_files": font_files,
"ext_comment": ext_comment,
}
def generate_cask_content(self, info: dict) -> str:
"""Generate the Ruby cask content for a font."""
font_name = info["font_name"]
formula_name = info["formula_name"]
font_files = info["font_files"]
ext_comment = info["ext_comment"]
# Build font artifact lines
font_lines = "\n".join(f' font "{f}"' for f in font_files)
# Human-readable name: replace hyphens/underscores with spaces, title case
display_name = re.sub(r"[-_]+", " ", formula_name).title()
return f"""# This file was generated by the font folder cleanup script
# Do not edit this file directly
# Installs: {ext_comment}
cask "{font_name}" do
version "1.0.0"
sha256 :no_check
url "http://clancy.genet-godzilla.ts.net:8085/clancy/homebrew-fonts/archive/main.tar.gz"
name "{display_name}"
homepage "http://clancy.genet-godzilla.ts.net:8085/clancy/homebrew-fonts"
{font_lines}
end
"""
def generate_casks(self):
"""Generate Homebrew casks for all font folders."""
for font_dir in sorted(self.fonts_dir.glob("font-*")):
if not font_dir.is_dir():
continue
info = self._collect_all_info(font_dir)
if not info["font_files"]:
print(f"Skipping {font_dir.name}: no TTF or OTF files")
continue
cask_content = self.generate_cask_content(info)
cask_path = self.casks_dir / f"{font_dir.name}.rb"
cask_path.write_text(cask_content)
print(f"Generated cask for {font_dir.name}")
def main():
script_dir = Path(__file__).parent
fonts_dir = script_dir.parent / "font_files"
if not fonts_dir.exists():
print(f"Error: Font directory not found: {fonts_dir}")
return
generator = HomebrewCaskGenerator(fonts_dir)
generator.generate_casks()
if __name__ == "__main__":
main()