-
Notifications
You must be signed in to change notification settings - Fork 0
177 lines (151 loc) · 5.96 KB
/
source-sync.yml
File metadata and controls
177 lines (151 loc) · 5.96 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
name: source-sync
on:
workflow_dispatch:
permissions:
contents: read
concurrency:
group: source-sync-${{ github.repository }}-main
cancel-in-progress: true
jobs:
source-sync:
runs-on: ubuntu-latest
steps:
- name: Check out generator repository
uses: actions/checkout@v4
with:
repository: "Open-Inflation/engine-reverse-ide"
ref: "main"
path: logic
token: ${{ secrets.SOURCE_SYNC_TOKEN }}
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install generator dependencies
working-directory: logic
run: |
python -m pip install --upgrade pip
python -m pip install -r "msra_codegen/requirements.txt"
- name: Check out source branch
uses: actions/checkout@v4
with:
repository: ${{ github.repository }}
ref: "source"
path: source
token: ${{ secrets.SOURCE_SYNC_TOKEN }}
fetch-depth: 0
- name: Check out target branch
uses: actions/checkout@v4
with:
repository: ${{ github.repository }}
ref: "main"
path: target
token: ${{ secrets.SOURCE_SYNC_TOKEN }}
fetch-depth: 0
- name: Generate artifact into a staging tree
working-directory: logic
run: |
source_msra_path="fixprice.msra"
python -m msra_codegen generate "../source/$source_msra_path" -o ../generated
- name: Replace target tree contents
run: |
python - <<'PY'
from fnmatch import fnmatch
from pathlib import Path
import shutil
target = Path("target")
generated = Path("generated")
preserved_root = Path("preserved")
preserve_paths = ["tests/__snapshots__"]
ignored_patterns = ["**/__pycache__", "**/*.pyc"]
def is_ignored(relative_path: Path) -> bool:
relative_text = relative_path.as_posix()
return any(fnmatch(relative_text, pattern) for pattern in ignored_patterns)
def copy_tree(source_root: Path, destination_root: Path) -> None:
for item in source_root.iterdir():
relative_path = item.relative_to(source_root)
if is_ignored(relative_path):
continue
destination_path = destination_root / relative_path
if item.is_dir():
destination_path.mkdir(parents=True, exist_ok=True)
copy_tree(item, destination_path)
else:
destination_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(item, destination_path)
for relative_path in preserve_paths:
source_path = target / relative_path
if not source_path.exists():
raise RuntimeError(f'Preserved target path "{relative_path}" does not exist.')
destination_path = preserved_root / relative_path
destination_path.parent.mkdir(parents=True, exist_ok=True)
if source_path.is_dir():
shutil.copytree(source_path, destination_path)
else:
shutil.copy2(source_path, destination_path)
for child in list(target.iterdir()):
if child.name == ".git":
continue
if child.is_dir():
shutil.rmtree(child)
else:
child.unlink()
copy_tree(generated, target)
PY
- name: Install target project dependencies
working-directory: target
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements-dev.txt
- name: Validate generated project
working-directory: logic
run: |
python -m msra_codegen validate ../target
- name: Restore preserved artifacts and remove generated noise
run: |
python - <<'PY'
from fnmatch import fnmatch
from pathlib import Path
import shutil
target = Path("target")
preserved_root = Path("preserved")
preserve_paths = ["tests/__snapshots__"]
ignored_patterns = ["**/__pycache__", "**/*.pyc"]
def is_ignored(relative_path: Path) -> bool:
relative_text = relative_path.as_posix()
return any(fnmatch(relative_text, pattern) for pattern in ignored_patterns)
def remove_ignored(root: Path) -> None:
ignored_paths = sorted(
[path for path in root.rglob("*") if is_ignored(path.relative_to(root))],
key=lambda path: len(path.parts),
reverse=True,
)
for path in ignored_paths:
if path.is_dir():
shutil.rmtree(path)
else:
path.unlink()
for relative_path in preserve_paths:
source_path = preserved_root / relative_path
destination_path = target / relative_path
if source_path.is_dir():
shutil.copytree(source_path, destination_path, dirs_exist_ok=True)
else:
destination_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source_path, destination_path)
remove_ignored(target)
shutil.rmtree(preserved_root, ignore_errors=True)
PY
- name: Commit and push
working-directory: target
run: |
git config user.name "msra-sync-bot"
git config user.email "msra-sync-bot@users.noreply.github.com"
git add -A
if git diff --cached --quiet; then
echo "No generated changes to commit."
exit 0
fi
git commit -m "Regenerate artifact from source"
git push origin HEAD:main