Skip to content

Commit

Permalink
Merge pull request #1136 from Automattic/iangmaia/swiftlint
Browse files Browse the repository at this point in the history
Update SwiftLint configuration in the project and run it on CI
  • Loading branch information
iangmaia authored Feb 27, 2024
2 parents 747ef11 + a0b9ea7 commit 8999959
Show file tree
Hide file tree
Showing 156 changed files with 184 additions and 702 deletions.
11 changes: 10 additions & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
common_params:
# Common plugin settings to use with the `plugins` key.
- &common_plugins
- automattic/a8c-ci-toolkit#3.0.1
- automattic/a8c-ci-toolkit#3.1.0
# Common environment values to use with the `env` key.
- &common_env
IMAGE_ID: xcode-15.2-xl
Expand All @@ -14,6 +14,15 @@ steps:
env: *common_env
plugins: *common_plugins

- label: ":swift: SwiftLint"
command: run_swiftlint --strict
plugins: *common_plugins
notify:
- github_commit_status:
context: "SwiftLint"
agents:
queue: "default"

- label: "🛠 Verify App Store Target Builds"
command: .buildkite/commands/build-and-test-app-store.sh
env: *common_env
Expand Down
2 changes: 1 addition & 1 deletion .buildkite/release-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
common_params:
# Common plugin settings to use with the `plugins` key.
- &common_plugins
- automattic/a8c-ci-toolkit#3.0.1
- automattic/a8c-ci-toolkit#3.1.0
# Common environment values to use with the `env` key.
- &common_env
IMAGE_ID: xcode-15.2-xl
Expand Down
7 changes: 0 additions & 7 deletions .hound.yml

This file was deleted.

10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
swiftlint_version: 0.54.0

# Project configuration
excluded:
- Pods
Expand All @@ -16,6 +18,10 @@ only_rules:
# if,for,while,do statements shouldn't wrap their conditionals in parentheses.
- control_statement

- discarded_notification_center_observer

- duplicate_imports

# Arguments can be omitted when matching enums with associated types if they
# are not used.
- empty_enum_arguments
Expand All @@ -30,6 +36,8 @@ only_rules:
# the declaration.
- opening_brace

- overridden_super_call

# Files should have a single trailing newline.
- trailing_newline

Expand All @@ -39,6 +47,8 @@ only_rules:
# Lines should not have trailing whitespace.
- trailing_whitespace

- vertical_whitespace

- custom_rules

# Rules configuration
Expand Down
14 changes: 14 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ APP_MACOS_DEPLOYMENT_TARGET = Gem::Version.new('10.14')
platform :osx, APP_MACOS_DEPLOYMENT_TARGET
workspace 'Simplenote.xcworkspace'

## Tools
## ===================
##

def swiftlint_version
require 'yaml'

YAML.load_file('.swiftlint.yml')['swiftlint_version']
end

abstract_target 'Tools' do
pod 'SwiftLint', swiftlint_version
end

# Main
#
abstract_target 'Automattic' do
Expand Down
6 changes: 5 additions & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ PODS:
- Simperium-OSX/SocketRocket (1.9.0)
- Simperium-OSX/SPReachability (1.9.0)
- Simperium-OSX/SSKeychain (1.9.0)
- SwiftLint (0.54.0)

DEPENDENCIES:
- Simperium-OSX (= 1.9.0)
- SwiftLint (= 0.54.0)

SPEC REPOS:
trunk:
- Simperium-OSX
- SwiftLint

SPEC CHECKSUMS:
Simperium-OSX: bc465c8830e5b731e2489085459acc96160104d6
SwiftLint: c1de071d9d08c8aba837545f6254315bc900e211

PODFILE CHECKSUM: c01d124e1d7acadfa1b6c7a961645f7721f5ed31
PODFILE CHECKSUM: 0c6e1eaa48de269bee6c152747e29ea455cb1e74

COCOAPODS: 1.14.1
79 changes: 2 additions & 77 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require 'yaml'
require 'digest'

# Constants
SWIFTLINT_VERSION = '0.41.0'
XCODE_WORKSPACE = 'Simplenote.xcworkspace'
XCODE_SCHEME = 'Simplenote'
XCODE_CONFIGURATION = 'Debug'
Expand All @@ -20,7 +19,7 @@ desc 'Install required dependencies'
task dependencies: %w[dependencies:check]

namespace :dependencies do
task check: %w[bundler:check bundle:check pod:check lint:check]
task check: %w[bundler:check bundle:check pod:check]

namespace :bundler do
task :check do
Expand Down Expand Up @@ -77,42 +76,6 @@ namespace :dependencies do
end
CLOBBER << 'Pods'
end

namespace :lint do
task :check do
if swiftlint_needs_install
dependency_failed('SwiftLint')
Rake::Task['dependencies:lint:install'].invoke
end
end

task :install do
fold('install.swiftlint') do
puts "Installing SwiftLint #{SWIFTLINT_VERSION} into #{swiftlint_path}"
Dir.mktmpdir do |tmpdir|
# Try first using a binary release
zipfile = "#{tmpdir}/swiftlint-#{SWIFTLINT_VERSION}.zip"
sh "curl --fail --location -o #{zipfile} https://github.com/realm/SwiftLint/releases/download/#{SWIFTLINT_VERSION}/portable_swiftlint.zip || true"
if File.exist?(zipfile)
extracted_dir = "#{tmpdir}/swiftlint-#{SWIFTLINT_VERSION}"
sh "unzip #{zipfile} -d #{extracted_dir}"
FileUtils.mkdir_p("#{swiftlint_path}/bin")
FileUtils.cp("#{extracted_dir}/swiftlint", "#{swiftlint_path}/bin/swiftlint")
else
sh "git clone --quiet https://github.com/realm/SwiftLint.git #{tmpdir}"
Dir.chdir(tmpdir) do
sh "git checkout --quiet #{SWIFTLINT_VERSION}"
sh 'git submodule --quiet update --init --recursive'
FileUtils.rm_rf(swiftlint_path)
FileUtils.mkdir_p(swiftlint_path)
sh "make prefix_install PREFIX='#{swiftlint_path}'"
end
end
end
end
end
CLOBBER << 'vendor/swiftlint'
end
end

CLOBBER << 'vendor'
Expand Down Expand Up @@ -147,20 +110,8 @@ task :clean do
xcodebuild(:clean)
end

desc 'Checks the source for style errors'
task lint: %w[dependencies:lint:check] do
swiftlint %w[lint --quiet]
end

namespace :lint do
desc 'Automatically corrects style errors where possible'
task autocorrect: %w[dependencies:lint:check] do
swiftlint %w[autocorrect]
end
end

namespace :git do
hooks = %w[pre-commit post-checkout post-merge]
hooks = %w[post-checkout post-merge]

desc 'Install git hooks'
task :install_hooks do
Expand Down Expand Up @@ -213,12 +164,6 @@ namespace :git do
end

namespace :git do
task pre_commit: %(dependencies:lint:check) do
swiftlint %w[lint --quiet --strict]
rescue StandardError
exit $CHILD_STATUS.exitstatus
end

task :post_merge do
check_dependencies_hook
end
Expand Down Expand Up @@ -254,26 +199,6 @@ def podfile_locked?
podfile_checksum == lockfile_checksum
end

def swiftlint_path
"#{PROJECT_DIR}/vendor/swiftlint"
end

def swiftlint(args)
args = [swiftlint_bin] + args
sh(*args)
end

def swiftlint_bin
"#{swiftlint_path}/bin/swiftlint"
end

def swiftlint_needs_install
return true unless File.exist?(swiftlint_bin)

installed_version = `"#{swiftlint_bin}" version`.chomp
(installed_version != SWIFTLINT_VERSION)
end

def xcodebuild(*build_cmds)
cmd = 'xcodebuild'
cmd += " -destination 'platform=iOS Simulator,name=iPhone 6s'"
Expand Down
7 changes: 1 addition & 6 deletions Scripts/fix-translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import Foundation


guard CommandLine.arguments.count > 2 else {
print("This helper tool replaces empty Localized Strings found in *File A*, with the ones found in *File B*")
print("Usage: fix-translation path/to/Localizable.strings path/to/Failsafe.strings")
exit(1)
}


/// Replaces all of the **Empty Strings** in the specified file, with the entries contained in a "Fallback Map".
///
func replaceEmptyTranslations(in filename: String, fallbackMap: [String: String]) throws {
Expand Down Expand Up @@ -42,7 +40,6 @@ func stringByFixingEmptyValue(in string: String, fallbackMap: [String: String])
return String(format: "\"%@\" = \"%@\";", key, fixedValue)
}


/// Extracts the substring from the specified TextCheckingResult Range, if possible.
///
func extractSubstring(from string: String, match: NSTextCheckingResult, rangeIndex: Int) -> String? {
Expand All @@ -54,10 +51,9 @@ func extractSubstring(from string: String, match: NSTextCheckingResult, rangeInd
return (string as NSString).substring(with: range)
}


/// Loads a Localizations file in memory, and returns a Dictionary with its contents.
///
func loadStrings(from filename: String) throws -> [String: String] {
func loadStrings(from filename: String) throws -> [String: String] {
let contents = try String(contentsOfFile: filename)
let regexp = try NSRegularExpression(pattern: "^\"(.*)\" = \"(.*)\";$", options: [])
var output = [String: String]()
Expand All @@ -81,7 +77,6 @@ func loadStrings(from filename: String) throws -> [String: String] {
return output
}


/// Main!
///
do {
Expand Down
19 changes: 19 additions & 0 deletions Simplenote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,7 @@
buildConfigurationList = 26F72AA914032D2A00A7935E /* Build configuration list for PBXNativeTarget "Simplenote" */;
buildPhases = (
38E241AE28F50F41EF5E8346 /* [CP] Check Pods Manifest.lock */,
3C6F268E2B8D16F900A66FDB /* SwiftLint */,
26F72A8414032D2A00A7935E /* Sources */,
26F72A8514032D2A00A7935E /* Frameworks */,
26F72A8614032D2A00A7935E /* Resources */,
Expand Down Expand Up @@ -1973,6 +1974,24 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
3C6F268E2B8D16F900A66FDB /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = SwiftLint;
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "[ $CI ] && exit 0\n./Pods/SwiftLint/swiftlint\n";
};
4E51756AEE6CFE89EAD49829 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
Loading

0 comments on commit 8999959

Please sign in to comment.