Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding graphviz support in dump #852

Merged
merged 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Add --project-root option to generate command. [#828](https://github.com/yonaskolb/XcodeGen/pull/828) @ileitch
- Add an ability to set an order of groups with `options.groupOrdering` [#613](https://github.com/yonaskolb/XcodeGen/pull/613) @Beniamiiin
- Add `staticBinary` linkType for Carthage dependency [#847](https://github.com/yonaskolb/XcodeGen/pull/847) @d-date
- Add the ability to output a dependency graph in graphviz format [#852](https://github.com/yonaskolb/XcodeGen/pull/852) @jeffctown

#### Fixed
- Fixed issue when linking and embeding static frameworks: they should be linked and NOT embed. [#820](https://github.com/yonaskolb/XcodeGen/pull/820) @acecilia
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"version": "4.4.0"
}
},
{
"package": "GraphViz",
"repositoryURL": "https://github.com/SwiftDocOrg/GraphViz.git",
"state": {
"branch": null,
"revision": "08e0cddd013fa2272379d27ec3e0093db51f34fb",
"version": "0.1.0"
}
},
{
"package": "JSONUtilities",
"repositoryURL": "https://github.com/yonaskolb/JSONUtilities.git",
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
.package(url: "https://github.com/tuist/XcodeProj.git", .exact("7.10.0")),
.package(url: "https://github.com/jakeheis/SwiftCLI.git", from: "6.0.0"),
.package(url: "https://github.com/mxcl/Version", from: "2.0.0"),
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", from: "0.1.0")
],
targets: [
.target(name: "XcodeGen", dependencies: [
Expand All @@ -39,6 +40,7 @@ let package = Package(
"XcodeProj",
"PathKit",
"Core",
"GraphViz",
]),
.target(name: "ProjectSpec", dependencies: [
"JSONUtilities",
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The project spec is a YAML or JSON file that defines your targets, configuration
- ✅ Distribute your spec amongst multiple files for easy **sharing** and overriding
- ✅ Easily create **multi-platform** frameworks
- ✅ Integrate **Carthage** frameworks without any work
- ✅ Export **Dependency Diagrams** to view in [Graphviz](https://www.graphviz.org)

Given a very simple project spec file like this:

Expand Down Expand Up @@ -132,6 +133,27 @@ Options:

There are other commands as well such as `xcodegen dump` which lets out output the resolved spec in many different formats, or write it to a file. Use `xcodegen help` to see more detailed usage information.

## Dependency Diagrams
<details>
<summary>Click to expand!</summary>

#### How to export dependency diagrams:

To stdout:

```
xcodegen dump --type graphviz
```

To a file:

```
xcodegen dump --type graphviz --file Graph.viz
```

During implementation, `graphviz` formatting was validated using [GraphvizOnline](https://dreampuf.github.io/GraphvizOnline/), [WebGraphviz](https://www.webgraphviz.com), and [Graphviz on MacOS](graphviz.org).
</details>

## Editing
```shell
git clone https://github.com/yonaskolb/XcodeGen.git
Expand Down
4 changes: 4 additions & 0 deletions Sources/XcodeGenCLI/Commands/DumpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PathKit
import ProjectSpec
import Yams
import Version
import XcodeGenKit

class DumpCommand: ProjectCommand {

Expand Down Expand Up @@ -40,6 +41,8 @@ class DumpCommand: ProjectCommand {
output = try Yams.dump(object: project.toJSONDictionary())
case .summary:
output = project.debugDescription
case .graphviz:
output = GraphVizGenerator().generateModuleGraphViz(targets: project.targets)
}

if let file = file {
Expand All @@ -58,6 +61,7 @@ private enum DumpType: String, ConvertibleFromString, CaseIterable {
case parsedJSON = "parsed-json"
case parsedYaml = "parsed-yaml"
case summary
case graphviz

static var defaultValue: DumpType { .yaml }
}
66 changes: 66 additions & 0 deletions Sources/XcodeGenKit/GraphVizGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import DOT
import Foundation
import GraphViz
import ProjectSpec

extension Dependency {
var graphVizName: String {
switch self.type {
case .bundle, .package, .sdk, .framework, .carthage:
return "[\(self.type)]\\n\(reference)"
case .target:
return reference
}
}
}

extension Dependency.DependencyType: CustomStringConvertible {
public var description: String {
switch self {
case .bundle: return "bundle"
case .package: return "package"
case .framework: return "framework"
case .carthage: return "carthage"
case .sdk: return "sdk"
case .target: return "target"
}
}
}

extension Node {
init(target: Target) {
self.init(target.name)
self.shape = .box
}

init(dependency: Dependency) {
self.init(dependency.reference)
self.shape = .box
self.label = dependency.graphVizName
}
}

public class GraphVizGenerator {

public init() {}

public func generateModuleGraphViz(targets: [Target]) -> String {
return DOTEncoder().encode(generateGraph(targets: targets))
}

func generateGraph(targets: [Target]) -> Graph {
var graph = Graph(directed: true)
targets.forEach { target in
target.dependencies.forEach { dependency in
let from = Node(target: target)
graph.append(from)
let to = Node(dependency: dependency)
graph.append(to)
var edge = Edge(from: from, to: to)
edge.style = .dashed
graph.append(edge)
}
}
return graph
}
}
106 changes: 106 additions & 0 deletions Tests/XcodeGenKitTests/GraphVizGeneratorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import ProjectSpec
import Spectre
@testable import XcodeGenKit
import XCTest

private let app = Target(
name: "MyApp",
type: .application,
platform: .iOS,
settings: Settings(buildSettings: ["SETTING_1": "VALUE"]),
dependencies: [
Dependency(type: .target, reference: "MyInternalFramework"),
Dependency(type: .bundle, reference: "Resources"),
Dependency(type: .carthage(findFrameworks: true, linkType: .static), reference: "MyStaticFramework"),
Dependency(type: .carthage(findFrameworks: true, linkType: .dynamic), reference: "MyDynamicFramework"),
Dependency(type: .framework, reference: "MyExternalFramework"),
Dependency(type: .package(product: "MyPackage"), reference:"MyPackage"),
Dependency(type: .sdk(root: "MySDK"), reference: "MySDK")
]
)

private let framework = Target(
name: "MyFramework",
type: .framework,
platform: .iOS,
settings: Settings(buildSettings: ["SETTING_2": "VALUE"])
)

private let uiTest = Target(
name: "MyAppUITests",
type: .uiTestBundle,
platform: .iOS,
settings: Settings(buildSettings: ["SETTING_3": "VALUE"]),
dependencies: [Dependency(type: .target, reference: "MyApp")]
)


private let targets = [app, framework, uiTest]

class GraphVizGeneratorTests: XCTestCase {

func testGraphOutput() throws {
describe {
let graph = GraphVizGenerator().generateGraph(targets: targets)
$0.it("generates the expected number of nodes") {
try expect(graph.nodes.count) == 16
}
$0.it("generates box nodes") {
try expect(graph.nodes.filter({ $0.shape == .box }).count) == 16
}
$0.it("generates the expected carthage nodes") {
try expect(graph.nodes.filter({ $0.label?.contains("[carthage]") ?? false }).count) == 2
}
$0.it("generates the expected sdk nodes") {
try expect(graph.nodes.filter({ $0.label?.contains("[sdk]") ?? false }).count) == 1
}
$0.it("generates the expected Framework nodes") {
try expect(graph.nodes.filter({ $0.label?.contains("[framework]") ?? false }).count) == 1
}
$0.it("generates the expected package nodes") {
try expect(graph.nodes.filter({ $0.label?.contains("[package]") ?? false }).count) == 1
}
$0.it("generates the expected bundle nodes") {
try expect(graph.nodes.filter({ $0.label?.contains("[bundle]") ?? false }).count) == 1
}
$0.it("generates the expected edges") {
try expect(graph.edges.count) == 8
}
$0.it("generates dashed edges") {
try expect(graph.edges.filter({ $0.style == .dashed }).count) == 8
}
$0.it("generates the expected output") {
let output = GraphVizGenerator().generateModuleGraphViz(targets: targets)
try expect(output) == """
digraph {
MyApp [shape=box]
MyInternalFramework [label=MyInternalFramework shape=box]
MyApp [shape=box]
Resources [label="[bundle]\\nResources" shape=box]
MyApp [shape=box]
MyStaticFramework [label="[carthage]\\nMyStaticFramework" shape=box]
MyApp [shape=box]
MyDynamicFramework [label="[carthage]\\nMyDynamicFramework" shape=box]
MyApp [shape=box]
MyExternalFramework [label="[framework]\\nMyExternalFramework" shape=box]
MyApp [shape=box]
MyPackage [label="[package]\\nMyPackage" shape=box]
MyApp [shape=box]
MySDK [label="[sdk]\\nMySDK" shape=box]
MyAppUITests [shape=box]
MyApp [label=MyApp shape=box]
MyApp -> MyInternalFramework [style=dashed]
MyApp -> Resources [style=dashed]
MyApp -> MyStaticFramework [style=dashed]
MyApp -> MyDynamicFramework [style=dashed]
MyApp -> MyExternalFramework [style=dashed]
MyApp -> MyPackage [style=dashed]
MyApp -> MySDK [style=dashed]
MyAppUITests -> MyApp [style=dashed]
}
"""
}
}
}
}