# peripheryapp/periphery architecture

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

- Interactive diagram: https://gitdiagram.com/peripheryapp/periphery
- Repository: https://github.com/peripheryapp/periphery

## Overview

##### Major Components
- Frontend & CLI: Represented by the Sources/Frontend directory. This is where the command‐line interface (including main.swift and various command handlers like ScanCommand, VersionCommand, etc.) initializes the tool, parses user inputs (or “guided setup”), and dispatches further processing.
- Project Drivers: Shown in Sources/ProjectDrivers. These are responsible for interfacing with different build systems and projects (Bazel, SPM, Xcode) and setting up the environment to generate the index store.
- Indexer: Under Sources/Indexer. This module collects source files, reads the index store, and builds an initial analysis pipeline with components like SourceFileCollector, JobPool, and specific indexers (e.g. SwiftIndexer, XibIndexer). This is the foundation for extracting declaration information.
- Source Graph & Graph Mutators: Represented by Sources/SourceGraph and its subdirectories (Elements, Mutators). Here the tool transforms index store data and parsed source files into an in-memory graph. Additional mutators refine this graph (e.g., marking entry points, identifying unused declarations) by applying specific rules.
- Syntax Analysis: Contained in Sources/SyntaxAnalysis. It dives deeper into source files (through visitors such as DeclarationSyntaxVisitor) to directly parse code segments for additional context (e.g. unused function parameters or protocol analysis).
- PeripheryKit & Output Formatting: Shown in Sources/PeripheryKit, this module handles the formatting (CodeClimate, JSON, Xcode formats, etc.) for output and reporting unused code findings.
- Xcode and Logger Support: Additional modules, such as Sources/XcodeSupport, augment integration with Xcode (and help in project setup via Xcode project schemes), while Sources/Logger manages logging and debugging output.
- Build/Configuration Components: Configuration files (e.g., swiftlint.yml, .bazelrc, .dockerignore, and MODULE.bazel) and automation tasks under .mise/ and bazel/ further round out the automation, CI integrations, and build system adaptations.

##### Relationships and Data Flow
- Indexing & Graph Formation: From the build system preparation, control flows to the “Indexer” which collects data and passes it to the “Source Graph” builder. Here, “Syntax Analysis” components may be shown as a supporting element that refines the raw data.
- Graph Mutation & Analysis: Within the “Source Graph” module, a sub-process labeled “Graph Mutators” (from Sources/SourceGraph/Mutators) adjusts the graph to mark entry points, prune unused declarations, and enforce rules (e.g. handling protocols, unused imports, redundant public declarations).
- Logging & Diagnostics: Connect “Logger” to every major module (Frontend, Indexer, etc.) to indicate that logging is consistently used across execution.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Clearly separate the CLI command parsing (Frontend), build system interfacing (Project Drivers), and core analysis (Indexer, Source Graph, Syntax Analysis). Each module has its defined responsibilities.
- Extensibility: Indicate that modules such as the Graph Mutators and Output Formatters are plug-in points for extending functionality (for example, adding support for additional formatting or analysis features).

##### Relevant Technologies & Frameworks
- Swift Language is the primary technology.
- Bazel, Xcode, and SPM are the build systems.
- Configuration files (.bazelrc, .dockerignore, swiftlint.yml, etc.) are used to manage coding style, continuous integration, and build automation.

## Diagram (Mermaid)

```mermaid
graph TD
    CLI("CLI (Sources/Frontend)"):::frontend
    PD("Project Drivers (Sources/ProjectDrivers)"):::integration
    IDX("Indexer (Sources/Indexer)"):::processing
    SG("Source Graph (Sources/SourceGraph)"):::processing
    GM("Graph Mutators (Sources/SourceGraph/Mutators)"):::processing
    SA("Syntax Analysis (Sources/SyntaxAnalysis)"):::analysis
    PK("Output Formatting (PeripheryKit/Results)"):::output
    XS("Xcode Support (Sources/XcodeSupport)"):::integration
    LOG("Logger (Sources/Logger)"):::logger
    EXT("External Build Systems (Bazel, Xcode,SPM)"):::external

    SW("swiftlint.yml"):::config
    BZ(".bazelrc"):::config
    DO(".dockerignore"):::config
    MB("MODULE.bazel"):::config
    MI(".mise/"):::config

    %% Data Flow
    CLI -->|"sends_commands"| PD
    PD -->|"interfaces_with"| EXT
    PD -->|"provides_index_store"| IDX
    IDX -->|"builds_analysis_pipeline"| SG
    SA -->|"enhances_graph"| SG
    SG -->|"applies_mutators"| GM
    GM -->|"outputs_refined_graph"| PK
    PK -->|"returns_results"| CLI
    PD -->|"handles_Xcode_integration"| XS

    %% Logger connections (dashed lines)
    LOG --- CLI
    LOG --- PD
    LOG --- IDX
    LOG --- SG
    LOG --- SA
    LOG --- PK
    LOG --- XS

    %% Configuration files feeding into Project Drivers
    SW --> PD
    BZ --> PD
    DO --> PD
    MB --> PD
    MI --> PD

    %% Click Events
    click CLI "https://github.com/peripheryapp/periphery/tree/master/Sources/Frontend"
    click PD "https://github.com/peripheryapp/periphery/tree/master/Sources/ProjectDrivers"
    click IDX "https://github.com/peripheryapp/periphery/tree/master/Sources/Indexer"
    click SG "https://github.com/peripheryapp/periphery/tree/master/Sources/SourceGraph"
    click GM "https://github.com/peripheryapp/periphery/tree/master/Sources/SourceGraph/Mutators"
    click SA "https://github.com/peripheryapp/periphery/tree/master/Sources/SyntaxAnalysis"
    click PK "https://github.com/peripheryapp/periphery/tree/master/Sources/PeripheryKit/Results"
    click XS "https://github.com/peripheryapp/periphery/tree/master/Sources/XcodeSupport"
    click LOG "https://github.com/peripheryapp/periphery/tree/master/Sources/Logger"
    click SW "https://github.com/peripheryapp/periphery/blob/master/swiftlint.yml"
    click BZ "https://github.com/peripheryapp/periphery/blob/master/.bazelrc"
    click DO "https://github.com/peripheryapp/periphery/blob/master/.dockerignore"
    click MB "https://github.com/peripheryapp/periphery/blob/master/MODULE.bazel"
    click MI "https://github.com/peripheryapp/periphery/tree/master/.mise/"

    %% Styles
    classDef frontend fill:#a8dadc,stroke:#457b9d,stroke-width:2px;
    classDef integration fill:#f4a261,stroke:#e76f51,stroke-width:2px;
    classDef processing fill:#e9c46a,stroke:#f4a261,stroke-width:2px;
    classDef analysis fill:#e76f51,stroke:#2a9d8f,stroke-width:2px;
    classDef output fill:#2a9d8f,stroke:#264653,stroke-width:2px;
    classDef config fill:#f1faee,stroke:#a8dadc,stroke-width:2px;
    classDef logger fill:#d8e2dc,stroke:#99a8b2,stroke-width:2px;
    classDef external fill:#ffb4a2,stroke:#f28482,stroke-width:2px;
```
