@@ -17,7 +17,7 @@ struct FormatterPlugin: CommandPlugin {
1717 var baseArguments : [ String ] = [ " --quiet " , " --cache " , cacheFile]
1818
1919 let configFile = context. package . directory. appending ( " .swiftformat " ) . string
20- if !FileManager. default. fileExists ( atPath: configFile) , let defaultPath = defaultConfigPath ( context : context) {
20+ if !FileManager. default. fileExists ( atPath: configFile) , let defaultPath = defaultConfigPath ( workDirectory : context. pluginWorkDirectory ) {
2121 baseArguments. append ( contentsOf: [ " --config " , defaultPath] )
2222 }
2323
@@ -49,19 +49,22 @@ struct FormatterPlugin: CommandPlugin {
4949 }
5050 }
5151
52- func defaultConfigPath( context : PluginContext ) -> String ? {
52+ func defaultConfigPath( workDirectory : Path ) -> String ? {
5353 let path = ( " ~/.swiftformat " as NSString ) . expandingTildeInPath
5454 if FileManager . default. fileExists ( atPath: path) {
5555 return path
5656 }
5757
58- let defaultPath = context . pluginWorkDirectory . appending ( " default-swift-format-config " ) . string
58+ let defaultPath = workDirectory . appending ( " default-swift-format-config " ) . string
5959 let defaultConfig = """
6060 # Default Elegant Chaos swift-format configuration options.
6161 # See https://github.com/nicklockwood/SwiftFormat#config-file for more details.
6262 # To override these, place a .swiftformat file into your source directory, or at ~/.swiftformat.
6363
6464 --ifdef no-indent
65+ --indentcase true
66+ --self init-only
67+ --indentstrings true
6568 """
6669
6770 do {
@@ -73,3 +76,49 @@ struct FormatterPlugin: CommandPlugin {
7376 }
7477 }
7578}
79+
80+ #if canImport(XcodeProjectPlugin)
81+ import XcodeProjectPlugin
82+
83+ extension FormatterPlugin : XcodeCommandPlugin {
84+ func performCommand( context: XcodePluginContext , arguments: [ String ] ) throws {
85+ let swiftFormatTool = try context. tool ( named: " swiftformat " )
86+ let swiftFormatExec = URL ( fileURLWithPath: swiftFormatTool. path. string)
87+
88+ let cacheFile = context. pluginWorkDirectory. appending ( " swift-format-cache " ) . string
89+ var baseArguments : [ String ] = [ " --quiet " , " --cache " , cacheFile]
90+
91+ let configFile = context. xcodeProject. directory. appending ( " .swiftformat " ) . string
92+ if !FileManager. default. fileExists ( atPath: configFile) , let defaultPath = defaultConfigPath ( workDirectory: context. pluginWorkDirectory) {
93+ baseArguments. append ( contentsOf: [ " --config " , defaultPath] )
94+ }
95+
96+ // let swiftVersionFile = context.xcodeProject.directory.appending(".swift-version").string
97+ // if !FileManager.default.fileExists(atPath: swiftVersionFile) {
98+ // let tools = context.xcodeProject.
99+ // let version = "\(tools.major).\(tools.minor)"
100+ // baseArguments.append(contentsOf: ["--swiftversion", version])
101+ // }
102+
103+ for target in context. xcodeProject. targets {
104+ guard let target = target as? SourceModuleTarget else { continue }
105+
106+ var arguments = baseArguments
107+ arguments. append ( target. directory. string)
108+
109+ let process = Process ( )
110+ process. executableURL = swiftFormatExec
111+ process. arguments = arguments
112+ try process. run ( )
113+ process. waitUntilExit ( )
114+
115+ if process. terminationReason == . exit && process. terminationStatus == 0 {
116+ Diagnostics . remark ( " Formatted the source code in \( target. directory) . " )
117+ } else {
118+ let problem = " \( process. terminationReason) : \( process. terminationStatus) "
119+ Diagnostics . error ( " swift-format invocation failed: \( problem) " )
120+ }
121+ }
122+ }
123+ }
124+ #endif
0 commit comments