### Install via Mint Source: https://github.com/realm/swiftlint/blob/main/README.md Install SwiftLint using the Mint package manager. ```bash mint install realm/SwiftLint ``` -------------------------------- ### Install via Homebrew Source: https://github.com/realm/swiftlint/blob/main/README.md Install the SwiftLint binary using the Homebrew package manager. ```bash brew install swiftlint ``` -------------------------------- ### Pass Multiple Configurations via Command Line Source: https://github.com/realm/swiftlint/blob/main/README.md Example of running SwiftLint with multiple configuration files specified in the command line. ```bash swiftlint --config .swiftlint.yml --config .swiftlint_child.yml ``` -------------------------------- ### Install Buildkite agent and tools via Homebrew Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Run this command on a new MacStadium worker to install necessary dependencies including aria2, bazelisk, and the Buildkite agent. ```bash brew install aria2 bazelisk htop buildkite/buildkite/buildkite-agent robotsandpencils/made/xcodes ``` -------------------------------- ### Define rule examples with focus Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Use the #examples macro to define triggering and non-triggering test cases. Marking an example with .focused() restricts test execution to only those marked examples. ```swift nonTriggeringExamples: #examples([ "let x: [Int]", "let x: [Int: String]".focused(), // Only this one will be run in tests. ]), triggeringExamples: #examples([ "let x: ↓Array", "let x: ↓Dictionary", ]) ``` -------------------------------- ### Install Xcode via xcodes Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Updates the xcodes tool and installs a specific version of Xcode. ```bash xcodes update && xcodes install 14.0.0 ``` -------------------------------- ### Define Local Child and Parent Configurations Source: https://github.com/realm/swiftlint/blob/main/README.md Example file structure and the corresponding YAML configuration referencing local parent and child files. ```txt ProjectRoot |_ .swiftlint.yml |_ .swiftlint_refinement.yml |_ Base |_ .swiftlint_base.yml ``` ```yaml child_config: .swiftlint_refinement.yml parent_config: Base/.swiftlint_base.yml ``` -------------------------------- ### Define Team-Wide and Project-Specific Configurations Source: https://github.com/realm/swiftlint/blob/main/README.md Examples of YAML configurations for shared team rules and project-specific overrides. ```yaml disabled_rules: - force_cast ``` ```yaml opt_in_rules: - force_cast ``` -------------------------------- ### Integrate SwiftLint via CocoaPods Source: https://github.com/realm/swiftlint/blob/main/README.md Executes the SwiftLint binary provided by the CocoaPods installation. ```bash "${PODS_ROOT}/SwiftLint/swiftlint" ``` -------------------------------- ### Reference Remote Configuration Source: https://github.com/realm/swiftlint/blob/main/README.md Example of referencing a remote configuration file via URL in a .swiftlint.yml file. ```yaml parent_config: https://myteamserver.com/our-base-swiftlint-config.yml ``` -------------------------------- ### Build and debug SwiftLint via command line Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Commands for building the project, running the binary, and attaching the LLDB debugger. ```shell swift build [-c release] ``` ```shell swift run [-c release] [swiftlint] [arguments] ``` ```shell .build/[release|debug]/swiftlint ``` ```shell lldb -- .build/[release|debug]/swiftlint [arguments] ``` -------------------------------- ### Display SwiftLint help information Source: https://github.com/realm/swiftlint/blob/main/README.md View the overview, usage, options, and subcommands available in the SwiftLint CLI. ```txt $ swiftlint help OVERVIEW: A tool to enforce Swift style and conventions. USAGE: swiftlint OPTIONS: --version Show the version. -h, --help Show help information. SUBCOMMANDS: analyze Run analysis rules docs Open SwiftLint documentation website in the default web browser generate-docs Generates markdown documentation for selected group of rules lint (default) Print lint warnings and errors baseline Operations on existing baselines reporters Display the list of reporters and their identifiers rules Display the list of rules and their identifiers version Display the current version of SwiftLint See 'swiftlint help ' for detailed help. ``` -------------------------------- ### Run SwiftLint with Bazel Source: https://github.com/realm/swiftlint/blob/main/README.md Execute SwiftLint in the current directory using Bazel. ```console bazel run -c opt @SwiftLint//:swiftlint ``` -------------------------------- ### Open project in Xcode Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Command to open the current directory in Xcode. ```shell xed . ``` -------------------------------- ### Run SwiftLint with Docker Source: https://github.com/realm/swiftlint/blob/main/README.md Commands to pull the SwiftLint Docker image and execute it against the current working directory. ```bash docker pull ghcr.io/realm/swiftlint:latest ``` ```bash docker run -it -v `pwd`:`pwd` -w `pwd` ghcr.io/realm/swiftlint:latest ``` ```bash $ docker run -it -v `pwd`:`pwd` -w `pwd` ghcr.io/realm/swiftlint:latest Linting Swift files in current working directory Linting 'RuleDocumentation.swift' (1/490) ... Linting 'YamlSwiftLintTests.swift' (490/490) Done linting! Found 0 violations, 0 serious in 490 files. ``` -------------------------------- ### Configure Git pre-commit Hook Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint to your .pre-commit-config.yaml file to automate linting in your repository. ```yaml repos: - repo: https://github.com/realm/SwiftLint rev: 0.57.1 hooks: - id: swiftlint ``` ```yaml - repo: https://github.com/realm/SwiftLint rev: 0.57.1 hooks: - id: swiftlint entry: swiftlint --fix --strict ``` -------------------------------- ### Run project tests Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Commands to execute the test suite using various build systems. ```shell xcodebuild -scheme swiftlint test -destination 'platform=macOS' swift test make bazel_test make docker_test ``` -------------------------------- ### Configure Bazel Dependency Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint to your Bazel project configuration. ```bzl bazel_dep(name = "swiftlint", version = "0.52.4", repo_name = "SwiftLint") ``` ```bzl load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "build_bazel_rules_apple", sha256 = "390841dd5f8a85fc25776684f4793d56e21b098dfd7243cd145b9831e6ef8be6", url = "https://github.com/bazelbuild/rules_apple/releases/download/2.4.1/rules_apple.2.4.1.tar.gz", ) load( "@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies", ) apple_rules_dependencies() load( "@build_bazel_rules_swift//swift:repositories.bzl", "swift_rules_dependencies", ) swift_rules_dependencies() load( "@build_bazel_rules_swift//swift:extras.bzl", "swift_rules_extra_dependencies", ) swift_rules_extra_dependencies() http_archive( name = "SwiftLint", sha256 = "c6ea58b9c72082cdc1ada4a2d48273ecc355896ed72204cedcc586b6ccb8aca6", url = "https://github.com/realm/SwiftLint/releases/download/0.52.4/bazel.tar.gz", ) load("@SwiftLint//bazel:repos.bzl", "swiftlint_repos") swiftlint_repos() load("@SwiftLint//bazel:deps.bzl", "swiftlint_deps") swiftlint_deps() ``` -------------------------------- ### Open project in Visual Studio Code Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Command to open the current directory in Visual Studio Code. ```shell code . ``` -------------------------------- ### Create Symbolic Link for SwiftLint Source: https://github.com/realm/swiftlint/blob/main/README.md Creates a symlink in /usr/local/bin to allow Xcode to locate the Homebrew-installed SwiftLint binary. ```bash ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint ``` -------------------------------- ### Configure SwiftLint as an Xcode Run Script Build Phase Source: https://github.com/realm/swiftlint/blob/main/README.md Standard shell script to execute SwiftLint during the build process, providing a fallback warning if the command is not found. ```bash if command -v swiftlint >/dev/null 2>&1 then swiftlint else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions." fi ``` -------------------------------- ### Execute SwiftLint in a Swift Package Source: https://github.com/realm/swiftlint/blob/main/README.md Script to locate and execute the SwiftLint binary within a Swift package's artifact bundle. ```bash SWIFT_PACKAGE_DIR="${SWIFT_PACKAGE_DIR:-${BUILD_DIR%Build/*}SourcePackages}" SWIFTLINT_CMD="$SWIFT_PACKAGE_DIR/artifacts/swiftlintplugins/SwiftLintBinary/SwiftLintBinary.artifactbundle/macos/swiftlint" if test -f "$SWIFTLINT_CMD" 2>&1 then "$SWIFTLINT_CMD" else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#xcode-run-script-build-phase for installation instructions." fi ``` -------------------------------- ### Configure PATH for Apple Silicon Homebrew Source: https://github.com/realm/swiftlint/blob/main/README.md Updates the PATH environment variable to include the Homebrew binary directory on Apple Silicon architectures. ```bash if [[ "$(uname -m)" == arm64 ]] then export PATH="/opt/homebrew/bin:$PATH" fi if command -v swiftlint >/dev/null 2>&1 then swiftlint else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions." fi ``` -------------------------------- ### Configure rules in .swiftlint.yml Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Define rule-specific settings and severity levels within the project's YAML configuration file. ```yaml force_cast: warning file_length: warning: 800 error: 1200 identifier_name: min_length: warning: 3 error: 2 max_length: 20 excluded: id ``` -------------------------------- ### Add to CocoaPods Podfile Source: https://github.com/realm/swiftlint/blob/main/README.md Include SwiftLint in your Podfile to manage it via CocoaPods. ```ruby pod 'SwiftLint' ``` -------------------------------- ### Run SwiftLint via Command Plugin Source: https://github.com/realm/swiftlint/blob/main/README.md Execute the SwiftLint command plugin from the terminal within a Swift package project. ```shell swift package plugin swiftlint ``` -------------------------------- ### Configure SwiftLint in Fastlane Source: https://github.com/realm/swiftlint/blob/main/README.md Use the official SwiftLint action within a Fastlane lane to lint or autocorrect Swift files. ```ruby swiftlint( mode: :lint, # SwiftLint mode: :lint (default) or :autocorrect executable: "Pods/SwiftLint/swiftlint", # The SwiftLint binary path (optional). Important if you've installed it via CocoaPods path: "/path/to/lint", # Specify path to lint (optional) output_file: "swiftlint.result.json", # The path of the output file (optional) reporter: "json", # The custom reporter to use (optional) config_file: ".swiftlint-ci.yml", # The path of the configuration file (optional) files: [ # List of files to process (optional) "AppDelegate.swift", "path/to/project/Model.swift" ], ignore_exit_status: true, # Allow fastlane to continue even if SwiftLint returns a non-zero exit status (Default: false) quiet: true, # Don't print status logs like 'Linting ' & 'Done linting' (Default: false) strict: true # Fail on warnings? (Default: false) ) ``` -------------------------------- ### Define Regex Custom Rules Source: https://github.com/realm/swiftlint/blob/main/README.md Configure custom linting rules using regular expressions within the SwiftLint configuration file. ```yaml custom_rules: # Rule identifier. pirates_beat_ninjas: # Optional regex that defines paths to include during linting. included: - ".*\\.swift" # Optional regex that defines paths to exclude during linting. excluded: - ".*Test\\.swift" # Optional rule name. name: "Pirates Beat Ninjas" # Matching pattern. regex: "([nN]inja)" # Number of regex capture group to highlight the rule violation at. Optional, defaults to 0 (the whole match). capture_group: 0 # SyntaxKinds to match. optional. match_kinds: - comment - identifier # Optional violation message. message: "Pirates are better than ninjas." # Optional violation severity. severity: error no_hiding_in_strings: regex: "([nN]inja)" # Syntax kinds to match. optional. match_kinds: string ``` -------------------------------- ### Publish CocoaPods manually Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Execute this command to manually publish the project to CocoaPods if the automated release workflow fails. ```bash make pod_publish ``` -------------------------------- ### Add Xcode Package Dependency Source: https://github.com/realm/swiftlint/blob/main/README.md Use this URL to add SwiftLint as a package dependency within Xcode. ```bash https://github.com/SimplyDanny/SwiftLintPlugins ``` -------------------------------- ### Disable and enable specific rules in code Source: https://github.com/realm/swiftlint/blob/main/README.md Use swiftlint:disable and swiftlint:enable comments to toggle specific rules for a block of code. ```swift // swiftlint:disable colon let noWarning :String = "" // No warning about colons immediately after variable names. // swiftlint:enable colon let hasWarning :String = "" // Warning generated about colons immediately after variable names. ``` -------------------------------- ### Define rule configuration Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Set the configuration property of a rule using a specific configuration type, such as SeverityConfiguration. ```swift var configuration = SeverityConfiguration(.warning) ``` -------------------------------- ### Configure Xcode CI for Unattended Plugin Use Source: https://github.com/realm/swiftlint/blob/main/README.md Bypass Xcode validation dialogs for plugins and macros during CI builds using xcodebuild flags or defaults settings. ```bash -skipPackagePluginValidation -skipMacroValidation ``` ```bash defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES ``` -------------------------------- ### Add SwiftLint to Package.swift Source: https://github.com/realm/swiftlint/blob/main/README.md Use these declarations to include SwiftLint as a dependency in your Swift Package Manager project. ```swift .package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", from: "") ``` ```swift .package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", exact: "") ``` -------------------------------- ### Include Custom Rules in only_rules Source: https://github.com/realm/swiftlint/blob/main/README.md Ensure custom rules are processed when using the only_rules configuration option. ```yaml only_rules: - custom_rules custom_rules: no_hiding_in_strings: regex: "([nN]inja)" match_kinds: string ``` -------------------------------- ### Path Merging Logic Source: https://github.com/realm/swiftlint/blob/main/README.md The mathematical logic used to resolve included and excluded paths when merging configurations. ```text merged.included = (parent.included - child.excluded) + child.included merged.excluded = (parent.excluded - child.included) + child.excluded ``` -------------------------------- ### Disable all rules in code Source: https://github.com/realm/swiftlint/blob/main/README.md Use the all keyword to suppress all SwiftLint warnings within a specific block. ```swift // swiftlint:disable all let noWarning :String = "" // No warning about colons immediately after variable names. let i = "" // Also no warning about short identifier names. // swiftlint:enable all let hasWarning :String = "" // Warning generated about colons immediately after variable names. let y = "" // Warning generated about short identifier names. ``` -------------------------------- ### Override Swift Toolchain Source: https://github.com/realm/swiftlint/blob/main/README.md Use the TOOLCHAINS environment variable to specify a particular Swift toolchain version for SwiftLint execution. ```shell TOOLCHAINS=com.apple.dt.toolchain.Swift_2_3 swiftlint --fix ``` -------------------------------- ### Add SwiftLint Plugin to Swift Package Target Source: https://github.com/realm/swiftlint/blob/main/README.md Include the SwiftLintBuildToolPlugin in the plugins array of your target definition within Package.swift. ```swift .target( ... plugins: [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins")] ), ``` -------------------------------- ### Apply rule modifiers to specific lines Source: https://github.com/realm/swiftlint/blob/main/README.md Append :previous, :this, or :next to disable or enable commands to restrict the scope to specific lines. ```swift // swiftlint:disable:next force_cast let noWarning = NSNumber() as! Int let hasWarning = NSNumber() as! Int let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast let noWarning3 = NSNumber() as! Int // swiftlint:disable:previous force_cast ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.