├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── Package.resolved ├── Package.swift ├── README.md ├── Sources └── flock │ ├── Commands │ ├── Command.swift │ ├── HelpCommand.swift │ ├── MainCommand.swift │ └── VersionCommand.swift │ ├── Digraph.swift │ ├── Edge.swift │ ├── Error.swift │ ├── Node.swift │ ├── Result.swift │ └── main.swift └── flock.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | 6 | *.dot 7 | bin 8 | *.tar.gz 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 0.1.0 (2017-03-09) 4 | 5 | ### Added 6 | * Fundamental features 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Naoto Kaneko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: build 2 | 3 | build: 4 | swift build 5 | mkdir -p bin 6 | cp .build/debug/flock bin/ 7 | tar czvf flock.tar.gz bin 8 | 9 | clean: 10 | swift build --clean 11 | rm -rf bin 12 | rm -rf Packages 13 | rm flock.tar.gz 14 | 15 | -------------------------------------------------------------------------------- /Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "object": { 3 | "pins": [ 4 | { 5 | "package": "Commandant", 6 | "repositoryURL": "https://github.com/Carthage/Commandant.git", 7 | "state": { 8 | "branch": null, 9 | "revision": "ab68611013dec67413628ac87c1f29e8427bc8e4", 10 | "version": "0.17.0" 11 | } 12 | }, 13 | { 14 | "package": "CwlCatchException", 15 | "repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git", 16 | "state": { 17 | "branch": null, 18 | "revision": "7cd2f8cacc4d22f21bc0b2309c3b18acf7957b66", 19 | "version": "1.2.0" 20 | } 21 | }, 22 | { 23 | "package": "CwlPreconditionTesting", 24 | "repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git", 25 | "state": { 26 | "branch": null, 27 | "revision": "c228db5d2ad1b01ebc84435e823e6cca4e3db98b", 28 | "version": "1.2.0" 29 | } 30 | }, 31 | { 32 | "package": "Nimble", 33 | "repositoryURL": "https://github.com/Quick/Nimble.git", 34 | "state": { 35 | "branch": null, 36 | "revision": "b02b00b30b6353632aa4a5fb6124f8147f7140c0", 37 | "version": "8.0.5" 38 | } 39 | }, 40 | { 41 | "package": "PathKit", 42 | "repositoryURL": "https://github.com/kylef/PathKit", 43 | "state": { 44 | "branch": null, 45 | "revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511", 46 | "version": "1.0.0" 47 | } 48 | }, 49 | { 50 | "package": "Quick", 51 | "repositoryURL": "https://github.com/Quick/Quick.git", 52 | "state": { 53 | "branch": null, 54 | "revision": "33682c2f6230c60614861dfc61df267e11a1602f", 55 | "version": "2.2.0" 56 | } 57 | }, 58 | { 59 | "package": "SourceKitten", 60 | "repositoryURL": "https://github.com/jpsim/SourceKitten", 61 | "state": { 62 | "branch": null, 63 | "revision": "77a4dbbb477a8110eb8765e3c44c70fb4929098f", 64 | "version": "0.29.0" 65 | } 66 | }, 67 | { 68 | "package": "Spectre", 69 | "repositoryURL": "https://github.com/kylef/Spectre.git", 70 | "state": { 71 | "branch": null, 72 | "revision": "f14ff47f45642aa5703900980b014c2e9394b6e5", 73 | "version": "0.9.0" 74 | } 75 | }, 76 | { 77 | "package": "SWXMLHash", 78 | "repositoryURL": "https://github.com/drmohundro/SWXMLHash.git", 79 | "state": { 80 | "branch": null, 81 | "revision": "a4931e5c3bafbedeb1601d3bb76bbe835c6d475a", 82 | "version": "5.0.1" 83 | } 84 | }, 85 | { 86 | "package": "Yams", 87 | "repositoryURL": "https://github.com/jpsim/Yams.git", 88 | "state": { 89 | "branch": null, 90 | "revision": "c947a306d2e80ecb2c0859047b35c73b8e1ca27f", 91 | "version": "2.0.0" 92 | } 93 | } 94 | ] 95 | }, 96 | "version": 1 97 | } 98 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "flockLib", 7 | products: [ 8 | .library( 9 | name: "flockLib", 10 | targets: ["flock"] 11 | ) 12 | ], 13 | dependencies: [ 14 | .package(url: "https://github.com/jpsim/SourceKitten", from: "0.29.0"), 15 | .package(url: "https://github.com/kylef/PathKit", from: "1.0.0") 16 | ], 17 | targets: [ 18 | .target( 19 | name: "flock", 20 | dependencies: ["SourceKittenFramework", "PathKit"] 21 | ) 22 | ] 23 | ) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flock 2 | 3 | Flock is a simple tool to create visual object graphs of Swift source code. 4 | 5 | ![flock](flock.png) 6 | 7 | To create the above diagram using [Graphviz](http://graphviz.org): 8 | ```sh 9 | $ cd 10 | $ flock Source > object_graph.dot 11 | $ dot -Tpng object_graph.dot -o flock.png 12 | ``` 13 | 14 | ## Installation 15 | 16 | ```sh 17 | $ brew tap naoty/misc 18 | $ brew install flock 19 | ``` 20 | 21 | ## Usage 22 | ```sh 23 | $ flock [folder of swift files] > object_graph.dot 24 | $ dot -Tpdf object_graph.dot -o object_graph.pdf 25 | ``` 26 | -------------------------------------------------------------------------------- /Sources/flock/Commands/Command.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Command.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/02/25. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | protocol Command { 10 | func run() -> Result 11 | } 12 | -------------------------------------------------------------------------------- /Sources/flock/Commands/HelpCommand.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HelpCommand.swift 3 | // flock 4 | // 5 | // Created by Naoto kaneko on 2017/02/25. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | struct HelpCommand: Command { 10 | func run() -> Result { 11 | let helpMessage = "USAGE: flock [file ...]" 12 | return .success(message: helpMessage) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Sources/flock/Commands/MainCommand.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MainCommand.swift 3 | // flock 4 | // 5 | // Created by Naoto kaneko on 2017/02/28. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | import SourceKittenFramework 10 | 11 | struct MainCommand: Command { 12 | private let structures: [Structure] 13 | 14 | init(paths: [String]) { 15 | do { 16 | structures = try paths.map({ File(path: $0) }).compactMap({ $0 }).map({ try Structure(file: $0) }) 17 | } 18 | catch { 19 | print("There was a serious error") 20 | structures = [] 21 | } 22 | } 23 | 24 | func run() -> Result { 25 | let edges = makeEdges() 26 | let digraph = Digraph(name: "flock", edges: edges) 27 | return .success(message: digraph.description) 28 | } 29 | 30 | private func makeEdges() -> Set { 31 | var edges = Set() 32 | var leftNodes = Set() 33 | 34 | for structure in structures { 35 | guard let substructures = structure.dictionary["key.substructure"] as? [[String: SourceKitRepresentable]] else { 36 | continue 37 | } 38 | 39 | for substructure in substructures { 40 | guard let kindValue = substructure["key.kind"] as? String else { 41 | continue 42 | } 43 | 44 | guard let kind = SwiftDeclarationKind(rawValue: kindValue) else { 45 | continue 46 | } 47 | 48 | guard [.struct, .class, .enum, .protocol].contains(kind) else { 49 | continue 50 | } 51 | 52 | guard let nameValue = substructure["key.name"] as? String else { 53 | continue 54 | } 55 | 56 | let leftNode = Node(name: nameValue) 57 | leftNodes.insert(leftNode) 58 | 59 | if let inheritedTypes = substructure["key.inheritedtypes"] as? [[String: SourceKitRepresentable]] { 60 | for inheritedType in inheritedTypes { 61 | guard let inheritedTypeName = inheritedType["key.name"] as? String else { 62 | continue 63 | } 64 | 65 | let rightNode = Node(name: inheritedTypeName) 66 | if leftNode == rightNode { 67 | continue 68 | } 69 | 70 | let edge = Edge(left: leftNode, right: rightNode) 71 | edges.insert(edge) 72 | } 73 | } 74 | 75 | guard let subsubstructures = substructure["key.substructure"] as? [[String: SourceKitRepresentable]] else { 76 | continue 77 | } 78 | 79 | let rightNodes = makeRightNodes(fromStructures: subsubstructures) 80 | for rightNode in rightNodes { 81 | if leftNode == rightNode { 82 | continue 83 | } 84 | 85 | let edge = Edge(left: leftNode, right: rightNode) 86 | edges.insert(edge) 87 | } 88 | } 89 | } 90 | 91 | // Filter out edges which right nodes are not included in left nodes 92 | edges = Set(edges.filter({ leftNodes.contains($0.right) })) 93 | 94 | return edges 95 | } 96 | 97 | private func makeRightNodes(fromStructures structures: [[String: SourceKitRepresentable]]) -> [Node] { 98 | var nodes: [Node] = [] 99 | 100 | for structure in structures { 101 | guard let kindValue = structure["key.kind"] as? String else { 102 | continue 103 | } 104 | 105 | guard let kind = SwiftDeclarationKind(rawValue: kindValue) else { 106 | continue 107 | } 108 | 109 | guard [.varInstance, .varLocal, .varParameter, .varStatic].contains(kind) else { 110 | continue 111 | } 112 | 113 | guard let nameValue = structure["key.typename"] as? String else { 114 | continue 115 | } 116 | 117 | let node = Node(name: nameValue) 118 | nodes.append(node) 119 | 120 | guard let substructures = structure["key.substructure"] as? [[String: SourceKitRepresentable]] else { 121 | continue 122 | } 123 | 124 | let subnodes = makeRightNodes(fromStructures: substructures) 125 | nodes += subnodes 126 | } 127 | 128 | return nodes 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Sources/flock/Commands/VersionCommand.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VersionCommand.swift 3 | // flock 4 | // 5 | // Created by naoto kaneko on 2017/02/25. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | struct VersionCommand: Command { 10 | let version: String 11 | 12 | func run() -> Result { 13 | return .success(message: version) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Sources/flock/Digraph.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Digraph.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/03/03. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | struct Digraph: CustomStringConvertible { 10 | let name: String 11 | let edges: Set 12 | 13 | private let indent = " " 14 | 15 | var description: String { 16 | var buffer: [String] = [] 17 | buffer.append("digraph \(name) {") 18 | 19 | for edge in edges { 20 | buffer.append("\(indent)\(edge)") 21 | } 22 | 23 | buffer.append("}") 24 | 25 | return buffer.joined(separator: "\n") 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/flock/Edge.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Edge.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/03/03. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | struct Edge: CustomStringConvertible { 10 | let left: Node 11 | let right: Node 12 | 13 | var description: String { 14 | return "\"\(left)\" -> \"\(right)\";" 15 | } 16 | } 17 | 18 | extension Edge: Equatable { 19 | static func == (left: Edge, right: Edge) -> Bool { 20 | return left.left == right.left && left.right == right.right 21 | } 22 | } 23 | 24 | extension Edge: Hashable { 25 | func hash(into hasher: inout Hasher) { 26 | hasher.combine(left.hashValue) 27 | hasher.combine(right.hashValue) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Sources/flock/Error.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Error.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/03/01. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | enum Error: Swift.Error { 10 | case unknown 11 | case cannotReadContent(path: String) 12 | 13 | var message: String { 14 | switch self { 15 | case .unknown: 16 | return "Unknown error" 17 | case let .cannotReadContent(path): 18 | return "Cannot read content from \(path)" 19 | } 20 | } 21 | 22 | var code: Int { 23 | return 1 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/flock/Node.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Node.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/03/03. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | struct Node: CustomStringConvertible { 10 | let name: String 11 | 12 | var description: String { 13 | return name 14 | } 15 | } 16 | 17 | extension Node: Equatable { 18 | static func == (left: Node, right: Node) -> Bool { 19 | return left.name == right.name 20 | } 21 | } 22 | 23 | extension Node: Hashable { 24 | func hash(into hasher: inout Hasher) { 25 | hasher.combine(name) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/flock/Result.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Result.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/02/25. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | enum Result { 10 | case success(message: String) 11 | case failure(error: Error) 12 | } 13 | -------------------------------------------------------------------------------- /Sources/flock/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // flock 4 | // 5 | // Created by Naoto Kaneko on 2017/02/25. 6 | // Copyright (c) 2017 Naoto Kaneko. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import PathKit 11 | 12 | let version = "0.1.0" 13 | 14 | func main() { 15 | let command = makeCommand(arguments: [String](CommandLine.arguments.dropFirst())) 16 | let result = command.run() 17 | 18 | switch result { 19 | case let .success(message): 20 | print(message) 21 | exit(0) 22 | case let .failure(error): 23 | print(error.message) 24 | exit(Int32(error.code)) 25 | } 26 | } 27 | 28 | func makeCommand(arguments: [String]) -> Command { 29 | guard let firstArgument = arguments.first else { 30 | let paths = allSourcePaths(directoryPath: ".") 31 | return MainCommand(paths: paths) 32 | } 33 | 34 | switch firstArgument { 35 | case "--help", "-h": 36 | return HelpCommand() 37 | case "--version", "-v": 38 | return VersionCommand(version: version) 39 | default: 40 | let paths = allSourcePaths(directoryPath: firstArgument) 41 | return MainCommand(paths: paths) 42 | } 43 | } 44 | 45 | func allSourcePaths(directoryPath: String) -> [String] { 46 | let absolutePath = Path(directoryPath).absolute() 47 | 48 | do { 49 | return try absolutePath.recursiveChildren().filter({ $0.extension == "swift" }).map({ $0.string }) 50 | } catch { 51 | return [] 52 | } 53 | } 54 | 55 | main() 56 | -------------------------------------------------------------------------------- /flock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/naoty/flock/5df454d79c4e37acf055ad8ff83c8f6ad587a12e/flock.png --------------------------------------------------------------------------------