Ciao a tutti cari amici di iProg in quest’articolo vorrei presentarvi un tool molto potente creato dal team di sviluppo di realm ovvero coloro che hanno creato anche jazzy .
Il tool che voglio introdurvi e’ capace di controllare il codice sorgente ed rilevare errori di tipo programmatici e stilistici, questo fantastico tool si chiama SwiftLint, esso si basa sulle linee guida di Swift.
Sostanzialmente questo tool ci consente di fare tre cose principali:
- mantenere una certa “coerenza” nel codice
- aumentare l’affidabilità del codice
- aumentare la leggibilita’ del codice
Installazione
Dopo questa piccola premessa andiamo a vedere come installare questo tool tramite il gestore di pacchetti brew:
brew install swiftlint
dopo aver installato il tool e’ possibile anche integrarlo direttamente all’interno di Xcode, in modo che ogni volta che andremo a buildare il nostro progetto verra’ effettuata anche una verifica del codice.
if which swiftlint >/dev/null; then swiftlint else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" fi
Configurazione
Se la configurazione di default non ci “aggrada” e’ possibile anche creare un file .swiflint.yml ed aggiunre o disabilitare le regole, ecco un esempio:
disabled_rules: # rule identifiers to exclude from running - colon - comma - control_statement opt_in_rules: # some rules are only opt-in - empty_count # Find all the available rules by running: # swiftlint rules included: # paths to include during linting. `--path` is ignored if present. - Source excluded: # paths to ignore during linting. Takes precedence over `included`. - Carthage - Pods - Source/ExcludedFolder - Source/ExcludedFile.swift - Source/*/ExcludedFile.swift # Exclude files with a wildcard # configurable rules can be customized from this configuration file # binary rules can set their severity level force_cast: warning # implicitly force_try: severity: warning # explicitly # rules that have both warning and error levels, can set just the warning level # implicitly line_length: 110 # they can set both implicitly with an array type_body_length: - 300 # warning - 400 # error # or they can set both explicitly file_length: warning: 500 error: 1200 # naming rules can set warnings/errors for min_length and max_length # additionally they can set excluded names type_name: min_length: 4 # only warning max_length: # warning and error warning: 40 error: 50 excluded: iPhone # excluded via string identifier_name: min_length: # only min_length error: 4 # only error excluded: # excluded via string array - id - URL - GlobalAPIKey reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji)
One more thing
Swiftlint e’ utilizzabile anche da linea di comando ecco a voi alcuni comandi utili
autocorrect Automatically correct warnings and errors help Display general or command-specific help lint Print lint warnings and errors for the Swift files in the current directory (default command) rules Display the list of rules and their identifiers version Display the current version of SwiftLint
Considerazioni
Consiglio a tutti di iniziare ad utilizzare questo tool sia se si lavora in team in maniera tale da mantere uno stile di codice omogeneo ma anche se si lavora da soli in quanto mantenere un codice “pulito” avra’ un grande effetto nel tempo.
Se quest’articolo ti e’ piaciuto fammelo sapere sulla nostra pagina di facebook o tramite twitter