rsbuild 0.5.1

A self-sufficient runtime to build projects
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Python project management commands.

use crate::cli::{ExecContext, PythonAction};
use crate::error::{Result, RsbuildError};
use crate::executor::{confirm_overwrite, print_status, print_warning};
use std::fs;
use std::path::Path;

/// Execute Python command.
pub fn run(action: PythonAction, ctx: &ExecContext) -> Result<()> {
    match action {
        PythonAction::Init { name, no_tests, no_devcontainer } => {
            init_project(name, no_tests, no_devcontainer, ctx)
        }
        PythonAction::SyncVersion => sync_version(ctx),
    }
}

/// Get the project name from the current directory or provided name.
fn get_project_name(name: Option<String>) -> Result<String> {
    if let Some(n) = name {
        return Ok(sanitize_package_name(&n));
    }

    let cwd = std::env::current_dir().map_err(|e| RsbuildError::ExecutionFailed(e.to_string()))?;
    let dir_name = cwd
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| RsbuildError::ExecutionFailed("Cannot determine project name".to_string()))?;

    Ok(sanitize_package_name(dir_name))
}

/// Sanitize a string to be a valid Python package name.
fn sanitize_package_name(name: &str) -> String {
    name.to_lowercase()
        .replace('-', "_")
        .replace(' ', "_")
        .chars()
        .filter(|c| c.is_alphanumeric() || *c == '_')
        .collect()
}

/// Write a file if it doesn't exist or user confirms overwrite.
fn write_file(path: &Path, content: &str, ctx: &ExecContext) -> Result<bool> {
    if !confirm_overwrite(path, ctx) {
        print_warning(&format!("Skipping: {}", path.display()), ctx);
        return Ok(false);
    }

    if ctx.dry_run {
        print_status(&format!("Would create: {}", path.display()), ctx);
        return Ok(true);
    }

    // Create parent directories if needed
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|e| RsbuildError::Io(e))?;
    }

    fs::write(path, content).map_err(|e| RsbuildError::Io(e))?;
    print_status(&format!("Created: {}", path.display()), ctx);
    Ok(true)
}

/// Initialize a new Python project.
fn init_project(name: Option<String>, no_tests: bool, no_devcontainer: bool, ctx: &ExecContext) -> Result<()> {
    let project_name = get_project_name(name)?;
    print_status(&format!("Initializing Python project: {}", project_name), ctx);

    // Create pyproject.toml
    write_file(
        Path::new("pyproject.toml"),
        &generate_pyproject_toml(&project_name),
        ctx,
    )?;

    // Create README.md
    write_file(
        Path::new("README.md"),
        &generate_readme(&project_name),
        ctx,
    )?;

    // Create .pre-commit-config.yaml
    write_file(
        Path::new(".pre-commit-config.yaml"),
        PRECOMMIT_CONFIG,
        ctx,
    )?;

    // Create .gitignore
    write_file(
        Path::new(".gitignore"),
        GITIGNORE,
        ctx,
    )?;

    // Create Taskfile.yml
    write_file(
        Path::new("Taskfile.yml"),
        &generate_taskfile(&project_name),
        ctx,
    )?;

    // Create package directory
    let pkg_dir = Path::new(&project_name);
    fs::create_dir_all(pkg_dir).ok();

    // Create __init__.py
    write_file(
        &pkg_dir.join("__init__.py"),
        &generate_init_py(&project_name),
        ctx,
    )?;

    // Create py.typed marker
    write_file(
        &pkg_dir.join("py.typed"),
        "",
        ctx,
    )?;

    // Create tests if not disabled
    if !no_tests {
        let tests_dir = pkg_dir.join("tests");
        fs::create_dir_all(&tests_dir).ok();

        write_file(
            &tests_dir.join("__init__.py"),
            "",
            ctx,
        )?;

        write_file(
            &tests_dir.join("test_version.py"),
            &generate_test_version(&project_name),
            ctx,
        )?;
    }

    // Create devcontainer if not disabled
    if !no_devcontainer {
        let devcontainer_dir = Path::new(".devcontainer");
        fs::create_dir_all(devcontainer_dir).ok();

        write_file(
            &devcontainer_dir.join("devcontainer.json"),
            &generate_devcontainer(&project_name),
            ctx,
        )?;

        write_file(
            &devcontainer_dir.join("Dockerfile"),
            DEVCONTAINER_DOCKERFILE,
            ctx,
        )?;
    }

    // Create Dockerfile
    write_file(
        Path::new("Dockerfile"),
        &generate_dockerfile(&project_name),
        ctx,
    )?;

    // Create docker-compose.yml
    write_file(
        Path::new("docker-compose.yml"),
        &generate_docker_compose(&project_name),
        ctx,
    )?;

    print_status("Python project initialized successfully!", ctx);
    print_status("Next steps:", ctx);
    println!("  1. Review and customize pyproject.toml");
    println!("  2. Install dependencies: uv sync");
    println!("  3. Install pre-commit hooks: pre-commit install");
    println!("  4. Run tests: task test");

    Ok(())
}

/// Sync version from pyproject.toml to package __init__.py.
fn sync_version(ctx: &ExecContext) -> Result<()> {
    print_status("Syncing version from pyproject.toml", ctx);

    // Read pyproject.toml
    let pyproject_path = Path::new("pyproject.toml");
    if !pyproject_path.exists() {
        return Err(RsbuildError::PathNotFound {
            path: pyproject_path.to_path_buf(),
        });
    }

    let pyproject_content = fs::read_to_string(pyproject_path)
        .map_err(|e| RsbuildError::Io(e))?;

    // Extract version and name
    let version = extract_toml_value(&pyproject_content, "version")
        .ok_or_else(|| RsbuildError::ExecutionFailed("Cannot find version in pyproject.toml".to_string()))?;
    let name = extract_toml_value(&pyproject_content, "name")
        .ok_or_else(|| RsbuildError::ExecutionFailed("Cannot find name in pyproject.toml".to_string()))?;

    let pkg_name = sanitize_package_name(&name);

    // Generate new __init__.py content
    let init_path = Path::new(&pkg_name).join("__init__.py");
    if !init_path.exists() {
        return Err(RsbuildError::PathNotFound {
            path: init_path.clone(),
        });
    }

    // Read current init.py to preserve any custom code after the header
    let current_content = fs::read_to_string(&init_path)
        .map_err(|e| RsbuildError::Io(e))?;

    // Find the end of the managed section (after __all__)
    let new_header = generate_init_py_header(&pkg_name, &version);

    // Check if there's custom code after __all__
    let custom_code = if let Some(pos) = current_content.find("__all__") {
        if let Some(end_pos) = current_content[pos..].find('\n') {
            let after_all = &current_content[pos + end_pos + 1..];
            if after_all.trim().is_empty() {
                String::new()
            } else {
                format!("\n{}", after_all)
            }
        } else {
            String::new()
        }
    } else {
        String::new()
    };

    let new_content = format!("{}{}", new_header, custom_code);

    if ctx.dry_run {
        print_status(&format!("Would update {} to version {}", init_path.display(), version), ctx);
        return Ok(());
    }

    fs::write(&init_path, new_content).map_err(|e| RsbuildError::Io(e))?;
    print_status(&format!("Updated {} to version {}", init_path.display(), version), ctx);

    Ok(())
}

/// Extract a value from TOML content (simple parser for common cases).
fn extract_toml_value(content: &str, key: &str) -> Option<String> {
    for line in content.lines() {
        let line = line.trim();
        if line.starts_with(&format!("{} ", key)) || line.starts_with(&format!("{}=", key)) {
            if let Some(value) = line.split('=').nth(1) {
                let value = value.trim().trim_matches('"').trim_matches('\'');
                return Some(value.to_string());
            }
        }
    }
    None
}

/// Generate the header portion of __init__.py.
fn generate_init_py_header(project_name: &str, version: &str) -> String {
    // Generate build timestamp
    let build = chrono_lite_now();

    format!(
        r#""""{project_name} - A Python package."""

__version__ = "{version}"
__build__ = "{build}"

__all__ = ["__version__", "__build__"]
"#,
        project_name = project_name,
        version = version,
        build = build
    )
}

/// Get current timestamp in a simple format (without external deps).
fn chrono_lite_now() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();

    let secs = duration.as_secs();

    // Simple date calculation (approximate, good enough for build timestamps)
    let days = secs / 86400;
    let years_since_1970 = days / 365;
    let year = 1970 + years_since_1970;

    let remaining_days = days % 365;
    let month = (remaining_days / 30) + 1;
    let day = (remaining_days % 30) + 1;

    let day_secs = secs % 86400;
    let hour = day_secs / 3600;
    let minute = (day_secs % 3600) / 60;
    let second = day_secs % 60;

    format!(
        "{:04}{:02}{:02}{:02}{:02}{:02}",
        year, month.min(12), day.min(31), hour, minute, second
    )
}

// =============================================================================
// TEMPLATES
// =============================================================================

fn generate_pyproject_toml(project_name: &str) -> String {
    format!(
        r#"[project]
name = "{project_name}"
version = "0.1.0"
description = "A Python package"
readme = "README.md"
requires-python = ">=3.10"
license = "MIT"
keywords = []
classifiers = [
    "Development Status :: 3 - Alpha",
    "Intended Audience :: Developers",
    "License :: OSI Approved :: MIT License",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3.11",
    "Programming Language :: Python :: 3.12",
]
dependencies = []

[project.optional-dependencies]
dev = [
    "pytest>=8.0",
    "pytest-cov>=4.0",
    "ruff>=0.4",
    "mypy>=1.0",
    "pre-commit>=3.0",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["{project_name}"]

[tool.ruff]
line-length = 120
target-version = "py310"

[tool.ruff.lint]
select = ["E", "F", "I", "N", "W", "UP", "B", "C4", "SIM"]
ignore = ["E501"]

[tool.mypy]
python_version = "3.10"
strict = true
warn_return_any = true
warn_unused_configs = true

[tool.pytest.ini_options]
testpaths = ["{project_name}/tests"]
addopts = "-v --cov={project_name} --cov-report=term-missing"

[tool.coverage.run]
source = ["{project_name}"]
omit = ["{project_name}/tests/*"]
"#,
        project_name = project_name
    )
}

fn generate_readme(project_name: &str) -> String {
    format!(
        r#"# {project_name}

A Python package.

## Installation

```bash
pip install {project_name}
```

## Development

```bash
# Install dependencies
uv sync --all-extras

# Run tests
task test

# Build wheel
task build

# Lint and format
task lint
```

## License

MIT
"#,
        project_name = project_name
    )
}

fn generate_init_py(project_name: &str) -> String {
    generate_init_py_header(project_name, "0.1.0")
}

fn generate_test_version(project_name: &str) -> String {
    format!(
        r#"""Tests for version information."""

from {project_name} import __build__, __version__


def test_version_exists() -> None:
    """Test that version is defined."""
    assert __version__ is not None
    assert isinstance(__version__, str)


def test_build_exists() -> None:
    """Test that build is defined."""
    assert __build__ is not None
    assert isinstance(__build__, str)


def test_version_format() -> None:
    """Test that version follows semver format."""
    parts = __version__.split(".")
    assert len(parts) >= 2
    assert all(part.isdigit() for part in parts[:2])
"#,
        project_name = project_name
    )
}

fn generate_taskfile(project_name: &str) -> String {
    format!(
        r##"# https://taskfile.dev

version: "3"

vars:
  PACKAGE: {project_name}

tasks:
  default:
    desc: Show available tasks
    cmds:
      - task --list

  install:
    desc: Install dependencies
    cmds:
      - uv sync --all-extras

  build:
    desc: Build wheel
    cmds:
      - task: sync-version
      - uv build --wheel

  test:
    desc: Run tests
    cmds:
      - uv run pytest

  test-cov:
    desc: Run tests with coverage
    cmds:
      - uv run pytest --cov={{{{.PACKAGE}}}} --cov-report=html

  lint:
    desc: Run linter
    cmds:
      - uv run ruff check {{{{.PACKAGE}}}}
      - uv run ruff format --check {{{{.PACKAGE}}}}

  format:
    desc: Format code
    cmds:
      - uv run ruff format {{{{.PACKAGE}}}}
      - uv run ruff check --fix {{{{.PACKAGE}}}}

  typecheck:
    desc: Run type checker
    cmds:
      - uv run mypy {{{{.PACKAGE}}}}

  clean:
    desc: Clean build artifacts
    cmds:
      - rm -rf dist build *.egg-info .pytest_cache .mypy_cache .ruff_cache htmlcov .coverage
      - find . -type d -name __pycache__ -exec rm -rf {{{{}}}} + 2>/dev/null || true

  sync-version:
    desc: Sync version from pyproject.toml to package
    cmds:
      - |
        VERSION=$(grep '^version' pyproject.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
        BUILD=$(date -u +"%Y%m%d%H%M%S")
        cat > {{{{.PACKAGE}}}}/__init__.py << EOF
        """{{{{.PACKAGE}}}} - A Python package."""

        __version__ = "$VERSION"
        __build__ = "$BUILD"

        __all__ = ["__version__", "__build__"]
        EOF
        echo "Synced version $VERSION with build $BUILD"

  docker-build:
    desc: Build Docker image
    cmds:
      - docker compose build

  docker-run:
    desc: Run Docker container
    cmds:
      - docker compose run --rm app

  pre-commit:
    desc: Run pre-commit hooks
    cmds:
      - pre-commit run --all-files

  ci:
    desc: Run all CI checks
    cmds:
      - task: lint
      - task: typecheck
      - task: test
"##,
        project_name = project_name
    )
}

fn generate_devcontainer(project_name: &str) -> String {
    format!(
        r#"{{
    "name": "{project_name}",
    "build": {{
        "dockerfile": "Dockerfile"
    }},
    "features": {{
        "ghcr.io/devcontainers/features/common-utils:2": {{}},
        "ghcr.io/devcontainers/features/git:1": {{}}
    }},
    "customizations": {{
        "vscode": {{
            "extensions": [
                "ms-python.python",
                "ms-python.vscode-pylance",
                "charliermarsh.ruff",
                "tamasfe.even-better-toml",
                "eamodio.gitlens"
            ],
            "settings": {{
                "python.defaultInterpreterPath": "/usr/local/bin/python",
                "python.analysis.typeCheckingMode": "basic",
                "[python]": {{
                    "editor.defaultFormatter": "charliermarsh.ruff",
                    "editor.formatOnSave": true,
                    "editor.codeActionsOnSave": {{
                        "source.fixAll.ruff": "explicit",
                        "source.organizeImports.ruff": "explicit"
                    }}
                }}
            }}
        }}
    }},
    "postCreateCommand": "uv sync --all-extras && pre-commit install",
    "remoteUser": "vscode"
}}
"#,
        project_name = project_name
    )
}

fn generate_dockerfile(project_name: &str) -> String {
    format!(
        r#"FROM python:3.12-slim

WORKDIR /app

# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Copy project files
COPY pyproject.toml uv.lock* ./
COPY {project_name}/ ./{project_name}/

# Install dependencies
RUN uv sync --frozen --no-dev

# Default command
CMD ["uv", "run", "python", "-c", "import {project_name}; print({project_name}.__version__)"]
"#,
        project_name = project_name
    )
}

fn generate_docker_compose(_project_name: &str) -> String {
    format!(
        r#"services:
  app:
    build: .
    volumes:
      - .:/app
    working_dir: /app

  dev:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/app
    working_dir: /app
    command: ["uv", "run", "python"]
    stdin_open: true
    tty: true
"#
    )
}

const PRECOMMIT_CONFIG: &str = r#"repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-toml
      - id: check-added-large-files
      - id: check-merge-conflict

  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.6
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.14.1
    hooks:
      - id: mypy
        additional_dependencies: []
"#;

const GITIGNORE: &str = r#"# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Virtual environments
.venv/
venv/
ENV/

# IDE
.idea/
.vscode/
*.swp
*.swo
*~

# Testing
.tox/
.nox/
.coverage
.coverage.*
htmlcov/
.pytest_cache/
.hypothesis/

# Type checking
.mypy_cache/
.dmypy.json
dmypy.json

# Ruff
.ruff_cache/

# Build
*.manifest
*.spec

# Jupyter
.ipynb_checkpoints/

# OS
.DS_Store
Thumbs.db

# Project specific
*.log
.env
.env.*
"#;

const DEVCONTAINER_DOCKERFILE: &str = r#"FROM mcr.microsoft.com/devcontainers/python:3.12

# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Install task
RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin

# Set up uv environment
ENV UV_LINK_MODE=copy
"#;