• Stars
    star
    101
  • Rank 338,094 (Top 7 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for Swift code modification intermediating between code generation and formatting.

swift-mod

A tool for Swift code modification intermediating between code generation and formatting.

swift release test lincense


Overview

swift-mod is a tool for Swift code modification that intermediating between code generator and formatter built on top of apple/SwiftSyntax.
It can generates boilerplate code, such as access control or memberwise initializers in modularized source code, taking into account the state of the AST.
You can improve your productivity for writing more advanced Swift codes with introducing swift-mod.

Example

  • Before
struct Avenger {
    var heroName: String
    internal var realName: String?
}

let avengers = [
    Avenger(heroName: "Iron Man", realName: "Tony Stark"),
    Avenger(heroName: "Captain America", realName: "Steve Rogers"),
    Avenger(heroName: "Thor"),
]
  • After
+ public struct Avenger {
+     public var heroName: String
    internal var realName: String?

+     public init(
+         heroName: String,
+         realName: String? = nil
+     ) {
+         self.heroName = heroName
+         self.realName = realName
+     }
}

+ public let avengers = [
    Avenger(heroName: "Iron Man", realName: "Tony Stark"),
    Avenger(heroName: "Captain America", realName: "Steve Rogers"),
    Avenger(heroName: "Thor"),
]

Getting Started

  1. Install swift-mod.

  2. Generates configuration file.

swift-mod init
  1. Check the all modification rules.
swift-mod rules
swift-mod rules --rule [RULE NAME] # Display more detailed rule description
  1. Edit your configuration file with an editor you like, refering to the documentation.

  2. Run

swift-mod <list of input files>

Command Usage

swift-mod [COMMAND] [OPTIONS]

All commands can be display a usage by option -h/--help.

  • run (or not specified)
OVERVIEW: Runs modification.

USAGE: swift-mod run [--mode <mode>] [--configuration <configuration>] [<paths> ...]

ARGUMENTS:
  <paths>                 Zero or more input filenames.

OPTIONS:
  -m, --mode <mode>                   Overrides running mode: modify|dry-run|check. (default: modify)
  -c, --configuration <configuration> The path to a configuration file.
  -h, --help                          Show help information.
  • init
OVERVIEW: Generates a modify configuration file.

USAGE: swift-mod init [--output <output>]

OPTIONS:
  -o, --output <output>   An output for the configuration file to be generated.
  -h, --help              Show help information.
  • rules
OVERVIEW: Display the list of rules.

USAGE: swift-mod rules [--rule <rule>]

OPTIONS:
  -r, --rule <rule>       A rule name to see detail.
  -h, --help              Show help information.

Configuration

Modification rules and targets are defines with YAML-formatted file. By default, it's searched by name .swift-mod.yml.
Any file name is allowed with passing with option like follows:

swift-mod --configuration <configuration>

Example

format:
  indent: 4
  lineBreakBeforeEachArgument: true
rules:
  defaultAccessLevel:
    accessLevel: openOrPublic
    implicitInternal: true
  defaultMemberwiseInitializer:
    implicitInitializer: false
    implicitInternal: true
    ignoreClassesWithInheritance: false

Format

Determines the format setting in all rules.
Format according to this setting only when changes occur.

KEY VALUE REQUIREMENT DEFAULT
indent The number of spaces, or tab by text Optional 4
lineBreakBeforeEachArgument Indicating whether to insert new lines before each function argument Optional true

Rules

Default Access Level

IDENTIFIER OVERVIEW
defaultAccessLevel Assigns the suitable access level to all declaration syntaxes if not present
KEY VALUE REQUIREMENT DEFAULT
accessLevel |openOrPublic|public|internal|fileprivate|private| Required
implicitInternal Indicating whether to omit the internal access level Optional true
struct Avenger {
    var heroName: String
    internal var realName: String?
}
+ public struct Avenger {
+    public var heroName: String
    internal var realName: String?
}

Default Memberwise Initializer

IDENTIFIER OVERVIEW
defaultMemberwiseInitializer Defines a memberwise initializer according to the access level in the type declaration if not present
KEY VALUE REQUIREMENT DEFAULT
implicitInitializer Indicating whether to omit the internal initializer in struct decalaration Optional false
implicitInternal Indicating whether to omit the internal access level Optional true
ignoreClassesWithInheritance Indicating whether to skip the classes having inheritance including protocol Optional false
struct Avenger {
    var heroName: String
    internal var realName: String?
}
struct Avenger {
    var heroName: String
    internal var realName: String?

+    init(
+        heroName: String,
+        realName: String? = nil
+    ) {
+        self.heroName = heroName
+        self.realName = realName
+    }
}

Ignoring Rules

swift-mod allows users to suppress modification for node and its children by comment like below.

  • Ignore all rules

// swift-mod-ignore

// swift-mod-ignore
struct Avenger {
    var heroName: String
    internal var realName: String?
}
  • Ignore specific rule(s)

// swift-mod-ignore: [COMMA DELIMITED RULE IDENTIFIERS]

// swift-mod-ignore: defaultAccessLevel, defaultMemberwiseInitializer
struct Avenger {
    var heroName: String
    internal var realName: String?
}

Installation

Add the following to the dependencies of your Package.swift:

dependencies: [
    .package(url: "https://github.com/ra1028/swift-mod.git", from: "swift-mod version"),
]

Run command:

swift run -c release swift-mod [COMMAND] [OPTIONS]

Install with Mint by following command:

mint install ra1028/swift-mod

Run command:

mint run ra1028/swift-mod [COMMAND] [OPTIONS]

Using a pre-built binary

You can also install swift-mod by downloading swift-mod.zip from the latest GitHub release.

Swift Version Support

swift-mod depends on SwiftSyntax that the version in use must match the toolchain version until Swift 5.7.
So you should use swift-mod version that built with compatible version of Swift you are using.

Swift Version Last Supported swift-mod Release
5.1 0.0.2
5.2 0.0.4
5.3 0.0.5
5.4 0.0.6
5.5 0.0.7
5.6 0.1.0
5.7 0.1.1
5.8 and later latest

Development

Pull requests, bug reports and feature requests are welcome 🚀.
See CONTRIBUTING.md file to learn how to contribute to swift-mod.

Please validate and test your code before you submit your changes by following commands:

 make autocorrect # Modifying, formatting, linting codes and generating Linux XCTest manifests.  
 make test

In addition, swift-mod supports running on Linux, so you should test by installing Docker and following command:

make docker-test

License

swift-mod is released under the Apache 2.0 License.

More Repositories

1

DifferenceKit

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
Swift
3,421
star
2

RACollectionViewReorderableTripletLayout

The custom collectionView layout that can perform reordering of cells by dragging it.
Objective-C
1,481
star
3

Former

Former is a fully customizable Swift library for easy creating UITableView based form.
Swift
1,307
star
4

Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
Swift
1,291
star
5

RAReorderableLayout

A UICollectionView layout whitch can move item with drag and drop.
Swift
867
star
6

DiffableDataSources

💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
Swift
836
star
7

swiftui-hooks

🪝 A SwiftUI implementation of React Hooks. Enhances reusability of stateful logic and gives state and lifecycle to function view.
Swift
479
star
8

SwiftUI-Combine

This is an example project of SwiftUI and Combine using GitHub API.
Swift
452
star
9

VueFlux

♻️ Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux
Swift
331
star
10

PathDynamicModal

A modal view using UIDynamicAnimator, like the Path for iOS.
Swift
286
star
11

swiftui-atom-properties

⚛️ Atomic approach state management and dependency injection for SwiftUI
Swift
232
star
12

FloatingActionSheetController

FloatingActionSheetController is a cool design ActionSheetController library written in Swift2.
Swift
140
star
13

DelegateProxy

Proxy for receive delegate events more practically
Swift
129
star
14

RASlideInViewController

RASlideInViewController has an transition effect expressing the depth, and you can dismiss it by draging
Objective-C
127
star
15

Alembic

⚗️ Functional JSON Parser - Linux Ready 🐧
Swift
116
star
16

SwiftUI-Flux

🚀 This is a tiny experimental application using SwiftUI with Flux architecture.
Swift
54
star
17

monkey-lang-swift

The Monkey Programming Language written in Swift -- Writing An Interpreter In Go
Swift
17
star
18

LiveStreamingApp

A sample app repository that broadcasting and player of HLS for iOS.
Swift
16
star
19

KenBurnsSlideshowView

Slideshow with Ken Burns effect for iOS.
Swift
11
star
20

VueFluxExample-GitHub

VueFlux VueFluxReactive example project
Swift
8
star
21

OwnKit

My own utility toolkit for ios
Swift
7
star
22

ra1028

2
star
23

Dribbble_client_sample

Swift
1
star