-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.swift
executable file
·75 lines (62 loc) · 2.04 KB
/
cleanup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/swift
// This is a script to clean up a SwiftPM environment.
import Foundation
let fileManager = FileManager.default
let homeDirectory = NSHomeDirectory()
let derivedDataPath = "\(homeDirectory)/Library/Developer/Xcode/DerivedData"
func pathsToClean(for path: String) -> [String] {
[
"\(path)/.build",
"\(path)/Package.resolved",
"\(path)/.swiftpm",
"\(homeDirectory)/.swiftpm",
"\(homeDirectory)/Library/Caches/org.swift.swiftpm",
]
}
func delete(path: String) {
do {
if fileManager.fileExists(atPath: path) {
try fileManager.removeItem(atPath: path)
print("✅ Deleted: \(path)")
} else {
print("❕ Path not found, skipped: \(path)")
}
} catch {
print("❌ Error deleting path \(path): \(error)")
}
}
func deleteDerivedData(for project: String) {
do {
let derivedDataContents = try fileManager.contentsOfDirectory(atPath: derivedDataPath)
let projectFolder = derivedDataContents.first { $0.contains(project) }
if let projectFolder = projectFolder {
let fullPath = "\(derivedDataPath)/\(projectFolder)"
delete(path: fullPath)
} else {
print("❕ No DerivedData found for project \(project).")
}
} catch {
print("❌ Failed to access DerivedData directory: \(error)")
}
}
let arguments = CommandLine.arguments
let shouldDeleteDerivedData = arguments.contains("--clean-derived-data")
let projectPath: String
if
let pathIndex = arguments.firstIndex(of: "--path"),
arguments.count > pathIndex + 1
{
projectPath = arguments[pathIndex + 1]
} else {
projectPath = fileManager.currentDirectoryPath
}
let projectName = URL(fileURLWithPath: projectPath).lastPathComponent
for path in pathsToClean(for: projectPath) {
delete(path: path)
}
if shouldDeleteDerivedData {
deleteDerivedData(for: projectName)
} else {
print("Skipping DerivedData. Use '--clean-derived-data' flag to clean it.")
}
print("🧹 Cleanup completed.")