Skip to content

Commit 05a279c

Browse files
authored
Merge pull request #105 from smartmobilefactory/feature/SMFIT-1033
SMFI-1033: Disable certain rules for SwiftUI Projects
2 parents 3c0778f + 57e59d3 commit 05a279c

4 files changed

Lines changed: 110 additions & 7 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ The script for an app should look like:
2727
```
2828
"${SRCROOT}/Submodules/SMF-iOS-CommonProjectSetupFiles/setup-common-project-files.sh" --buildconfig "${CONFIGURATION}" --targettype "${PRODUCT_TYPE}"
2929
```
30+
31+
If the App uses SwiftUI, this call should be used:
32+
33+
```
34+
"${SRCROOT}/Submodules/SMF-iOS-CommonProjectSetupFiles/setup-common-project-files.sh" --buildconfig "${CONFIGURATION}" --targettype "${PRODUCT_TYPE}" --SwiftUI
35+
```
36+
3037
If you're developing on a framework use this line on the all of your Unit Test targets:
3138

3239
```

SwiftLint/copy-and-run-swiftlint-config.sh

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ readonly temporarySwiftLintConfigFilename=".$(uuidgen)-swiftlint.yml"
1818

1919
projectDir="$1"
2020
isFramework=$2
21+
swiftUI=$3
2122

2223
#
2324
# Check requirements
2425
#
2526

2627
# Check if project dir is provided. If not: Use the scripts base directory.
2728
if [ -z "$1" ]; then
28-
projectDir="$scriptBaseFolderPath"
29+
projectDir="$scriptBaseFolderPath"
2930
fi
3031

3132
# Contains the local SwiftLint settings (.project-swiftlint.yml). Global so we dont read it multiple times.
@@ -35,7 +36,7 @@ declare -a local_lines
3536
function read_local_settings() {
3637
let i=0
3738
while IFS=$'\n' read -r -a line_data; do
38-
local_lines[i]="${line_data}"
39+
local_lines[i]="${line_data}"
3940
((++i))
4041
done < $1
4142
}
@@ -64,7 +65,7 @@ function write_local_settings() {
6465
done
6566
}
6667

67-
function merge_commons_with_project_excluded_paths () {
68+
function merge_commons_with_project_excluded_paths() {
6869

6970
read_local_settings "$projectDir/.project-swiftlint.yml"
7071

@@ -83,6 +84,73 @@ function merge_commons_with_project_excluded_paths () {
8384
return 0
8485
}
8586

87+
# Adds disabled rules to diabled section and removes custom rules for the custom rules section
88+
function add_swiftUI_disabled_rules() {
89+
disabled_pattern="disabled_rules:"
90+
disabled_flag_1=false
91+
disabled_flag_2=false
92+
in_rule=false
93+
temporarySwiftUILintConfigFilename=".$(uuidgen)-swiftlint-swift-ui.yml"
94+
local base_file=$1
95+
96+
touch "$temporarySwiftUILintConfigFilename"
97+
98+
while IFS= read -r line_1 || [[ -n "$line_1" ]]; do
99+
100+
if [[ $disabled_flag_1 == true ]]; then
101+
102+
while IFS= read -r line_2 || [[ -n "$line_2" ]]; do
103+
104+
if [[ $disabled_flag_2 == true ]]; then
105+
echo "$line_2" >> "$temporarySwiftUILintConfigFilename"
106+
fi
107+
108+
if [[ $line_2 =~ $disabled_pattern ]]; then
109+
disabled_flag_2=true
110+
fi
111+
done < "swiftlint+swiftUI.yml"
112+
disabled_flag_1=false
113+
fi
114+
115+
disabled_flag_2=false
116+
117+
if [[ $line_1 =~ $disabled_pattern ]]; then
118+
disabled_flag_1=true
119+
echo $line_1 >> "$temporarySwiftUILintConfigFilename"
120+
else
121+
while IFS= read -r line_2 || [[ -n "$line_2" ]]; do
122+
if [[ $disabled_flag_2 == true ]]; then
123+
if [[ $line_1 =~ ${line_2:3} ]]; then
124+
in_rule=first
125+
fi
126+
fi
127+
128+
if [[ $line_2 =~ $disabled_pattern ]]; then
129+
disabled_flag_2=true
130+
fi
131+
done < "swiftlint+swiftUI.yml"
132+
133+
if [[ $in_rule == false ]]; then
134+
echo "$line_1" >> "$temporarySwiftUILintConfigFilename"
135+
elif [[ $in_rule == first ]]; then
136+
in_rule=true
137+
elif [[ ${line_1:3:1} != " " ]]; then
138+
echo "$line_1" >> "$temporarySwiftUILintConfigFilename"
139+
in_rule=false
140+
fi
141+
142+
if [[ $line_1 =~ $disabled_pattern ]]; then
143+
disabled_flag_1=true
144+
fi
145+
fi
146+
147+
done < "$base_file"
148+
149+
cp "$temporarySwiftUILintConfigFilename" "$temporarySwiftLintConfigFilename"
150+
rm "$temporarySwiftUILintConfigFilename"
151+
return 0
152+
}
153+
86154
#
87155
# Logic
88156
#
@@ -92,10 +160,19 @@ cd "$scriptBaseFolderPath"
92160

93161
# Merge the excluded paths of the commons and the project specific configuration
94162
if [ -f "$projectDir/.project-swiftlint.yml" ]; then
95-
merge_commons_with_project_excluded_paths
163+
merge_commons_with_project_excluded_paths
164+
165+
if [[ $swiftUI == true ]]; then
166+
add_swiftUI_disabled_rules "$temporarySwiftLintConfigFilename"
167+
fi
96168
else
97-
# Copy the normal swiftlint file as tempoary one as this file is used later
98-
cp "swiftlint.yml" "$temporarySwiftLintConfigFilename"
169+
# Disabled certain rules listet in swiftlint+swiftUI.yml if this is a swiftUI project
170+
if [[ $swiftUI == true ]]; then
171+
add_swiftUI_disabled_rules "swiftlint.yml"
172+
else
173+
# Copy the normal swiftlint file as tempoary one as this file is used later
174+
cp "swiftlint.yml" "$temporarySwiftLintConfigFilename"
175+
fi
99176
fi
100177

101178
if [ $isFramework = true ]; then

SwiftLint/swiftlint+swiftUI.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
###
2+
# swiftlint+swiftUI.yml
3+
# For a swiftUI projects only
4+
#
5+
# This list of disabled rules gets appended to the swiftlint.yml when it gets copied into the root project directory.
6+
# You can add more "disabled_rules" here.
7+
8+
# rule identifiers to exclude from running
9+
disabled_rules:
10+
- multiple_closures_with_trailing_closure
11+
- missing_closure_name

setup-common-project-files.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ readonly noXcodeCheck="--no-xcodecheck"
1717
readonly buildConfigurationFlag="--buildconfig"
1818
readonly targetTypeFlag="--targettype"
1919
readonly breakingInternalFrameworkVersioningFlag="--use-breaking-internal-framework-versioning"
20+
readonly swiftUIFlag="--SwiftUI"
2021

2122
readonly scriptBaseFolderPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2223

@@ -33,6 +34,7 @@ callCodeClimate=true
3334
checkXcodeVersion=true
3435
projectDir="$(pwd)"
3536
isDebugConfiguration=false
37+
isSwiftUIProject=false
3638

3739
#
3840
# Methods
@@ -45,6 +47,7 @@ function display_usage () {
4547
echo -e "$noCodebeatFlag\t\t\t\t- Don't copy the default SMF codebeat configuration"
4648
echo -e "$noCodeClimateFlag\t\t\t- Don't copy the default SMF Code Climate configuration"
4749
echo -e "$breakingInternalFrameworkVersioningFlag\t -Use the \"BreakingInternal framework versioning system\" (only for frameworks)"
50+
echo -e "$swiftUIFlag\t\t\t\t- Use custom SwiftLint rules for swift ui projects"
4851
echo -e "\nUsage:\n$ $0 $noCodebeatFlag"
4952
echo -e "or:\n$ $0 $noCodebeatFlag /Code/Project/Test"
5053
}
@@ -93,6 +96,11 @@ while test $# -gt 0; do
9396
shift
9497
# break
9598
;;
99+
$swiftUIFlag)
100+
isSwiftUIProject=true
101+
shift
102+
# break
103+
;;
96104
$noCodeClimateFlag)
97105
callCodeClimate=false
98106
shift
@@ -123,7 +131,7 @@ cd "$scriptBaseFolderPath"
123131
#
124132

125133
if [ $callSwiftlint = true ]; then
126-
./SwiftLint/copy-and-run-swiftlint-config.sh "$projectDir" $isFramework || exit 1;
134+
./SwiftLint/copy-and-run-swiftlint-config.sh "$projectDir" $isFramework $isSwiftUIProject || exit 1;
127135
fi
128136

129137
if [ $callCodebeat = true ]; then

0 commit comments

Comments
 (0)