-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
executable file
·494 lines (385 loc) · 13.8 KB
/
Copy pathtasks.py
File metadata and controls
executable file
·494 lines (385 loc) · 13.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env python3
"""
Build task runner for http-tun project.
Usage:
python tasks.py <task> [options]
./tasks.py <task> [options]
Tasks:
dev Start development servers (UI + Tauri)
build Build production release
test Run all tests
lint Run linters
format Format code
clean Clean build artifacts
install Install the application
release Build and package for release
Options:
--upx Compress binaries with UPX (~70% smaller, adds startup latency)
--debug Build in debug mode (no release optimizations)
-v Verbose output
"""
import argparse
import os
import shutil
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
# Project paths
ROOT = Path(__file__).parent.resolve()
UI_DIR = ROOT / "ui"
TAURI_DIR = ROOT / "crates" / "tauri-app"
SCRIPTS_DIR = ROOT / "scripts"
TARGET_DIR = ROOT / "target"
@dataclass
class Config:
"""Build configuration."""
verbose: bool = False
release: bool = True
parallel: bool = True
upx: bool = False # Enable UPX compression
def run(
cmd: list[str],
cwd: Optional[Path] = None,
env: Optional[dict] = None,
check: bool = True,
capture: bool = False,
) -> subprocess.CompletedProcess:
"""Run a command with proper error handling."""
merged_env = {**os.environ, **(env or {})}
cwd = cwd or ROOT
if capture:
return subprocess.run(
cmd,
cwd=cwd,
env=merged_env,
check=check,
capture_output=True,
text=True,
)
result = subprocess.run(cmd, cwd=cwd, env=merged_env, check=False)
if check and result.returncode != 0:
print(f"\n❌ Command failed: {' '.join(cmd)}")
sys.exit(result.returncode)
return result
def has_command(cmd: str) -> bool:
"""Check if a command is available."""
return shutil.which(cmd) is not None
def ensure_command(cmd: str, install_hint: str = "") -> None:
"""Ensure a command is available, exit with helpful message if not."""
if not has_command(cmd):
msg = f"❌ Required command not found: {cmd}"
if install_hint:
msg += f"\n Install: {install_hint}"
print(msg)
sys.exit(1)
# =============================================================================
# Tasks
# =============================================================================
def task_dev(cfg: Config) -> None:
"""Start development servers."""
ensure_command("bun", "https://bun.sh")
ensure_command("cargo")
print("🚀 Starting development servers...")
print(" UI: http://localhost:5173")
print(" Tauri: bunx tauri dev\n")
# Start UI dev server in background
ui_proc = subprocess.Popen(
["bun", "run", "dev"],
cwd=UI_DIR,
env={**os.environ, "FORCE_COLOR": "1"},
)
try:
# Start Tauri dev from root (finds config in crates/tauri-app/)
run(["bunx", "@tauri-apps/cli", "dev"], cwd=ROOT)
finally:
ui_proc.terminate()
ui_proc.wait()
def compress_with_upx(binary_path: Path, cfg: Config) -> bool:
"""Compress a binary with UPX. Returns True if successful."""
if not has_command("upx"):
return False
if not binary_path.exists():
return False
original_size = binary_path.stat().st_size
print(f" Compressing {binary_path.name}...")
# Use --best for maximum compression, --lzma for better ratio
args = ["upx", "--best", "--lzma", str(binary_path)]
if cfg.verbose:
args.insert(1, "-v")
else:
args.insert(1, "-q")
result = run(args, check=False, capture=True)
if result.returncode != 0:
print(f" ⚠️ UPX failed for {binary_path.name}")
return False
new_size = binary_path.stat().st_size
reduction = (1 - new_size / original_size) * 100
print(f" ✓ {original_size / 1024 / 1024:.1f}MB → {new_size / 1024 / 1024:.1f}MB ({reduction:.0f}% smaller)")
return True
def task_build(cfg: Config) -> None:
"""Build production release."""
ensure_command("bun", "https://bun.sh")
ensure_command("cargo")
print("📦 Building production release...")
# Build UI
print("\n→ Building UI...")
run(["bun", "run", "build"], cwd=UI_DIR)
# Build Tauri app from root (finds config in crates/tauri-app/)
print("\n→ Building Tauri app...")
args = ["bunx", "@tauri-apps/cli", "build"]
if cfg.verbose:
args.append("--verbose")
# NO_STRIP: linuxdeploy's bundled strip is too old for modern glibc
# APPIMAGE_EXTRACT_AND_RUN: works around FUSE issues on some systems
run(args, cwd=ROOT, env={
"NO_STRIP": "1",
"APPIMAGE_EXTRACT_AND_RUN": "1",
})
# UPX compression (optional)
if cfg.upx:
print("\n→ Compressing with UPX...")
if not has_command("upx"):
print(" ⚠️ UPX not found, skipping compression")
print(" Install: sudo pacman -S upx # or apt install upx-ucl")
else:
desktop_bin = TARGET_DIR / "release" / "http-tun-desktop"
compress_with_upx(desktop_bin, cfg)
print("\n✅ Build complete!")
print(f" Output: {TARGET_DIR / 'release' / 'bundle'}")
def task_build_cli(cfg: Config) -> None:
"""Build CLI tools only."""
ensure_command("cargo")
print("📦 Building CLI...")
args = ["cargo", "build", "-p", "proxyvpn", "-p", "proxyvpn-cli"]
if cfg.release:
args.append("--release")
if cfg.verbose:
args.append("--verbose")
run(args, cwd=ROOT)
# UPX compression (optional)
if cfg.upx and cfg.release:
print("\n→ Compressing with UPX...")
if not has_command("upx"):
print(" ⚠️ UPX not found, skipping compression")
print(" Install: sudo pacman -S upx # or apt install upx-ucl")
else:
for binary in ["proxyvpn", "proxyvpn-cli"]:
bin_path = TARGET_DIR / "release" / binary
compress_with_upx(bin_path, cfg)
print("\n✅ CLI build complete!")
def task_test(cfg: Config) -> None:
"""Run all tests."""
ensure_command("cargo")
ensure_command("bun", "https://bun.sh")
print("🧪 Running tests...\n")
# Rust tests
print("→ Rust unit tests...")
run(["cargo", "test", "--workspace"], cwd=ROOT)
# UI tests/lint
print("\n→ UI lint...")
run(["bun", "run", "lint"], cwd=UI_DIR)
print("\n✅ All tests passed!")
def task_test_privileged(cfg: Config) -> None:
"""Run privileged tests (requires root)."""
ensure_command("cargo")
if os.geteuid() != 0:
print("🔐 Elevating to root...")
os.execvp("sudo", ["sudo", "-E", sys.executable, __file__, "test:privileged"])
print("🧪 Running privileged tests...\n")
env = {
"PROXYVPN_PRIV_TESTS_ALLOW_FIREWALL": "1",
"PROXYVPN_PRIV_TESTS_ALLOW_MARK": "1",
"PROXYVPN_PRIV_TESTS_ALLOW_NETLINK": "1",
"PROXYVPN_PRIV_TESTS_ALLOW_DNS": "1",
}
packages = [
"proxyvpn-firewall",
"proxyvpn-mark",
"proxyvpn-netlink",
"proxyvpn-selftest",
]
for pkg in packages:
print(f"→ Testing {pkg}...")
run(
[
"cargo",
"test",
"-p",
pkg,
"--features",
"privileged-tests",
"--",
"--ignored",
],
cwd=ROOT,
env=env,
)
print("\n✅ Privileged tests passed!")
def task_test_e2e(cfg: Config) -> None:
"""Run end-to-end tests."""
proxy_url = os.environ.get("PROXYVPN_E2E_PROXY_URL")
if not proxy_url:
print("❌ PROXYVPN_E2E_PROXY_URL is required")
print(
" Example: PROXYVPN_E2E_PROXY_URL=http://user:pass@proxy:8080 ./tasks.py test:e2e"
)
sys.exit(1)
# Run the Python e2e script (handles root elevation internally)
run([sys.executable, str(SCRIPTS_DIR / "test_e2e.py")], cwd=ROOT)
def task_lint(cfg: Config) -> None:
"""Run all linters."""
ensure_command("cargo")
ensure_command("bun", "https://bun.sh")
print("🔍 Running linters...\n")
# Rust
print("→ Cargo clippy...")
run(["cargo", "clippy", "--workspace", "--", "-D", "warnings"], cwd=ROOT)
print("\n→ Cargo fmt check...")
run(["cargo", "fmt", "--check"], cwd=ROOT)
# UI
print("\n→ Biome check...")
run(["bun", "run", "lint"], cwd=UI_DIR)
print("\n✅ All linters passed!")
def task_format(cfg: Config) -> None:
"""Format all code."""
ensure_command("cargo")
ensure_command("bun", "https://bun.sh")
print("✨ Formatting code...\n")
# Rust
print("→ Cargo fmt...")
run(["cargo", "fmt"], cwd=ROOT)
# UI
print("→ Biome format...")
run(["bun", "run", "format"], cwd=UI_DIR)
print("\n✅ Formatting complete!")
def task_clean(cfg: Config) -> None:
"""Clean build artifacts."""
print("🧹 Cleaning build artifacts...\n")
# Cargo clean
if TARGET_DIR.exists():
print(f"→ Removing {TARGET_DIR}...")
run(["cargo", "clean"], cwd=ROOT)
# UI dist
ui_dist = UI_DIR / "dist"
if ui_dist.exists():
print(f"→ Removing {ui_dist}...")
shutil.rmtree(ui_dist)
# Node modules (optional, only if --full)
print("\n✅ Clean complete!")
def task_deps_ui(cfg: Config) -> None:
"""Install UI (npm) dependencies."""
ensure_command("bun", "https://bun.sh")
print("📥 Installing UI dependencies...\n")
run(["bun", "install"], cwd=UI_DIR)
print("\n✅ UI dependencies installed!")
def task_release(cfg: Config) -> None:
"""Build release packages."""
print("📦 Building release packages...\n")
# Clean first
task_clean(cfg)
# Build
task_build(cfg)
# Find bundles
bundle_dir = TARGET_DIR / "release" / "bundle"
if bundle_dir.exists():
print("\n📦 Release packages:")
for item in bundle_dir.iterdir():
if item.is_dir():
for pkg in item.iterdir():
size = pkg.stat().st_size / (1024 * 1024)
print(f" {pkg.name} ({size:.1f} MB)")
print("\n✅ Release complete!")
def task_debug_collect(cfg: Config) -> None:
"""Collect debug information."""
# Run the Python debug script (handles root elevation internally)
run([sys.executable, str(SCRIPTS_DIR / "collect_debug.py")], cwd=ROOT)
def task_install(cfg: Config) -> None:
"""Install http-tun to system."""
# Detect target dir (respect CARGO_TARGET_DIR)
target_dir = os.environ.get("CARGO_TARGET_DIR", str(TARGET_DIR))
# Run the Python install script (skip deps since they can be installed separately)
run(
[
"sudo", sys.executable, str(SCRIPTS_DIR / "install.py"),
"--no-deps", "--target-dir", target_dir,
],
cwd=ROOT,
)
def task_install_deps(cfg: Config) -> None:
"""Install build dependencies only."""
run(
["sudo", sys.executable, str(SCRIPTS_DIR / "install.py"), "--deps-only"],
cwd=ROOT,
)
def task_uninstall(cfg: Config) -> None:
"""Uninstall http-tun from system."""
run(
["sudo", sys.executable, str(SCRIPTS_DIR / "install.py"), "--uninstall"],
cwd=ROOT,
)
# =============================================================================
# Task Registry
# =============================================================================
TASKS = {
# Development
"dev": (task_dev, "Start development servers"),
"build": (task_build, "Build production release"),
"build:cli": (task_build_cli, "Build CLI tools only"),
# Testing
"test": (task_test, "Run all tests"),
"test:privileged": (task_test_privileged, "Run privileged tests (requires root)"),
"test:e2e": (task_test_e2e, "Run end-to-end tests"),
# Code quality
"lint": (task_lint, "Run all linters"),
"format": (task_format, "Format all code"),
# Installation
"install": (task_install, "Install to system (requires sudo)"),
"install:deps": (task_install_deps, "Install system build dependencies"),
"uninstall": (task_uninstall, "Uninstall from system"),
"deps:ui": (task_deps_ui, "Install UI (npm) dependencies"),
# Maintenance
"clean": (task_clean, "Clean build artifacts"),
"release": (task_release, "Build release packages"),
"debug:collect": (task_debug_collect, "Collect debug information"),
}
def print_help() -> None:
"""Print help message."""
print(__doc__)
print("Available tasks:")
max_len = max(len(name) for name in TASKS)
for name, (_, desc) in TASKS.items():
print(f" {name:<{max_len + 2}} {desc}")
print()
def main() -> None:
parser = argparse.ArgumentParser(
description="Build task runner for http-tun",
add_help=False,
)
parser.add_argument("task", nargs="?", help="Task to run")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
parser.add_argument("-h", "--help", action="store_true", help="Show help")
parser.add_argument("--debug", action="store_true", help="Debug build (no release)")
parser.add_argument("--upx", action="store_true", help="Compress binaries with UPX")
args = parser.parse_args()
if args.help or not args.task:
print_help()
sys.exit(0)
if args.task not in TASKS:
print(f"❌ Unknown task: {args.task}")
print(" Run './tasks.py --help' for available tasks")
sys.exit(1)
cfg = Config(
verbose=args.verbose,
release=not args.debug,
upx=args.upx,
)
task_fn, _ = TASKS[args.task]
try:
task_fn(cfg)
except KeyboardInterrupt:
print("\n\n⚠️ Interrupted")
sys.exit(130)
if __name__ == "__main__":
main()