# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "ユーザー環境"
        ZSHRC[".zshrc<br/>(ユーザー設定)"]
        ZSH_SHELL["Zsh Shell"]
    end

    subgraph "Oh My Zsh Framework"
        subgraph "Core Layer"
            OMZ_SH["oh-my-zsh.sh<br/>(エントリーポイント)"]
        end

        subgraph "Tools Layer"
            INSTALL["install.sh"]
            UPGRADE["upgrade.sh"]
            CHECK_UPGRADE["check_for_upgrade.sh"]
            UNINSTALL["uninstall.sh"]
            CHANGELOG["changelog.sh"]
        end

        subgraph "Library Layer"
            CLI["cli.zsh<br/>(omzコマンド)"]
            GIT_LIB["git.zsh<br/>(Git統合)"]
            ASYNC["async_prompt.zsh<br/>(非同期処理)"]
            COMPLETION["completion.zsh<br/>(補完設定)"]
            FUNCTIONS["functions.zsh<br/>(ユーティリティ)"]
            COMPFIX["compfix.zsh<br/>(セキュリティ)"]
            OTHER_LIBS["その他lib/*.zsh"]
        end

        subgraph "Plugin Layer"
            GIT_PLUGIN["git plugin"]
            DOCKER_PLUGIN["docker plugin"]
            AWS_PLUGIN["aws plugin"]
            OTHER_PLUGINS["... 350+ plugins"]
        end

        subgraph "Theme Layer"
            ROBBYRUSSELL["robbyrussell"]
            AGNOSTER["agnoster"]
            OTHER_THEMES["... 140+ themes"]
        end

        subgraph "Custom Layer"
            CUSTOM_PLUGINS["custom/plugins/"]
            CUSTOM_THEMES["custom/themes/"]
            CUSTOM_CONFIG["custom/*.zsh"]
        end

        subgraph "Cache / Storage"
            ZCOMPDUMP[".zcompdump<br/>(補完キャッシュ)"]
            ZSH_UPDATE[".zsh-update<br/>(更新状態)"]
            CACHE_DIR["cache/"]
        end
    end

    subgraph "外部システム"
        GIT["Git"]
        GITHUB_API["GitHub API"]
    end

    %% 接続
    ZSH_SHELL --> ZSHRC
    ZSHRC --> OMZ_SH

    OMZ_SH --> CHECK_UPGRADE
    OMZ_SH --> COMPFIX
    OMZ_SH --> CLI
    OMZ_SH --> GIT_LIB
    OMZ_SH --> ASYNC
    OMZ_SH --> COMPLETION
    OMZ_SH --> FUNCTIONS
    OMZ_SH --> OTHER_LIBS

    OMZ_SH --> GIT_PLUGIN
    OMZ_SH --> DOCKER_PLUGIN
    OMZ_SH --> AWS_PLUGIN
    OMZ_SH --> OTHER_PLUGINS

    OMZ_SH --> ROBBYRUSSELL
    OMZ_SH --> AGNOSTER
    OMZ_SH --> OTHER_THEMES

    OMZ_SH --> CUSTOM_PLUGINS
    OMZ_SH --> CUSTOM_THEMES
    OMZ_SH --> CUSTOM_CONFIG

    COMPLETION --> ZCOMPDUMP
    CHECK_UPGRADE --> ZSH_UPDATE
    CHECK_UPGRADE --> GITHUB_API

    CLI --> UPGRADE
    CLI --> CHANGELOG
    UPGRADE --> GIT
    CHECK_UPGRADE --> GIT

    INSTALL --> GIT
```

## レイヤー構成図

```mermaid
graph LR
    subgraph "Layer 1: Core"
        A1["oh-my-zsh.sh"]
    end

    subgraph "Layer 2: Tools"
        B1["install.sh"]
        B2["upgrade.sh"]
        B3["check_for_upgrade.sh"]
        B4["uninstall.sh"]
    end

    subgraph "Layer 3: Library"
        C1["cli.zsh"]
        C2["git.zsh"]
        C3["async_prompt.zsh"]
        C4["completion.zsh"]
        C5["functions.zsh"]
        C6["..."]
    end

    subgraph "Layer 4: Plugins"
        D1["git/"]
        D2["docker/"]
        D3["aws/"]
        D4["... 355 plugins"]
    end

    subgraph "Layer 5: Themes"
        E1["robbyrussell"]
        E2["agnoster"]
        E3["... 143 themes"]
    end

    subgraph "Layer 6: Custom"
        F1["custom/plugins/"]
        F2["custom/themes/"]
        F3["custom/*.zsh"]
    end

    A1 --> B3
    A1 --> C1
    A1 --> C2
    A1 --> C3
    A1 --> C4
    A1 --> C5
    A1 --> C6
    A1 --> D1
    A1 --> D2
    A1 --> D3
    A1 --> D4
    A1 --> E1
    A1 --> E2
    A1 --> E3
    A1 --> F1
    A1 --> F2
    A1 --> F3
    C1 --> B2
```

## 初期化フロー図

```mermaid
sequenceDiagram
    participant User as ユーザー
    participant Zsh as Zsh Shell
    participant ZSHRC as .zshrc
    participant OMZ as oh-my-zsh.sh
    participant Tools as Tools
    participant Lib as lib/*.zsh
    participant Plugins as plugins/
    participant Themes as themes/
    participant Custom as custom/

    User->>Zsh: シェル起動
    Zsh->>ZSHRC: source ~/.zshrc
    ZSHRC->>OMZ: source oh-my-zsh.sh

    Note over OMZ: 環境変数初期化<br/>ZSH, ZSH_CUSTOM, ZSH_CACHE_DIR

    OMZ->>Tools: source check_for_upgrade.sh
    Tools-->>OMZ: 更新確認完了

    Note over OMZ: fpath設定<br/>autoload compaudit, compinit

    loop 各プラグイン
        OMZ->>OMZ: is_plugin() チェック
        OMZ->>OMZ: fpath追加
    end

    Note over OMZ: compinit実行<br/>.zcompdump検証

    loop 各ライブラリ
        OMZ->>Lib: _omz_source lib/*.zsh
    end

    loop 各プラグイン
        OMZ->>Plugins: _omz_source plugins/*/*.plugin.zsh
    end

    loop カスタム設定
        OMZ->>Custom: source custom/*.zsh
    end

    OMZ->>Themes: source $ZSH_THEME.zsh-theme
    Themes-->>OMZ: PROMPT設定完了

    OMZ-->>ZSHRC: 初期化完了
    ZSHRC-->>Zsh: 設定完了
    Zsh-->>User: プロンプト表示
```

## 非同期プロンプト処理図

```mermaid
sequenceDiagram
    participant User as ユーザー
    participant Zsh as Zsh Shell
    participant Precmd as precmd hook
    participant Async as async_prompt.zsh
    participant Handler as 非同期ハンドラ
    participant Prompt as プロンプト

    User->>Zsh: コマンド実行
    Zsh->>Precmd: precmdフック実行
    Precmd->>Async: _omz_async_request()

    loop 各登録ハンドラ
        Async->>Async: 既存プロセスキャンセル
        Async->>Handler: fork サブシェル
        Note over Handler: バックグラウンド実行
    end

    Async->>Zsh: zle -F (FD監視登録)
    Async-->>Precmd: 即座に戻る
    Precmd-->>Zsh: 完了
    Zsh-->>Prompt: 初期プロンプト表示

    Handler-->>Async: FD書き込み完了
    Async->>Async: _omz_async_callback()

    alt 出力変更あり
        Async->>Zsh: zle .reset-prompt
        Zsh-->>Prompt: プロンプト再描画
    end
```

## プラグインシステム構成図

```mermaid
graph TB
    subgraph "プラグイン検出"
        IS_PLUGIN["is_plugin()"]
        ZSH_PLUGINS["$ZSH/plugins/"]
        CUSTOM_PLUGINS["$ZSH_CUSTOM/plugins/"]
    end

    subgraph "プラグイン構造"
        PLUGIN_DIR["plugin_name/"]
        PLUGIN_ZSH["plugin_name.plugin.zsh"]
        PLUGIN_COMP["_plugin_name (補完)"]
        PLUGIN_README["README.md"]
    end

    subgraph "プラグイン機能"
        ALIASES["エイリアス定義"]
        FUNCTIONS["関数定義"]
        COMPLETIONS["補完定義"]
        BINDINGS["キーバインド"]
    end

    subgraph "読み込みプロセス"
        OMZ["oh-my-zsh.sh"]
        FPATH["fpath追加"]
        SOURCE["source plugin.zsh"]
    end

    IS_PLUGIN --> ZSH_PLUGINS
    IS_PLUGIN --> CUSTOM_PLUGINS

    CUSTOM_PLUGINS --> PLUGIN_DIR
    ZSH_PLUGINS --> PLUGIN_DIR

    PLUGIN_DIR --> PLUGIN_ZSH
    PLUGIN_DIR --> PLUGIN_COMP
    PLUGIN_DIR --> PLUGIN_README

    OMZ --> IS_PLUGIN
    IS_PLUGIN --> FPATH
    FPATH --> SOURCE

    SOURCE --> ALIASES
    SOURCE --> FUNCTIONS
    SOURCE --> COMPLETIONS
    SOURCE --> BINDINGS

    style CUSTOM_PLUGINS fill:#e1f5fe
    style PLUGIN_ZSH fill:#c8e6c9
```

## 更新システム構成図

```mermaid
stateDiagram-v2
    [*] --> CheckEnabled: シェル起動

    CheckEnabled --> Disabled: update_mode = disabled
    CheckEnabled --> CheckTime: update_mode != disabled

    Disabled --> [*]

    CheckTime --> NotDue: epoch < frequency
    CheckTime --> CheckAvailable: epoch >= frequency

    NotDue --> [*]

    CheckAvailable --> NoUpdate: ローカル = リモート
    CheckAvailable --> UpdateAvailable: ローカル != リモート

    NoUpdate --> UpdateLastEpoch
    UpdateLastEpoch --> [*]

    UpdateAvailable --> ReminderMode: mode = reminder
    UpdateAvailable --> AutoMode: mode = auto
    UpdateAvailable --> PromptMode: mode = prompt
    UpdateAvailable --> BackgroundMode: mode = background-alpha

    ReminderMode --> ShowReminder
    ShowReminder --> [*]

    AutoMode --> RunUpgrade
    BackgroundMode --> RunUpgradeBackground

    PromptMode --> AskUser
    AskUser --> RunUpgrade: Yes
    AskUser --> ShowManualReminder: No

    RunUpgrade --> UpdateSuccess
    RunUpgrade --> UpdateFailed

    RunUpgradeBackground --> NotifySuccess
    RunUpgradeBackground --> NotifyFailed

    UpdateSuccess --> ReloadShell
    UpdateFailed --> [*]

    NotifySuccess --> [*]
    NotifyFailed --> [*]

    ShowManualReminder --> [*]
    ReloadShell --> [*]
```

## omz CLI コマンド構造図

```mermaid
graph TB
    subgraph "omz コマンド"
        OMZ["omz"]
    end

    subgraph "サブコマンド"
        HELP["help"]
        CHANGELOG["changelog [version]"]
        PLUGIN["plugin"]
        PR["pr"]
        RELOAD["reload"]
        SHOP["shop"]
        THEME["theme"]
        UPDATE["update"]
        VERSION["version"]
    end

    subgraph "plugin サブコマンド"
        P_DISABLE["disable <plugin>"]
        P_ENABLE["enable <plugin>"]
        P_INFO["info <plugin>"]
        P_LIST["list [--enabled]"]
        P_LOAD["load <plugin>"]
    end

    subgraph "pr サブコマンド"
        PR_CLEAN["clean"]
        PR_TEST["test <PR_number>"]
    end

    subgraph "theme サブコマンド"
        T_LIST["list"]
        T_SET["set <theme>"]
        T_USE["use <theme>"]
    end

    OMZ --> HELP
    OMZ --> CHANGELOG
    OMZ --> PLUGIN
    OMZ --> PR
    OMZ --> RELOAD
    OMZ --> SHOP
    OMZ --> THEME
    OMZ --> UPDATE
    OMZ --> VERSION

    PLUGIN --> P_DISABLE
    PLUGIN --> P_ENABLE
    PLUGIN --> P_INFO
    PLUGIN --> P_LIST
    PLUGIN --> P_LOAD

    PR --> PR_CLEAN
    PR --> PR_TEST

    THEME --> T_LIST
    THEME --> T_SET
    THEME --> T_USE
```

## ファイルシステム構成図

```mermaid
graph TB
    subgraph "$ZSH (Oh My Zsh ルート)"
        ROOT["oh-my-zsh/"]

        subgraph "Core"
            OMZ_SH["oh-my-zsh.sh"]
        end

        subgraph "lib/"
            LIB_CLI["cli.zsh"]
            LIB_GIT["git.zsh"]
            LIB_ASYNC["async_prompt.zsh"]
            LIB_COMP["completion.zsh"]
            LIB_FUNC["functions.zsh"]
            LIB_OTHER["..."]
        end

        subgraph "tools/"
            INSTALL["install.sh"]
            UPGRADE["upgrade.sh"]
            CHECK["check_for_upgrade.sh"]
            UNINSTALL["uninstall.sh"]
        end

        subgraph "plugins/ (355)"
            PLUGIN_GIT["git/"]
            PLUGIN_DOCKER["docker/"]
            PLUGIN_OTHER["..."]
        end

        subgraph "themes/ (143)"
            THEME_ROBBY["robbyrussell.zsh-theme"]
            THEME_AGNO["agnoster.zsh-theme"]
            THEME_OTHER["..."]
        end

        subgraph "custom/"
            CUSTOM_EX["example.zsh"]
            CUSTOM_P["plugins/"]
            CUSTOM_T["themes/"]
        end

        subgraph "templates/"
            TEMPLATE["zshrc.zsh-template"]
        end

        subgraph "cache/"
            CACHE_COMP["completions/"]
        end
    end

    subgraph "$HOME"
        ZSHRC["~/.zshrc"]
        ZCOMPDUMP["~/.zcompdump-*"]
    end

    subgraph "$ZSH_CACHE_DIR"
        ZSH_UPDATE[".zsh-update"]
        CACHE_FILES["completions/"]
    end

    ROOT --> OMZ_SH
    ROOT --> LIB_CLI
    ROOT --> INSTALL
    ROOT --> PLUGIN_GIT
    ROOT --> THEME_ROBBY
    ROOT --> CUSTOM_EX
    ROOT --> TEMPLATE
    ROOT --> CACHE_COMP

    ZSHRC -.-> OMZ_SH
```
