diff --git a/Plugins/FormatPlugin/plugin.swift b/Plugins/FormatPlugin/plugin.swift index a89f7d65..0cf049c8 100644 --- a/Plugins/FormatPlugin/plugin.swift +++ b/Plugins/FormatPlugin/plugin.swift @@ -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 { @@ -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 ) } diff --git a/Plugins/LintPlugin/plugin.swift b/Plugins/LintPlugin/plugin.swift index 4dd72204..54a34e2a 100644 --- a/Plugins/LintPlugin/plugin.swift +++ b/Plugins/LintPlugin/plugin.swift @@ -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 { @@ -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 ) }