From 17301353a70d41b8ac333e11d01261fb46284fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20K=C3=B6tte?= Date: Thu, 5 May 2022 00:00:26 -0700 Subject: [PATCH] Do not create a PR if a dependency was not found (#105) --- .../Services/Git.swift | 12 ++++++++-- .../SubCommands/GitHubCommand.swift | 21 +++++++++------- .../SubCommands/UpdateCommand.swift | 9 ++++++- .../Update.swift | 24 +++++++------------ 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Sources/SwiftDependencyUpdaterLibrary/Services/Git.swift b/Sources/SwiftDependencyUpdaterLibrary/Services/Git.swift index 1d4b799..d79ab3e 100644 --- a/Sources/SwiftDependencyUpdaterLibrary/Services/Git.swift +++ b/Sources/SwiftDependencyUpdaterLibrary/Services/Git.swift @@ -84,11 +84,19 @@ class Git: GitProvider { } func doesRemoteBranchExist(_ name: String) -> Bool { - doesBranchExist(ref: "refs/remotes/\(remoteName)/\(name)") + let result = doesBranchExist(ref: "refs/remotes/\(remoteName)/\(name)") + if result { + print("Branch \(name) already exists on the remote.".yellow) + } + return result } func doesLocalBranchExist(_ name: String) -> Bool { - doesBranchExist(ref: "refs/heads/\(name)") + let result = doesBranchExist(ref: "refs/heads/\(name)") + if result { + print("Branch \(name) already exists locally.".yellow) + } + return result } private func doesBranchExist(ref: String) -> Bool { diff --git a/Sources/SwiftDependencyUpdaterLibrary/SubCommands/GitHubCommand.swift b/Sources/SwiftDependencyUpdaterLibrary/SubCommands/GitHubCommand.swift index 1ef4e50..aa1430f 100644 --- a/Sources/SwiftDependencyUpdaterLibrary/SubCommands/GitHubCommand.swift +++ b/Sources/SwiftDependencyUpdaterLibrary/SubCommands/GitHubCommand.swift @@ -16,21 +16,26 @@ struct GitHubCommand: ParsableCommand { 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) + do { + 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() + } catch let SwiftPackageError.resultCountMismatch(name, count) where count == 0 { // false positive, count is an integer swiftlint:disable:this empty_count + print("Warning: Could not find version requirement for \(name) in Package.swift - " + + "this could be due to the dependency only beeing required on a specific platform.".yellow) + } catch { + throw error } - try git.backToBaseBranch() } } diff --git a/Sources/SwiftDependencyUpdaterLibrary/SubCommands/UpdateCommand.swift b/Sources/SwiftDependencyUpdaterLibrary/SubCommands/UpdateCommand.swift index 8eb9465..3b0c517 100644 --- a/Sources/SwiftDependencyUpdaterLibrary/SubCommands/UpdateCommand.swift +++ b/Sources/SwiftDependencyUpdaterLibrary/SubCommands/UpdateCommand.swift @@ -30,7 +30,14 @@ struct UpdateCommand: ParsableCommand { print("Everything is already up-to-date!".green) } else { try dependencies.forEach { - try $0.update(in: folder) + do { + try $0.update(in: folder) + } catch let SwiftPackageError.resultCountMismatch(name, count) where count == 0 { // false positive, count is an integer swiftlint:disable:this empty_count + print("Warning: Could not find version requirement for \(name) in Package.swift - " + + "this could be due to the dependency only beeing required on a specific platform.".yellow) + } catch { + throw error + } } } } catch { diff --git a/Sources/SwiftDependencyUpdaterLibrary/Update.swift b/Sources/SwiftDependencyUpdaterLibrary/Update.swift index 9cb7a91..3586b1f 100644 --- a/Sources/SwiftDependencyUpdaterLibrary/Update.swift +++ b/Sources/SwiftDependencyUpdaterLibrary/Update.swift @@ -45,23 +45,15 @@ enum Update: Equatable { case let .withChangingRequirements(version): print("Updating \(dependency.name): \(dependency.resolvedVersion.versionNumberOrRevision) -> \(version)".bold) let swiftPackage = SwiftPackage(in: folder) - do { - let packageUpdate = try swiftPackage.performUpdate(self, of: dependency) - print("Updated Package.swift".green) - if packageUpdate { - try shellOut(to: "swift", arguments: ["package", "--package-path", "\"\(folder.path)\"", "update", dependency.name ]) - print("Resolved to new version".green) - } else { - try shellOut(to: "swift", arguments: ["package", "--package-path", "\"\(folder.path)\"", "update", "resolve", ]) - print("Resolved Version".green) - } - } catch let SwiftPackageError.resultCountMismatch(name, count) where count == 0 { // false positive, count is an integer swiftlint:disable:this empty_count - print("Warning: Could not find version requirement for \(name) in Package.swift - " + - "this could be due to the dependency only beeing required on a specific platform.".yellow) - } catch { - throw error + let packageUpdate = try swiftPackage.performUpdate(self, of: dependency) + print("Updated Package.swift".green) + if packageUpdate { + try shellOut(to: "swift", arguments: ["package", "--package-path", "\"\(folder.path)\"", "update", dependency.name ]) + print("Resolved to new version".green) + } else { + try shellOut(to: "swift", arguments: ["package", "--package-path", "\"\(folder.path)\"", "update", "resolve", ]) + print("Resolved Version".green) } - case let .withoutChangingRequirements(version): print("Updating \(dependency.name): \(dependency.resolvedVersion.versionNumberOrRevision) -> \(version)".bold) try shellOut(to: "swift", arguments: ["package", "--package-path", "\"\(folder.path)\"", "update", dependency.name, ])