#!/usr/bin/env python3 """Generate an ignored Swift config source from optional build environment.""" import os from pathlib import Path def swift_string(value: str) -> str: escaped = [] for character in value: codepoint = ord(character) if character == "\\": escaped.append("\\\\") elif character == '"': escaped.append('\\"') elif character == "\n": escaped.append("\\n") elif character == "\r": escaped.append("\\r") elif character == "\t": escaped.append("\\t") elif codepoint < 0x20 or codepoint in (0x2028, 0x2029): escaped.append(f"\\u{{{codepoint:x}}}") else: escaped.append(character) return '"' + "".join(escaped) + '"' def main() -> None: key = os.environ.get("HYDRAULOPHONE_LASTFM_API_KEY", "").strip() secret = os.environ.get("HYDRAULOPHONE_LASTFM_API_SECRET", "").strip() if not key or not secret: key_literal = "nil" secret_literal = "nil" else: key_literal = swift_string(key) secret_literal = swift_string(secret) output = Path(".build/generated/LastFMBuildConfiguration.swift") output.parent.mkdir(parents=True, exist_ok=True) output.write_text( "// Generated by Scripts/generate_lastfm_build_configuration.py; ignored by git.\n" "enum LastFMBuildConfiguration {\n" f" static let apiKey: String? = {key_literal}\n" f" static let sharedSecret: String? = {secret_literal}\n" "}\n", encoding="utf-8", ) if __name__ == "__main__": main()