Skip to content

Commit 06cc6f2

Browse files
authored
Merge pull request #12 from rofle100lvl/SmallImprovements
Small improvements
2 parents 000b68a + 441b16d commit 06cc6f2

File tree

6 files changed

+60
-24
lines changed

6 files changed

+60
-24
lines changed

Sources/UseGraph/UseGraphCommand.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public struct UseGraphCommand: AsyncParsableCommand {
1111
version: "0.0.1",
1212
subcommands: [
1313
UseGraphFrontendCommand.self,
14-
]
14+
],
15+
defaultSubcommand: UseGraphFrontendCommand.self
1516
)
1617
}

Sources/UseGraphFrontend/UseGraph.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public struct UseGraphFrontendCommand: AsyncParsableCommand {
1111
abstract: "Swift CLI to work with Use Graph tool",
1212
version: "0.0.1",
1313
subcommands: [
14-
UseGraphBuildCommand.self,
15-
UseGraphAnalyzeCommand.self,
1614
UseGraphPeripheryAnalyzeCommand.self,
1715
UseGraphPeripheryBuildCommand.self
1816
]

Sources/UseGraphPeriphery/GraphBuilder.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum OutputFormat {
77
case svg
88
case png
99
case gv
10+
case csv
1011

1112
public static func parse(format: String) throws -> OutputFormat {
1213
switch format.lowercased() {
@@ -16,6 +17,8 @@ enum OutputFormat {
1617
.png
1718
case "gv":
1819
.gv
20+
case "csv":
21+
.csv
1922
default:
2023
throw FormatError.formatIsNotCorrect
2124
}
@@ -48,8 +51,6 @@ final class GraphBuilder {
4851

4952
guard let edgesData = edgesCSV.data(using: .utf8),
5053
let nodesData = nodesCSV.data(using: .utf8) else { fatalError() }
51-
print(FileManager.default.createFile(atPath: edgesUrl.path(), contents: edgesData))
52-
print(FileManager.default.createFile(atPath: nodesUrl.path(), contents: nodesData))
5354
}
5455

5556
func buildGraph(edges: [Edge], format: OutputFormat) async throws {
@@ -60,10 +61,11 @@ final class GraphBuilder {
6061
let url = URL(fileURLWithPath: #file).deletingLastPathComponent().appending(path: "Graph.\(format.rawValue)")
6162
guard let fileContents = String(data: data, encoding: .utf8) else { fatalError() }
6263

63-
print(FileManager.default.createFile(atPath: url.path(), contents: fileContents.data(using: .utf8)))
6464
Task {
6565
System.shared.run("open \(url.path())")
6666
}
67+
case .csv:
68+
csvBuildGraph(edges: edges)
6769
}
6870
}
6971

@@ -92,6 +94,8 @@ extension GraphBuilder {
9294
.png
9395
case .gv:
9496
.gv
97+
case .csv:
98+
nil
9599
}
96100
}
97101
}

Sources/UseGraphPeriphery/UseGraphPeripheryBuildCommand.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct UseGraphPeripheryBuildCommand: AsyncParsableCommand {
4747
public init() {}
4848

4949
public static let configuration = CommandConfiguration(
50-
commandName: "usage_graph_dynamic",
50+
commandName: "usage_graph",
5151
abstract: "Command to build graph of usage.",
5252
version: "0.0.1"
5353
)
@@ -60,6 +60,9 @@ public struct UseGraphPeripheryBuildCommand: AsyncParsableCommand {
6060

6161
@Option(help: "Paths to index store")
6262
var indexStore: String? = nil
63+
64+
@Option(help: "Output file format. Now available: CSV, SVG, PNG, GV")
65+
var format: String = "csv"
6366

6467
public func run() async throws {
6568
let configuration = Configuration()
@@ -114,7 +117,7 @@ public struct UseGraphPeripheryBuildCommand: AsyncParsableCommand {
114117
let edges = edgeDict.compactMap {
115118
Edge(from: $0.key.from, to: $0.key.to, references: $0.value)
116119
}
117-
GraphBuilder.shared.csvBuildGraph(edges: edges)
120+
try await GraphBuilder.shared.buildGraph(edges: edges, format: OutputFormat.parse(format: format))
118121
}
119122
}
120123

Sources/UseGraphPeriphery/UseGraphPeripheryCommand.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public struct UseGraphPeripheryAnalyzeCommand: AsyncParsableCommand {
1313
public init() {}
1414

1515
public static let configuration = CommandConfiguration(
16-
commandName: "usage_graph_dynamic_analyze",
16+
commandName: "monolith_destroyer",
1717
abstract: "Command to build graph of usage.",
1818
version: "0.0.1"
1919
)
2020

2121
@Option(help: "Path to project (.xcodeproj)")
2222
var projectPath: String? = nil
23-
24-
@Option(help: "Paths to folder with sources - \"path1,path2,path3\"")
25-
var folderPaths: String
23+
24+
@Option(help: "Paths to your monolith")
25+
var monolithPath: String
2626

2727
@Option(help: "Paths to index store")
2828
var indexStore: String? = nil
@@ -32,10 +32,8 @@ public struct UseGraphPeripheryAnalyzeCommand: AsyncParsableCommand {
3232

3333
public func run() async throws {
3434
var projectURL: URL?
35-
let folderURLs: [String] = try folderPaths.split(separator: ",").map {
36-
guard let folderURL = URL(string: String($0)) else { throw PathError.pathIsNotCorrect }
37-
return folderURL.path()
38-
}
35+
36+
let folderURLs: [String] = findSubdirectories(atPath: monolithPath)
3937

4038
if let projectPath {
4139
projectURL = URL(string: projectPath)
@@ -136,6 +134,27 @@ public struct UseGraphPeripheryAnalyzeCommand: AsyncParsableCommand {
136134
print(folderPath + " - " + String(edgesInFolder.count))
137135
}
138136
}
137+
138+
139+
func findSubdirectories(atPath path: String) -> [String] {
140+
let fileManager = FileManager.default
141+
var subdirectories: [String] = []
142+
143+
do {
144+
let contents = try fileManager.contentsOfDirectory(atPath: path)
145+
for item in contents {
146+
let fullPath = (path as NSString).appendingPathComponent(item)
147+
var isDirectory: ObjCBool = false
148+
if fileManager.fileExists(atPath: fullPath, isDirectory: &isDirectory), isDirectory.boolValue {
149+
subdirectories.append(fullPath)
150+
}
151+
}
152+
} catch {
153+
print("Error while reading the document: \(error)")
154+
}
155+
156+
return subdirectories
157+
}
139158
}
140159

141160
extension String {

Sources/Utils/HTMLBuilder.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,22 @@ public final class HTMLGenerator {
2121
<meta charset="UTF-8">
2222
<style>
2323
svg {
24-
border: 1px solid blue;
24+
}
25+
.svg-container {
26+
resize: horizontal;
27+
overflow: hidden;
28+
width: auto;
29+
height: auto;
30+
}
31+
.overlay-image {
32+
position: fixed;
33+
bottom: 20px;
34+
right: 20px;
35+
width: 150px;
36+
height: 150px;
37+
z-index: 9999;
38+
box-shadow: 0 0 10px rgba(0,0,0,0.5);
39+
border-radius: 10px;
2540
}
2641
</style>
2742
</head>
@@ -46,21 +61,17 @@ public final class HTMLGenerator {
4661

4762
htmlString += """
4863
</table>
49-
<div style={{
50-
backgroundColor: 'lightpink',
51-
resize: 'horizontal',
52-
overflow: 'hidden',
53-
width: '1000px',
54-
height: 'auto',
55-
}}>
64+
<div class="svg-container">
5665
<svg viewBox="0 0 3000 3000">
5766
\(svgString)
5867
</svg>
5968
</div>
69+
<img class="overlay-image" src="https://storage.yandexcloud.net/swift-ui-course/MonolithDestroyer.png" alt="Overlay Image">
6070
</body>
6171
</html>
6272
"""
6373

6474
return htmlString
6575
}
76+
6677
}

0 commit comments

Comments
 (0)