@@ -34,14 +34,19 @@ public struct CodePrinter {
3434 }
3535 public var indentationText : String = " "
3636
37- public static func toString( _ block: ( inout CodePrinter ) -> ( ) ) -> String {
37+ public static func toString( _ block: ( inout CodePrinter ) throws -> ( ) ) rethrows -> String {
3838 var printer = CodePrinter ( )
39- block ( & printer)
39+ try block ( & printer)
4040 return printer. finalize ( )
4141 }
4242
43- public init ( ) {
44-
43+ var ioMode : PrintMode
44+ public enum PrintMode {
45+ case accumulateAll
46+ case flushToFileOnWrite
47+ }
48+ public init ( io: PrintMode = . flushToFileOnWrite) {
49+ self . ioMode = . accumulateAll
4550 }
4651
4752 internal mutating func append( _ text: String ) {
@@ -91,6 +96,16 @@ public struct CodePrinter {
9196 }
9297 }
9398
99+ /// Print a plain newline, e.g. to separate declarations.
100+ public mutating func println(
101+ _ terminator: PrinterTerminator = . newLine,
102+ function: String = #function,
103+ file: String = #fileID,
104+ line: UInt = #line
105+ ) {
106+ print ( " " )
107+ }
108+
94109 public mutating func print(
95110 _ text: Any ,
96111 _ terminator: PrinterTerminator = . newLine,
@@ -159,6 +174,12 @@ public struct CodePrinter {
159174 public var isEmpty : Bool {
160175 self . contents. isEmpty
161176 }
177+
178+ public mutating func dump( file: String = #fileID, line: UInt = #line) {
179+ Swift . print ( " // CodePrinter.dump @ \( file) : \( line) " )
180+ Swift . print ( contents)
181+ }
182+
162183}
163184
164185public enum PrinterTerminator : String {
@@ -185,3 +206,49 @@ public enum PrinterTerminator: String {
185206 }
186207 }
187208}
209+
210+ extension CodePrinter {
211+ package mutating func writeContents(
212+ outputDirectory: String ,
213+ javaPackagePath: String ? ,
214+ filename: String
215+ ) throws {
216+ guard self . ioMode != . accumulateAll else {
217+ // if we're accumulating everything, we don't want to finalize/flush any contents
218+ // let's mark that this is where a write would have happened though:
219+ print ( " // ^^^^ Contents of: \( outputDirectory) / \( filename) " )
220+ return
221+ }
222+
223+ let contents = finalize ( )
224+ if outputDirectory == " - " {
225+ print (
226+ " // ==== --------------------------------------------------------------------------------------------------- "
227+ )
228+ if let javaPackagePath {
229+ print ( " // \( javaPackagePath) / \( filename) " )
230+ } else {
231+ print ( " // \( filename) " )
232+ }
233+ print ( contents)
234+ return
235+ }
236+
237+ let targetDirectory = [ outputDirectory, javaPackagePath] . compactMap { $0 } . joined (
238+ separator: PATH_SEPARATOR)
239+ log. trace ( " Prepare target directory: \( targetDirectory) " )
240+ try FileManager . default. createDirectory (
241+ atPath: targetDirectory, withIntermediateDirectories: true )
242+
243+ let targetFilePath = [ javaPackagePath, filename] . compactMap { $0 } . joined (
244+ separator: PATH_SEPARATOR)
245+ Swift . print ( " Writing ' \( targetFilePath) '... " , terminator: " " )
246+ try contents. write (
247+ to: Foundation . URL ( fileURLWithPath: targetDirectory) . appendingPathComponent ( filename) ,
248+ atomically: true ,
249+ encoding: . utf8
250+ )
251+ Swift . print ( " done. " . green)
252+ }
253+
254+ }
0 commit comments