Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftFormat #794

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--swiftversion 5.10

--exclude Tests/Fixtures/
--exclude Tests/AccessibilityTests/AccessibilityProject/
--exclude Tests/XcodeTests/UIKitProject/
--exclude Tests/XcodeTests/SwiftUIProject/
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ build_release: clean build_x86_64 build_arm64

show_bin_path:
@echo ${EXECUTABLE}

swiftformat:
@./scripts/lint/swiftformat.sh
97 changes: 48 additions & 49 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,39 @@ var dependencies: [Package.Dependency] = [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/ileitch/swift-indexstore", from: "9.0.4"),
.package(url: "https://github.com/apple/swift-syntax", from: "510.0.3"),
.package(url: "https://github.com/ileitch/swift-filename-matcher", from: "0.0.0")
.package(url: "https://github.com/ileitch/swift-filename-matcher", from: "0.0.0"),
]

#if os(macOS)
dependencies.append(
.package(
url: "https://github.com/tuist/xcodeproj",
from: "8.16.0"
dependencies.append(
.package(
url: "https://github.com/tuist/xcodeproj",
from: "8.16.0"
)
)
)
#endif

var projectDriverDependencies: [PackageDescription.Target.Dependency] = [
.target(name: "SourceGraph"),
.target(name: "Shared"),
.target(name: "Indexer"),
.target(name: "SourceGraph"),
.target(name: "Shared"),
.target(name: "Indexer"),
]

#if os(macOS)
projectDriverDependencies.append(.target(name: "XcodeSupport"))
projectDriverDependencies.append(.target(name: "XcodeSupport"))
#endif


var targets: [PackageDescription.Target] = [
.executableTarget(
name: "Frontend",
dependencies: [
.target(name: "Shared"),
.target(name: "SourceGraph"),
.target(name: "PeripheryKit"),
.target(name: "ProjectDrivers"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher")
]
.target(name: "Shared"),
.target(name: "SourceGraph"),
.target(name: "PeripheryKit"),
.target(name: "ProjectDrivers"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher"),
]
),
.target(
name: "PeripheryKit",
Expand All @@ -54,7 +53,7 @@ var targets: [PackageDescription.Target] = [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftIndexStore", package: "swift-indexstore"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher")
.product(name: "FilenameMatcher", package: "swift-filename-matcher"),
]
),
.target(
Expand All @@ -66,8 +65,8 @@ var targets: [PackageDescription.Target] = [
]
),
.target(
name: "ProjectDrivers",
dependencies: projectDriverDependencies
name: "ProjectDrivers",
dependencies: projectDriverDependencies
),
.target(
name: "SyntaxAnalysis",
Expand All @@ -82,79 +81,79 @@ var targets: [PackageDescription.Target] = [
name: "SourceGraph",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.target(name: "Shared")
.target(name: "Shared"),
]
),
.target(
name: "Shared",
dependencies: [
.product(name: "Yams", package: "Yams"),
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "FilenameMatcher", package: "swift-filename-matcher")
.product(name: "FilenameMatcher", package: "swift-filename-matcher"),
]
),
.target(
name: "TestShared",
dependencies: [
.target(name: "PeripheryKit"),
.target(name: "ProjectDrivers")
.target(name: "ProjectDrivers"),
],
path: "Tests/Shared"
),
.testTarget(
name: "PeripheryTests",
dependencies: [
.target(name: "TestShared"),
.target(name: "PeripheryKit")
.target(name: "PeripheryKit"),
]
),
.testTarget(
name: "SPMTests",
dependencies: [
.target(name: "TestShared"),
.target(name: "PeripheryKit")
.target(name: "PeripheryKit"),
],
exclude: ["SPMProject"]
),
.testTarget(
name: "AccessibilityTests",
dependencies: [
.target(name: "TestShared"),
.target(name: "PeripheryKit")
.target(name: "PeripheryKit"),
],
exclude: ["AccessibilityProject"]
)
),
]

#if os(macOS)
targets.append(contentsOf: [
.target(
name: "XcodeSupport",
dependencies: [
.target(name: "SourceGraph"),
.target(name: "Shared"),
.target(name: "PeripheryKit"),
.product(name: "XcodeProj", package: "XcodeProj")
]
),
.testTarget(
name: "XcodeTests",
dependencies: [
.target(name: "ProjectDrivers"),
.target(name: "TestShared"),
.target(name: "PeripheryKit"),
],
exclude: ["UIKitProject", "SwiftUIProject"]
)
])
targets.append(contentsOf: [
.target(
name: "XcodeSupport",
dependencies: [
.target(name: "SourceGraph"),
.target(name: "Shared"),
.target(name: "PeripheryKit"),
.product(name: "XcodeProj", package: "XcodeProj"),
]
),
.testTarget(
name: "XcodeTests",
dependencies: [
.target(name: "ProjectDrivers"),
.target(name: "TestShared"),
.target(name: "PeripheryKit"),
],
exclude: ["UIKitProject", "SwiftUIProject"]
),
])
#endif

let package = Package(
name: "Periphery",
platforms: [.macOS(.v13)],
products: [
.executable(name: "periphery", targets: ["Frontend"]),
.library(name: "PeripheryKit", targets: ["PeripheryKit"])
.library(name: "PeripheryKit", targets: ["PeripheryKit"]),
],
dependencies: dependencies,
targets: targets,
Expand Down
6 changes: 3 additions & 3 deletions Sources/Frontend/Commands/ScanBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ScanBehavior {
self.logger = logger
}

func setup(_ configPath: FilePath?) -> Result<(), PeripheryError> {
func setup(_ configPath: FilePath?) -> Result<Void, PeripheryError> {
do {
try configuration.load(from: configPath)
} catch let error as PeripheryError {
Expand All @@ -24,7 +24,7 @@ final class ScanBehavior {
return .success(())
}

func main(_ block: (Project) throws -> [ScanResult]) -> Result<(), PeripheryError> {
func main(_ block: (Project) throws -> [ScanResult]) -> Result<Void, PeripheryError> {
logger.contextualized(with: "version").debug(PeripheryVersion)

let project: Project
Expand Down Expand Up @@ -82,7 +82,7 @@ final class ScanBehavior {

updateChecker.notifyIfAvailable()

if !filteredResults.isEmpty && configuration.strict {
if !filteredResults.isEmpty, configuration.strict {
throw PeripheryError.foundIssues(count: filteredResults.count)
}
} catch let error as PeripheryError {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Frontend/GuidedSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Shared

#if canImport(XcodeSupport)
import XcodeSupport
import XcodeSupport
#endif

final class GuidedSetup: SetupGuideHelpers {
Expand All @@ -23,16 +23,16 @@ final class GuidedSetup: SetupGuideHelpers {
}

#if canImport(XcodeSupport)
if let guide = XcodeProjectSetupGuide.detect() {
projectGuides.append(guide)
}
if let guide = XcodeProjectSetupGuide.detect() {
projectGuides.append(guide)
}
#endif

var projectGuide_: SetupGuide?

if projectGuides.count > 1 {
print(colorize("Please select which project to use:", .bold))
let kindName = select(single: projectGuides.map { $0.projectKindName })
let kindName = select(single: projectGuides.map(\.projectKindName))
projectGuide_ = projectGuides.first { $0.projectKindName == kindName }
print("")
} else {
Expand Down
14 changes: 7 additions & 7 deletions Sources/Frontend/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class Project {
return self.init(kind: .xcode(projectPath: path))
} else if let path = configuration.genericProjectConfig {
return self.init(kind: .generic(genericProjectConfig: path))
} else if BazelProjectDriver.isSupported && configuration.bazel {
return self.init(kind: .bazel)
} else if BazelProjectDriver.isSupported, configuration.bazel {
return self.init(kind: .bazel)
} else if SPM.isSupported {
return self.init(kind: .spm)
}
Expand All @@ -28,17 +28,17 @@ final class Project {

func driver() throws -> ProjectDriver {
switch kind {
case .xcode(let projectPath):
case let .xcode(projectPath):
#if canImport(XcodeSupport)
return try XcodeProjectDriver.build(projectPath: projectPath)
return try XcodeProjectDriver.build(projectPath: projectPath)
#else
fatalError("Xcode projects are not supported on this platform.")
fatalError("Xcode projects are not supported on this platform.")
#endif
case .spm:
return try SPMProjectDriver.build()
case .bazel:
return try BazelProjectDriver.build()
case .generic(let genericProjectConfig):
return try BazelProjectDriver.build()
case let .generic(genericProjectConfig):
return try GenericProjectDriver.build(genericProjectConfig: genericProjectConfig)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Frontend/Scan.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import ProjectDrivers
import Indexer
import PeripheryKit
import ProjectDrivers
import Shared
import SourceGraph

Expand Down
35 changes: 18 additions & 17 deletions Sources/Frontend/UpdateChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Shared

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

final class UpdateChecker {
Expand All @@ -17,7 +17,7 @@ final class UpdateChecker {

required init(logger: Logger = .init(), configuration: Configuration = .shared) {
self.logger = logger
self.debugLogger = logger.contextualized(with: "update-check")
debugLogger = logger.contextualized(with: "update-check")
self.configuration = configuration
let config = URLSessionConfiguration.ephemeral
urlSession = URLSession(configuration: config)
Expand All @@ -33,7 +33,7 @@ final class UpdateChecker {
// We only perform the update check with xcode format because it may interfere with
// parsing json and csv.
guard !configuration.disableUpdateCheck,
configuration.outputFormat.supportsAuxiliaryOutput else { return }
configuration.outputFormat.supportsAuxiliaryOutput else { return }

var urlRequest = URLRequest(url: latestReleaseURL)
urlRequest.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
Expand All @@ -50,22 +50,23 @@ final class UpdateChecker {
}

guard let jsonData = data,
let jsonObject = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [AnyHashable: Any],
let tagName = jsonObject["tag_name"] as? String else {
var json = "N/A"

if let data {
let decoded = String(decoding: data, as: UTF8.self)
if !decoded.isEmpty {
json = decoded
}
let jsonObject = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [AnyHashable: Any],
let tagName = jsonObject["tag_name"] as? String
else {
var json = "N/A"

if let data {
let decoded = String(decoding: data, as: UTF8.self)
if !decoded.isEmpty {
json = decoded
}
}

let message = "Failed to identify latest release tag in: \(json)"
strongSelf.error = PeripheryError.updateCheckError(message: message)
strongSelf.debugLogger.debug(message)
strongSelf.semaphore.signal()
return
let message = "Failed to identify latest release tag in: \(json)"
strongSelf.error = PeripheryError.updateCheckError(message: message)
strongSelf.debugLogger.debug(message)
strongSelf.semaphore.signal()
return
}

strongSelf.latestVersion = tagName
Expand Down
12 changes: 6 additions & 6 deletions Sources/Frontend/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ struct PeripheryCommand: FrontendCommand {
static let configuration = CommandConfiguration(
commandName: "periphery",
subcommands: [
ScanCommand.self,
CheckUpdateCommand.self,
ClearCacheCommand.self,
VersionCommand.self
ScanCommand.self,
CheckUpdateCommand.self,
ClearCacheCommand.self,
VersionCommand.self,
]
)
}

signal(SIGINT) { _ in
let logger = Logger()
logger.warn(
"Termination can result in a corrupt index. Try the '--clean-build' flag if you get erroneous results such as false-positives and incorrect source file locations.",
newlinePrefix: true // Print a newline after ^C
"Termination can result in a corrupt index. Try the '--clean-build' flag if you get erroneous results such as false-positives and incorrect source file locations.",
newlinePrefix: true // Print a newline after ^C
)
Shell.shared.interruptRunning()
exit(0)
Expand Down
Loading
Loading