Skip to content

Commit

Permalink
Add support for swiftformat (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
mormaer authored Aug 23, 2023
1 parent 58ed715 commit 50977d6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
28 changes: 28 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

export PATH="$PATH:/opt/homebrew/bin"

regex="\(Mlem/.*\).swift$"
formatter=$(which swiftformat)

check_for_swiftformat() {
if [ ! -x "$formatter" ]
then
1>&2 echo "Unable to find swiftformat - no formatting will take place"
exit 0
fi
}

format_staged_files() {
git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while read line; do
$formatter "${line}"
git add "$line"
done
}

main() {
check_for_swiftformat
format_staged_files
}

main
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ iOSInjectionProject/
**/xcshareddata/WorkspaceSettings.xcsettings

# End of https://www.toptal.com/developers/gitignore/api/xcode,swift,macos

# Brew
Brewfile.lock.json
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.8
6 changes: 6 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--wraparguments before-first
--commas inline
--disable wrapMultilineStatementBraces
--trimwhitespace nonblank-lines
--self init-only
--stripunusedargs closure-only
14 changes: 11 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ If you're reading this, you probably want to contribute to Mlem. Welcome! We're

## Prerequesites

This project makes use of [SwiftLint](https://github.com/realm/SwiftLint#swiftlint). This runs as part of the Xcode build phases.
This project makes use of the following tools:

In order to benefit please ensure you have [Homebrew](https://brew.sh) installed on your system and then run the following command to install Swiftlint:
[SwiftLint](https://github.com/realm/SwiftLint#swiftlint). This runs as part of the Xcode build phases.
[Swiftformat](https://github.com/nicklockwood/SwiftFormat#what-is-this). This runs as a pre-commit hook.

`brew install swiftlint`
In order to benefit please ensure you have [Homebrew](https://brew.sh) installed on your system and then run the following commands inside the project directory:

`cd /path/to/this/repo`
`brew update`
`brew bundle`
`git config --local --add core.hooksPath .git-hooks`

With these steps completed each time you build your code will be linted, and each time you commit your code will be formatted.

## Getting started

Expand Down
2 changes: 2 additions & 0 deletions brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
brew "swiftlint"
brew "swiftformat"

0 comments on commit 50977d6

Please sign in to comment.