add-font.py helper for the repo.
This commit is contained in:
commit
5a1f46b683
1 changed files with 93 additions and 0 deletions
93
add-font.py
Normal file
93
add-font.py
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import os
|
||||
import hashlib
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_font_files(font_folder):
|
||||
"""Find all .ttf and .otf files in the given folder."""
|
||||
font_files = []
|
||||
for root, _, files in os.walk(font_folder):
|
||||
for file in files:
|
||||
if file.lower().endswith(('.ttf', '.otf')):
|
||||
font_files.append(os.path.join(root, file))
|
||||
return font_files
|
||||
|
||||
|
||||
def generate_sha256(file_path):
|
||||
"""Generate the sha256 hash of the given file."""
|
||||
sha256_hash = hashlib.sha256()
|
||||
with open(file_path, "rb") as f:
|
||||
for byte_block in iter(lambda: f.read(4096), b""):
|
||||
sha256_hash.update(byte_block)
|
||||
return sha256_hash.hexdigest()
|
||||
|
||||
|
||||
def create_zip(font_files, font_folder, zip_path):
|
||||
"""Create a zip file from the font files."""
|
||||
print(f"Creating zip file at {zip_path} with the following font files:")
|
||||
for font_file in font_files:
|
||||
print(f" - {font_file}")
|
||||
|
||||
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for file in font_files:
|
||||
# Add the font file with its relative path
|
||||
arcname = os.path.relpath(file, font_folder) # Now using font_folder
|
||||
zipf.write(file, arcname)
|
||||
print(f"Added {file} to the zip as {arcname}")
|
||||
|
||||
|
||||
def create_ruby_file(font_name, zip_path, sha256_hash):
|
||||
"""Generate a Ruby formula file for the font."""
|
||||
formula_content = f"""class {font_name} < Formula
|
||||
desc "Font for {font_name}"
|
||||
homepage "https://github.com/{font_name}"
|
||||
url "{zip_path}"
|
||||
sha256 "{sha256_hash}"
|
||||
version "latest"
|
||||
|
||||
def install
|
||||
(share/"fonts").install Dir["*.ttf", "*.otf"]
|
||||
end
|
||||
|
||||
test do
|
||||
assert_predicate share/"fonts/{font_name}-Regular.ttf", :exist?
|
||||
end
|
||||
end
|
||||
"""
|
||||
formula_path = f"./{font_name}/{font_name.lower()}.rb"
|
||||
with open(formula_path, 'w') as formula_file:
|
||||
formula_file.write(formula_content)
|
||||
print(f"Ruby file created at {formula_path}")
|
||||
|
||||
|
||||
def main():
|
||||
font_folder = input("Enter the location of the font folder: ").strip()
|
||||
|
||||
if not os.path.isdir(font_folder):
|
||||
print("The folder does not exist. Please provide a valid path.")
|
||||
return
|
||||
|
||||
font_files = get_font_files(font_folder)
|
||||
if not font_files:
|
||||
print("No .ttf or .otf font files found in the specified folder.")
|
||||
return
|
||||
|
||||
font_name = input("Enter the name of the font (e.g., FiraCode): ").strip()
|
||||
|
||||
# Create the folder for the font
|
||||
font_folder_path = Path(f"./{font_name.lower()}")
|
||||
font_folder_path.mkdir(exist_ok=True)
|
||||
|
||||
zip_path = f"./{font_name.lower()}/{font_name.lower()}.zip"
|
||||
create_zip(font_files, font_folder, zip_path)
|
||||
|
||||
sha256_hash = generate_sha256(zip_path)
|
||||
print(f"SHA256 hash generated: {sha256_hash}")
|
||||
|
||||
create_ruby_file(font_name, zip_path, sha256_hash)
|
||||
print(f"Font {font_name} has been added successfully.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue