Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 7d9406e

Browse files
committed
Integration improvements.
1 parent 52ae47a commit 7d9406e

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ I'm gonna provide a static factory for google fonts later.
103103
)
104104
```
105105

106+
### Integration
106107

108+
You can use `generateIntegration` methods from CSSKit itself by passing needed arguments or use **[this](https://makeupstudio.herokuapp.com/CSSKit)** tool for autogenerating integration code.
107109

108110
---
109111

Sources/CSSKit/Integration.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Tests/CSSKitTests/CSSKitTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,51 @@ final class CSSKitTests: XCTestCase {
1616
let expectation = "align-self:center;background:#A52A2AFF;border-radius:1.0px;"
1717
XCTAssertEqual(styles.render(), expectation)
1818
}
19+
20+
func testGenerateHTML() {
21+
let content =
22+
"""
23+
//
24+
// Style+StaticFactory.swift
25+
// CSS
26+
//
27+
// Created by Maxim Krouk on 9/1/19.
28+
// Copyright © 2019 MakeupStudio. All rights reserved.
29+
//
30+
31+
extension Style {
32+
33+
public static func alignItems(_ value: String) -> Self {
34+
return .init(key: "align-items", value: value)
35+
}
36+
37+
public static func alignSelf(_ value: String) -> Self {
38+
return .init(key: "align-self", value: value)
39+
}
40+
41+
}
42+
"""
43+
44+
let expectation =
45+
"""
46+
extension HTML {
47+
48+
public func alignItems(_ value: String) -> Self {
49+
appendingStyle(.alignItems(value))
50+
}
1951
52+
public func alignSelf(_ value: String) -> Self {
53+
appendingStyle(.alignSelf(value))
54+
}
55+
56+
}
57+
"""
58+
59+
let output = generateIntegration(in: "HTML", builder: "appendingStyle", from: content)
60+
61+
XCTAssertEqual(output, expectation)
62+
}
63+
2064
static var allTests = [
2165
("testExample", testExample),
2266
("testRendering", testRendering),

0 commit comments

Comments
 (0)