Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 9f2d60d
Author: 유재호 <[email protected]>
Date:   Mon Nov 28 23:53:34 2022 +0900

    Add missing space after some comment delimiters (airbnb#208)

commit e8412c7
Author: Hanton Yang <[email protected]>
Date:   Fri Nov 18 21:28:12 2022 -0800

    Fix typo in SE-0156 link (airbnb#207)

commit 07bb2e0
Author: Cal Stephens <[email protected]>
Date:   Fri Nov 11 09:21:26 2022 -0800

    Update rule for long function declarations to specify effects (`async`, `throws`) should be written on the line after the closing paren (airbnb#205)

commit 9ea3428
Author: Cal Stephens <[email protected]>
Date:   Fri Nov 4 17:24:56 2022 -0700

    Update `Package.swift` to explicitly not support Linux (airbnb#206)

commit 7884f26
Author: Michael Bachand <[email protected]>
Date:   Thu Oct 20 20:26:36 2022 -0700

    Update language around when an underscore prefix may be appropriate (airbnb#203)

commit 105d7cc
Author: Michael Bachand <[email protected]>
Date:   Thu Oct 20 20:25:27 2022 -0700

    Update README.md (airbnb#204)

commit 831d3c2
Author: 유재호 <[email protected]>
Date:   Tue Oct 18 11:17:12 2022 +0900

    Fix typos in rule 'prefer opaque generic parameter syntax' (airbnb#202)

commit fff6fdc
Author: Cal Stephens <[email protected]>
Date:   Mon Oct 17 16:42:22 2022 -0700

    Bump SwiftFormat version (airbnb#201)

commit f600ed0
Author: Cal Stephens <[email protected]>
Date:   Mon Oct 17 11:04:28 2022 -0700

    Add rule to prefer opaque generic parameter syntax (airbnb#193)

commit f7f2ff9
Author: 유재호 <[email protected]>
Date:   Tue Oct 18 02:13:17 2022 +0900

    Fix naming exception example(underscore prefix) and typos (airbnb#200)

commit f5ae3d7
Author: Cal Stephens <[email protected]>
Date:   Fri Oct 14 11:18:27 2022 -0700

    Add rule to prefer simplified generic extension syntax (airbnb#194)

commit 00216e3
Author: Cal Stephens <[email protected]>
Date:   Thu Oct 13 16:49:19 2022 -0700

    Add rule to prefer shorthand `if let` optional unwrapping syntax (airbnb#192)

commit 860f0d5
Author: Cal Stephens <[email protected]>
Date:   Tue Oct 11 14:39:32 2022 -0700

    Add rule to prefer trailing closure syntax for closure arguments with no parameter name (airbnb#199)

commit eff3f23
Author: 유재호 <[email protected]>
Date:   Tue Aug 23 01:15:12 2022 +0900

    Fix typos in README.md (airbnb#191)

commit f602b12
Author: Cal Stephens <[email protected]>
Date:   Tue Aug 16 16:52:56 2022 -0500

    Update to SwiftFormat build based on 0.49.16 (airbnb#190)

commit d1b1bda
Author: 유재호 <[email protected]>
Date:   Mon Aug 15 22:55:29 2022 +0900

    Fix typo and broken type annotations (airbnb#189)

commit cec2928
Author: Cal Stephens <[email protected]>
Date:   Fri Jul 29 18:30:21 2022 -0700

    Improve handling of `--swift-format` and `--exclude` arguments (airbnb#182)

commit 1bf9613
Author: Cal Stephens <[email protected]>
Date:   Thu Jul 28 09:21:53 2022 -0700

    Infer Swift version from Package.swift swift-tools-version (airbnb#180)

commit b53ed2a
Author: Cal Stephens <[email protected]>
Date:   Thu Jul 28 08:54:24 2022 -0700

    Add `XcodeCommandPlugin` to fully support Xcode 14  (airbnb#181)

commit 30e8f68
Author: Michael Bachand <[email protected]>
Date:   Wed Jul 27 09:35:21 2022 -0700

    Remove executable as product (airbnb#179)

commit 1d8bb91
Author: Cal Stephens <[email protected]>
Date:   Mon Jul 25 15:23:08 2022 -0700

    Add Swift Package Index badges (airbnb#178)

commit 7ecef5d
Author: Cal Stephens <[email protected]>
Date:   Mon Jul 25 13:39:16 2022 -0700

    Add info about Swift Package Manager command plugin to README.md (airbnb#176)

commit 5542090
Author: Jay Hickey <[email protected]>
Date:   Sat Jul 23 13:56:43 2022 -0700

    Add `--paths` argument to only lint specific paths (airbnb#177)

commit 503c5bc
Author: Cal Stephens <[email protected]>
Date:   Fri Jul 22 14:17:09 2022 -0700

    Support target selection dialog in Xcode 14 (airbnb#175)

commit 7079bc3
Author: Cal Stephens <[email protected]>
Date:   Fri Jul 22 14:06:02 2022 -0700

    Improve error handling, add support for `--exclude` flag (airbnb#173)

commit 852a2e3
Author: Cal Stephens <[email protected]>
Date:   Fri Jul 22 11:46:12 2022 -0700

    Bump to latest commit of SwiftLint (airbnb#174)

commit 20290d8
Author: Michael Bachand <[email protected]>
Date:   Thu Jul 21 13:46:20 2022 -0700

    Aesthetics for new Swift command plugin (airbnb#172)

    * restrucutre

    * reorganize again

    * one more change

    * update ci

    * Update Package.swift

    * Update main.yml

commit 640b1ee
Author: Cal Stephens <[email protected]>
Date:   Thu Jul 21 08:20:35 2022 -0700

    Update Swift version to 5.6 (airbnb#169)

commit fb658f4
Author: Cal Stephens <[email protected]>
Date:   Wed Jul 20 14:23:23 2022 -0700

    Update xcode_settings.sh script to enable Xcode spell check (airbnb#171)

commit b844956
Author: Cal Stephens <[email protected]>
Date:   Tue Jul 19 19:14:56 2022 -0700

    Add Swift Package Manager command plugin (airbnb#168)

commit c60d1f6
Author: Cal Stephens <[email protected]>
Date:   Tue Jul 19 11:59:19 2022 -0700

    Include a single space in a set of empty braces (`{ }`) (airbnb#167)

commit d53c95b
Author: Cal Stephens <[email protected]>
Date:   Tue Jul 19 11:58:54 2022 -0700

    Use commas instead of `&&` operators in conditional statements (airbnb#166)

commit 15309c9
Author: Cal Stephens <[email protected]>
Date:   Tue Jul 5 10:02:40 2022 -0700

    Add rule to remove blank lines from the top and bottom of scopes (excluding type bodies) (airbnb#164)

commit a310253
Author: Cal Stephens <[email protected]>
Date:   Sat Jul 2 19:55:48 2022 -0700

    Add `actor` to `--organizetypes` option (airbnb#165)

commit ea85cf2
Author: Michael Bachand <[email protected]>
Date:   Wed Jun 8 15:00:29 2022 -0700

    Fix href for #whitespace-around-comment-delimiters (airbnb#163)

commit 5034513
Author: Cal Stephens <[email protected]>
Date:   Tue Jun 7 12:11:13 2022 -0700

    Add rule that there should be no spaces inside brackets of collection literals (airbnb#162)

commit 9785e35
Author: Cal Stephens <[email protected]>
Date:   Tue Jun 7 12:11:03 2022 -0700

    Add rule that there should be no spaces before or inside the parentheses of function calls / declarations (airbnb#161)

commit 904b72d
Author: Cal Stephens <[email protected]>
Date:   Tue Jun 7 12:10:52 2022 -0700

    Add rule to include spaces before and after comment delimiters (airbnb#160)
  • Loading branch information
Halle committed Dec 6, 2022
1 parent 2ab2bf6 commit 7c77f88
Show file tree
Hide file tree
Showing 11 changed files with 845 additions and 35 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test-package-plugin:
name: Test Package Plugin
runs-on: macos-12
steps:
- uses: actions/checkout@v2
- name: Test Package Plugin
run: swift package --allow-writing-to-package-directory format --lint
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build
.swiftpm
.DS_Store
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "e394bf350e38cb100b6bc4172834770ede1b7232",
"version" : "1.0.3"
}
}
],
"version" : 2
}
54 changes: 54 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// swift-tools-version: 5.6
import PackageDescription

let package = Package(
name: "AirbnbSwift",
platforms: [.macOS(.v10_13)],
products: [
.plugin(name: "FormatSwift", targets: ["FormatSwift"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.3"),
],
targets: [
.plugin(
name: "FormatSwift",
capability: .command(
intent: .custom(
verb: "format",
description: "Formats Swift source files according to the Airbnb Swift Style Guide"),
permissions: [
.writeToPackageDirectory(reason: "Format Swift source files"),
]),
dependencies: [
"AirbnbSwiftFormatTool",
"SwiftFormat",
"SwiftLintBinary",
]),

.executableTarget(
name: "AirbnbSwiftFormatTool",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
resources: [
.process("airbnb.swiftformat"),
.process("swiftlint.yml"),
]),

.binaryTarget(
name: "SwiftFormat",
url: "https://github.com/calda/SwiftFormat/releases/download/0.51-beta-6/SwiftFormat.artifactbundle.zip",
checksum: "8583456d892c99f970787b4ed756a7e0c83a0d9645e923bb4dae10d581c59bc3"),

.binaryTarget(
name: "SwiftLintBinary",
url: "https://github.com/realm/SwiftLint/releases/download/0.48.0/SwiftLintBinary-macos.artifactbundle.zip",
checksum: "9c255e797260054296f9e4e4cd7e1339a15093d75f7c4227b9568d63edddba50"),
])

// Emit an error on Linux, so Swift Package Manager's platform support detection doesn't say this package supports Linux
// https://github.com/airbnb/swift/discussions/197#discussioncomment-4055303
#if os(Linux)
#error("Linux is currently not supported")
#endif
20 changes: 20 additions & 0 deletions Plugins/FormatSwift/CommandContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import PackagePlugin
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
#endif

// MARK: - CommandContext

/// Shared methods implemented by `PluginContext` and `XcodePluginContext`
protocol CommandContext {
var pluginWorkDirectory: Path { get }
func tool(named name: String) throws -> PluginContext.Tool
}

// MARK: - PluginContext + CommandContext

extension PluginContext: CommandContext { }

#if canImport(XcodeProjectPlugin)
extension XcodePluginContext: CommandContext { }
#endif
168 changes: 168 additions & 0 deletions Plugins/FormatSwift/Plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import Foundation
import PackagePlugin
#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin
#endif

// MARK: - AirbnbSwiftFormatPlugin

/// A Swift Package Manager `CommandPlugin` and `XcodeCommandPlugin` that executes
/// `AirbnbSwiftFormatTool` to format source code in Swift package targets according
/// to the Airbnb Swift Style Guide.
@main
struct AirbnbSwiftFormatPlugin {

/// Calls the `AirbnbSwiftFormatTool` executable with the given arguments
func performCommand(
context: CommandContext,
inputPaths: [String],
arguments: [String])
throws
{
var argumentExtractor = ArgumentExtractor(arguments)

// Filter out any excluded paths passed in with `--exclude`
let excludedPaths = argumentExtractor.extractOption(named: "exclude")
let inputPaths = inputPaths.filter { path in
!excludedPaths.contains(where: { excludedPath in
path.hasSuffix(excludedPath)
})
}

let launchPath = try context.tool(named: "AirbnbSwiftFormatTool").path.string
let arguments = inputPaths + [
"--swift-format-path",
try context.tool(named: "swiftformat").path.string,
"--swift-lint-path",
try context.tool(named: "swiftlint").path.string,
// The process we spawn doesn't have read/write access to the default
// cache file locations, so we pass in our own cache paths from
// the plugin's work directory.
"--swift-format-cache-path",
context.pluginWorkDirectory.string + "/swiftformat.cache",
"--swift-lint-cache-path",
context.pluginWorkDirectory.string + "/swiftlint.cache",
] + argumentExtractor.remainingArguments

if arguments.contains("--log") {
// swiftlint:disable:next no_direct_standard_out_logs
print("[Plugin]", launchPath, arguments.joined(separator: " "))
}

let process = Process()
process.launchPath = launchPath
process.arguments = arguments
try process.run()
process.waitUntilExit()

switch process.terminationStatus {
case EXIT_SUCCESS:
break
case EXIT_FAILURE:
throw CommandError.lintFailure
default:
throw CommandError.unknownError(exitCode: process.terminationStatus)
}
}

}

// MARK: CommandPlugin

extension AirbnbSwiftFormatPlugin: CommandPlugin {

// MARK: Internal

func performCommand(context: PluginContext, arguments: [String]) async throws {
var argumentExtractor = ArgumentExtractor(arguments)

// When ran from Xcode, the plugin command is invoked with `--target` arguments,
// specifying the targets selected in the plugin dialog.
let inputTargets = argumentExtractor.extractOption(named: "target")

// If given, lint only the paths passed to `--paths`
var inputPaths = argumentExtractor.extractOption(named: "paths")

if !inputTargets.isEmpty {
// If a set of input targets were given, lint/format the directory for each of them
inputPaths += try context.package.targets(named: inputTargets).map { $0.directory.string }
} else if inputPaths.isEmpty {
// Otherwise if no targets or paths listed we default to linting/formatting
// the entire package directory.
inputPaths = try self.inputPaths(for: context.package)
}

// When running on a SPM package we infer the minimum Swift version from the
// `swift-tools-version` in `Package.swift` by default if the user doesn't
// specify one manually
let swiftVersion = argumentExtractor.extractOption(named: "swift-version").last
?? "\(context.package.toolsVersion.major).\(context.package.toolsVersion.minor)"

let arguments = [
"--swift-version",
swiftVersion,
] + argumentExtractor.remainingArguments

try performCommand(
context: context,
inputPaths: inputPaths,
arguments: arguments)
}

// MARK: Private

/// Retrieves the list of paths that should be formatted / linted
///
/// By default this tool runs on all subdirectories of the package's root directory,
/// plus any Swift files directly contained in the root directory. This is a
/// workaround for two interesting issues:
/// - If we lint `content.package.directory`, then SwiftLint lints the `.build` subdirectory,
/// which includes checkouts for any SPM dependencies, even if we add `.build` to the
/// `excluded` configuration in our `swiftlint.yml`.
/// - We could lint `context.package.targets.map { $0.directory }`, but that excludes
/// plugin targets, which include Swift code that we want to lint.
private func inputPaths(for package: Package) throws -> [String] {
let packageDirectoryContents = try FileManager.default.contentsOfDirectory(
at: URL(fileURLWithPath: package.directory.string),
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles])

let subdirectories = packageDirectoryContents.filter { $0.hasDirectoryPath }
let rootSwiftFiles = packageDirectoryContents.filter { $0.pathExtension.hasSuffix("swift") }
return (subdirectories + rootSwiftFiles).map { $0.path }
}

}

#if canImport(XcodeProjectPlugin)
extension AirbnbSwiftFormatPlugin: XcodeCommandPlugin {

func performCommand(context: XcodePluginContext, arguments: [String]) throws {
var argumentExtractor = ArgumentExtractor(arguments)

// When ran from Xcode, the plugin command is invoked with `--target` arguments,
// specifying the targets selected in the plugin dialog.
// - Unlike SPM targets which are just directories, Xcode targets are
// an arbitrary collection of paths.
let inputTargetNames = Set(argumentExtractor.extractOption(named: "target"))
let inputPaths = context.xcodeProject.targets.lazy
.filter { inputTargetNames.contains($0.displayName) }
.flatMap { $0.inputFiles }
.map { $0.path.string }
.filter { $0.hasSuffix(".swift") }

try performCommand(
context: context,
inputPaths: Array(inputPaths),
arguments: argumentExtractor.remainingArguments)
}

}
#endif

// MARK: - CommandError

enum CommandError: Error {
case lintFailure
case unknownError(exitCode: Int32)
}
Loading

0 comments on commit 7c77f88

Please sign in to comment.