Skip to content

Instantly share code, notes, and snippets.

@billypchan
Created December 1, 2020 16:15
Show Gist options
  • Save billypchan/68b0b549ae91992a67cb34191ef255f8 to your computer and use it in GitHub Desktop.
Save billypchan/68b0b549ae91992a67cb34191ef255f8 to your computer and use it in GitHub Desktop.
Scan Cartfile.resolved, update azure-pipelines.yml, then create PR on every child repo
//
// main.swift
// CreateChangeAzureBranchPR
//
// Created by bill on 30.11.20.
//
import Foundation
let branch = "chore/azure_xcode12"
createChangeAzureBranchPR()
//MARK: - methods
func createChangeAzureBranchPR() {
/// read Cartfile.resolved
let contents = openFile(filename: "/Users/bill/Documents/wire/wire-ios/Cartfile.resolved")
guard let lines = contents?.split(separator:"\n") else { return }
for line in lines {
processCartfileLine(line: String(line))
}
}
func openFile(filename: String) -> String? {
let contents = try? String(contentsOfFile: filename)
return contents
}
func processCartfileLine(line: String) {
// clone the repo
let splited = line.split(separator: " ")
let repo = splited[1].replacingOccurrences(of: "\"", with: "", options: .literal, range: nil)
let repoFolder = String(repo.split(separator: "/").last!)
let url: URL = URL(string:"https://github.com/\(repo)")!
print("url: \(url)")
printShell("rm -rf ./\(repoFolder)")
shell("git clone \(url.absoluteString)")
/// edit azure-pipelines.yml
if !changeAzureYmlBranch(repoFolder: repoFolder) {
return
}
/// commit
createBranchAddAndCommit(repoFolder: repoFolder)
///open PR
openPR(repoFolder: repoFolder)
}
func currentDirectory() -> String {
var pwd = shell("pwd")
pwd = pwd.trimmingCharacters(in: .whitespacesAndNewlines)
return pwd
}
func openPR(repoFolder: String) {
let workingGitPath = "\(currentDirectory())/\(repoFolder)"
printShell(#"pr"#,
launchPath: "/usr/local/bin/gh",
arguments:["create",
"--title",
"chore: update Azure to Xcode 12",
"--body",
"bump for components",
"--head",
"\(branch)",
"--label",
"DO NOT MERGE"
],
currentDirectoryPath: workingGitPath
)
}
func createBranchAddAndCommit(repoFolder: String) {
let workingGitPath = "\(currentDirectory())/\(repoFolder)"
print("Working on Git folder: \(workingGitPath)")
print(shell("git --git-dir=\(workingGitPath)/.git branch \(branch)"))
print(shell("git --git-dir=\(workingGitPath)/.git checkout \(branch)"))
printShell("git -C \(workingGitPath) add \"\(workingGitPath)/azure-pipelines.yml\"")
print(shell("git --git-dir=\(workingGitPath)/.git commit -m'Azure with xcode 12'"))
printShell("git -C \(workingGitPath) push --set-upstream origin \(branch)")
}
/// Update Azure YML file for change wire-ios-shared-resources branch to "chore/xcode12"
/// - Parameter repoFolder: repo folder
/// - Returns: return false if file not exist
func changeAzureYmlBranch(repoFolder: String) -> Bool {
let filePath = "./\(repoFolder)/azure-pipelines.yml"
guard var azureYml = openFile(filename: filePath) else {
print("⚠️ azure YML not exist, skip this repo: \(repoFolder)")
return false
}
azureYml = azureYml.replacingOccurrences(of: "ref: refs/heads/master", with: "ref: refs/heads/chore/xcode12", options: .literal, range: nil)
// print(azureYml)
do {
let path = Process().currentDirectoryPath
let url = (URL(fileURLWithPath: path).appendingPathComponent(repoFolder + "/azure-pipelines.yml"))
try azureYml.write(to: url, atomically: true, encoding: String.Encoding.utf8)
} catch {
print(error)
}
return true
}
func printShell(_ command: String, launchPath: String = "/bin/bash", arguments: [String]? = nil, currentDirectoryPath: String? = nil) {
let output = shell(command, launchPath: launchPath, arguments: arguments, currentDirectoryPath: currentDirectoryPath)
print(output)
}
@discardableResult
func shell(_ command: String, launchPath: String = "/bin/bash", arguments: [String]? = nil, currentDirectoryPath: String? = nil) -> String {
let task = Process()
if let currentDirectoryPath = currentDirectoryPath {
task.currentDirectoryPath = currentDirectoryPath
}
let pipe = Pipe()
task.standardOutput = pipe
if arguments == nil {
task.arguments = ["-c", command]
} else {
task.arguments = [command] + arguments!
}
task.executableURL = URL(fileURLWithPath: launchPath)
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment