scm-harmonic-cmts-admin/merge_code.py

96 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
# ==========================================
# 設定區
# ==========================================
# 輸出的合併檔案名稱
OUTPUT_FILE = "all_code.txt"
# ⚠️ 嚴格排除的資料夾 (完全不掃描這些目錄)
EXCLUDE_DIRS = {
"__pycache__",
".git",
".vscode", # VS Code 設定檔
".continue", # Continue.dev 設定檔
"cmts_api_env", # 🚨 Python 虛擬環境 (極度重要!絕對要排除)
"venv", # 其他常見的虛擬環境名稱
".venv",
"node_modules",
"dist",
"build"
}
# ⚠️ 排除的檔案副檔名 (不讀取這些格式)
EXCLUDE_EXTENSIONS = {
".png", ".jpg", ".jpeg", ".gif", ".ico", ".svg", # 圖片
".pyc", ".pyo", ".pyd", # Python 編譯檔
".exe", ".dll", ".so", ".dylib", # 執行檔/函式庫
".zip", ".tar", ".gz", ".rar", # 壓縮檔
".pdf", ".doc", ".docx", # 文件檔
".sqlite3", ".db" # 資料庫
}
# ⚠️ 排除的特定檔案名稱 (例如腳本自己、隱藏檔等)
EXCLUDE_FILES = {
"merge_code.py", # 排除這支腳本自己
"all_code.txt", # 排除輸出的檔案
".DS_Store", # Mac 系統檔
".clineignore", # AI 輔助工具的忽略檔
".clinerules", # AI 輔助工具的規則檔
".gitignore", # Git 忽略檔
"requirements.txt" # 依賴清單 (通常不需要給 AI 看,除非你要問套件問題)
}
def should_process_file(filename):
"""判斷該檔案是否應該被處理"""
if filename in EXCLUDE_FILES:
return False
ext = os.path.splitext(filename)[1].lower()
if ext in EXCLUDE_EXTENSIONS:
return False
return True
def merge_files():
# 取得當前腳本所在的目錄 (專案根目錄)
root_dir = os.path.dirname(os.path.abspath(__file__))
with open(OUTPUT_FILE, "w", encoding="utf-8") as outfile:
# 寫入一個總標題
outfile.write("=" * 80 + "\n")
outfile.write("PROJECT SOURCE CODE EXPORT\n")
outfile.write("=" * 80 + "\n\n")
# 走訪目錄
for dirpath, dirnames, filenames in os.walk(root_dir):
# 排除不需要的目錄 (原地修改 dirnames 列表os.walk 就不會進去)
dirnames[:] = [d for d in dirnames if d not in EXCLUDE_DIRS]
for filename in filenames:
if should_process_file(filename):
file_path = os.path.join(dirpath, filename)
# 計算相對路徑 (例如: routers/config.py)
rel_path = os.path.relpath(file_path, root_dir)
try:
with open(file_path, "r", encoding="utf-8") as infile:
content = infile.read()
# 寫入漂亮的檔名標籤
outfile.write("\n" + "=" * 80 + "\n")
outfile.write(f"FILE: {rel_path}\n")
outfile.write("=" * 80 + "\n")
# 寫入檔案內容
outfile.write(content)
outfile.write("\n")
print(f"✅ 已合併: {rel_path}")
except Exception as e:
print(f"❌ 讀取失敗 {rel_path}: {e}")
print(f"\n🎉 合併完成!所有程式碼已儲存至: {OUTPUT_FILE}")
if __name__ == "__main__":
merge_files()