#!/usr/bin/env python3 """归一化 profile 的 cordis.patch.yml:删除所有「平台段」(任意标记形式)后,追加唯一一份标准段。 用法: python3 normalize-picker-patch.py [--dry-run] [ ...] """ import io import sys BEGIN_RE = '# >>> platform: workspace-scoped-picker' END_RE = '# <<< platform: workspace-scoped-picker' BLOCK = """# >>> platform: workspace-scoped-picker (managed by ensure-workspace-picker.cjs) # 目录选择器收敛(档案 18 v3):官方对话框 UI 保留(单独插入 client 面), # host 面换成受限实现(根=自有 ws,越界拒绝);插件 client 面再注入 CSS 隐藏「改路径」入口。 - insert: - id: workspace-scoped-picker name: "@dsh-local/workspace-scoped-picker" - id: ui-directory-picker-browse name: "@deepseek-ai/dsh-client-ui-directory-picker-browse" - id: directory-picker name: "@deepseek-ai/dsh-host-directory-picker-auto" disabled: true # <<< platform: workspace-scoped-picker """ dry = '--dry-run' in sys.argv files = [a for a in sys.argv[1:] if not a.startswith('--')] for path in files: src = io.open(path, encoding='utf-8').read() lines = src.split('\n') kept, removed, skipping = [], 0, False for line in lines: if line.startswith(BEGIN_RE): skipping = True removed += 1 continue if skipping: if line.startswith(END_RE): skipping = False continue kept.append(line) body = '\n'.join(kept).strip() if body in ('', '[]'): body = '' out = (body + '\n\n' if body else '') + BLOCK # 校验:每个关键 id 恰好出现一次 checks = { 'workspace-scoped-picker': out.count('- id: workspace-scoped-picker'), 'ui-directory-picker-browse': out.count('- id: ui-directory-picker-browse'), 'directory-picker': out.count('- id: directory-picker'), } print(' %s: 移除旧平台段 %d 份 → %s' % (path.split('/')[-4][:8], removed, 'OK ' + str(checks) if all(v == 1 for v in checks.values()) else 'CHECK ' + str(checks))) if not dry: io.open(path, 'w', encoding='utf-8', newline='').write(out)