@@ -88,54 +88,29 @@ fileprivate func processSources(
88
88
fileprivate func loadConfiguration(
89
89
forSwiftFile swiftFilePath: String ? , configFilePath: String ?
90
90
) -> Configuration {
91
- if let path = configFilePath {
92
- return decodedConfiguration ( fromFileAtPath : path )
91
+ if let configFilePath = configFilePath {
92
+ return decodedConfiguration ( fromFile : URL ( fileURLWithPath : configFilePath ) )
93
93
}
94
- // Search for a ".swift-format" configuration file in the directory of the current .swift file,
95
- // or its nearest parent.
96
- var configurationFilePath : String ? = nil
97
- if let swiftFilePath = swiftFilePath {
98
- configurationFilePath = findConfigurationFile (
99
- forSwiftFile: URL ( fileURLWithPath: swiftFilePath) . path)
100
- }
101
- return decodedConfiguration ( fromFileAtPath: configurationFilePath)
102
- }
103
94
104
- /// Look for a ".swift-format" configuration file in the same directory as "forSwiftFile", or its
105
- /// nearest parent. If one is not found, return "nil".
106
- fileprivate func findConfigurationFile( forSwiftFile: String ) -> String ? {
107
- let cwd = FileManager . default. currentDirectoryPath
108
- var path = URL (
109
- fileURLWithPath: AbsolutePath ( forSwiftFile, relativeTo: AbsolutePath ( cwd) ) . pathString)
110
- let configFilename = " .swift-format "
111
-
112
- repeat {
113
- path = path. deletingLastPathComponent ( )
114
- let testPath = path. appendingPathComponent ( configFilename) . path
115
- if FileManager . default. isReadableFile ( atPath: testPath) {
116
- return testPath
117
- }
118
- } while path. path != " / "
95
+ if let swiftFileURL = swiftFilePath. map ( URL . init ( fileURLWithPath: ) ) ,
96
+ let configFileURL = Configuration . url ( forConfigurationFileApplyingTo: swiftFileURL) {
97
+ return decodedConfiguration ( fromFile: configFileURL)
98
+ }
119
99
120
- return nil
100
+ return Configuration ( )
121
101
}
122
102
103
+
123
104
/// Loads and returns a `Configuration` from the given JSON file if it is found and is valid. If the
124
105
/// file does not exist or there was an error decoding it, the program exits with a non-zero exit
125
106
/// code.
126
- fileprivate func decodedConfiguration( fromFileAtPath path: String ? ) -> Configuration {
127
- if let path = path {
128
- do {
129
- let url = URL ( fileURLWithPath: path)
130
- let data = try Data ( contentsOf: url)
131
- return try JSONDecoder ( ) . decode ( Configuration . self, from: data)
132
- } catch {
133
- // TODO: Improve error message, write to stderr.
134
- print ( " Could not load configuration at \( path) : \( error) " )
135
- exit ( 1 )
136
- }
137
- } else {
138
- return Configuration ( )
107
+ fileprivate func decodedConfiguration( fromFile url: Foundation . URL ) -> Configuration {
108
+ do {
109
+ return try Configuration ( contentsOf: url)
110
+ } catch {
111
+ // TODO: Improve error message, write to stderr.
112
+ print ( " Could not load configuration at \( url) : \( error) " )
113
+ exit ( 1 )
139
114
}
140
115
}
141
116
0 commit comments