Skip to content
Open
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
25 changes: 11 additions & 14 deletions Plugins/FormatPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import Foundation

@main
struct FormatPlugin {
func format(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws {
private func format(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws {
let swiftFormatExec = URL(fileURLWithPath: tool.path.string)

var arguments: [String] = ["format"]

arguments.append(contentsOf: targetDirectories)

arguments.append(contentsOf: ["--recursive", "--parallel", "--in-place"])

if let configurationFilePath = configurationFilePath {
arguments.append(contentsOf: ["--configuration", configurationFilePath])
var swiftFormatArgs = ["format"]
swiftFormatArgs.append(contentsOf: targetDirectories)
swiftFormatArgs.append(contentsOf: ["--recursive", "--parallel", "--in-place"])
if let configurationFilePath {
swiftFormatArgs.append(contentsOf: ["--configuration", configurationFilePath])
}

let process = try Process.run(swiftFormatExec, arguments: arguments)
let process = try Process.run(swiftFormatExec, arguments: swiftFormatArgs)
process.waitUntilExit()

if process.terminationReason == .exit && process.terminationStatus == 0 {
Expand All @@ -38,15 +35,15 @@ extension FormatPlugin: CommandPlugin {

var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let targetsToFormat = try context.package.targets(named: targetNames)
let targetDirectories = try context.package.targets(named: targetNames)
.compactMap { $0 as? SourceModuleTarget }
.map(\.directory.string)

let configurationFilePath = argExtractor.extractOption(named: "configuration").first

let sourceCodeTargets = targetsToFormat.compactMap{ $0 as? SourceModuleTarget }

try format(
tool: swiftFormatTool,
targetDirectories: sourceCodeTargets.map(\.directory.string),
targetDirectories: targetDirectories,
configurationFilePath: configurationFilePath
)
}
Expand Down
25 changes: 11 additions & 14 deletions Plugins/LintPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import Foundation

@main
struct LintPlugin {
func lint(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws {
private func lint(tool: PluginContext.Tool, targetDirectories: [String], configurationFilePath: String?) throws {
let swiftFormatExec = URL(fileURLWithPath: tool.path.string)

var arguments: [String] = ["lint"]

arguments.append(contentsOf: targetDirectories)

arguments.append(contentsOf: ["--recursive", "--parallel", "--strict"])

if let configurationFilePath = configurationFilePath {
arguments.append(contentsOf: ["--configuration", configurationFilePath])
var swiftFormatArgs = ["lint"]
swiftFormatArgs.append(contentsOf: targetDirectories)
swiftFormatArgs.append(contentsOf: ["--recursive", "--parallel", "--strict"])
if let configurationFilePath {
swiftFormatArgs.append(contentsOf: ["--configuration", configurationFilePath])
}

let process = try Process.run(swiftFormatExec, arguments: arguments)
let process = try Process.run(swiftFormatExec, arguments: swiftFormatArgs)
process.waitUntilExit()

if process.terminationReason == .exit && process.terminationStatus == 0 {
Expand All @@ -39,15 +36,15 @@ extension LintPlugin: CommandPlugin {
// Extract the arguments that specify what targets to format.
var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let targetsToFormat = try context.package.targets(named: targetNames)
let targetDirectories = try context.package.targets(named: targetNames)
.compactMap { $0 as? SourceModuleTarget }
.map(\.directory.string)

let configurationFilePath = argExtractor.extractOption(named: "configuration").first

let sourceCodeTargets = targetsToFormat.compactMap { $0 as? SourceModuleTarget }

try lint(
tool: swiftFormatTool,
targetDirectories: sourceCodeTargets.map(\.directory.string),
targetDirectories: targetDirectories,
configurationFilePath: configurationFilePath
)
}
Expand Down