Sign in

    This version of the module is deprecated: Published under the wrong owner. Use lillian-dot/moonchsrc instead.

    moonchsrc

    A MoonBit-native, preview-first source and registry switcher

    mirror
    registry
    cli
    developer-tools
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    last month
    Downloads
    10

    #MoonChsrc

    MoonChsrc 是一个用 MoonBit 编写的跨工具链换源引擎与命令行工具。项目受 chsrc 启发,但采用独立的 MoonBit 数据模型与实现,不复制上游 C 源码。

    0.1.0 提供一个安全优先、可实际使用的首个版本:

    • 列出内置目标与镜像;
    • 支持 npm、pnpm、Yarn 2+、Node 聚合目标、pip、Go modules、Composer、Cargo、RubyGems 和 mooncakes;
    • 为 project、user、system 三种作用域生成明确的换源命令;
    • set --dry-run 只预览,不改写用户配置;
    • set 通过原生进程 API 直接执行,不使用 shell 字符串拼接;
    • get 查询当前生效的源;
    • reset 将目标恢复到官方上游源;
    • 写操作前保存当前配置,restore 可恢复最近一次快照;
    • 多步骤失败时自动回滚,含凭据的源地址不会明文写入历史。
    • measure 使用原生 HTTPS 并发探测镜像,逐源超时后稳定排序。

    #安装

    Linux / macOS:

    curl -fsSL https://raw.githubusercontent.com/trail-it/moonchsrc/main/scripts/install.sh | sh

    Windows PowerShell:

    irm https://raw.githubusercontent.com/trail-it/moonchsrc/main/scripts/install.ps1 | iex

    安装器会下载当前 GitHub Release 中与平台匹配的原生程序,并使用 SHA256SUMS 验证文件。目前提供 Linux x64/ARM64、macOS ARM64 和 Windows x64 原生程序。也可以从源码运行或安装 Mooncakes 模块:

    moon add trail-it/moonchsrc

    #运行

    moon run cmd/main -- list moon run cmd/main -- list npm moon run cmd/main -- set --dry-run npm npmmirror user moon run cmd/main -- set --dry-run pip tuna project moon run cmd/main -- get --dry-run node user moon run cmd/main -- set --dry-run go goproxycn user moon run cmd/main -- set --dry-run composer aliyun user moon run cmd/main -- set --dry-run npm auto user moon run cmd/main -- set --dry-run pip first project moon run cmd/main -- set --dry-run cargo rsproxy user moon run cmd/main -- set --dry-run moonbit ustc user moon run cmd/main -- set --dry-run rubygems rubychina user moon run cmd/main -- set npm npmmirror user moon run cmd/main -- reset npm user moon run cmd/main -- restore --dry-run npm user moon run cmd/main -- restore npm user moon run cmd/main -- measure npm

    #MoonBit API

    ///|
    test "build a source-change plan" {
    let plan = plan_change("npm", "npmmirror", scope=Project)
    assert_eq(plan.target_id, "npm")
    assert_eq(plan.scope, Project)
    }

    #路线图

    完整里程碑、完成标准和质量门见 docs/ROADMAP.md。 架构和 recipe 扩展说明见 docs/ARCHITECTURE.md。 参与开发请见 CONTRIBUTING.md,recipe 编写清单见 docs/RECIPES.md。

    #来源与许可证

    • 参考项目:RubyMetric/chsrc
    • 上游链接:https://github.com/RubyMetric/chsrc
    • 上游许可证:GPL-3.0-or-later(部分组件为 MIT)
    • 参考范围:命令行领域模型与“目标/镜像/recipe”概念;本仓库为独立实现。
    • 本项目许可证:Apache-2.0

    #MoonChsrc

    MoonBit 编写的跨工具链换源引擎与命令行工具。完整说明与可测试示例见 README.mbt.md。

    PlanError

    pub(all) suberror PlanError {
    UnknownTarget(String)
    UnknownMirror(target~ : String, mirror~ : String)
    InvalidScope(String)
    UnsupportedScope(target~ : String, scope~ : Scope)
    InvalidSnapshot(target~ : String, expected~ : Int, actual~ : Int)
    SnapshotNotRestorable(String)
    NoSelectableMirror(String)
    ConfigConflict(path~ : String, detail~ : String)
    UnsafeConfigValue(String)
    } derive(Eq,
    Debug
    )

    Capability

    pub(all) enum Capability {
    CanGet
    CanSet
    CanReset
    CanMeasure
    } derive(Eq,
    Debug
    )

    Operations implemented by a target recipe.

    Capability::to_text

    fn Capability::to_text(self : Capability) -> String

    ChangePlan

    pub(all) struct ChangePlan {
    operation : Operation
    target_id : String
    mirror_id : String?
    scope : Scope
    commands : Array[Command]
    file_steps : Array[FileStep]
    } derive(Eq,
    Debug
    )

    A resolved source-change operation.

    ChangePlan::to_text

    fn ChangePlan::to_text(self : ChangePlan) -> String

    CliAction

    pub(all) enum CliAction {
    Show(String)
    Execute(ChangePlan)
    RestoreLatest(String, Scope, Bool)
    MeasureTarget(String)
    AutoSelect(String, Scope, Bool)
    } derive(Eq,
    Debug
    )

    A parsed CLI request. Execution is kept separate from argument parsing so tests never need to launch external processes.

    Command

    pub(all) struct Command {
    program : String
    args : Array[String]
    } derive(Eq,
    Debug
    )

    A process invocation represented without shell interpolation.

    Command::to_text

    fn Command::to_text(self : Command) -> String

    ConfigPath

    pub(all) enum ConfigPath {
    CargoProject
    CargoUser
    MoonUser
    } derive(Eq,
    Debug
    )

    A platform-neutral configuration path resolved by the native executor.

    ConfigPath::to_text

    fn ConfigPath::to_text(self : ConfigPath) -> String

    FileOperation

    pub(all) enum FileOperation {
    ReadCargoSource
    SetCargoSource(String)
    ResetCargoSource
    RestoreContent(SavedValue)
    ReadMooncakesSource
    SetMooncakesSource(registry~ : String, index~ : String, symbols~ : String)
    ResetMooncakesSource
    } derive(Eq,
    Debug
    )

    File operations stay typed so dry-run rendering and execution never rely on shell redirection or text-substitution commands.

    FileStep

    pub(all) struct FileStep {
    path : ConfigPath
    operation : FileOperation
    } derive(Eq,
    Debug
    )

    FileStep::to_text

    fn FileStep::to_text(self : FileStep) -> String

    Measurement

    pub(all) struct Measurement {
    mirror_id : String
    url : String
    latency_ms : Int64?
    http_status : Int?
    } derive(Eq,
    Debug
    )

    Result of probing one mirror endpoint. Missing latency/status means that the request failed or exceeded its timeout.

    Measurement::is_reachable

    fn Measurement::is_reachable(self : Measurement) -> Bool

    Mirror

    pub(all) struct Mirror {
    id : String
    name : String
    } derive(Eq,
    Debug
    )

    A public mirror provider.

    Operation

    pub(all) enum Operation {
    GetSource
    SetSource
    ResetSource
    RestoreSource
    } derive(Eq,
    Debug
    )

    The source-changing operation represented by a plan.

    Operation::to_text

    fn Operation::to_text(self : Operation) -> String

    SavedValue

    pub(all) enum SavedValue {
    Present(String)
    Missing
    Redacted
    } derive(Eq, ToJson,
    Debug
    ,
    FromJson
    )

    A previously observed source value. Redacted values are deliberately not persisted, preventing credentials embedded in URLs from entering history.

    Scope

    pub(all) enum Scope {
    Project
    User
    System
    } derive(Eq, ToJson,
    Debug
    ,
    FromJson
    )

    Configuration scope supported by a target recipe.

    Scope::parse

    fn Scope::parse(text : StringView) -> Scope?

    Scope::to_text

    fn Scope::to_text(self : Scope) -> String

    Snapshot

    pub(all) struct Snapshot {
    target_id : String
    scope : Scope
    values : Array[SavedValue]
    captured_at : UInt64
    } derive(Eq, ToJson,
    Debug
    ,
    FromJson
    )

    A point-in-time snapshot captured before a source-changing operation.

    Snapshot::is_restorable

    fn Snapshot::is_restorable(self : Snapshot) -> Bool

    Source

    pub(all) struct Source {
    mirror_id : String
    url : String
    } derive(Eq,
    Debug
    )

    A mirror endpoint supported by one target.

    Target

    pub(all) struct Target {
    id : String
    name : String
    aliases : Array[String]
    scopes : Array[Scope]
    capabilities : Array[Capability]
    sources : Array[Source]
    } derive(Eq,
    Debug
    )

    A package manager or developer tool whose source can be changed.

    capture_command_value

    fn capture_command_value(target_name : StringView, exit_code : Int, output : StringView) -> SavedValue

    capture_config_file

    fn capture_config_file(content : String) -> SavedValue

    Capture a complete configuration file. Unlike command output, an empty existing file must remain distinguishable from a file that did not exist.

    capture_value

    fn capture_value(exit_code : Int, output : StringView) -> SavedValue

    Convert command output into a safe value for local history.

    dispatch_cli

    fn dispatch_cli(args : ArrayView[String]) -> CliAction

    Parse CLI arguments into either display output or an execution plan.

    fastest_reachable

    fn fastest_reachable(measurements : ArrayView[Measurement]) -> Measurement?

    find_mirror

    fn find_mirror(id : StringView) -> Mirror?

    find_target

    fn find_target(name : StringView) -> Target?

    get_cargo_mirror_config

    fn get_cargo_mirror_config(content : String) -> String? raise PlanError

    Read the registry URL from MoonChsrc's managed block. Unmanaged Cargo configuration is intentionally not interpreted as one of our mirrors.

    get_mooncakes_mirror_config

    fn get_mooncakes_mirror_config(content : String) -> String? raise PlanError

    help_text

    fn help_text() -> String

    mirrors

    fn mirrors() -> Array[Mirror]

    Built-in mirror providers known to MoonChsrc.

    plan_change

    fn plan_change(target_name : StringView, mirror_name : StringView, scope? : Scope) -> ChangePlan raise PlanError

    Resolve a target, mirror, and scope into an exact command plan.

    plan_first

    fn plan_first(target_name : StringView, scope? : Scope) -> ChangePlan raise PlanError

    Select the first maintainer-preferred mirror. Official upstream is kept as the reset destination and is therefore skipped here.

    plan_get

    fn plan_get(target_name : StringView, scope? : Scope) -> ChangePlan raise PlanError

    Build a read-only plan that prints the currently effective source.

    plan_reset

    fn plan_reset(target_name : StringView, scope? : Scope) -> ChangePlan raise PlanError

    Reset a target to its official upstream source.

    plan_restore

    fn plan_restore(snapshot : Snapshot) -> ChangePlan raise PlanError

    Convert a previously captured snapshot into exact restoration commands.

    rank_measurements

    fn rank_measurements(measurements : ArrayView[Measurement]) -> Array[Measurement]

    Rank reachable mirrors by latency and keep failed probes last. Insertion sorting intentionally preserves recipe order when two results tie.

    render_error

    fn render_error(error : PlanError) -> String

    render_measurements

    fn render_measurements(measurements : ArrayView[Measurement]) -> String

    render_mirrors

    fn render_mirrors() -> String

    render_target

    fn render_target(name : StringView) -> String raise PlanError

    render_targets

    fn render_targets() -> String

    reset_cargo_mirror_config

    fn reset_cargo_mirror_config(content : String) -> String raise PlanError

    Remove only the block owned by MoonChsrc.

    reset_mooncakes_mirror_config

    fn reset_mooncakes_mirror_config(content : String) -> String raise PlanError

    run_cli

    fn run_cli(args : ArrayView[String]) -> String

    Run the pure display form of the CLI. Execution requests are rendered but never launched; the native executable handles Execute explicitly.

    set_cargo_mirror_config

    fn set_cargo_mirror_config(content : String, registry_url : String) -> String raise PlanError

    Add or replace MoonChsrc's isolated Cargo source block. Existing Cargo source tables outside the managed block are rejected instead of overwritten.

    set_mooncakes_mirror_config

    fn set_mooncakes_mirror_config(content : String, registry : String, index : String, symbols : String) -> String raise PlanError

    targets

    fn targets() -> Array[Target]

    Built-in target recipes.

    version

    fn version() -> String

    MoonChsrc is a MoonBit-native source and registry switcher.

    Return the released MoonChsrc version.