The Swift Code Metrics Project
Overview
The Swift Code Metrics project is a code metrics analyzer designed specifically for projects written in the Swift programming language. Its primary goal is to offer developers a way to gauge the architectural state of their Swift software, which can consist of several interconnected modules.
Inspiration
The project draws inspiration from Robert C. Martin's esteemed book, Clean Architecture. The tool scans the Swift project to determine various components and evaluates common industry code metrics, providing insights into the project’s health and structure.
Key Metrics Analyzed
Swift Code Metrics assesses several crucial aspects of a project:
- Concrete Classes and Interfaces: Evaluates the total number of these elements in the codebase.
- Instability and Abstractness: Measures how balanced or imbalanced the code components are.
- Distance from the Main Sequence: Analyzes how far the architecture deviates from a desired structure.
- Lines of Code (LOC): Counts the total lines in the project.
- Number of Comments (NOC) and Percentage of Comments (POC): Quantifies the amount and proportion of comments in the code.
- Number of Methods (NOM): Counts all methods in the project.
- Number of Concrete Types: Tallies the classes and structures.
- Number Of Tests (NOT): Assesses test coverage by counting test cases.
- Number Of Imports (NOI): Tracks the imports statements used.
- Dependencies Graph: Visualizes internal and external dependencies within the framework structure.
System Requirements
Swift Code Metrics is a Python 3 script that requires several Python libraries, including matplotlib
, adjustText
, pyfunctional
, and pygraphviz
. Additionally, the Graphviz tool must be installed to ensure proper graph generation. On macOS, Graphviz can be easily installed using the Homebrew package manager with the command brew install graphviz
.
Installation and Usage
The package is available on PyPI, and can be installed via pip using pip3 install swift-code-metrics
.
To run the analyzer, use the command:
swift-code-metrics --source <path-to-swift-project> --artifacts <output-directory> --exclude <excluded-folders> --tests-paths <test-paths> --generate-graphs
--source
specifies the directory containing the main Xcode project or workspace.--artifacts
points to the directory where the reportoutput.json
will be saved.--exclude
allows exclusion of specific paths from the analysis.--tests-paths
defines paths looking for test classes, with a default ofTest
andTests
.--generate-graphs
when used, creates and saves analysis graphs in the artifacts folder.
Development and Documentation
Developers can use the provided scripts ./install.sh
and ./build_and_test.sh
to install dependencies and perform tests. The repository also offers a VS Code setup for debugging and running tests. For a practical example, follow the documentation guide provided in the project’s repository.
Limitations
The tool is tailored for medium to large codebases multiple frameworks. Limitations include:
- Dependency on project directory structure for framework identification.
- Incompatibility with Swift Package Manager (
spm
). - Framework names deduced from directory structure rather than inspecting
xcodeproj
. - Current limitations in listing methods and handling inline comments.
- Exclusive support for XCTest frameworks.
Future Work
The project intends ongoing improvements and is open to suggestions for enhancements.
Contact
For further information or inquiries, please reach out to the developer Mattia Campolese.