Skip to content

Commit 5f5eb72

Browse files
committed
swiftformat
1 parent dd7978b commit 5f5eb72

File tree

12 files changed

+117
-77
lines changed

12 files changed

+117
-77
lines changed

.swiftformat

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--swiftversion 5.5
2+
3+
--allman false
4+
--assetliterals visual-width
5+
--closingparen balanced
6+
--commas always
7+
--conflictmarkers reject
8+
--elseposition same-line
9+
--emptybraces no-space
10+
--exponentcase lowercase
11+
--extensionacl on-extension
12+
# --fragment false
13+
--funcattributes preserve
14+
--guardelse auto
15+
--header strip
16+
--hexliteralcase uppercase
17+
--ifdef indent
18+
--importgrouping alpha
19+
--indent 4
20+
--linebreaks lf
21+
--maxwidth none
22+
--modifierorder
23+
--nevertrailing
24+
--nospaceoperators ...,..<
25+
--nowrapoperators
26+
--operatorfunc spaced
27+
--patternlet hoist
28+
--ranges spaced
29+
--redundanttype inferred
30+
--self remove
31+
--semicolons never
32+
--shortoptionals always
33+
--stripunusedargs always
34+
--trailingclosures
35+
--trimwhitespace always
36+
--typeattributes preserve
37+
--varattributes preserve
38+
--voidtype void
39+
--wraparguments before-first
40+
--wrapcollections before-first
41+
--wrapconditions preserve
42+
--wrapparameters before-first
43+
--wrapreturntype preserve
44+
--yodaswap always

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ let package = Package(
1313
.library(
1414
name: "RuneterraWallpapersDownloader",
1515
targets: ["RuneterraWallpapersDownloader"]
16-
)
16+
),
1717
],
1818
dependencies: [
19-
// .package(url: "https://github.com/marmelroy/Zip", from: "2.1.1"),
19+
// .package(url: "https://github.com/marmelroy/Zip", from: "2.1.1"),
2020
// https://github.com/marmelroy/Zip/pull/221
2121
.package(url: "https://github.com/maparoni/Zip.git", .revisionItem("059e7346082d02de16220cd79df7db18ddeba8c3")),
2222
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.1"),

Sources/CLI/App.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import RuneterraWallpapersDownloader
44

55
struct Download: AsyncParsableCommand {
66
static var configuration = CommandConfiguration(commandName: "runeterraWallpaper")
7-
7+
88
@Argument(help: "Directory to save the wallpapers.", transform: URL.init(fileURLWithPath:))
99
var destination: URL
10-
10+
1111
@Option(name: .customLong("set"), help: "Specify all the Card Sets you want download. Use the set reference. By default all sets will be downloaded. Ex: --set 1 --set 2.")
1212
var sets: [Int] = []
13-
13+
1414
@Flag(help: "If true it reads the destination folder to find the zips instead of downloading them again.")
1515
var skipDownload: Bool = false
16-
16+
1717
@Flag(help: "If true downloaded zips won't be removed. Useful if you want to use `skipDownload` later.")
1818
var keepZips: Bool = false
19-
19+
2020
mutating func runAsync() async throws {
2121
print("Fetching card sets information...")
2222
let fetchedSets = try await CardSets().fetchSets()
@@ -28,7 +28,7 @@ struct Download: AsyncParsableCommand {
2828
} else {
2929
setsToDownload = fetchedSets
3030
.filter { sets.contains($0.number) }
31-
31+
3232
// Inform user of sets that couldn't be found.
3333
if setsToDownload.count != sets.count {
3434
let missingSets = sets
@@ -37,24 +37,24 @@ struct Download: AsyncParsableCommand {
3737
print(">> Set \(set) not found.")
3838
}
3939
}
40-
40+
4141
guard setsToDownload.isEmpty == false else {
4242
print("Nothing to download.")
4343
return
4444
}
45-
45+
4646
let formatter = ListFormatter()
4747
formatter.locale = .init(identifier: "en_US_POSIX")
4848
let names = setsToDownload.map(\.name)
4949
let stringList = formatter.string(from: names) ?? names.joined(separator: ",")
50-
print("Downloading \(setsToDownload.count) requested \(setsToDownload.count > 1 ? "sets": "set"): \(stringList).")
50+
print("Downloading \(setsToDownload.count) requested \(setsToDownload.count > 1 ? "sets" : "set"): \(stringList).")
5151
}
5252
print("")
53-
53+
5454
try await withThrowingTaskGroup(of: Void.self) { group in
5555
let progressBar = ProgressBar()
5656

57-
for `set` in setsToDownload {
57+
for set in setsToDownload {
5858
let ifNeeded = !skipDownload
5959
let destination = destination
6060
let keepZips = keepZips
@@ -88,7 +88,7 @@ private func download(
8888
let zipUrl = destination
8989
.appendingPathComponent(set.ref)
9090
.appendingPathExtension("zip")
91-
91+
9292
let exists = FileManager.default.fileExists(atPath: zipUrl.path)
9393
if !exists {
9494
print("\(zipUrl.lastPathComponent) not found. Skipping this set.")
@@ -107,7 +107,7 @@ private func download(
107107
progressBar.update(progress, for: set)
108108
}
109109
}
110-
110+
111111
// MARK: Extraction
112112

113113
private func extract(_ url: URL, destination: URL, keepZips: Bool) async {
@@ -125,15 +125,15 @@ private func extract(_ url: URL, destination: URL, keepZips: Bool) async {
125125
func log(sets: [CardSet], destination: URL) {
126126
let imagesCount = try! FileManager.default
127127
.contentsOfDirectory(atPath: destination.path)
128-
.filter({ URL(fileURLWithPath: $0).pathExtension == "png" })
128+
.filter { URL(fileURLWithPath: $0).pathExtension == "png" }
129129
.count
130-
130+
131131
let log = Log(
132132
date: Date(),
133133
sets: sets,
134134
numberOfWallpapers: imagesCount
135135
)
136-
136+
137137
let logLineString = log.lineString() + "\n"
138138
guard let logLine = logLineString.data(using: .utf8) else {
139139
print("Error creating log line.")
@@ -142,7 +142,7 @@ func log(sets: [CardSet], destination: URL) {
142142
let logFile = destination
143143
.appendingPathComponent("log")
144144
.appendingPathExtension("csv")
145-
145+
146146
if FileManager.default.fileExists(atPath: logFile.path) {
147147
if let fileHandle = try? FileHandle(forWritingTo: logFile) {
148148
fileHandle.seekToEndOfFile()
@@ -155,9 +155,8 @@ func log(sets: [CardSet], destination: URL) {
155155
}
156156

157157
@main
158-
struct App {
158+
enum App {
159159
static func main() async {
160160
await Download.main()
161161
}
162162
}
163-

Sources/CLI/Helpers.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import Foundation
22

3-
43
extension String {
54
func leftpad(_ n: Int) -> String {
6-
let padding = (0..<max(0, n-self.count)).map { _ in " " }.joined()
5+
let padding = (0..<max(0, n - count)).map { _ in " " }.joined()
76
return "\(padding)\(self)"
87
}
98
}

Sources/CLI/Log.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ struct Log {
1010
extension Log {
1111
func lineString() -> String {
1212
let dateFormatter = DateFormatter()
13-
dateFormatter.dateFormat = "yyyy/MM/dd"
13+
dateFormatter.dateFormat = "yyyy/MM/dd"
1414
let dateString = dateFormatter.string(from: date)
15-
15+
1616
let setsString = sets
1717
.map(\.ref)
1818
.joined(separator: "-")
19-
19+
2020
return "\(dateString),\(setsString),\(numberOfWallpapers)"
2121
}
2222
}

Sources/CLI/ProgressBar.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import Foundation
22
import RuneterraWallpapersDownloader
33

44
final class ProgressBar {
5-
var progressDict = Dictionary<CardSet, Progress>()
5+
var progressDict = [CardSet: Progress]()
66
var lastTime = Date()
7-
7+
88
func update(_ progress: Progress, for cardset: CardSet) {
99
progressDict[cardset] = progress
1010
guard Date().timeIntervalSince(lastTime) > 1 else { return }
1111
printProgress(progressDict: progressDict)
1212
lastTime = Date()
1313
}
14-
14+
1515
private func printProgress(
16-
progressDict: Dictionary<CardSet, Progress>
16+
progressDict: [CardSet: Progress]
1717
) {
1818
let maxNameLength = progressDict.keys.map(\.name.count).max() ?? 0
1919
let maxDescriptionLength = max(
@@ -24,13 +24,13 @@ final class ProgressBar {
2424
var printed = 0
2525
for (set, progress) in progressDict.sorted(by: { $0.0.ref < $1.0.ref }) {
2626
let bar = progressBar(fraction: progress.fractionCompleted, length: 20)
27-
let percentage = "\(String(format: "%.2f", progress.fractionCompleted*100).leftpad(6))%"
28-
27+
let percentage = "\(String(format: "%.2f", progress.fractionCompleted * 100).leftpad(6))%"
28+
2929
print("\(set.name.leftpad(maxNameLength)): \(progress.localizedAdditionalDescription?.leftpad(maxDescriptionLength) ?? "") \(bar) \(percentage)")
30-
30+
3131
printed += 1
3232
}
33-
33+
3434
eraseLines(printed)
3535
}
3636
}

Sources/RuneterraWallpapersDownloader/AssetsDownloader.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import Foundation
22

33
public final class AssetDownloader {
44
private var downloadDirectory: URL?
5-
5+
66
public init(downloadDirectory: URL?) {
77
self.downloadDirectory = downloadDirectory
88
}
9-
9+
1010
public func download(
11-
sets: Array<CardSet>,
11+
sets: [CardSet],
1212
cardsetProgress: @escaping (CardSet, Progress) -> Void
1313
) async throws -> [URL] {
14-
return try await withThrowingTaskGroup(of: URL.self, returning: [URL].self, body: { group in
14+
try await withThrowingTaskGroup(of: URL.self, returning: [URL].self, body: { group in
1515
for set in sets {
1616
_ = group.addTaskUnlessCancelled {
1717
try await self.downloadSet(
@@ -21,12 +21,12 @@ public final class AssetDownloader {
2121
}
2222
}
2323

24-
let folders = try await group.reduce(into: [], { $0.append($1) })
24+
let folders = try await group.reduce(into: []) { $0.append($1) }
2525
assert(folders.count == sets.count)
2626
return folders
2727
})
2828
}
29-
29+
3030
public func downloadSet(
3131
_ set: CardSet,
3232
onProgress: @escaping (CardSet, Progress) -> Void
@@ -36,7 +36,7 @@ public final class AssetDownloader {
3636
onProgress: { onProgress(set, $0) }
3737
)
3838
let zipURL: URL
39-
if let downloadURL = self.downloadDirectory {
39+
if let downloadURL = downloadDirectory {
4040
zipURL = downloadURL.appendingPathComponent(set.ref).appendingPathExtension("zip")
4141
} else {
4242
zipURL = localURL.appendingPathExtension("zip")

Sources/RuneterraWallpapersDownloader/CardSets.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public struct CardSet: Hashable {
55
public let ref: String
66
public let name: String
77
public let url: URL
8-
8+
99
public var number: Int {
1010
Int(String(ref.last!))!
1111
}
@@ -16,9 +16,8 @@ extension CardSet: CustomStringConvertible {
1616
}
1717

1818
public final class CardSets {
19-
2019
public init() {}
21-
20+
2221
// https://dd.b.pvp.net/latest/core-en_us.zip
2322
// en_us/data/globals-en_us.json["sets"]
2423
public func fetchSets() async throws -> [CardSet] {
@@ -33,12 +32,12 @@ public final class CardSets {
3332
.appendingPathComponent(UUID().uuidString)
3433
.appendingPathExtension("zip")
3534
try FileManager.default.moveItem(at: localURL, to: zipURL)
36-
35+
3736
// Unzip
3837
let destination = URL(fileURLWithPath: NSTemporaryDirectory())
3938
.appendingPathComponent("sets_\(UUID().uuidString)")
4039
try Zip.unzipFile(zipURL, destination: destination, overwrite: true, password: nil)
41-
40+
4241
// Read globals file
4342
let globalsURL = destination
4443
.appendingPathComponent("en_us")
@@ -47,7 +46,7 @@ public final class CardSets {
4746
.appendingPathExtension("json")
4847
let data = try Data(contentsOf: globalsURL)
4948
let globals = try JSONDecoder().decode(Globals.self, from: data)
50-
49+
5150
let cardSets = globals.sets
5251
.filter {
5352
// Ignore the Events set. Is an only in game set,
@@ -61,15 +60,15 @@ public final class CardSets {
6160
}
6261
.map(CardSet.init(set:))
6362
.sorted(by: { $0.ref < $1.ref })
64-
63+
6564
return cardSets
6665
}
6766
}
6867

6968
/// Decoder for globals json file
7069
private struct Globals: Decodable {
7170
let sets: [Set]
72-
71+
7372
struct Set: Decodable {
7473
public let name: String
7574
public let nameRef: String

Sources/RuneterraWallpapersDownloader/URLSessionAsync.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ private final class Box<T> {
55
init(_ value: T) { self.value = value }
66
}
77

8-
extension URLSession {
8+
public extension URLSession {
99
/// Convenience method to download using an URLRequest, creates and resumes an URLSessionDownloadTask internally.
1010
///
1111
/// - Parameter request: The URLRequest for which to download.
1212
/// - Parameter onProgress: Closure to call when the progress of the task is updated.
1313
/// - Parameter delegate: Task-specific delegate.
1414
/// - Returns: Downloaded file URL and response. The file will not be removed automatically.
15-
public func download(
15+
func download(
1616
for request: URLRequest,
1717
onProgress: @escaping (Progress) -> Void,
1818
delegate: URLSessionTaskDelegate? = nil
@@ -46,7 +46,7 @@ extension URLSession {
4646
observation.value = task.progress.observe(\.fractionCompleted) { progress, _ in
4747
onProgress(progress)
4848
}
49-
49+
5050
task.resume()
5151
}
5252
} onCancel: {

0 commit comments

Comments
 (0)