-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathspmready.swift
executable file
·171 lines (134 loc) · 4.04 KB
/
spmready.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/swift
import Foundation
class Pod {
init(name: String) {
self.name = name
}
let name: String
var repo: String?
var spmready: Bool = false
}
extension Pod {
func readyOrNot() -> String {
if spmready {
return "✅"
}
return "❌"
}
func format() -> String {
"\(readyOrNot()) | \(name) : \(repo ?? "not found")"
}
}
extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression: \(pattern).")
}
}
}
extension NSRegularExpression {
func matches(_ string: String) -> NSTextCheckingResult? {
let range = NSRange(location: 0, length: string.utf16.count)
return firstMatch(in: string, options: [], range: range)
}
}
func fetchPods(_ path: String) -> [Pod]? {
var pods: [Pod] = []
do {
// Get the contents
let contents = try String(contentsOfFile: path, encoding: .utf8)
let lines = contents.split(separator: "\n")
for line in lines {
let regex = NSRegularExpression("pod '([A-Za-z0-9-]*)'")
if let match = regex.matches(String(line)), let podNameRange = Range(match.range(at: 1), in: line) {
let podName = line[podNameRange]
pods.append(Pod(name: String(podName)))
}
}
// sprint(contents)
} catch {
print("Failed to open Podfile at \(path)")
print("make sure the file exists")
return nil
}
return pods
}
func fetchUrl(pod: String) -> String {
let semaphore = DispatchSemaphore(value: 0)
let path = "https://cocoapods.org/pods/\(pod)"
var result = ""
guard let url = URL(string: path) else {
return ""
}
let task = URLSession.shared.dataTask(with: url) { data, _, _ in
if let data = data {
result = String(data: data, encoding: .utf8) ?? ""
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .distantFuture)
return result
}
func isSpmReady(pod: Pod) -> Bool {
guard let repo = pod.repo else {
return false
}
let spmUrl = repo.replacingOccurrences(of: ".git", with: "") + "/blob/master/Package.swift"
var result = false
let semaphore = DispatchSemaphore(value: 0)
let url = URL(string: spmUrl)!
let task = URLSession.shared.dataTask(with: url) { _, response, _ in
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
result = true
}
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .distantFuture)
return result
}
func fetchRepoOnline(podName: String) -> String? {
let page = fetchUrl(pod: podName)
let regex = NSRegularExpression("(((https?):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\.&]*))\">GitHub Repo</a>")
if let match = regex.matches(String(page)) {
if let podNameRange = Range(match.range(at: 1), in: page) {
let repo = page[podNameRange]
return String(repo)
}
}
return nil
}
let path: String
if CommandLine.arguments.count == 2 {
path = CommandLine.arguments[1]
} else {
let arg = CommandLine.arguments.first!
path = arg.prefix(upTo: arg.lastIndex(of: "/")!) + "/Podfile"
}
// Set the file path
guard let pods = fetchPods(path) else {
exit(1)
}
print("Found \(pods.count) pod")
for pod in pods {
if let url = fetchRepoOnline(podName: pod.name) {
pod.repo = url
if isSpmReady(pod: pod) {
pod.spmready = true
}
print(pod.format())
}
}
let ready = pods.filter { $0.spmready }.count
let notReady = pods.filter { !$0.spmready }.count
if ready == pods.count {
print("🎊 you are ready for Swift Package Manager")
} else {
print("Sorry 😢 - ✅ \(ready) | ❌ \(notReady)")
print("Help to improve SPM capablility by opening an issue or contribute via PR")
}