-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlaygroundRuntimeUI.swift
58 lines (47 loc) · 1.69 KB
/
PlaygroundRuntimeUI.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// PlaygroundRuntime.swift provides the logging built-ins for playground.
//
// These functions are executed by the Standard Library for code compiled with
// the playground frontend action.
//
// @see the transform logic here
// https://github.com/apple/swift/blob/master/lib/Sema/PlaygroundTransform.cpp
// https://github.com/apple/swift/blob/e156713/test/PlaygroundTransform/Inputs/PlaygroundsRuntime.swift
func __builtin_send_data(_ record: AnyObject?) {
let record = record as! LogRecord
guard record.api == "$builtin_log" else { return }
if let imageRepresentable = record.object as? OpaqueImageRepresentable {
let fileName: String = "\(record.range.text)_\(record.api)_repr"
// NB: assetDirectory variable is generated via runner script.
let outputPath: String = "\(assetDirectory)/\(fileName)@2x.png"
if let pngData = imageRepresentable.pngData(), pngData.count > 0 {
_ = try? pngData.write(to: URL(fileURLWithPath: outputPath), options: [.atomic])
}
}
print(record.text)
}
private protocol OpaqueImageRepresentable {
func pngData() -> Data?
}
#if os(iOS) || os(tvOS)
import UIKit
extension UIView: OpaqueImageRepresentable {
func pngData() -> Data? {
return UIGraphicsImageRenderer(size: bounds.size).pngData { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
#endif
#if os(iOS) || os(tvOS)
extension UIImage: OpaqueImageRepresentable {}
#endif
#if os(iOS) || os(tvOS)
import CoreGraphics
extension CGImage: OpaqueImageRepresentable {
func pngData() -> Data? {
#if os(iOS) || os(tvOS)
return UIImage(cgImage: self).pngData()
#endif
}
}
#endif