@@ -157,15 +157,37 @@ public func readConfiguration(sourceDir: String, file: String = #fileID, line: U
157157 return try readConfiguration ( configPath: configPath, file: file, line: line)
158158}
159159
160+ /// Read a swift-java.config file at the specified path.
161+ ///
162+ /// Configuration is expected to be "JSON-with-comments".
163+ /// Specifically "//" comments are allowed and will be trimmed before passing the rest of the config into a standard JSON parser.
160164public func readConfiguration( configPath: URL , file: String = #fileID, line: UInt = #line) throws -> Configuration ? {
161165 guard let configData = try ? Data ( contentsOf: configPath) else {
162166 return nil
163167 }
164168
169+ guard let configString = String ( data: configData, encoding: . utf8) else {
170+ return nil
171+ }
172+
173+ return try readConfiguration ( string: configString, configPath: configPath)
174+ }
175+
176+ public func readConfiguration( string: String , configPath: URL ? , file: String = #fileID, line: UInt = #line) throws -> Configuration ? {
177+ let cleanedConfigString = string
178+ . split ( separator: " \n " )
179+ . filter { line in
180+ !line. trimmingCharacters ( in: . whitespaces) . starts ( with: " // " )
181+ } . joined ( separator: " \n " )
182+
183+ guard let configData = cleanedConfigString. data ( using: . utf8) else {
184+ return nil
185+ }
186+
165187 do {
166188 return try JSONDecoder ( ) . decode ( Configuration . self, from: configData)
167189 } catch {
168- throw ConfigurationError ( message: " Failed to parse SwiftJava configuration at ' \( configPath. absoluteURL) '! \( #fileID) : \( #line) " , error: error,
190+ throw ConfigurationError ( message: " Failed to parse SwiftJava configuration at ' \( configPath. map ( { $0 . absoluteURL. description } ) ?? " <no-path> " ) '! \( #fileID) : \( #line) " , error: error,
169191 file: file, line: line)
170192 }
171193}
0 commit comments