Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thumbtack/star

Generate reports on how frequently specified Swift types are being used in your iOS codebase
https://github.com/thumbtack/star

ios swift swift-package swift-package-manager swift-syntax

Last synced: about 2 months ago
JSON representation

Generate reports on how frequently specified Swift types are being used in your iOS codebase

Awesome Lists containing this project

README

        

[![Build Status](https://badgen.net/travis/thumbtack/star)](https://travis-ci.com/thumbtack/star)
[![SPM Latest Version](https://img.shields.io/github/v/release/thumbtack/star?label=SPM)](https://swiftpackageindex.com/thumbtack/star)
[![License](https://img.shields.io/github/license/thumbtack/star?color=important)](https://github.com/thumbtack/star/blob/main/LICENSE)

# Swift Type Adoption Reporter (STAR)

Generate reports on how frequently specified Swift types are being used in your iOS codebase with a simple command-line interface.

## Install

```
$ cd star
$ make install
```

## Usage

```
$ star --types Avatar Button Color Pill --files ./

Avatar used 27 times.
Button used 167 times.
Color used 2711 times.
Pill used 9 times.
```

To report on types which are in a separate module, specify a `--moduleName`. This will ensure that references like `Thumbprint.Button()` are captured too.

```
$ star --types Avatar Button Color Pill --files ./ --module Thumbprint

Avatar used 30 times.
Button used 182 times.
Color used 2786 times.
Pill used 11 times.
```

### Options

```
USAGE: star [--types ...] [--module ] [--format ] [--files ...] [--includeTypeInheritance] [--verbose]

OPTIONS:
-t, --types List of types on which to report
-m, --module Name of module containing types, to ensure types referenced by . are counted
-f, --format Output format (humanReadable|json) (default: humanReadable)
--files Paths in which to look for Swift source
--includeTypeInheritance
Include subclass and protocol conformance declarations in usage counts
-v, --verbose Print additional information about source as it is parsed
-h, --help Show help information.
```

## How it Works

STAR uses [SwiftSyntax](https://github.com/apple/swift-syntax) to traverse the AST and find references to the specified identifiers.
Since STAR operates on the untyped AST, usage reports may contain imperfect information when linking a reference to its identifier would require full type checking.

The reporter attempts to provide as useful information as possible, so some types of references are intentionally filtered out. For example, the line of code
```
let foo: UIView = UIView()
```
technically includes two nodes in the AST for the `UIView` identifier: one in the type annotation, and one in the constructor call. For most business uses, though, it is preferable to only count this line as a single use of `UIView`. Therefore, type annotations are ignored by STAR.

Some other examples of intentionally ignored references are code comments, class/struct/extension/etc. declarations, and inner classes within components (e.g., `MyComponent.SomeInnerClass` will match neither `MyComponent`, nor `SomeInnerClass`).

## Uninstall

```
$ cd star
$ make uninstall
```

## Importing into another Swift package

In addition to the command-line executable `star`, STAR's core functionality is also available through Swift Package Manager as the static library `STARLib`. To use `STARLib` in your Swift package, add the following to your Package.swift:
```
let package = Package(
...
dependencies: [
...
.package(name: "SwiftTypeAdoptionReporter", url: "https://github.com/thumbtack/star.git", ),
],
targets: [
.target(
...
dependencies: [
...
.product(name: "STARLib", package: "SwiftTypeAdoptionReporter"),
]
),
]
)
```

## Contributing

If you have ideas to make STAR more useful, open an issue or submit a pull request! See below for instructions on building/testing locally.

```
$ git clone [email protected]/thumbtack/star.git
$ cd star
$ open -a Xcode .
```

### To build & run locally:
```
$ swift run star ...
```

Passing in the `--verbose` argument will print out additional information which can be useful for debugging.

### To run test suite:

#### From command line:
```
$ swift test
```

#### With Xcode:
1. Create Xcode project:

```
$ swift package generate-xcodeproj
```
2. Open `SwiftTypeAdoptionReporter.xcodeproj`
3. In Xcode, **Product -> Test**