-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dependency.swift
92 lines (82 loc) · 3.44 KB
/
Dependency.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
import Foundation
import Rainbow
import Releases
enum DependencyError: Error {
case loadingFailed(String)
case parsingFailed(String, String)
}
struct Dependency {
let name: String
let url: URL
let requirement: DependencyRequirement?
let resolvedVersion: ResolvedVersion
let update: Update?
var branchNameForUpdate: String {
"swift-dependency-updater/\(name.components(separatedBy: CharacterSet.alphanumerics.inverted).joined(separator: "-").lowercased())"
}
var changeDescription: String {
switch update {
case let .withoutChangingRequirements(version):
return "Bump \(name) from \(resolvedVersion.versionNumberOrRevision) to \(version)"
case let .withChangingRequirements(version):
return "Bump \(name) from \(resolvedVersion.versionNumberOrRevision) to \(version)"
default:
return ""
}
}
static func loadDependencies(from folder: URL) throws -> [Dependency] {
let packageDescription = try PackageDescription.loadPackageDescription(from: folder)
if packageDescription.dependencies.isEmpty {
return []
}
let resolvedPackage = try ResolvedPackage.loadResolvedPackage(from: folder)
let swiftPackageUpdates = try SwiftPackageUpdate.checkUpdates(in: folder)
return try mergeDependencies(packageDescription: packageDescription, resolvedPackage: resolvedPackage, swiftPackageUpdates: swiftPackageUpdates)
}
private static func mergeDependencies(
packageDescription: PackageDescription,
resolvedPackage: ResolvedPackage,
swiftPackageUpdates: [SwiftPackageUpdate]
) throws -> [Dependency] {
try resolvedPackage.dependencies.map { resolvedDependency in
let packageDependency = packageDescription.dependencies.first { $0.url == resolvedDependency.url }
let swiftPackageUpdate = swiftPackageUpdates.first { $0.name == resolvedDependency.name }
let latestRelease = try Version.getLatestRelease(from: resolvedDependency.url)
let update = try Update.getUpdate(
name: resolvedDependency.name,
currentVersion: resolvedDependency.version.version,
swiftPackageUpdate: swiftPackageUpdate,
latestRelease: latestRelease
)
return Dependency(
name: resolvedDependency.name,
url: resolvedDependency.url,
requirement: packageDependency?.requirement,
resolvedVersion: resolvedDependency.version,
update: update)
}
}
func update(in folder: URL) throws {
try update?.execute(for: self, in: folder)
}
}
extension Dependency: CustomStringConvertible {
public var description: String {
"""
\(name.bold) \(url.absoluteString.italic)
Required: \(requirement == nil ? "No" : "\(requirement!)")
Resolved: \(resolvedVersion)
Update: \(update == nil ? "No update available".green : "\(update!)")
"""
}
}
extension DependencyError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .loadingFailed(error):
return "Could not get package data, swift package dump-package failed: \(error)"
case let .parsingFailed(error, packageData):
return "Could not parse package data: \(error)\n\nPackage Data: \(packageData)"
}
}
}