Update documentation and scripts for font addition process; introduce uv run add-font CLI command for streamlined font management. Enhance formula generation with improved class name formatting and validation steps. Remove outdated font formula files.

This commit is contained in:
Matt Troutman 2026-02-09 22:04:01 -06:00
parent 69d8156b09
commit 56b64d0b34
No known key found for this signature in database
266 changed files with 1187 additions and 8145 deletions

View file

@ -1,22 +1,87 @@
#!/usr/bin/env python3
import os
import re
from pathlib import Path
def formula_name_to_class(formula_name: str) -> str:
"""Convert formula name (e.g. 'acrylic-hand', 'graham_hand') to valid Ruby PascalCase."""
# Split on hyphens and underscores, capitalize each segment, join (no separators)
parts = re.split(r"[-_]+", formula_name)
return "".join(p.capitalize() for p in parts if p)
class HomebrewFormulaGenerator:
def __init__(self, fonts_dir):
self.fonts_dir = Path(fonts_dir)
self.formula_dir = self.fonts_dir.parent / 'Formula'
self.formula_dir = self.fonts_dir.parent / "Formula"
self.formula_dir.mkdir(exist_ok=True)
self.web_extensions = (".woff", ".woff2", ".eot", ".svg")
def generate_formula_content(self, font_name, formula_name):
def _font_has_files(self, font_dir: Path) -> tuple[bool, bool, bool, bool]:
"""Return (has_ttf, has_otf, has_web, has_other) for the font folder."""
has_ttf = any((font_dir / "ttf").glob("*.ttf"))
has_otf = any((font_dir / "otf").glob("*.otf"))
web_dir = font_dir / "web"
has_web = any(
f.suffix.lower() in self.web_extensions for f in web_dir.glob("*") if f.is_file()
)
other_dir = font_dir / "other_files"
has_other = any(other_dir.iterdir()) if other_dir.exists() else False
return (has_ttf, has_otf, has_web, has_other)
def _generate_test_block(
self, formula_name: str, has_ttf: bool, has_otf: bool, has_web: bool, has_other: bool
) -> str:
"""Generate test block that asserts at least one install path has files for this formula."""
assertions = []
if has_ttf:
assertions.append('assert (share/"fonts/truetype").glob("*.ttf").any?, "No TTF fonts installed"')
if has_otf:
assertions.append('assert (share/"fonts/opentype").glob("*.otf").any?, "No OTF fonts installed"')
if has_web:
assertions.append(
'assert (share/"fonts/webfonts").glob("*").any?, "No web fonts installed"'
)
if has_other:
assertions.append(
f'assert_predicate share/"{formula_name}", :directory?, "Other files dir missing"'
)
if not assertions:
assertions.append(
f'assert_predicate share/"{formula_name}", :directory?, "Formula share dir missing"'
)
return "\n ".join(assertions)
def generate_formula_content(
self,
font_name: str,
formula_name: str,
has_ttf: bool,
has_otf: bool,
has_web: bool,
has_other: bool,
) -> str:
"""Generate the Ruby formula content for a font."""
return f'''# typed: false
class_name = formula_name_to_class(formula_name)
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"
test_block = self._generate_test_block(
formula_name, has_ttf, has_otf, has_web, has_other
)
return f"""# typed: false
# frozen_string_literal: true
# This file was generated by the font folder cleanup script
# Do not edit this file directly
# Installs: {ext_comment}
class Font{formula_name.capitalize()} < Formula
class Font{class_name} < Formula
desc "Font: {formula_name}"
homepage "http://clancy.genet-godzilla.ts.net:3002/Fonts/homebrew-fonts"
url "http://clancy.genet-godzilla.ts.net:3002/Fonts/homebrew-fonts/archive/main.tar.gz"
@ -55,34 +120,33 @@ class Font{formula_name.capitalize()} < Formula
def caveats
<<~EOS
Fonts have been installed to:
#{'{share}/fonts/truetype'}
#{'{share}/fonts/opentype'}
#{'{share}/fonts/webfonts'}
#{{share}}/fonts/truetype
#{{share}}/fonts/opentype
#{{share}}/fonts/webfonts
Additional files are available in:
#{'{share}/' + formula_name}
#{{share}}/{formula_name}
EOS
end
test do
# Verify font installation
assert_predicate share/"fonts/truetype", :directory?
assert_predicate share/"fonts/opentype", :directory?
assert_predicate share/"fonts/webfonts", :directory?
{test_block}
end
end
'''
"""
def generate_formulas(self):
"""Generate Homebrew formulas for all font folders."""
for font_dir in self.fonts_dir.glob('font-*'):
for font_dir in sorted(self.fonts_dir.glob("font-*")):
if not font_dir.is_dir():
continue
font_name = font_dir.name
formula_name = font_name.replace('font-', '')
formula_content = self.generate_formula_content(font_name, formula_name)
formula_name = font_name.replace("font-", "", 1)
has_ttf, has_otf, has_web, has_other = self._font_has_files(font_dir)
formula_content = self.generate_formula_content(
font_name, formula_name, has_ttf, has_otf, has_web, has_other
)
formula_path = self.formula_dir / f"font-{formula_name}.rb"
formula_path.write_text(formula_content)
print(f"Generated formula for font-{formula_name}")