# Code Custodian File ID: 1E934C09-2F64-4ACC-A925-96F049705B44 import sys import os import json import urllib.request import urllib.error def main(): if len(sys.argv) != 4: print("Usage: python3 stub_generator.py ") sys.exit(1) framework_name = sys.argv[1] input_dir = sys.argv[2] output_file = sys.argv[3] api_key = os.environ.get("GEMINI_API_KEY") if not api_key: print("❌ Error: GEMINI_API_KEY environment variable not found.") sys.exit(1) print(f"🔍 Scanning {input_dir} for Swift files...") swift_content = "" for root, dirs, files in os.walk(input_dir): for file in files: if file.endswith(".swift") and not file.endswith(".api.swift"): filepath = os.path.join(root, file) with open(filepath, 'r', encoding='utf-8') as f: swift_content += f"\n\n// --- FILE: {file} ---\n" swift_content += f.read() if not swift_content.strip(): print(f"❌ Error: No Swift files found in {input_dir}") sys.exit(1) prompt = f""" You are a strict Swift code parser. I will provide the raw Swift source code for the framework '{framework_name}'. Your ONLY job is to return a syntactically valid C-header style API stub. CRITICAL RULES: 1. Extract ONLY `public` or `open` classes, structs, enums, protocols, extensions, variables, and function signatures. 2. ALL functions MUST have exactly `{{ fatalError() }}` as their body. Do not leave them blank. 3. ALL computed variables must have `{{ fatalError() }}` as their body. 4. ALL `static let`, `public let`, or `public var` variables MUST be provided a dummy initializer! (e.g. `public static let shared: ClassName = ClassName()`, or `= ""` for String). Do not leave a variable uninitialized. 5. Do not write `public static let` inside a `public extension`. Just write `static let`. 6. Return raw text only, no markdown fences. SOURCE CODE: {swift_content} """ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key={api_key}" payload = {"contents": [{"parts": [{"text": prompt}]}], "generationConfig": {"temperature": 0.0}} req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers={'Content-Type': 'application/json'}) try: print(f"🤖 Sending {framework_name} to Gemini Minion...") with urllib.request.urlopen(req) as response: res_json = json.loads(response.read()) stub_text = res_json['candidates'][0]['content']['parts'][0]['text'].strip() if stub_text.startswith("```swift"): stub_text = stub_text[8:].strip() elif stub_text.startswith("```"): stub_text = stub_text[3:].strip() if stub_text.endswith("```"): stub_text = stub_text[:-3].strip() with open(output_file, 'w', encoding='utf-8') as f: f.write(f"// FRAMEWORK: {framework_name}\n// Generated API Stub via Gemini Minion\n\n{stub_text}") print(f"✅ Stub successfully written to {output_file}") except Exception as e: print(f"❌ Error: {e}") if __name__ == "__main__": main()