@@ -5,10 +5,14 @@ A command-line tool to analyze Swift code complexity and quality metrics using s
55## Features
66
77- ** Multiple Complexity Metrics** : Supports cyclomatic and cognitive complexity analysis
8+ - ** Xcode Integration** : Seamless integration with Xcode via Build Tool Plugin for complexity feedback during build phase.
9+ - ** Xcode Diagnostics** : Display complexity warnings and errors directly in Xcode editor with accurate line numbers
10+ - ** Configurable Thresholds** : Set custom complexity thresholds via Xcode Build Settings or environment variables
811- ** Exit Code Integration** : Returns exit code 1 when complexity thresholds are exceeded, perfect for CI/CD pipelines
9- - ** Multiple Output Formats** : Text, JSON, and XML output for different use cases
12+ - ** Multiple Output Formats** : Text, JSON, XML, and Xcode diagnostics output for different use cases
1013- ** Flexible Analysis** : Single files, directories, or recursive directory analysis
1114- ** Swift Syntax Based** : Uses ` swift-syntax ` for accurate Swift code parsing
15+ - ** Single Package Dependency** : Both CLI tool and Xcode plugin available from one package
1216- ** Extensible Architecture** : Designed to support additional quality metrics in the future
1317
1418## Quick Start
@@ -25,25 +29,29 @@ swift build -c release
2529
2630``` bash
2731# Analyze a single file
28- swift run swift-complexity path/to/file.swift
32+ swift run SwiftComplexityCLI path/to/file.swift
2933
3034# Analyze a directory with threshold enforcement
31- swift run swift-complexity Sources --threshold 10
35+ swift run SwiftComplexityCLI Sources --threshold 10
3236
3337# JSON output for tooling integration
34- swift run swift-complexity Sources --format json --recursive
38+ swift run SwiftComplexityCLI Sources --format json --recursive
39+
40+ # Xcode diagnostics format (for IDE integration)
41+ swift run SwiftComplexityCLI Sources --format xcode --threshold 15
3542```
3643
3744## CLI Integration
3845
3946The tool returns exit code 1 when any function exceeds the specified complexity threshold, making it ideal for:
47+
4048- ** CI/CD Pipelines** : Fail builds when complexity thresholds are exceeded
4149- ** Git Hooks** : Prevent commits with overly complex code
4250- ** Code Quality Gates** : Enforce complexity standards across teams
4351
4452``` bash
4553# Example: Fail if any function has complexity > 15
46- swift run swift-complexity Sources --threshold 15 --recursive
54+ swift run SwiftComplexityCLI Sources --threshold 15 --recursive
4755# Exit code 0: All functions below threshold
4856# Exit code 1: One or more functions exceed threshold
4957```
@@ -64,35 +72,33 @@ swift run swift-complexity Sources --threshold 15 --recursive
6472
6573## Package Structure
6674
67- This project uses a multi-target architecture :
75+ Unified package with three targets :
6876
69- - ** SwiftComplexityCore** : Core analysis library (reusable)
77+ - ** SwiftComplexityCore** : Core analysis library
7078- ** SwiftComplexityCLI** : Command-line interface
71- - ** SwiftComplexityPlugin** : Xcode Build Tool Plugin for automatic analysis
79+ - ** SwiftComplexityPlugin** : Xcode Build Tool Plugin
7280
7381## Usage Examples
7482
7583``` bash
7684# Analyze with verbose output
77- swift run swift-complexity Sources --verbose --recursive
85+ swift run SwiftComplexityCLI Sources --verbose --recursive
7886
79- # Exclude test files with pattern matching
80- swift run swift-complexity Sources --recursive --exclude " *Test*.swift"
87+ # Exclude test files with pattern matching
88+ swift run SwiftComplexityCLI Sources --recursive --exclude " *Test*.swift"
8189
8290# Show only cognitive complexity above threshold
83- swift run swift-complexity Sources --cognitive-only --threshold 5
91+ swift run SwiftComplexityCLI Sources --cognitive-only --threshold 5
8492```
8593
8694## Xcode Build Tool Plugin
8795
88- swift-complexity can be integrated into your Xcode build process as a Build Tool Plugin:
89-
90- ### Package.swift Integration
96+ Integrates with both Swift Package Manager and Xcode projects for automatic complexity analysis during builds.
9197
92- Add swift-complexity as a dependency and apply the plugin to your targets:
98+ ### Swift Package Manager Integration
9399
94100``` swift
95- // swift-tools-version: 5.9
101+ // swift-tools-version: 6.1
96102import PackageDescription
97103
98104let package = Package (
@@ -103,7 +109,6 @@ let package = Package(
103109 targets : [
104110 .target (
105111 name : " YourTarget" ,
106- dependencies : [],
107112 plugins : [
108113 .plugin (name : " SwiftComplexityPlugin" , package : " swift-complexity" )
109114 ]
@@ -112,23 +117,35 @@ let package = Package(
112117)
113118```
114119
120+ ### Xcode Project Integration
121+
122+ 1 . Add swift-complexity package to your Xcode project
123+ 2 . In Build Phases, add "SwiftComplexityPlugin" to Run Build Tool Plug-ins
124+ 3 . Configure threshold in Build Settings (optional)
125+
115126### Configuration
116127
117- The plugin supports environment-based configuration:
128+ ** Xcode Build Settings: **
118129
119- - ` SWIFT_COMPLEXITY_THRESHOLD ` : Set complexity threshold (default: 10)
130+ - Key: ` SWIFT_COMPLEXITY_THRESHOLD `
131+ - Value: ` 15 ` (or any number, defaults to 10)
120132
121- ### Automatic Analysis
133+ ** Environment Variable (SPM): **
122134
123- Once configured, the plugin will:
124- - Run automatically during builds
125- - Analyze all Swift files in your target
126- - Generate JSON complexity reports
127- - Fail the build if complexity thresholds are exceeded
135+ - ` SWIFT_COMPLEXITY_THRESHOLD=15 `
128136
129- ## Output Example
137+ ### Features
130138
131- ```
139+ - ** Real-time feedback** : Complexity warnings appear directly in Xcode editor
140+ - ** Accurate positioning** : Errors show at exact function locations
141+ - ** Build integration** : Builds fail when thresholds are exceeded
142+ - ** Configurable per target** : Different thresholds for different modules
143+
144+ ## Output Examples
145+
146+ ### CLI Text Output
147+
148+ ``` text
132149File: Sources/ComplexityAnalyzer.swift
133150+------------------+----------+----------+
134151| Function/Method | Cyclo. | Cogn. |
@@ -140,11 +157,18 @@ File: Sources/ComplexityAnalyzer.swift
140157Total: 2 functions, Average Cyclomatic: 4.0, Average Cognitive: 4.5
141158```
142159
160+ ### Xcode Diagnostics Output
161+
162+ ``` text
163+ /path/to/Sources/MyFile.swift:45:1: error: Function 'complexFunction' has high complexity (Cyclomatic: 15, Cognitive: 23, Threshold: 10)
164+ /path/to/Sources/MyFile.swift:89:1: warning: Function 'anotherFunction' has high complexity (Cyclomatic: 12, Cognitive: 18, Threshold: 10)
165+ ```
166+
143167## Requirements
144168
145169- Swift 6.1+
146170- macOS 14+ or Linux (Ubuntu 22.04+)
147171
148172## License
149173
150- MIT License
174+ MIT License
0 commit comments