# dart-lang/pub architecture

> Architecture diagram of the GitHub repository dart-lang/pub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dart-lang/pub
- Repository: https://github.com/dart-lang/pub

## Overview

##### Main Components

a. Command Layer
- CommandRunner as the entry point
- Commands in lib/src/command/ (get, add, remove, upgrade, etc.)
- To the core services layer

b. Core Services Layer
- Package Management Services
- Source Handling (hosted, git, path, sdk)
- Version Solver
- System Cache Management
- Authentication Services

c. External Integrations
- Pub.dev API Integration
- Git Repository Integration
- Local File System
- Package Cache Storage

##### Architectural Patterns to Highlight

a. Command Pattern
- How each command (in lib/src/command/) implements a common interface
- Demonstrate the command routing through the CommandRunner

b. Source Strategy Pattern
- Different package sources (hosted, git, path, sdk)
- How they implement a common source interface

c. Solver Architecture
- Version solver component
- Package resolution logic
- Dependency graph management

##### Component Relationships

**[Command Runner] → [Commands] → [Core Services] → [External Services]**
- ↓
- [Source Handlers]
- ↓
- [Version Solver]

##### Implementation Details

- How commands are registered and routed
- Package source resolution flow
- Version solving process
- Cache management strategy
- Authentication flow
- File system interactions

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph CLI
        CR["Command Runner"]:::entry
        click CR "https://github.com/dart-lang/pub/blob/master/lib/src/command_runner.dart"
    end

    subgraph CommandLayer
        CMD["Commands Directory"]:::command
        click CMD "https://github.com/dart-lang/pub/tree/master/lib/src/command/"
        CR --> CMD
    end

    subgraph CoreServices
        PM["Package Management"]:::core
        click PM "https://github.com/dart-lang/pub/blob/master/lib/src/package.dart"
        VS["Version Solver"]:::core
        click VS "https://github.com/dart-lang/pub/tree/master/lib/src/solver/"
        SC["System Cache"]:::core
        click SC "https://github.com/dart-lang/pub/blob/master/lib/src/system_cache.dart"
        LF["Lock File Manager"]:::core
        click LF "https://github.com/dart-lang/pub/blob/master/lib/src/lock_file.dart"
        
        CMD --> PM
        CMD --> VS
        CMD --> SC
        PM --> LF
    end

    subgraph SourceHandlers["Source Handlers"]
        SH["Source Base"]:::source
        click SH "https://github.com/dart-lang/pub/tree/master/lib/src/source/"
        HP["Hosted Packages"]:::source
        click HP "https://github.com/dart-lang/pub/blob/master/lib/src/source/hosted.dart"
        GS["Git Source"]:::source
        click GS "https://github.com/dart-lang/pub/blob/master/lib/src/source/git.dart"
        PS["Path Source"]:::source
        click PS "https://github.com/dart-lang/pub/blob/master/lib/src/source/path.dart"
        SDK["SDK Source"]:::source
        click SDK "https://github.com/dart-lang/pub/tree/master/lib/src/sdk/"

        SH --> HP
        SH --> GS
        SH --> PS
        SH --> SDK
    end

    subgraph Security
        Auth["Authentication"]:::security
        click Auth "https://github.com/dart-lang/pub/tree/master/lib/src/authentication/"
        OAuth["OAuth2"]:::security
        click OAuth "https://github.com/dart-lang/pub/blob/master/lib/src/oauth2.dart"
        
        Auth --> OAuth
    end

    subgraph Validation
        PV["Package Validator"]:::validation
        click PV "https://github.com/dart-lang/pub/tree/master/lib/src/validator/"
    end

    subgraph ExternalServices
        HTTP["HTTP Client"]:::external
        click HTTP "https://github.com/dart-lang/pub/blob/master/lib/src/http.dart"
        PubDev["pub.dev API"]:::external
        Git["Git Repos"]:::external
        FS["File System"]:::external
    end

    PM --> SH
    PM --> PV
    HP --> HTTP
    HP --> PubDev
    GS --> Git
    PS --> FS
    Auth --> HTTP

    classDef entry fill:#2196F3,stroke:#1565C0,color:white
    classDef command fill:#FF9800,stroke:#F57C00,color:white
    classDef core fill:#4CAF50,stroke:#388E3C,color:white
    classDef source fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef security fill:#F44336,stroke:#D32F2F,color:white
    classDef validation fill:#795548,stroke:#5D4037,color:white
    classDef external fill:#607D8B,stroke:#455A64,color:white

    subgraph Legend
        E[Entry Point]:::entry
        C[Command]:::command
        CO[Core Service]:::core
        S[Source Handler]:::source
        SE[Security]:::security
        V[Validation]:::validation
        EX[External]:::external
    end
```
