How peripheryapp/periphery works
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.