Skip to content

Commit 33a5749

Browse files
authored
Update GraphViz (#8)
* Update GraphViz * Update Snapshots + remove linux test files * Remove linux directive * Revert "Remove linux directive" This reverts commit 001808b. * Fix error on linux * Update package version * Update macOS CI * Fix Linux ci * Fix everything
1 parent 3d46e60 commit 33a5749

16 files changed

Lines changed: 169 additions & 166 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
2121

2222
- name: Test
23-
run: swift test -v --skip-update --parallel --enable-code-coverage
23+
run: swift test --parallel --enable-code-coverage
2424
env:
2525
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
2626

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
// swift-tools-version:5.2
1+
// swift-tools-version:5.3
22

33
import PackageDescription
44

55
let package = Package(
66
name: "FirebladeGraph",
7+
platforms: [
8+
.macOS(.v11),
9+
.iOS(.v13),
10+
],
711
products: [
812
.library(
913
name: "FirebladeGraph",
10-
targets: ["FirebladeGraph"]),
14+
targets: ["FirebladeGraph"]
15+
),
1116
],
1217
dependencies: [
13-
.package(url: "https://github.com/ctreffs/GraphViz.git", .branch("master")),
18+
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", from: "0.4.1"),
1419
.package(url: "https://github.com/davecom/SwiftGraph.git", from: "3.1.0"),
15-
.package(name: "SnapshotTesting", url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.11.0")
20+
.package(name: "SnapshotTesting", url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.11.0"),
1621
],
1722
targets: [
1823
.target(
1924
name: "FirebladeGraph",
20-
dependencies: ["GraphViz", "SwiftGraph"]),
25+
dependencies: ["GraphViz", "SwiftGraph"]
26+
),
2127
.testTarget(
2228
name: "FirebladeGraphTests",
23-
dependencies: ["FirebladeGraph", "SnapshotTesting"]),
29+
dependencies: ["FirebladeGraph", "SnapshotTesting"]
30+
),
2431
]
2532
)

Sources/FirebladeGraph/GraphvizRepresentable.swift

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@
77

88
import struct Foundation.Data
99
import struct Foundation.UUID
10-
import DOT
1110
import GraphViz
1211

1312
public protocol GraphVizNodeRepresentable {
1413
func graphVizNodeDescription() -> String
1514
}
1615

1716
extension GraphVizNodeRepresentable {
18-
internal func graphVizNode() -> GraphViz.Node {
17+
func graphVizNode() -> GraphViz.Node {
1918
.init(graphVizNodeDescription())
2019
}
2120
}
2221

2322
extension String: GraphVizNodeRepresentable {
2423
public func graphVizNodeDescription() -> String { self }
2524
}
25+
2626
extension Int: GraphVizNodeRepresentable {
2727
public func graphVizNodeDescription() -> String { "\(self)" }
2828
}
29+
2930
extension UInt: GraphVizNodeRepresentable {
3031
public func graphVizNodeDescription() -> String { "\(self)" }
3132
}
33+
3234
extension UInt8: GraphVizNodeRepresentable {
3335
public func graphVizNodeDescription() -> String { "\(self)" }
3436
}
@@ -38,32 +40,52 @@ extension UUID: GraphVizNodeRepresentable {
3840
}
3941

4042
public protocol GraphVizRenderable {
41-
func renderGraph(as format: Format) -> Data?
43+
func renderGraph(as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void)
44+
}
45+
46+
public enum ImageError: Swift.Error {
47+
case failedToCreateImage(_ from: Data)
4248
}
4349

4450
#if canImport(AppKit)
45-
import class AppKit.NSImage
46-
public typealias Image = NSImage
47-
extension GraphVizRenderable {
48-
public func renderGraphAsImage() -> Image? {
49-
guard let data = renderGraph(as: .png) else {
50-
return nil
51-
}
51+
import class AppKit.NSImage
52+
public typealias Image = NSImage
53+
public extension GraphVizRenderable {
54+
func renderGraphAsImage(completion: @escaping (Result<Image, Swift.Error>) -> Void) {
55+
renderGraph(as: .png) { result in
56+
switch result {
57+
case let .success(data):
58+
if let image = Image(data: data) {
59+
completion(.success(image))
60+
} else {
61+
completion(.failure(ImageError.failedToCreateImage(data)))
62+
}
5263

53-
return Image(data: data)
64+
case let .failure(failure):
65+
completion(.failure(failure))
66+
}
67+
}
68+
}
5469
}
55-
}
5670

5771
#elseif canImport(UIKit)
58-
import class UIKit.UIImage
59-
public typealias Image = UIImage
60-
extension GraphVizRenderable {
61-
public final func renderGraphAsImage() -> Image? {
62-
guard let data = renderGraph(as: .png) else {
63-
return nil
64-
}
72+
import class UIKit.UIImage
73+
public typealias Image = UIImage
74+
public extension GraphVizRenderable {
75+
func renderGraphAsImage(completion: @escaping (Result<Image, Swift.Error>) -> Void) {
76+
renderGraph(as: .png) { result in
77+
switch result {
78+
case let .success(data):
79+
if let image = Image(data: data) {
80+
completion(.success(image))
81+
} else {
82+
completion(.failure(ImageError.failedToCreateImage(data)))
83+
}
6584

66-
return Image(data: data)
85+
case let .failure(failure):
86+
completion(.failure(failure))
87+
}
88+
}
89+
}
6790
}
68-
}
6991
#endif

Sources/FirebladeGraph/Node+Graphviz.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@
55
// Created by Christian Treffs on 24.03.20.
66
//
77

8-
import GraphViz
98
import struct Foundation.Data
9+
import GraphViz
1010

1111
extension Node: GraphVizRenderable where Content: GraphVizNodeRepresentable {
12-
public final func renderGraph(as format: Format) -> Data? {
12+
public final func renderGraph(as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) {
1313
var graph = Graph(directed: true, strict: true)
1414

1515
descend { node in
1616
node.renderNode(in: &graph)
1717
}
1818

19-
do {
20-
return try graph.render(using: .dot, to: format)
21-
} catch {
22-
return nil
23-
}
19+
graph.render(using: .dot, to: format, completion: completion)
2420
}
2521

2622
final func renderNode(in graph: inout GraphViz.Graph) {

Sources/FirebladeGraph/Node.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ open class Node<Content> {
2525

2626
public init(_ content: Content) {
2727
self.content = content
28-
self.children = []
28+
children = []
2929
}
3030

3131
deinit {
@@ -164,66 +164,70 @@ open class Node<Content> {
164164
/// Splitting the implementation of the update away from the update call
165165
/// itself allows the detail to be overridden without disrupting the
166166
/// general sequence of updateFromParent (e.g. raising events).
167-
open func updateFromParent() {
168-
}
167+
open func updateFromParent() {}
169168

170169
open func childrenNeedingUpdate() -> AnyIterator<ChildNode> {
171170
AnyIterator(children.makeIterator())
172171
}
173172
}
174173

175174
// MARK: Equatable
175+
176176
extension Node: Equatable where Content: Equatable {
177177
public static func == (lhs: Node<Content>, rhs: Node<Content>) -> Bool {
178178
lhs.content == rhs.content
179179
}
180180
}
181181

182182
// MARK: Comparable
183+
183184
extension Node: Comparable where Content: Comparable {
184185
public static func < (lhs: Node<Content>, rhs: Node<Content>) -> Bool {
185186
lhs.content < rhs.content
186187
}
187188
}
188189

189190
// MARK: CustomStringConvertible
191+
190192
extension Node: CustomStringConvertible {
191-
open var description: String {
193+
public var description: String {
192194
"<\(type(of: self))>"
193195
}
194196
}
195197

196198
// MARK: CustomDebugStringConvertible
199+
197200
extension Node: CustomDebugStringConvertible {
198-
open var debugDescription: String {
201+
public var debugDescription: String {
199202
"<\(type(of: self)) \(content)>"
200203
}
201204
}
202205

203206
// MARK: Recursive description
204-
extension Node {
207+
208+
public extension Node {
205209
/// Recursively descripes this node and all it's children.
206-
public var descriptionDescending: String {
210+
var descriptionDescending: String {
207211
describeDescending(self) { $0.description }
208212
}
209213

210214
/// Recursively debug descripes this node and all it's children.
211-
public var debugDescriptionDescending: String {
215+
var debugDescriptionDescending: String {
212216
describeDescending(self) { $0.debugDescription }
213217
}
214218

215219
/// Recursively describe given node and all it's children using a given closure.
216220
/// - Parameter node: the start node.
217221
/// - Parameter level: current indentation level.
218222
/// - Parameter closure: a closure to apply for each node.
219-
public func describeDescending(_ node: Node<Content>, _ level: Int = 0, using closure: (Node<Content>) -> String) -> String {
223+
func describeDescending(_ node: Node<Content>, _ level: Int = 0, using closure: (Node<Content>) -> String) -> String {
220224
let prefix = String(repeating: " ", count: level) + ""
221-
return prefix + closure(node) + "\n" + self.children.map { $0.describeDescending($0, level + 1, using: closure) }.joined()
225+
return prefix + closure(node) + "\n" + children.map { $0.describeDescending($0, level + 1, using: closure) }.joined()
222226
}
223227
}
224228

225-
extension Node where Content == Void {
226-
public convenience init() {
229+
public extension Node where Content == Void {
230+
convenience init() {
227231
self.init(())
228232
}
229233
}

Sources/FirebladeGraph/SwiftGraph+Graphviz.swift

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,29 @@
55
// Created by Christian Treffs on 24.03.20.
66
//
77

8-
import DOT
98
import Foundation
109
import GraphViz
1110
@_exported import SwiftGraph
1211

1312
extension UniqueElementsGraph: GraphVizRenderable where V: GraphVizNodeRepresentable {
14-
public final func renderGraph(as format: Format) -> Data? {
15-
drawGraphUnweigted(self, as: format)
13+
public final func renderGraph(as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) {
14+
drawGraphUnweigted(self, as: format, completion: completion)
1615
}
1716
}
1817

1918
extension UnweightedGraph: GraphVizRenderable where V: GraphVizNodeRepresentable {
20-
public final func renderGraph(as format: Format) -> Data? {
21-
drawGraphUnweigted(self, as: format)
19+
public final func renderGraph(as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) {
20+
drawGraphUnweigted(self, as: format, completion: completion)
2221
}
2322
}
2423

2524
extension WeightedGraph: GraphVizRenderable where V: GraphVizNodeRepresentable, W: Numeric {
26-
public final func renderGraph(as format: Format) -> Data? {
27-
drawGraphWeigted(self, as: format)
25+
public final func renderGraph(as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) {
26+
drawGraphWeigted(self, as: format, completion: completion)
2827
}
2928
}
3029

31-
private func drawGraphUnweigted<G>(_ graph: G, as format: Format) -> Data? where G: SwiftGraph.Graph, G.V: GraphVizNodeRepresentable {
30+
private func drawGraphUnweigted<G>(_ graph: G, as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) where G: SwiftGraph.Graph, G.V: GraphVizNodeRepresentable {
3231
let directed = graph.isDAG
3332
var graphvizGraph = Graph(directed: directed, strict: true)
3433

@@ -47,14 +46,10 @@ private func drawGraphUnweigted<G>(_ graph: G, as format: Format) -> Data? where
4746

4847
let layout: LayoutAlgorithm = directed ? .dot : .sfdp
4948

50-
do {
51-
return try graphvizGraph.render(using: layout, to: format)
52-
} catch {
53-
return nil
54-
}
49+
graphvizGraph.render(using: layout, to: format, completion: completion)
5550
}
5651

57-
private func drawGraphWeigted<G, W>(_ graph: G, as format: Format) -> Data? where G: SwiftGraph.Graph, G.V: GraphVizNodeRepresentable, G.E == WeightedEdge<W>, W: Numeric {
52+
private func drawGraphWeigted<G, W>(_ graph: G, as format: Format, completion: @escaping (Result<Data, Swift.Error>) -> Void) where G: SwiftGraph.Graph, G.V: GraphVizNodeRepresentable, G.E == WeightedEdge<W>, W: Numeric {
5853
let directed = graph.isDAG
5954
var graphvizGraph = Graph(directed: directed, strict: true)
6055

@@ -76,9 +71,5 @@ private func drawGraphWeigted<G, W>(_ graph: G, as format: Format) -> Data? wher
7671

7772
let layout: LayoutAlgorithm = directed ? .dot : .sfdp
7873

79-
do {
80-
return try graphvizGraph.render(using: layout, to: format)
81-
} catch {
82-
return nil
83-
}
74+
graphvizGraph.render(using: layout, to: format, completion: completion)
8475
}

0 commit comments

Comments
 (0)