|
| 1 | +// |
| 2 | +// DotPIFSerializer.swift |
| 3 | +// SwiftPM |
| 4 | +// |
| 5 | +// Created by Paulo Mattos on 2025-04-18. |
| 6 | +// |
| 7 | + |
| 8 | +import Basics |
| 9 | +import Foundation |
| 10 | +import protocol TSCBasic.OutputByteStream |
| 11 | + |
| 12 | +#if canImport(SwiftBuild) |
| 13 | +import SwiftBuild |
| 14 | + |
| 15 | +/// Serializes the specified PIF as a **Graphviz** directed graph. |
| 16 | +/// |
| 17 | +/// * [DOT command line](https://graphviz.org/doc/info/command.html) |
| 18 | +/// * [DOT language specs](https://graphviz.org/doc/info/lang.html) |
| 19 | +func writePIF(_ workspace: PIF.Workspace, toDOT outputStream: OutputByteStream) { |
| 20 | + var graph = DotPIFSerializer() |
| 21 | + |
| 22 | + graph.node( |
| 23 | + id: workspace.id, |
| 24 | + label: "<workspace>\n\(workspace.id)", |
| 25 | + shape: "box3d", |
| 26 | + color: .black, |
| 27 | + fontsize: 7 |
| 28 | + ) |
| 29 | + |
| 30 | + for project in workspace.projects.map(\.underlying) { |
| 31 | + graph.edge(from: workspace.id, to: project.id, color: .lightskyblue) |
| 32 | + graph.node( |
| 33 | + id: project.id, |
| 34 | + label: "<project>\n\(project.id)", |
| 35 | + shape: "box3d", |
| 36 | + color: .gray56, |
| 37 | + fontsize: 7 |
| 38 | + ) |
| 39 | + |
| 40 | + for target in project.targets { |
| 41 | + graph.edge(from: project.id, to: target.id, color: .lightskyblue) |
| 42 | + |
| 43 | + switch target { |
| 44 | + case .target(let target): |
| 45 | + graph.node( |
| 46 | + id: target.id, |
| 47 | + label: "<target>\n\(target.id)\nproduct type: \(target.productType)\n\(target.buildPhases.summary)", |
| 48 | + shape: "box", |
| 49 | + color: .gray88, |
| 50 | + fontsize: 5 |
| 51 | + ) |
| 52 | + |
| 53 | + case .aggregate: |
| 54 | + graph.node( |
| 55 | + id: target.id, |
| 56 | + label: "<aggregate target>\n\(target.id)", |
| 57 | + shape: "folder", |
| 58 | + color: .gray88, |
| 59 | + fontsize: 5, |
| 60 | + style: "bold" |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + for targetDependency in target.common.dependencies { |
| 65 | + let linked = target.isLinkedAgainst(dependencyId: targetDependency.targetId) |
| 66 | + graph.edge(from: target.id, to: targetDependency.targetId, color: .gray40, style: linked ? "filled" : "dotted") |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + graph.write(to: outputStream) |
| 72 | +} |
| 73 | + |
| 74 | +fileprivate struct DotPIFSerializer { |
| 75 | + private var objects: [String] = [] |
| 76 | + |
| 77 | + mutating func write(to outputStream: OutputByteStream) { |
| 78 | + func write(_ object: String) { outputStream.write("\(object)\n") } |
| 79 | + |
| 80 | + write("digraph PIF {") |
| 81 | + write("dpi=260;") // i.e., MacBook Pro 16" is 226 pixels per inch (3072 x 1920). |
| 82 | + for object in objects { |
| 83 | + write(" \(object);") |
| 84 | + } |
| 85 | + write("}") |
| 86 | + } |
| 87 | + |
| 88 | + mutating func node( |
| 89 | + id: PIF.GUID, |
| 90 | + label: String? = nil, |
| 91 | + shape: String? = nil, |
| 92 | + color: Color? = nil, |
| 93 | + fontname: String? = "SF Mono Light", |
| 94 | + fontsize: Int? = nil, |
| 95 | + style: String? = nil, |
| 96 | + margin: Int? = nil |
| 97 | + ) { |
| 98 | + var attributes: [String] = [] |
| 99 | + |
| 100 | + if let label { attributes.append("label=\(label.quote)") } |
| 101 | + if let shape { attributes.append("shape=\(shape)") } |
| 102 | + if let color { attributes.append("color=\(color)") } |
| 103 | + |
| 104 | + if let fontname { attributes.append("fontname=\(fontname.quote)") } |
| 105 | + if let fontsize { attributes.append("fontsize=\(fontsize)") } |
| 106 | + |
| 107 | + if let style { attributes.append("style=\(style)") } |
| 108 | + if let margin { attributes.append("margin=\(margin)") } |
| 109 | + |
| 110 | + var node = " \(id.quote)" |
| 111 | + if !attributes.isEmpty { |
| 112 | + let attributesList = attributes.joined(separator: ", ") |
| 113 | + node += " [\(attributesList)]" |
| 114 | + } |
| 115 | + objects.append(node) |
| 116 | + } |
| 117 | + |
| 118 | + mutating func edge( |
| 119 | + from left: PIF.GUID, |
| 120 | + to right: PIF.GUID, |
| 121 | + color: Color? = nil, |
| 122 | + style: String? = nil |
| 123 | + ) { |
| 124 | + var attributes: [String] = [] |
| 125 | + |
| 126 | + if let color { attributes.append("color=\(color)") } |
| 127 | + if let style { attributes.append("style=\(style)") } |
| 128 | + |
| 129 | + var edge = " \(left.quote) -> \(right.quote)" |
| 130 | + if !attributes.isEmpty { |
| 131 | + let attributesList = attributes.joined(separator: ", ") |
| 132 | + edge += " [\(attributesList)]" |
| 133 | + } |
| 134 | + objects.append(edge) |
| 135 | + } |
| 136 | + |
| 137 | + /// Graphviz default color scheme is **X11**: |
| 138 | + /// * https://graphviz.org/doc/info/colors.html |
| 139 | + enum Color: String { |
| 140 | + case black |
| 141 | + case gray |
| 142 | + case gray40 |
| 143 | + case gray56 |
| 144 | + case gray88 |
| 145 | + case lightskyblue |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// MARK: - Helpers |
| 150 | + |
| 151 | +fileprivate extension ProjectModel.BaseTarget { |
| 152 | + func isLinkedAgainst(dependencyId: ProjectModel.GUID) -> Bool { |
| 153 | + for buildPhase in self.common.buildPhases { |
| 154 | + switch buildPhase { |
| 155 | + case .frameworks(let frameworksPhase): |
| 156 | + for buildFile in frameworksPhase.files { |
| 157 | + switch buildFile.ref { |
| 158 | + case .reference(let id): |
| 159 | + if dependencyId == id { return true } |
| 160 | + case .targetProduct(let id): |
| 161 | + if dependencyId == id { return true } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + case .sources, .shellScript, .headers, .copyFiles, .copyBundleResources: |
| 166 | + break |
| 167 | + } |
| 168 | + } |
| 169 | + return false |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +fileprivate extension [ProjectModel.BuildPhase] { |
| 174 | + var summary: String { |
| 175 | + var phases: [String] = [] |
| 176 | + |
| 177 | + for buildPhase in self { |
| 178 | + switch buildPhase { |
| 179 | + case .sources(let sourcesPhase): |
| 180 | + var sources = "sources: " |
| 181 | + if sourcesPhase.files.count == 1 { |
| 182 | + sources += "1 source file" |
| 183 | + } else { |
| 184 | + sources += "\(sourcesPhase.files.count) source files" |
| 185 | + } |
| 186 | + phases.append(sources) |
| 187 | + |
| 188 | + case .frameworks(let frameworksPhase): |
| 189 | + var frameworks = "frameworks: " |
| 190 | + if frameworksPhase.files.count == 1 { |
| 191 | + frameworks += "1 linked target" |
| 192 | + } else { |
| 193 | + frameworks += "\(frameworksPhase.files.count) linked targets" |
| 194 | + } |
| 195 | + phases.append(frameworks) |
| 196 | + |
| 197 | + case .shellScript: |
| 198 | + phases.append("shellScript: 1 shell script") |
| 199 | + |
| 200 | + case .headers, .copyFiles, .copyBundleResources: |
| 201 | + break |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + guard !phases.isEmpty else { return "" } |
| 206 | + return phases.joined(separator: "\n") |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +fileprivate extension PIF.GUID { |
| 211 | + var quote: String { |
| 212 | + self.value.quote |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +fileprivate extension String { |
| 217 | + /// Quote the name and escape the quotes and backslashes. |
| 218 | + var quote: String { |
| 219 | + "\"" + self |
| 220 | + .replacing("\"", with: "\\\"") |
| 221 | + .replacing("\\", with: "\\\\") |
| 222 | + .replacing("\n", with: "\\n") + |
| 223 | + "\"" |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +#endif |
0 commit comments