-
Notifications
You must be signed in to change notification settings - Fork 7
/
spmlib2rss.swift
executable file
·218 lines (188 loc) · 7.1 KB
/
spmlib2rss.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/swift sh
import Foundation
import Shell // @AlwaysRightInstitute
let PATH = "/Users/helge/dev/Swift/Catalogs/SwiftPMLibrary"
let FILE = "packages.json"
let debug = false
let count = 50
setenv("TZ", "GMT", 1)
FileManager.default.changeCurrentDirectoryPath(PATH)
let dateParser : ISO8601DateFormatter = {
let fmt = ISO8601DateFormatter()
fmt.timeZone = TimeZone(secondsFromGMT: 0)
return fmt
}()
// git log -- packages.json
func findCommitsThatChangedFile(_ file: String)
-> [ ( hash: String, date: Date, author: String ) ]
{
return shell.git("log", "--date=iso-strict-local", "--", file)
.stdout
.components(separatedBy: "\ncommit ")
.filter { !$0.isEmpty }
.map {
let lines = $0.split(separator: "\n")
let hash = lines[0].hasPrefix("commit ")
? lines[0].dropFirst(7)
: lines[0]
// 2020-03-20T20:22:38+00:00
let dateString = lines.first(where: { $0.hasPrefix("Date:") } )!
.dropFirst(5)
.trimmingCharacters(in: .whitespaces)
let author = lines.first(where: { $0.hasPrefix("Author:") } )!
.dropFirst(7)
.trimmingCharacters(in: .whitespaces)
let date = dateParser.date(from: dateString)!
return ( hash: String(hash), date: date, author: author )
}
}
func loadPackageURLsInCommit(_ hash: String) -> Set<URL> {
let archive = shell.git("archive", hash, FILE, "| tar -x -O")
let json = archive.stdout.data(using: .utf8)!
guard let urls = try? JSONDecoder().decode([ URL ].self, from: json) else {
return [] // This DOES happen! The repo had invalid JSON early on.
}
return Set(urls.map { $0.deletingPathExtension() })
}
let orderedCommitHashes = findCommitsThatChangedFile(FILE) [0..<count]
var hashToSet = [ String : Set<URL> ]()
if debug {
print("Loading:", terminator: ""); fflush(stdout)
}
for ( hash, _, _ ) in orderedCommitHashes {
hashToSet[hash] = loadPackageURLsInCommit(hash)
if debug {
print(" ★", terminator: ""); fflush(stdout)
}
}
if debug {
print()
print("Done: #\(hashToSet.count) sets.")
}
func calculatePackageAdditions() ->
[ ( hash: String, newURLs: [ URL ], date: Date, author: String ) ]
{
var result = [( hash: String, newURLs: [URL], date: Date, author: String )]()
for ( idx, ( hash, date, author ) ) in orderedCommitHashes.enumerated() {
if idx + 1 == orderedCommitHashes.endIndex { break }
let predHash = orderedCommitHashes[idx + 1].hash
let predSet = hashToSet[predHash]!
var workingSet = hashToSet[hash]!
workingSet.subtract(predSet)
guard !workingSet.isEmpty else { continue }
// skip unreadable sets, this is not 100 correct
guard !predSet.isEmpty else { continue }
result.append( (
hash : hash,
newURLs : workingSet.sorted { $0.absoluteString < $1.absoluteString },
date : date,
author : author
) )
}
return result
}
func parseAuthor(_ s: String) -> ( name: String, email: String? ) {
guard let o = s.firstIndex(of: "<"),
let c = s.lastIndex (of: ">"),
let a = s.firstIndex(of: "@") else { return ( name: s, email: nil ) }
let name = s[..<o]
let email = s[s.index(after: o)..<c]
return ( name: String(name), email: email.isEmpty ? nil : String(email) )
}
// MARK: - Generate RSS
let httpFormatter : DateFormatter = {
let df = DateFormatter()
df.locale = .init(identifier: "en_US_POSIX")
df.calendar = .init(identifier: .gregorian)
df.timeZone = TimeZone(secondsFromGMT: 0)
df.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
return df
}()
func xmlEscape(_ s: String) -> String {
return s.replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "\"", with: """)
.replacingOccurrences(of: "'", with: "'")
.replacingOccurrences(of: "<", with: "<")
.replacingOccurrences(of: ">", with: ">")
}
func urlEscape(_ s: String) -> String {
return s.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? s
}
let now = Date()
let ttlInMinutes = 240
print(
"""
<?xml version="1.0" encoding=\"UTF-8\"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content">
<channel>
<title>SwiftPM Library - New Packages</title>
<link>https://swiftpm.co</link>
<language>en-us</language>
<lastBuildDate>\(httpFormatter.string(for: now)!)</lastBuildDate>
<pubDate>\(httpFormatter.string(for: now)!)</pubDate>
<ttl>\(ttlInMinutes)</ttl>
"""
)
defer { print("</channel>\n</rss>") }
for ( hash, newURLs, date, author ) in calculatePackageAdditions() {
if debug {
print("New in:", hash)
if newURLs.count > 10 {
print("TOO MANY NEW URLS?!", newURLs.count, author, date)
}
for url in newURLs.prefix(10) { // limit
print(" ", url)
}
}
guard !newURLs.isEmpty else { continue }
let ( authorName, email ) = parseAuthor(author)
for url in newURLs.prefix(30) {
let guid = hash + "|" + url.absoluteString
let pc = url.deletingPathExtension().path.split(separator: "/")
let package = pc.dropFirst().joined(separator: " / ")
let org = pc.first ?? "?"
print(" <item>"); defer { print(" </item>") }
print(
"""
<guid>\(guid)</guid>
<title>Added “\(xmlEscape(package))” @\(org)</title>
<pubDate>\(httpFormatter.string(for: date)!)</pubDate>
<link>\(url.absoluteString)</link>
<description>
\(xmlEscape(author)) was so kind to submit a new
Swift package called
"\(xmlEscape(package))" (@\(org))
to the SwiftPM Library.
</description>
"""
)
do {
print(" <content:encoded><![CDATA[", terminator: "")
defer { print("]]></content:encoded>") }
print(author)
print(" was so kind to submit a new Swift package called ")
print("<a href='\(url.absoluteString)'><b>\(package)</b></a>")
print(" (")
print("<a href='https://github.com/\(urlEscape(String(org)))'>@\(org)</a>")
print(") to the ")
print("<a href='https://swiftpm.co/?query=\(urlEscape(package))'>SwiftPM Library</a>.")
print("<br /><hr /><small>")
print("<li>New Package on GitHub: <a href='\(url.absoluteString)'>\(package)</a></li>")
print("<li>GitHub Organization: ")
print("<a href='https://github.com/\(urlEscape(String(org)))'>@\(org)</a>")
print("</li>")
if let email = email, !email.isEmpty {
print("<li>Send \(authorName) an email: <a href='mailto:\(email)'>\(email)</a>")
print("</li>")
}
print("<li><a href='https://swiftpm.co/'>SwiftPM Library</a> ")
print("by <a href='https://daveverwer.com'>Dave Verwer</a>")
print("</li>")
print("<li>")
print("A small macOS app to browse packages:")
print("<a href='https://zeezide.com/en/products/swiftpmcatalog/index.html'>SwiftPM Catalog</a></li>")
print("</small>")
}
}
}