forked from DOG729/wwm_russian
-
Notifications
You must be signed in to change notification settings - Fork 2
161 lines (133 loc) · 5.39 KB
/
compile-on-merge.yml
File metadata and controls
161 lines (133 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Compila variáveis {{VAR}} para valores reais ao fazer merge para main
#
# REPOSITÓRIO: wwm_brasileiro_auto_path
#
# FLUXO:
# 1. Dev branch tem pt-br.tsv com variáveis {{JIANGHU}}, {{QINGGONG}}...
# 2. Quando faz merge dev → main, esta action executa
# 3. Busca glossary.json do repositório wwm_brasileiro
# 4. Substitui as variáveis pelos valores reais
# 5. Commit automático no main com o arquivo compilado
#
# IMPORTANTE: Coloque este arquivo em wwm_brasileiro_auto_path/.github/workflows/
name: Compile Translations on Merge
on:
push:
branches:
- main
paths:
- 'pt-br.tsv'
# ========== FILA AUTOMÁTICA ==========
# Evita conflitos quando múltiplos merges são feitos em sequência
concurrency:
group: compile-translations-queue
cancel-in-progress: false
jobs:
compile:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout wwm_brasileiro_auto_path
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download glossary.json from wwm_brasileiro
run: |
echo "📥 Baixando glossary.json do repositório wwm_brasileiro..."
curl -sSL "https://raw.githubusercontent.com/rodrigomiquilino/wwm_brasileiro/main/docs/glossary.json" -o glossary.json
if [ ! -f glossary.json ]; then
echo "❌ Falha ao baixar glossary.json"
exit 1
fi
echo "✅ Glossário baixado com sucesso"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Check for variables in pt-br.tsv
id: check-vars
run: |
if grep -q '{{[A-Z_0-9]*}}' pt-br.tsv 2>/dev/null; then
echo "has_variables=true" >> $GITHUB_OUTPUT
echo "✅ Variáveis encontradas - compilação necessária"
else
echo "has_variables=false" >> $GITHUB_OUTPUT
echo "ℹ️ Nenhuma variável encontrada - pulando compilação"
fi
- name: Compile translations
if: steps.check-vars.outputs.has_variables == 'true'
run: |
python << 'EOF'
import json
import re
# Carregar glossário (baixado do wwm_brasileiro)
with open('glossary.json', 'r', encoding='utf-8') as f:
glossary = json.load(f)
# Criar mapa de variáveis
variable_map = {}
for term in glossary.get('terms', []):
var_name = f"{{{{{term['id'].upper().replace('-', '_')}}}}}"
variable_map[var_name] = term['translation']
print(f"📚 Carregadas {len(variable_map)} variáveis do glossário")
# Ler pt-br.tsv
with open('pt-br.tsv', 'r', encoding='utf-8') as f:
content = f.read()
# Contar linhas originais
original_lines = len(content.split('\n'))
# Substituir variáveis
replaced_count = 0
unknown_vars = set()
def replace_var(match):
global replaced_count
full_var = match.group(0)
if full_var in variable_map:
replaced_count += 1
return variable_map[full_var]
else:
unknown_vars.add(full_var)
return full_var
compiled = re.sub(r'\{\{([A-Z_0-9]+)\}\}', replace_var, content)
# Salvar arquivo compilado
with open('pt-br.tsv', 'w', encoding='utf-8') as f:
f.write(compiled)
print(f"✅ Compilação concluída!")
print(f" 📄 {original_lines:,} linhas processadas")
print(f" 🔄 {replaced_count:,} variáveis substituídas")
if unknown_vars:
print(f" ⚠️ {len(unknown_vars)} variáveis não encontradas:")
for v in sorted(unknown_vars)[:10]:
print(f" - {v}")
EOF
- name: Cleanup glossary
run: rm -f glossary.json
- name: Check for changes
id: git-check
run: |
git diff --quiet pt-br.tsv && echo "changed=false" >> $GITHUB_OUTPUT || echo "changed=true" >> $GITHUB_OUTPUT
- name: Commit compiled translations
if: steps.git-check.outputs.changed == 'true'
run: |
git config user.name "GitHub Actions Bot"
git config user.email "actions@github.com"
git add pt-br.tsv
git commit -m "🔄 Auto-compile: variáveis {{VAR}} substituídas por valores reais
Glossário usado: rodrigomiquilino/wwm_brasileiro/docs/glossary.json
Compilado automaticamente via GitHub Actions"
# ========== PUSH COM RETRY ==========
for attempt in 1 2 3; do
echo "📤 Tentativa $attempt de push..."
git pull --rebase origin main 2>/dev/null || true
if git push origin main; then
echo "✅ Push realizado com sucesso!"
break
else
if [ $attempt -lt 3 ]; then
echo "⚠️ Falha no push, aguardando $((attempt * 2))s..."
sleep $((attempt * 2))
else
echo "❌ Falha após 3 tentativas"
exit 1
fi
fi
done