-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitHubCommand.swift
66 lines (59 loc) · 2.61 KB
/
GitHubCommand.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
import ArgumentParser
import Foundation
struct GitHubCommand: ParsableCommand {
static var configuration = CommandConfiguration(commandName: "github", abstract: "Updates dependencies and creates a PR for each one")
@Argument(help: "Path of the swift package") var folder: String = "."
@ArgumentParser.Flag(help: "Do not change version requirements in the Package.swift file.") private var keepRequirements = false
static func update(_ dependencies: [Dependency], in folder: URL, git: GitProvider? = nil, gitHub: GitHubProvider? = nil) throws {
let git = try git ?? Git(in: folder)
let gitHub = gitHub ?? GitHub(git: git)
try dependencies.forEach {
let branchName = $0.branchNameForUpdate
let remoteBranchExist = git.doesRemoteBranchExist(branchName)
if remoteBranchExist {
print("Branch \(branchName) already exists on the remote.".yellow)
print("All changes in the branch will be overridden".yellow.bold)
}
if git.doesLocalBranchExist(branchName) {
print("Branch \(branchName) already exists locally.".yellow)
try git.removeLocalBranch(name: branchName)
}
try git.createBranch(name: branchName)
try $0.update(in: folder)
try git.commit(message: $0.changeDescription)
try git.pushBranch(name: branchName)
if !remoteBranchExist {
try gitHub.createPullRequest(branchName: branchName, title: $0.changeDescription)
}
try git.backToBaseBranch()
}
}
func run() throws {
let folder = URL(fileURLWithPath: folder)
guard folder.hasDirectoryPath else {
print("Folder argument must be a directory.")
throw ExitCode.failure
}
do {
var dependencies = try Dependency.loadDependencies(from: folder)
dependencies = dependencies.filter { $0.update != nil && $0.update != .skipped }
if keepRequirements {
dependencies = dependencies.filter {
if case .withChangingRequirements = $0.update {
return false
} else {
return true
}
}
}
if dependencies.isEmpty {
print("Everything is already up-to-date!".green)
} else {
try Self.update(dependencies, in: folder)
}
} catch {
print(error.localizedDescription)
throw ExitCode.failure
}
}
}