Skip to content

Commit c6eed4c

Browse files
author
Matt Boran
committed
Change to simpler xcode plugin
1 parent 4c75259 commit c6eed4c

6 files changed

Lines changed: 19 additions & 179 deletions

File tree

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Selective (Swift) Linter
22

3-
This is a package to be used as a pre-commit git hook to lint changes in .swift files using [Swiftlint Installed via Homebrew](https://formulae.brew.sh/formula/swiftlint).
3+
This is a package to be used to lint changes in .swift files using [Swiftlint Installed via Homebrew](https://formulae.brew.sh/formula/swiftlint). It's meant to be used as a run script in Xcode.
44

55
### Installation and Dependencies
66

@@ -14,16 +14,13 @@ To install this Python3 package
1414
$ pip3 install selective_linter
1515
```
1616

17-
To install the git hook to a repository, navigate to the root of that repository and run
17+
To install this into an Xcode project, add the following run script build phase in project settings:
1818

19+
Shell: `/usr/bin/env bash`
1920
```sh
20-
$ selective_linter -i
21-
```
22-
23-
This also adds the `.lint_cache` file to your `.gitignore`.
24-
25-
To deactivate the script run
26-
27-
```sh
28-
$ git config hooks.skipswiftlint true
21+
if [ "${CONFIGURATION}" == "Debug" ]; then
22+
if which selective_linter >/dev/null; then
23+
selective_linter
24+
fi
25+
fi
2926
```

selective_linter/git_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _print_debug_info(self):
7272
print("Files staged: {}".format("\n\t".join(sorted(self.staged_files))))
7373

7474
def _get_files_changed(self):
75-
files = self._git('--no-pager', 'diff', '--staged', '--name-only')
75+
files = self._git('--no-pager', 'diff', '--name-only')
7676
return [self._directory + '/' + filename
7777
for filename in files.split('\n')
7878
if filename and filename.endswith(".swift") and os.path.isfile(filename)]

selective_linter/installer.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

selective_linter/output.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
import hashlib
33
import os
44

5-
from colorama import Fore, Style
6-
7-
8-
class Warning:
9-
10-
BYPASS_WARNING = Fore.YELLOW + "Committing warnings or errors." + Fore.RESET
11-
12-
135
class LintError:
146

157
def __init__(self, error):
@@ -23,42 +15,4 @@ def __init__(self, error):
2315
self.code = error[aux_length:]
2416

2517
def __str__(self):
26-
where = self.file + ":" + self.line
27-
if "warning" in self.error_type:
28-
error_type = LintError._warning(self.description)
29-
elif "error" in self.error_type:
30-
error_type = LintError._error(self.description)
31-
return where + '\n' + error_type + self.code
32-
33-
@staticmethod
34-
def _warning(warning):
35-
return Fore.YELLOW + "(warning)" + warning + ":" + Fore.RESET
36-
37-
@staticmethod
38-
def _error(error):
39-
return Fore.RED + "(error)" + error + ":" + Fore.RESET
40-
41-
42-
class ChangeVerifier:
43-
44-
CACHE_NAME = '.lint_cache'
45-
46-
def __init__(self, hunks):
47-
hashes = [hashlib.md5(hunk.encode()).hexdigest() for hunk in hunks]
48-
self.long_hash = "".join(hashes)
49-
50-
def has_unchanged_cache(self):
51-
if not os.path.isfile(self.CACHE_NAME):
52-
return False
53-
long_hash = None
54-
with open(self.CACHE_NAME, 'r') as file:
55-
long_hash = file.read()
56-
return self.long_hash == long_hash
57-
58-
def write_cache(self):
59-
with open(self.CACHE_NAME, 'w+') as file:
60-
file.write(self.long_hash)
61-
62-
def clear_cache(self):
63-
if os.path.isfile(self.CACHE_NAME):
64-
os.remove(self.CACHE_NAME)
18+
return self.file + ":" + self.line + ": warning:" + self.description

selective_linter/selective_linter.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,33 @@
99
action='store',
1010
type=str,
1111
default=os.getcwd())
12-
parser.add_argument('-i', '--install',
13-
action='store_true')
14-
parser.set_defaults(install=False)
15-
1612
running_as_script = False
1713

1814

1915
def main():
2016
# Imports for running locally and running as module
2117
if running_as_script:
22-
from git_diff import GitDiff, GitHunk
18+
from git_diff import GitDiff
2319
from linter import SwiftLint
24-
from output import LintError, ChangeVerifier, Warning
25-
from installer import Installer
20+
from output import LintError
2621
else:
2722
from selective_linter.git_diff import GitDiff
2823
from selective_linter.linter import SwiftLint
29-
from selective_linter.output import LintError, ChangeVerifier, Warning
30-
from selective_linter.installer import Installer
24+
from selective_linter.output import LintError
3125

32-
# Parse command line arguments and install if necessary
3326
args = parser.parse_args()
34-
if args.install:
35-
installer = Installer(args.dir)
36-
if installer.is_repository():
37-
installer.install()
38-
return 0
39-
else:
40-
print("Failed to install to directory")
41-
return 1
42-
27+
4328
# Process diffs and lint
4429
differ = GitDiff(args.dir)
4530
differ.diff(verbose=running_as_script)
4631
linter = SwiftLint(dir=args.dir, files=differ.staged_files)
4732
errors = linter.check_errors_against_diff(differ.diff_lines)
4833
exit_code = 0
49-
cache = ChangeVerifier(differ.hunks)
5034
if errors:
51-
# If the user made the same commit twice, bypass this check
52-
# The cache is cleared by the pre commit hook
53-
if cache.has_unchanged_cache():
54-
print(Warning.BYPASS_WARNING)
55-
return exit_code
56-
cache.write_cache()
5735
for error in sorted(errors):
5836
print(LintError(error))
59-
print("\nErrors found. Please fix errors and retry or make no changes and re-commit to bypass.")
60-
exit_code = 1
61-
return exit_code
37+
return 0
38+
return 0
6239

6340

6441
if __name__ == "__main__":

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55

66
setuptools.setup(
77
name="selective_linter",
8-
version="0.3.0",
8+
version="1.0.0",
99
author="Matt Boran",
1010
author_email="mattboran@gmail.com",
11-
description="A script to be used as a pre-commit git hook for projects written in Swift",
11+
description="A script to be run from Xcode to lint files as you make changes",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
url="https://github.com/mattboran/SelectiveSwiftLinter",
15-
download_url="https://github.com/mattboran/SelectiveSwiftLinter/releases/download/0.3.0/selective_linter-0.3.0-py3-none-any.whl",
15+
download_url="https://github.com/mattboran/SelectiveSwiftLinter/releases/download/1.0.0/selective_linter-1.0.0-py3-none-any.whl",
1616
packages=setuptools.find_packages(),
1717
classifiers=[
1818
"Programming Language :: Python :: 3",
1919
"License :: OSI Approved :: MIT License",
2020
"Operating System :: MacOS :: MacOS X",
2121
],
2222
install_requires=[
23-
'colorama',
2423
'sh'
2524
],
2625
python_requires='>3.0.0',

0 commit comments

Comments
 (0)