PulseCanvas / app · pulsecanvas.ai / scripts / generate-store-project.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generate the deterministic macOS Xcode app project.""" |
| 3 | from pathlib import Path |
| 4 | import hashlib |
| 5 | import json |
| 6 | from xcode_zipfoundation import add_zipfoundation |
| 7 | |
| 8 | root = Path(__file__).resolve().parent.parent |
| 9 | objects = {} |
| 10 | def ident(label): return hashlib.sha256(label.encode()).hexdigest()[:24].upper() |
| 11 | def obj(label, isa, **fields): |
| 12 | key = ident(label) |
| 13 | objects[key] = dict(isa=isa, **fields) |
| 14 | return key |
| 15 | def ref(path, kind): |
| 16 | return obj('file:'+path, 'PBXFileReference', lastKnownFileType=kind, path=path, sourceTree='<group>') |
| 17 | def build(key): return obj('build:'+key, 'PBXBuildFile', fileRef=key) |
| 18 | def phase(label, isa, files): return obj(label, isa, buildActionMask=2147483647, files=files, runOnlyForDeploymentPostprocessing=0) |
| 19 | |
| 20 | creators_only = {'ProjectMEngine.swift', 'AppUpdater.swift', 'PulseCanvasAgentAccess.swift'} |
| 21 | sources = sorted(str(p.relative_to(root)) for p in (root/'Sources/PulseCanvas').glob('*.swift') |
| 22 | if p.name not in creators_only and not p.name.startswith('PulseCanvasMCP')) + ['macOS/BundleResources.swift'] |
| 23 | source_refs = [ref(p,'sourcecode.swift') for p in sources] |
| 24 | resources = [ref('Localization/Localizable.xcstrings', 'text.json.xcstrings'), ref('.build/store-resources/Resources','folder'),ref('.build/store-resources/AppIcon.icns','image.icns'),ref('.build/store-resources/Legal','folder'),ref('macOS/PrivacyInfo.xcprivacy','text.xml')] |
| 25 | config = ref('macOS/Release.xcconfig','text.xcconfig') |
| 26 | other_refs = [ref('Version.xcconfig','text.xcconfig'), config, ref('macOS/Info.plist','text.plist.xml'),ref('macOS/PulseCanvas.entitlements','text.plist.entitlements')] |
| 27 | product = obj('product','PBXFileReference',explicitFileType='wrapper.application',path='PulseCanvas Studio.app',sourceTree='BUILT_PRODUCTS_DIR',includeInIndex=0) |
| 28 | products = obj('products','PBXGroup',children=[product],name='Products',sourceTree='<group>') |
| 29 | group = obj('main','PBXGroup',children=source_refs+resources+other_refs+[products],sourceTree='<group>') |
| 30 | prepare = obj('prepare','PBXShellScriptBuildPhase',buildActionMask=2147483647,files=[],inputPaths=[],outputPaths=[],runOnlyForDeploymentPostprocessing=0,shellPath='/bin/bash',shellScript='"$SRCROOT/scripts/prepare-store-resources.sh"\n',name='Prepare Metal-only resources, icon and notices',alwaysOutOfDate=1) |
| 31 | srcphase=phase('sources','PBXSourcesBuildPhase',[build(k) for k in source_refs]) |
| 32 | resphase=phase('resources','PBXResourcesBuildPhase',[build(k) for k in resources]) |
| 33 | frameworkphase=phase('frameworks','PBXFrameworksBuildPhase',[]) |
| 34 | def configs(label,target=False): |
| 35 | keys=[] |
| 36 | for name in ['Debug','Release']: |
| 37 | settings={'SDKROOT':'macosx','CLANG_ENABLE_MODULES':'YES'} |
| 38 | if target: |
| 39 | settings.update(SWIFT_OPTIMIZATION_LEVEL='-Onone' if name=='Debug' else '-O',DEBUG_INFORMATION_FORMAT='dwarf' if name=='Debug' else 'dwarf-with-dsym',SWIFT_EMIT_LOC_STRINGS='NO',SWIFT_ACTIVE_COMPILATION_CONDITIONS='$(inherited) DEBUG' if name=='Debug' else '$(inherited)') |
| 40 | keys.append(obj(label+name,'XCBuildConfiguration',buildSettings=settings,name=name,**({'baseConfigurationReference':config} if target else {}))) |
| 41 | return obj(label+'list','XCConfigurationList',buildConfigurations=keys,defaultConfigurationIsVisible=0,defaultConfigurationName='Release') |
| 42 | target=obj('target','PBXNativeTarget',buildConfigurationList=configs('targetconfig',True),buildPhases=[prepare,srcphase,frameworkphase,resphase],buildRules=[],dependencies=[],name='PulseCanvas',productName='PulseCanvas Studio',productReference=product,productType='com.apple.product-type.application') |
| 43 | project=obj('project','PBXProject',attributes={'LastUpgradeCheck':'2700','TargetAttributes':{target:{'CreatedOnToolsVersion':'27.0','ProvisioningStyle':'Automatic','SystemCapabilities':{'com.apple.Sandbox':{'enabled':1},'com.apple.iCloud':{'enabled':1}}}}},buildConfigurationList=configs('projectconfig'),compatibilityVersion='Xcode 14.0',developmentRegion='en',hasScannedForEncodings=0,knownRegions=['en', 'Base'],mainGroup=group,productRefGroup=products,projectDirPath='',projectRoot='',targets=[target]) |
| 44 | add_zipfoundation(objects, obj, target, project) |
| 45 | def encode(v,level=0): |
| 46 | if isinstance(v,dict): return '{\n'+''.join('\t'*(level+1)+json.dumps(str(k))+' = '+encode(x,level+1)+';\n' for k,x in v.items())+'\t'*level+'}' |
| 47 | if isinstance(v,list): return '(\n'+''.join('\t'*(level+1)+encode(x,level+1)+',\n' for x in v)+'\t'*level+')' |
| 48 | if isinstance(v,int):return str(v) |
| 49 | return json.dumps(v,ensure_ascii=False) |
| 50 | folder=root/'PulseCanvas.xcodeproj' |
| 51 | folder.mkdir(exist_ok=True) |
| 52 | (folder/'project.pbxproj').write_text('// !$*UTF8*$!\n'+encode(dict(archiveVersion=1,classes={},objectVersion=56,objects=objects,rootObject=project))+'\n') |
| 53 | scheme=folder/'xcshareddata/xcschemes/PulseCanvas.xcscheme' |
| 54 | scheme.parent.mkdir(parents=True,exist_ok=True) |
| 55 | reference=f'<BuildableReference BuildableIdentifier="primary" BlueprintIdentifier="{target}" BuildableName="PulseCanvas Studio.app" BlueprintName="PulseCanvas" ReferencedContainer="container:PulseCanvas.xcodeproj"/>' |
| 56 | scheme.write_text(f'''<?xml version="1.0" encoding="UTF-8"?> |
| 57 | <Scheme LastUpgradeVersion="2700" version="1.3"> |
| 58 | <BuildAction parallelizeBuildables="YES" buildImplicitDependencies="YES"><BuildActionEntries><BuildActionEntry buildForTesting="YES" buildForRunning="YES" buildForProfiling="YES" buildForArchiving="YES" buildForAnalyzing="YES">{reference}</BuildActionEntry></BuildActionEntries></BuildAction> |
| 59 | <TestAction buildConfiguration="Debug"/> |
| 60 | <LaunchAction buildConfiguration="Debug" selectedDebuggerIdentifier="Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier="Xcode.IDEFoundation.Launcher.LLDB" launchStyle="0" useCustomWorkingDirectory="NO" ignoresPersistentStateOnLaunch="NO" debugDocumentVersioning="YES" debugServiceExtension="internal" allowLocationSimulation="NO"><BuildableProductRunnable runnableDebuggingMode="0">{reference}</BuildableProductRunnable></LaunchAction> |
| 61 | <ProfileAction buildConfiguration="Release"><BuildableProductRunnable runnableDebuggingMode="0">{reference}</BuildableProductRunnable></ProfileAction> |
| 62 | <AnalyzeAction buildConfiguration="Debug"/> |
| 63 | <ArchiveAction buildConfiguration="Release" revealArchiveInOrganizer="YES"/> |
| 64 | </Scheme> |
| 65 | ''') |
| 66 | print(folder) |