2024-04-20 22:24:20 +08:00
|
|
|
import os
|
2024-04-20 22:43:46 +08:00
|
|
|
import random
|
2024-04-20 22:24:20 +08:00
|
|
|
|
2024-04-20 21:51:11 +08:00
|
|
|
from subprocess import run
|
|
|
|
from pathlib import Path
|
|
|
|
|
2024-04-20 22:24:20 +08:00
|
|
|
ON_CF = os.getenv("CF_PAGES") == "1"
|
|
|
|
|
2024-04-20 22:37:30 +08:00
|
|
|
if ON_CF:
|
|
|
|
print("Running on Cloudflare Pages, trying to git fetch --all")
|
2024-04-20 22:51:06 +08:00
|
|
|
# add remote
|
|
|
|
run(
|
|
|
|
["git", "remote", "add", "origin", "https://github.com/shenjackyuanjie/fast-namerena.git"],
|
|
|
|
)
|
2024-04-20 22:46:53 +08:00
|
|
|
result = run(["git", "fetch", "--all"], check=False)
|
2024-04-20 22:51:06 +08:00
|
|
|
print(f"git fetch --all: {result}")
|
2024-04-20 22:37:30 +08:00
|
|
|
|
|
|
|
|
2024-04-20 21:51:11 +08:00
|
|
|
def get_env_info() -> dict[str, str]:
|
|
|
|
# 读取环境变量
|
|
|
|
env_info = {}
|
|
|
|
# git branch
|
2024-04-20 22:51:06 +08:00
|
|
|
if ON_CF:
|
|
|
|
branch = os.getenv("CF_PAGES_BRANCH") or "unknown"
|
|
|
|
else:
|
|
|
|
branch = run(
|
|
|
|
["git", "branch", "--show-current"],
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
|
|
|
encoding="utf-8",
|
|
|
|
).stdout
|
2024-04-20 22:24:20 +08:00
|
|
|
env_info["branch"] = branch.strip()
|
2024-04-20 21:51:11 +08:00
|
|
|
# git commit hash
|
2024-04-20 22:51:06 +08:00
|
|
|
if ON_CF:
|
|
|
|
commit = os.getenv("CF_PAGES_COMMIT_SHA") or "unknown"
|
|
|
|
else:
|
|
|
|
commit = run(
|
|
|
|
["git", "rev-parse", "HEAD"],
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
|
|
|
encoding="utf-8",
|
|
|
|
).stdout
|
2024-04-20 22:24:20 +08:00
|
|
|
env_info["commit"] = commit.strip()
|
2024-04-20 21:51:11 +08:00
|
|
|
# git commit message
|
|
|
|
message = run(
|
2024-04-20 22:24:20 +08:00
|
|
|
["git", "log", "-1", "--pretty=%B"],
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
|
|
|
encoding="utf-8",
|
2024-04-20 21:51:11 +08:00
|
|
|
)
|
|
|
|
env_info["message"] = message.stdout.strip()
|
|
|
|
# git tag
|
2024-04-20 22:51:06 +08:00
|
|
|
if ON_CF:
|
2024-04-20 22:53:42 +08:00
|
|
|
tag = "cf_pages" or run(
|
|
|
|
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
|
|
|
|
).stdout.split("-")[0]
|
2024-04-20 22:51:06 +08:00
|
|
|
else:
|
|
|
|
tag = run(
|
|
|
|
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
|
2024-04-20 22:53:42 +08:00
|
|
|
).stdout.split("-")[0]
|
2024-04-20 22:24:20 +08:00
|
|
|
env_info["tag"] = tag.strip()
|
2024-04-20 21:51:11 +08:00
|
|
|
return env_info
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# 虽然但是, 我还是决定用 python 写这个脚本
|
|
|
|
|
|
|
|
border_raw = "/* border: 2px solid marker_color */"
|
|
|
|
border_template = "border: 2px solid {};"
|
|
|
|
marker_raw = '<div id="version-marker" style="display: none;"></div>'
|
|
|
|
marker_template = '<div id="version-marker">{}</div>'
|
|
|
|
|
|
|
|
# 读取环境变量
|
|
|
|
env_info = get_env_info()
|
|
|
|
tag = env_info["tag"]
|
|
|
|
branch = env_info["branch"]
|
|
|
|
commit = env_info["commit"]
|
|
|
|
message = env_info["message"]
|
|
|
|
|
|
|
|
for file in Path.cwd().rglob("index.html"):
|
|
|
|
try:
|
|
|
|
with open(file, "r", encoding="utf-8") as f:
|
|
|
|
raw_content = f.read()
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
continue
|
|
|
|
print(f"Reading: {file}")
|
|
|
|
|
|
|
|
# 替换内容
|
|
|
|
# 首先判断是否是 /branch 目录下的 index.html
|
|
|
|
if "branch" in str(file):
|
2024-04-20 22:43:46 +08:00
|
|
|
# 如果是, 则将颜色替换为 random(这里是为了区分不同的分支, 并且颜色相对固定)
|
2024-04-20 21:51:11 +08:00
|
|
|
file_branch_name = file.parent.name
|
2024-04-20 22:43:46 +08:00
|
|
|
randomer = random.Random(file_branch_name)
|
|
|
|
hash_color = randomer.randint(0, 0xFFFFFF)
|
2024-04-20 21:51:11 +08:00
|
|
|
border = border_template.format(f"#{hash_color:06x}")
|
2024-04-20 22:24:20 +08:00
|
|
|
|
2024-04-20 21:51:11 +08:00
|
|
|
# git 信息:
|
2024-04-20 22:53:42 +08:00
|
|
|
version_info = f"{file_branch_name}/{branch}:{tag}-{commit[:6]}<br/>{message}"
|
2024-04-20 21:51:11 +08:00
|
|
|
marker = marker_template.format(version_info)
|
2024-04-20 22:24:20 +08:00
|
|
|
|
2024-04-20 21:51:11 +08:00
|
|
|
print(f"Branch: {file_branch_name}\n{border}\n{marker}\n")
|
|
|
|
|
|
|
|
else:
|
2024-04-20 22:43:46 +08:00
|
|
|
# 淡绿色!
|
|
|
|
border = border_template.format("greenyellow")
|
2024-04-20 21:51:11 +08:00
|
|
|
|
|
|
|
# git 信息:
|
2024-04-20 22:53:42 +08:00
|
|
|
version_info = f"{branch}:{tag}-{commit[:6]}"
|
2024-04-20 21:51:11 +08:00
|
|
|
marker = marker_template.format(version_info)
|
|
|
|
print(f"Master: {border}\n{marker}\n")
|
2024-04-20 22:24:20 +08:00
|
|
|
|
|
|
|
raw_content = raw_content.replace(border_raw, border).replace(
|
|
|
|
marker_raw, marker
|
|
|
|
)
|
2024-04-20 21:51:11 +08:00
|
|
|
|
|
|
|
# 写入文件
|
|
|
|
try:
|
|
|
|
with open(file, "w", encoding="utf-8") as f:
|
|
|
|
f.write(raw_content)
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error writing file: {e}")
|
|
|
|
continue
|