import yaml import os from jinja2 import Environment, FileSystemLoader # Configuration SRC_DIR = 'src' TEMPLATES_DIR = 'templates' DIST_DIR = 'dist' SCHEME_FILES = ['neon.yaml', 'aeon.yaml'] def load_scheme(filename): with open(os.path.join(SRC_DIR, filename), 'r') as f: return yaml.safe_load(f) def render_template(template_path, context): env = Environment(loader=FileSystemLoader(TEMPLATES_DIR)) template = env.get_template(template_path) return template.render(context) def build(): # Load all schemes first schemes = [] for f in SCHEME_FILES: schemes.append(load_scheme(f)) # Ensure dist directory exists if not os.path.exists(DIST_DIR): os.makedirs(DIST_DIR) # Walk through templates for root, dirs, files in os.walk(TEMPLATES_DIR): # Check for meta.yaml in the current directory (app directory) meta_file = os.path.join(root, 'meta.yaml') strategy = 'individual' # Default strategy if os.path.exists(meta_file): try: with open(meta_file, 'r') as f: meta = yaml.safe_load(f) if meta and 'strategy' in meta: strategy = meta['strategy'] except Exception as e: print(f"Warning: Failed to load meta.yaml in {root}: {e}") for file in files: if not file.endswith('.j2'): continue rel_path = os.path.relpath(root, TEMPLATES_DIR) app_name = rel_path # e.g., 'nvim', 'zed', 'kitty' template_rel_path = os.path.join(rel_path, file) output_filename_base = file.replace('.j2', '') # Create app directory in dist app_dist_dir = os.path.join(DIST_DIR, app_name) if not os.path.exists(app_dist_dir): os.makedirs(app_dist_dir) print(f"Processing template: {template_rel_path} [Strategy: {strategy}]") # Strategy: Aggregated (Single output for all schemes) if strategy == 'aggregated': context = {'schemes': schemes} rendered_content = render_template(template_rel_path, context) # Output file: Matches template base name (e.g., apex.json or gtk.css) output_path = os.path.join(app_dist_dir, output_filename_base) with open(output_path, 'w') as f: f.write(rendered_content) print(f" -> {output_path}") # Strategy: Individual (Per-scheme output) else: for scheme in schemes: scheme_slug = scheme['scheme'].lower().replace(' ', '-') if 'apex' in output_filename_base: final_filename = output_filename_base.replace('apex', f"apex-{scheme_slug.replace('apex-', '')}") else: final_filename = f"{scheme_slug}-{output_filename_base}" final_filename = final_filename.replace('apex-apex-', 'apex-') output_path = os.path.join(app_dist_dir, final_filename) rendered_content = render_template(template_rel_path, scheme) with open(output_path, 'w') as f: f.write(rendered_content) print(f" -> {output_path} ({scheme['scheme']})") if __name__ == '__main__': build()