-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
253 lines (202 loc) · 8.97 KB
/
build.py
File metadata and controls
253 lines (202 loc) · 8.97 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
"""Build script with module filtering support.
Provides canonical commands (compile, test-compile, module-tests, quality-gate, coverage, verify)
with optional module filtering similar to Maven's -pl flag.
Usage:
./pw build compile # All production sources
./pw build compile pm-dev-frontend # Single bundle
./pw build module-tests # All tests
./pw build module-tests plan-marshall # Single test directory
./pw build verify pm-dev-java # Full verification on single bundle
"""
import argparse
import os
import subprocess
import sys
from pathlib import Path
# Base paths
BUNDLES_DIR = Path('marketplace/bundles')
TEST_DIR = Path('test')
CLAUDE_DIR = Path('.claude')
# Single source of truth: delegate to collect_script_dirs so mypy_path matches runtime PYTHONPATH.
def _compute_mypypath() -> str:
bundles_root = Path(__file__).parent / 'marketplace' / 'bundles'
shared_scripts = str(bundles_root / 'plan-marshall' / 'skills' / 'script-shared' / 'scripts')
if shared_scripts not in sys.path:
sys.path.insert(0, shared_scripts)
from marketplace_bundles import collect_script_dirs
return os.pathsep.join(collect_script_dirs(bundles_root))
def run(cmd: list[str], description: str, env: dict[str, str] | None = None) -> int:
"""Run a command and return exit code."""
print(f'>>> {description}')
print(f' {" ".join(cmd)}')
result = subprocess.run(cmd, env=env)
return result.returncode
def get_bundle_path(module: str | None) -> str:
"""Get bundle path, optionally filtered by module."""
if module:
path = BUNDLES_DIR / module
if not path.exists():
print(f'Error: Bundle not found: {path}', file=sys.stderr)
sys.exit(1)
return str(path)
return str(BUNDLES_DIR)
def get_test_path(module: str | None) -> str:
"""Get test path, optionally filtered by module."""
if module:
path = TEST_DIR / module
if not path.exists():
print(f'Error: Test directory not found: {path}', file=sys.stderr)
sys.exit(1)
return str(path)
return str(TEST_DIR)
def cmd_compile(module: str | None) -> int:
"""Run mypy on production sources."""
path = get_bundle_path(module)
mypy_env = {**os.environ, 'MYPYPATH': _compute_mypypath()}
if module:
return run(['uv', 'run', 'mypy', path], f'compile: mypy {path}', env=mypy_env)
else:
paths = [path]
# Include .claude/ only if it exists and contains at least one .py file.
# Passing an empty directory makes mypy fail with "There are no .py[i]
# files in directory '.claude'" (exit 2), which breaks CI whenever the
# repo happens to not ship any top-level skill scripts there.
if CLAUDE_DIR.exists() and any(CLAUDE_DIR.rglob('*.py')):
paths.append(str(CLAUDE_DIR))
return run(['uv', 'run', 'mypy'] + paths, f'compile: mypy {" ".join(paths)}', env=mypy_env)
def cmd_test_compile(module: str | None) -> int:
"""Run mypy on test sources."""
path = get_test_path(module)
mypy_env = {**os.environ, 'MYPYPATH': _compute_mypypath()}
return run(['uv', 'run', 'mypy', path], f'test-compile: mypy {path}', env=mypy_env)
def cmd_module_tests(module: str | None, parallel: bool = False) -> int:
"""Run pytest on test sources."""
path = get_test_path(module)
cmd = ['uv', 'run', 'pytest', path]
if parallel:
cmd.extend(['-n', 'auto'])
return run(cmd, f'module-tests: pytest {path}')
def cmd_quality_gate(module: str | None) -> int:
"""Run mypy + ruff + plugin-doctor static-analysis on production sources.
For full-tree quality-gate (module is None), also runs the plugin-doctor
quality-gate subcommand which enforces marketplace-wide static-analysis
invariants (argparse safety, extension-point contracts, argument-naming
cluster). Module-scoped quality-gate skips the marketplace-wide sweep
because it is scoped to a single bundle.
"""
exit_code = cmd_compile(module)
if exit_code != 0:
return exit_code
bundle_path = get_bundle_path(module)
test_path = get_test_path(module) if module else str(TEST_DIR)
# If module specified, only check that module's bundle and tests
if module:
paths = [bundle_path]
if Path(test_path).exists():
paths.append(test_path)
else:
# Include .claude/ scripts when running full quality-gate
paths = [str(BUNDLES_DIR), str(TEST_DIR), str(CLAUDE_DIR)]
exit_code = run(['uv', 'run', 'ruff', 'check'] + paths, f'quality-gate: ruff check {" ".join(paths)}')
if exit_code != 0:
return exit_code
if module is None:
doctor_script = (
BUNDLES_DIR / 'pm-plugin-development' / 'skills' / 'plugin-doctor'
/ 'scripts' / 'doctor-marketplace.py'
)
doctor_env = {**os.environ, 'PYTHONPATH': _compute_mypypath()}
exit_code = run(
['python3', str(doctor_script), 'quality-gate'],
'quality-gate: plugin-doctor static-analysis (marketplace-wide invariants)',
env=doctor_env,
)
return exit_code
def cmd_coverage(module: str | None) -> int:
"""Run pytest with coverage."""
test_path = get_test_path(module)
bundle_path = get_bundle_path(module)
# Ensure output directory exists
Path('.plan/temp').mkdir(parents=True, exist_ok=True)
cmd = [
'uv', 'run', 'pytest', test_path,
f'--cov={bundle_path}',
'--cov-report=html:.plan/temp/htmlcov'
]
return run(cmd, f'coverage: pytest {test_path} --cov={bundle_path}')
def cmd_verify(module: str | None) -> int:
"""Run full verification: quality-gate + module-tests."""
print(f'=== verify: {"all" if not module else module} ===')
exit_code = cmd_quality_gate(module)
if exit_code != 0:
print('verify: quality-gate failed', file=sys.stderr)
return exit_code
exit_code = cmd_module_tests(module)
if exit_code != 0:
print('verify: module-tests failed', file=sys.stderr)
return exit_code
print('=== verify: SUCCESS ===')
return 0
def cmd_clean() -> int:
"""Clean build artifacts."""
dirs = ['.venv', '.pytest_cache', '.mypy_cache', '.ruff_cache', '.plan/temp']
for d in dirs:
path = Path(d)
if path.exists():
print(f'Removing {d}')
import shutil
shutil.rmtree(path)
return 0
def main():
parser = argparse.ArgumentParser(
description='Build script with module filtering (canonical commands from extension_base.py)',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
Examples:
%(prog)s compile # mypy marketplace/bundles/
%(prog)s compile pm-dev-frontend # mypy marketplace/bundles/pm-dev-frontend
%(prog)s module-tests # pytest test/
%(prog)s module-tests plan-marshall # pytest test/plan-marshall
%(prog)s verify pm-dev-java # Full verification on single bundle
'''
)
subparsers = parser.add_subparsers(dest='command', required=True)
# compile
p = subparsers.add_parser('compile', help='mypy on production sources')
p.add_argument('module', nargs='?', help='Bundle name (e.g., pm-dev-frontend)')
# test-compile
p = subparsers.add_parser('test-compile', help='mypy on test sources')
p.add_argument('module', nargs='?', help='Test directory (e.g., plan-marshall)')
# module-tests
p = subparsers.add_parser('module-tests', help='pytest on test sources')
p.add_argument('module', nargs='?', help='Test directory (e.g., plan-marshall)')
p.add_argument('--parallel', '-p', action='store_true', help='Run tests in parallel')
# quality-gate
p = subparsers.add_parser('quality-gate', help='mypy + ruff check on sources')
p.add_argument('module', nargs='?', help='Module name (e.g., pm-dev-frontend)')
# coverage
p = subparsers.add_parser('coverage', help='pytest with coverage')
p.add_argument('module', nargs='?', help='Module name (e.g., pm-dev-frontend)')
# verify
p = subparsers.add_parser('verify', help='Full verification (quality-gate + module-tests)')
p.add_argument('module', nargs='?', help='Module name (e.g., pm-dev-frontend)')
# clean
subparsers.add_parser('clean', help='Remove build artifacts')
args = parser.parse_args()
if args.command == 'compile':
sys.exit(cmd_compile(args.module))
elif args.command == 'test-compile':
sys.exit(cmd_test_compile(args.module))
elif args.command == 'module-tests':
sys.exit(cmd_module_tests(args.module, getattr(args, 'parallel', False)))
elif args.command == 'quality-gate':
sys.exit(cmd_quality_gate(args.module))
elif args.command == 'coverage':
sys.exit(cmd_coverage(args.module))
elif args.command == 'verify':
sys.exit(cmd_verify(args.module))
elif args.command == 'clean':
sys.exit(cmd_clean())
if __name__ == '__main__':
main()