Skip to main content

FOSSA Status License Build Status codecov Codacy Badge PyPI

swift-code-metrics

Code metrics analyzer for Swift projects.

Example code distribution Example deviation main sequence
Example internal distribution

Introduction

The goal of this software is to provide an insight of the architectural state of a software written in Swift that consists in several modules. Inspired by the book of Robert C. Martin, Clean Architecture, the software will scan the project to identify the different components in order to assess several common code metrics in the software industry:

  • the overall number of concrete classes and interfaces
  • the instability and abstractness of the framework
  • the distance from the main sequence
  • LOC (Lines Of Code)
  • NOC (Numbers Of Comments)
  • POC (Percentage Of Comments)
  • NOM (Number of Methods)
  • Number of concretes (Number of classes and structs)
  • NOT (Number Of Tests)
  • NOI (Number Of Imports)
  • Frameworks dependency graph (number of internal and external dependencies)

Requirements

This is a Python 3 script that depends on matplotlib, adjustText, pyfunctional and pygraphviz.

This latest package depends on the Graphviz binary that must be installed before. If you're in a Mac environment, you can install it directly with brew install graphviz.

Usage

The package is available on pip with pip3 install swift-code-metrics.

The syntax is:

swift-code-metrics --source <path-to-swift-project> --artifacts <output-directory> --exclude <excluded-folders> --tests-paths <test-paths> --generate-graphs

  • --source is the path to the folder that contains the main Xcode project or Workspace
  • --artifacts path to the folder that will contain the generated output.json report
  • --exclude (optional) space separated list of path substrings to exclude from analysis (e.g. Tests will ignore all files/folders that contain Tests)
  • --tests-paths (default: Test Tests) space separated list of path substrings matching test classes
  • --generate-graphs (optional) if passed, it will generate the graphs related to the analysis and save them in the artifacts folder

Development

Please run ./install.sh and ./build_and_test.sh to install dependencies and run the tests.

The repo comes with a predefined setup for VS Code to debug and run tests as well.

Documentation

Please follow the guide with a practical example to get started.

Current limitations

  • This tool is designed for medium/large codebases composed by different frameworks. The script will scan the directory and it will identify the frameworks by the name of the 'root' folder, so it's strictly dependent on the file hierarchy (unless a project path override file is specified)

  • Libraries built with spm are not supported.

  • The framework name is inferred using the directory structure. If the file is in the root dir, the default_framework_name will be used. No inspection of the xcodeproj will be made.

  • The list of methods currently doesn't support computed vars

  • Inline comments in code (such as struct Data: {} //dummy data) are currently not supported

  • Only XCTest test frameworks are currently supported

TODOs

  • Code improvements
  • Other (open to suggestions)

Contact

Mattia Campolese

Metadata

Release files for swift-code-metrics 1.5.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swift-code-metrics 1.5.4
File Size Uploaded
swift-code-metrics-1.5.4.tar.gz 25.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swift-code-metrics 1.5.4
File Interpreter ABI Platform
swift_code_metrics-1.5.4-py3-none-any.whl Python 3 none any Details

Total release size: 54.1 kB

Release files / swift-code-metrics-1.5.4.tar.gz

Download URL swift-code-metrics-1.5.4.tar.gz
Size 25.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c69c023a48bdf8e81477bb3a210249cfce2d17422c3ae26b079c811accac12c8
BLAKE2b-256 checksum
How to use checksums
4b1a3e4884b490ff72e1f30f4c0e97596c35abddd60662d7f5d3e57198dd717e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / swift_code_metrics-1.5.4-py3-none-any.whl

Download URL swift_code_metrics-1.5.4-py3-none-any.whl
Size 28.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4896db6bec555045f782865b0e790acc0007abacef1eceafc46693855fa19cae
BLAKE2b-256 checksum
How to use checksums
952314cfaabf6f854660a3b4e96a060caf9d3142748ff670da0a0e5fe2cd4859
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.5.4 This release

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page