|
| 1 | +#!/bin/bash |
| 2 | +# Copies the Swiflint configuration to the projects base folder |
| 3 | +# |
| 4 | +# Author Hans Seiffert |
| 5 | +# Updated Kevin Delord |
| 6 | +# |
| 7 | +# Last revised 17/09/2020 |
| 8 | + |
| 9 | +# |
| 10 | +# Constants |
| 11 | +# |
| 12 | + |
| 13 | +readonly scriptBaseFolderPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 14 | +readonly reportFileName="swiftlint-rules-report.txt" |
| 15 | +readonly regexFirstOccurenceRuleName="([A-Za-z_]+).*" |
| 16 | +readonly regexEnabledRuleInReport="([A-Za-z_]+)[ ]+(no|yes)[ ]+(no|yes)[ ]+(no|yes)" |
| 17 | + |
| 18 | +# |
| 19 | +# Variables |
| 20 | +# |
| 21 | + |
| 22 | +projectDir="$1" |
| 23 | +outputFile="$2" |
| 24 | + |
| 25 | +# |
| 26 | +# Check requirements |
| 27 | +# |
| 28 | + |
| 29 | +# Check if project directory is provided. If not: Use the scripts base directory. |
| 30 | +if ( [[ -z "$1" ]] || [[ ! -d "$1" ]] ); then |
| 31 | + projectDir="$scriptBaseFolderPath" |
| 32 | +fi |
| 33 | + |
| 34 | +# Check if the output file is a directory |
| 35 | +if ( [[ ! -z "$2" ]] && [[ -d "$2" ]] ); then |
| 36 | + outputFile="$2/$reportFileName" |
| 37 | +fi |
| 38 | + |
| 39 | +# Or check if the output file is not provided |
| 40 | +if ( [[ -z "$2" ]] ); then |
| 41 | + outputFile="$projectDir/$reportFileName" |
| 42 | +fi |
| 43 | + |
| 44 | +# |
| 45 | +# Global Arrays |
| 46 | +# |
| 47 | + |
| 48 | +# Contains a list of disabled rules (strings) |
| 49 | +declare -a all_disabled_rules |
| 50 | + |
| 51 | +# Extract the values from a YML section. |
| 52 | +# Parameters: |
| 53 | +# $1: String representing the section to find adn extract the values from |
| 54 | +function extract_values_from_section() { |
| 55 | + # Variable which is true if we are in the excluded section |
| 56 | + local is_in_matched_section=false |
| 57 | + |
| 58 | + # Section to look for |
| 59 | + local matched_section=$2 |
| 60 | + |
| 61 | + # Prepare storage |
| 62 | + unset all_disabled_rules |
| 63 | + let i=0 |
| 64 | + |
| 65 | + while IFS= read -r line; do |
| 66 | + # for line in "${local_lines[@]}"; do |
| 67 | + |
| 68 | + # Determine if we are in the excluded section |
| 69 | + if [[ "$line" == "$matched_section" ]]; then |
| 70 | + is_in_matched_section=true |
| 71 | + continue |
| 72 | + elif [[ "$line" =~ (^$) ]]; then |
| 73 | + is_in_matched_section=false |
| 74 | + fi |
| 75 | + |
| 76 | + # IF the current line is within the matched section |
| 77 | + if $is_in_matched_section; then |
| 78 | + # If any, remove the prefix for commenting out disabled rules. |
| 79 | + prefix="# \[Disabled Custom Rule\] " |
| 80 | + line="${line/#$prefix}" |
| 81 | + # AND IF the action is to "match and store the rule" it into an array. |
| 82 | + if [[ $line =~ $regexFirstOccurenceRuleName ]]; then |
| 83 | + disabled_rule="${BASH_REMATCH[1]}" |
| 84 | + # Apply new format as to better identify the custom rules. |
| 85 | + # The listing of a rule is always "| name_of_rule" |
| 86 | + # Format: { prefix: "| " } |
| 87 | + all_disabled_rules[i]="$disabled_rule" |
| 88 | + ((++i)) |
| 89 | + fi |
| 90 | + fi |
| 91 | + done < "$1" |
| 92 | +} |
| 93 | + |
| 94 | +# An error message occurs when a custom rule is disabled but its declaration within the 'disbaled_rules' |
| 95 | +# section is still active. This function comments out the rule declaration declaration. |
| 96 | +# Parameters: |
| 97 | +# $1: The base configuration file to comment out the disabled custom rules from. |
| 98 | +function remove_disabled_rules_from_report() { |
| 99 | + |
| 100 | + # Create output file |
| 101 | + tmpOutputFile="$1.clean" |
| 102 | + touch "$tmpOutputFile" |
| 103 | + |
| 104 | + # Parse the source file and comment out the disabled custom rules |
| 105 | + while IFS= read -r line; do |
| 106 | + if [[ $line =~ $regexFirstOccurenceRuleName ]]; then |
| 107 | + rule_name="${BASH_REMATCH[1]}" |
| 108 | + # If the current line contains a disabled rule declaration then remove it from the report |
| 109 | + if [[ ! " ${all_disabled_rules[@]} " =~ " ${rule_name} " ]]; then |
| 110 | + echo "$line" >> "$tmpOutputFile" |
| 111 | + fi |
| 112 | + else |
| 113 | + # It is not a rule but an decorative element of the report |
| 114 | + echo "$line" >> "$tmpOutputFile" |
| 115 | + fi |
| 116 | + done < "$1" |
| 117 | + |
| 118 | + # Replace the source file by the new output file |
| 119 | + mv "$tmpOutputFile" "$1" |
| 120 | +} |
| 121 | + |
| 122 | +function remove_enabled_rules_from_report() { |
| 123 | + |
| 124 | + # Create output file |
| 125 | + tmpOutputFile="$1.clean" |
| 126 | + touch "$tmpOutputFile" |
| 127 | + |
| 128 | + # Parse the source file and comment out the disabled custom rules |
| 129 | + while IFS= read -r line; do |
| 130 | + # Create a preformated string to better work with REGEX in bash... (remove '|') |
| 131 | + preformated_line="${line//|/ }" |
| 132 | + if [[ $preformated_line =~ $regexEnabledRuleInReport ]]; then |
| 133 | + is_rule_enabled="${BASH_REMATCH[4]}" |
| 134 | + if [[ $is_rule_enabled == "no" ]]; then |
| 135 | + # If the rule is not enabled in the swiftlint config, add it to the report. |
| 136 | + echo "$line" >> "$tmpOutputFile" |
| 137 | + fi |
| 138 | + else |
| 139 | + # It is not a rule but an decorative element of the report |
| 140 | + echo "$line" >> "$tmpOutputFile" |
| 141 | + fi |
| 142 | + done < "$1" |
| 143 | + |
| 144 | + # Replace the source file by the new output file |
| 145 | + mv "$tmpOutputFile" "$1" |
| 146 | +} |
| 147 | + |
| 148 | +# |
| 149 | +# Logic |
| 150 | +# |
| 151 | + |
| 152 | +# Go to the project folder and run swiftlint from there |
| 153 | +cd "$projectDir" |
| 154 | + |
| 155 | +# Extract the list of disabled rules |
| 156 | +extract_values_from_section ".swiftlint.yml" "disabled_rules:" |
| 157 | + |
| 158 | +# Get the complete overview of the default swiftlint rules |
| 159 | +touch "$outputFile" |
| 160 | + |
| 161 | +SWIFTLINT_EXECUTABLE="$scriptBaseFolderPath/portable_swiftlint/swiftlint" |
| 162 | +if [ -f "$SWIFTLINT_EXECUTABLE" ]; then |
| 163 | + $SWIFTLINT_EXECUTABLE rules > $outputFile |
| 164 | +elif which swiftlint >/dev/null; then |
| 165 | + echo "found it" |
| 166 | + swiftlint rules > $outputFile |
| 167 | +else |
| 168 | + echo "SwiftLint does not exist, please add it to the Alpha target in your Podfile" |
| 169 | + exit 1 |
| 170 | +fi |
| 171 | + |
| 172 | +if [ -e "$projectDir/Pods/SwiftLint" ]; then |
| 173 | + echo "warning: SwiftLint should not be added as Pod, as it is already located in SMF-iOS-CommonProjectSetupFiles" |
| 174 | +fi |
| 175 | + |
| 176 | +# Remove lines containing the disabled rules |
| 177 | +remove_disabled_rules_from_report $outputFile |
| 178 | + |
| 179 | +# Remove lines with "enabled in your config: yes" |
| 180 | +remove_enabled_rules_from_report $outputFile |
| 181 | + |
0 commit comments