From 50977d6663eac4219c1baa3315310f8eb199ac45 Mon Sep 17 00:00:00 2001 From: mormaer Date: Wed, 23 Aug 2023 21:21:17 +0100 Subject: [PATCH] Add support for `swiftformat` (#537) --- .git-hooks/pre-commit | 28 ++++++++++++++++++++++++++++ .gitignore | 3 +++ .swift-version | 1 + .swiftformat | 6 ++++++ CONTRIBUTING.md | 14 +++++++++++--- brewfile | 2 ++ 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100755 .git-hooks/pre-commit create mode 100644 .swift-version create mode 100644 .swiftformat create mode 100644 brewfile diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100755 index 000000000..87356d2fa --- /dev/null +++ b/.git-hooks/pre-commit @@ -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 diff --git a/.gitignore b/.gitignore index fb97cfa20..592bad339 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,6 @@ iOSInjectionProject/ **/xcshareddata/WorkspaceSettings.xcsettings # End of https://www.toptal.com/developers/gitignore/api/xcode,swift,macos + +# Brew +Brewfile.lock.json diff --git a/.swift-version b/.swift-version new file mode 100644 index 000000000..3659ea2fa --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +5.8 diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 000000000..89905fd2c --- /dev/null +++ b/.swiftformat @@ -0,0 +1,6 @@ +--wraparguments before-first +--commas inline +--disable wrapMultilineStatementBraces +--trimwhitespace nonblank-lines +--self init-only +--stripunusedargs closure-only diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e86406f93..f80cf3149 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/brewfile b/brewfile new file mode 100644 index 000000000..84e4a39fe --- /dev/null +++ b/brewfile @@ -0,0 +1,2 @@ +brew "swiftlint" +brew "swiftformat"