Created
September 17, 2021 08:21
-
-
Save billypchan/f8e73c522e819e65ccd1e9dd347459e7 to your computer and use it in GitHub Desktop.
Create a PR for each frameworks in Cartfile.resolved for edited azure-pipelines.yml for Xcode 13 update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// carthageGraph | |
// | |
// Created by Bill, Yiu Por Chan on 17.09.21. | |
// | |
import Foundation | |
var path: String! = "/Users/bill/ios_dev/wire/xcode13/wire-ios/Cartfile.resolved" | |
func read(path: String) -> String? { | |
let cartfileUrl = URL(fileURLWithPath: path) | |
print("ℹ️ cartfileUrl: \(cartfileUrl)") | |
do { | |
return try String(contentsOf: cartfileUrl, encoding: String.Encoding.utf8) | |
} catch { | |
print("❌ Error: \(error.localizedDescription)") | |
return nil | |
} | |
} | |
func readFile(path: String) -> [String]? { | |
let content = read(path: path) | |
return content?.components(separatedBy: "\n") | |
} | |
func shell(_ command: String) -> String { | |
let task = Process() | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.standardError = pipe | |
task.arguments = ["-c", command] | |
task.launchPath = "/bin/zsh" | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output = String(data: data, encoding: .utf8)! | |
return output | |
} | |
let xcode13Branch = "chore/xcode13" | |
let prBody = """ | |
## What's new in this PR? | |
Build with Xcode 13 | |
### dependency | |
- [ ] https://github.com/wireapp/wire-ios-shared-resources/pull/52 | |
""" | |
func processRepo(line: String) { | |
let components = line.components(separatedBy: " ") | |
if components.count >= 2 { | |
let repo = components[1].replacingOccurrences(of: "\"", with: "") | |
let repoFolder = repo.components(separatedBy: "/").last ?? "" | |
print(repoFolder) | |
// check it has chore/xcode13 branch | |
let branchExist = shell("git ls-remote --heads https://github.com/\(repo) \(xcode13Branch) | wc -l") | |
let cdCmd = "cd \(repoFolder) ; " | |
func checkout() { | |
let checkout = shell(cdCmd + "git checkout \(xcode13Branch)") | |
} | |
func updateYml() { | |
let ymlPath = "./\(repoFolder)/azure-pipelines.yml" | |
let yml = read(path: ymlPath) | |
print("yml content: \(yml)") | |
let updatedYml = yml?.replacingOccurrences(of: "ref: refs/heads/master", with: "ref: refs/heads/\(xcode13Branch)") | |
print("updatedYml content: \(updatedYml)") | |
do { | |
let ymlUrl = URL(fileURLWithPath: ymlPath) | |
try updatedYml?.write(to: ymlUrl, atomically: false, encoding: .utf8) | |
} | |
catch { | |
print("❌ write error: \(error.localizedDescription)") | |
} | |
} | |
func commit() { | |
let commit = shell(cdCmd + "git add . ; git commit -m\"change wire-ios-shared-resources branch\"") | |
print("commit: \(commit)") | |
let push = shell(cdCmd + "git push --set-upstream origin \(xcode13Branch)") | |
print("push: \(push)") | |
print("ℹ️ open PR") | |
///need to specify gh path otherwise shell method does not find it | |
let pr = shell(cdCmd + "/opt/homebrew/bin/gh pr create --head \(xcode13Branch) --title \"chore: Xcode13\" --body \"\(prBody)\" --label \"DO NOT MERGE\"") | |
print("create pr: \(pr)") | |
} | |
// clone the repo | |
print("ℹ️ cloning \(repo)...") | |
print(shell("rm -Rf \(repoFolder)")) | |
let clone: String | |
///TODO: slow cmd, output progress? | |
if (branchExist != " 0\n") { | |
print("ℹ️ branch exists, skip create branch") | |
clone = shell("git clone --depth 1 https://github.com/\(repo) --branch \(xcode13Branch)") | |
} else { | |
clone = shell("git clone --depth 1 https://github.com/\(repo)") | |
let branch = shell(cdCmd + "git branch \(xcode13Branch)") | |
} | |
print(clone) | |
///TODO: cd not work | |
// let cdpwd = shell(cdCmd + "pwd ; ls") | |
// print(cdpwd) | |
checkout() | |
updateYml() | |
commit() | |
} | |
} | |
let lines = readFile(path: path) | |
lines?.forEach() { line in | |
if line.contains("wire-ios-") { | |
processRepo(line: line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment