|
| 1 | +// |
| 2 | +// Integration.swift |
| 3 | +// CSSKit |
| 4 | +// |
| 5 | +// Created by Maxim Krouk on 4/21/20. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +#if DEBUG |
| 11 | +/// Should only be used for integration generations |
| 12 | +/// |
| 13 | +/// Builder function should have this signature: |
| 14 | +/// ``` |
| 15 | +/// func <name>(_ style: Style) -> Self |
| 16 | +/// ``` |
| 17 | +/// |
| 18 | +/// - Parameter type: type to integrate the framework in |
| 19 | +/// - Parameter builder: builder function of the target type |
| 20 | +/// - Parameter input: path to `Style+StaticFactory.swift` |
| 21 | +/// - Parameter output: path to output file |
| 22 | +@discardableResult |
| 23 | +@available(*, deprecated, message: "Should only be used for integration generations") |
| 24 | +public func generateIntegration(in type: String, builder: String, input: String, output: String) -> Bool { |
| 25 | + let manager = FileManager.default |
| 26 | + guard let data = manager.contents(atPath: input) else { return false } |
| 27 | + guard let content = String(data: data, encoding: .utf8) else { return false } |
| 28 | + |
| 29 | + let output = generateIntegration(in: type, builder: builder, from: content) |
| 30 | + guard let outputData = output.data(using: .utf8) else { return false } |
| 31 | + |
| 32 | + return manager.createFile(atPath: output, contents: outputData, attributes: .none) |
| 33 | +} |
| 34 | + |
| 35 | +/// Should only be used for integration generations |
| 36 | +/// |
| 37 | +/// Builder function should have this signature: |
| 38 | +/// ``` |
| 39 | +/// func <name>(_ style: Style) -> Self |
| 40 | +/// ``` |
| 41 | +/// |
| 42 | +/// - Parameter type: type to integrate the framework in |
| 43 | +/// - Parameter builder: builder function of the target type |
| 44 | +/// - Parameter content: content of `Style+StaticFactory.swift` file |
| 45 | +@available(*, deprecated, message: "Should only be used for integration generations") |
| 46 | +public func generateIntegration(in type: String, builder: String, from content: String) -> String { |
| 47 | + var lines = content.components(separatedBy: .newlines) |
| 48 | + lines = Array(lines.drop(while: { !$0.contains("extension") })) |
| 49 | + |
| 50 | + var result = [String]() |
| 51 | + var _identifier = "" |
| 52 | + |
| 53 | + lines.forEach { line in |
| 54 | + if line.contains("extension") { |
| 55 | + result.append("extension \(type) {") |
| 56 | + } else if line.contains("public static func ") { |
| 57 | + result.append(line.replacingOccurrences(of: "public static func", with: "public func")) |
| 58 | + _identifier = String(line.trimmingCharacters(in: .whitespaces).dropFirst(19)) |
| 59 | + _identifier = String(_identifier[_identifier.startIndex..<_identifier.firstIndex(of: "(")!]) |
| 60 | + } else if line.count > 5 { |
| 61 | + result.append(" \(builder)(.\(_identifier)(value))") |
| 62 | + } else { |
| 63 | + result.append(line) |
| 64 | + } |
| 65 | + } |
| 66 | + return result.joined(separator: "\n") |
| 67 | +} |
| 68 | +#endif |
0 commit comments