# rubymetric/chsrc architecture

> Architecture diagram of the GitHub repository rubymetric/chsrc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rubymetric/chsrc
- Repository: https://github.com/rubymetric/chsrc

## Overview

This project is a C99-based, command-line “mirror switcher” tool and framework (named chsrc) that uses an MVC-like design philosophy to let users and contributors add new “recipes” (i.e. platform- or package-specific mirror configurations) without needing deep C knowledge. The overall purpose is to change and manage the mirror (源) settings across various operating systems, programming languages, and packages.

##### Main Components
- CLI/Entry Point: Start with the main executable (src/chsrc-main.c). This is the interface where user commands (like “set”, “list”, “reset”, etc.) are received.
- Core Engine/Framework: Identify the “framework” layer (src/framework directory). This includes core.c (or the “business logic”) and mirror.c, as well as supporting data structures in struct.h. These modules implement the core functionalities such as mirror switching logic, recipe management, and maybe even the MVC separation.
- Recipe Modules: Under src/recipe, there are subdirectories for “lang” (programming languages like Python, Node.js, Rust, etc.), “os” (Operating systems recipes like APT, YUM, pacman etc.) and “ware” (for software like Homebrew, Winget, etc.). Every recipe file (e.g., Ruby.c, Node.js files, etc.) encapsulates the specific instructions needed to handle mirror switching for that target.
- Installer/Deployment Scripts: Note the installer scripts in the tool directory and additional packaging scripts (Makefile, pkg/README.md) to represent how the system is delivered on multiple platforms.
- CI/CD Pipeline: Although not part of the runtime architecture, the .github/workflows set up different build pipelines for various OS platforms (Linux x64, macOS, Windows, and multiple CPU architectures). This can be illustrated as an ancillary component ensuring quality and cross-platform compatibility.

##### Relationships & Data Flow
- Data/configuration flows that might include runtime parameters (like mirror URLs, selection strategies from auto测速) passed between the core and the recipes.
- That the CI/CD system (the build pipeline configurations) is connected to the code base – ensuring that every module, including recipes and core functionality, is built and tested across platforms.

##### Architectural Patterns & Design Principles
- Plugin/Extensibility: Given the large number of recipe modules, illustrate that these are designed to be loaded or called dynamically based on the command. Clearly mark them as extensible components.
- Cross-Platform Considerations: Indicate that the system is built for Linux, Windows (MSYS2, Cygwin), macOS, BSD, etc., and that CI workflows support multiple CPU architectures. This can be encapsulated in a “Deployment/Build” block.

##### Technologies & Tools
- C (specifically C99) is used for the core implementation.
- GitHub Actions for CI/CD (as seen in the workflows directory).
- Various packaging tools (apt, brew, winget, AUR, etc.) are supported by installer scripts – portray these as external systems that the tool interacts with.
- The MVC-inspired framework allows developers to add new recipes quickly.

##### Elements
- Clear labels for each component (CLI, Core Framework, Recipe Subsystems, Installer Scripts, CI/CD Pipelines).
- Annotations that indicate the role of each module (e.g., “Handles mirror selection”, “Manages platform-specific mirror switching”, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Controller"
        CLI["CLI/Entry Point"]:::internal
    end

    subgraph "Core Layer"
        CORE["Core Engine/Framework"]:::internal
    end

    subgraph "Recipe Modules"
        LANG["Language Recipes"]:::recipe
        OS["OS Recipes"]:::recipe
        SOFT["Software/Package Recipes"]:::recipe
    end

    subgraph "Deployment/Build"
        INSTALLER["Installer/Deployment Scripts"]:::external
        CI["CI/CD Pipeline"]:::external
    end

    %% Data Flow & Control Arrows
    CLI -->|"command_input"| CORE
    CORE -->|"runtime_dispatch"| LANG
    CORE -->|"runtime_dispatch"| OS
    CORE -->|"runtime_dispatch"| SOFT
    CI -->|"build_test"| CORE
    CI -->|"build_test"| LANG
    CI -->|"build_test"| OS
    CI -->|"build_test"| SOFT
    INSTALLER -->|"deploy"| CLI

    %% Click Events
    click CLI "https://github.com/rubymetric/chsrc/blob/main/src/chsrc-main.c"
    click CORE "https://github.com/rubymetric/chsrc/tree/main/src/framework"
    click LANG "https://github.com/rubymetric/chsrc/tree/main/src/recipe/lang"
    click OS "https://github.com/rubymetric/chsrc/tree/main/src/recipe/os"
    click SOFT "https://github.com/rubymetric/chsrc/tree/main/src/recipe/ware"
    click INSTALLER "https://github.com/rubymetric/chsrc/tree/main/tool"
    click CI "https://github.com/rubymetric/chsrc/tree/main/.github/workflows"

    %% Styles
    classDef internal fill:#aaddff,stroke:#333,stroke-width:2px;
    classDef recipe fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef external fill:#ffdd99,stroke:#333,stroke-width:2px;
```
