Last active
October 14, 2019 23:50
-
-
Save rockbruno/991cc0988bda7f2aaa417d0b552f21f1 to your computer and use it in GitHub Desktop.
Buck: Script to find out which rules should be tested based on what was changed
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
import Foundation | |
func run(command: String, wait: Bool = true) -> String? { | |
let task = Process() | |
task.launchPath = "/bin/bash" | |
task.arguments = ["-c", command] | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.launch() | |
guard wait else { | |
return "" | |
} | |
task.waitUntilExit() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
return String(data: data, encoding: .utf8) | |
} | |
func runTargetsCommand() -> String? { | |
return run(command: "./buck targets //... --show-target-hash") | |
} | |
func process(_ hashes: String) -> [String: String] { | |
let rules = hashes.components(separatedBy: "\n") | |
var dict = [String: String]() | |
for rule in rules { | |
let data = rule.components(separatedBy: " ") | |
guard data.count == 2 else { | |
continue | |
} | |
dict[data[0]] = data[1] | |
} | |
return dict | |
} | |
guard let newHashesResult = runTargetsCommand() else { | |
fatalError("Failed to run buck targets command") | |
} | |
let newHashes = process(newHashesResult) | |
let oldHashesResult = run(command: "cat ./buck-out/cache/ifood_hashes.txt") | |
_ = run(command: "echo \"\(newHashesResult)\" > ./buck-out/cache/ifood_hashes.txt") | |
if oldHashesResult == "" { | |
print("--all") | |
exit(0) | |
} | |
let oldHashes = process(oldHashesResult!) | |
var targetsToBuild = [String]() | |
for (k, v) in newHashes where k.hasSuffix("Tests") { | |
if oldHashes[k] != v { | |
targetsToBuild.append(k) | |
} | |
} | |
guard targetsToBuild.isEmpty == false else { | |
// Return an empty test so we don't test anything | |
print("//Libraries/Config/uUseless:UselessTests") | |
exit(0) | |
} | |
let result = targetsToBuild.joined(separator: " ") | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment