|
| 1 | +#!/usr/bin/swift |
| 2 | + |
| 3 | +// This script is designed to make it easier to review a large number of snapshot reference images that have been |
| 4 | +// recorded on a new iOS version, for which git does not identify the files as renames. It requires having Kaleidoscope |
| 5 | +// installed, including the ksdiff utility. |
| 6 | +// |
| 7 | +// Here is a sample workflow for updating reference images: |
| 8 | +// |
| 9 | +// 1. Add a new worktree and check out the commit that contains the most recent version of the old snapshots. |
| 10 | +// |
| 11 | +// git worktree add ../AccessibilitySnapshot-snapshots-old <commit> |
| 12 | +// |
| 13 | +// 2. On your main worktree, add the new snapshots you wish to review. |
| 14 | +// |
| 15 | +// 3. Copy the image paths from the staging area to a text file. |
| 16 | +// |
| 17 | +// 4. Run this script with the appropriate paths and the relevant versions. For example, when updating snapshots from |
| 18 | +// iOS 13.1 to 13.2.2, this would be: |
| 19 | +// |
| 20 | +// ./Scripts/CompareRenamedSnapshots.swift /path/to/image/list.txt ../AccessibilitySnapshot-snapshots-old 13.2.2 13.1 |
| 21 | +// |
| 22 | +// The script will open Kaleidoscope with each image pair in sequence (when you close one diff, the next will open). |
| 23 | + |
| 24 | +import Foundation |
| 25 | + |
| 26 | +enum TaskError: Error { |
| 27 | + case code(Int32) |
| 28 | +} |
| 29 | + |
| 30 | +func execute(commandPath: String, arguments: [String]) throws { |
| 31 | + let task = Process() |
| 32 | + task.launchPath = commandPath |
| 33 | + task.arguments = arguments |
| 34 | + |
| 35 | + task.launch() |
| 36 | + |
| 37 | + task.waitUntilExit() |
| 38 | + |
| 39 | + guard task.terminationStatus == 0 else { |
| 40 | + throw TaskError.code(task.terminationStatus) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +if CommandLine.arguments.count < 5 || CommandLine.arguments[1] == "--help" || CommandLine.arguments[1] == "-h" { |
| 45 | + print("usage: CompareRenamedSnapshots SNAPSHOT_LIST_FILE OLD_REPO_PATH NEW_DEVICE_ID OLD_DEVICE_ID") |
| 46 | + exit(0) |
| 47 | +} |
| 48 | + |
| 49 | +let snapshotListFilePath = CommandLine.arguments[1] |
| 50 | +let snapshotImageList = String(data: NSData(contentsOfFile: snapshotListFilePath)! as Data, encoding: .utf8)!.split(separator: "\n") |
| 51 | + |
| 52 | +let oldRepoPath = CommandLine.arguments[2] |
| 53 | + |
| 54 | +let newDeviceId = CommandLine.arguments[3] |
| 55 | +let oldDeviceId = CommandLine.arguments[4] |
| 56 | +let newDeviceIdFBSnapshotTestCase = newDeviceId.replacingOccurrences(of: ".", with: "_") |
| 57 | +let oldDeviceIdFBSnapshotTestCase = oldDeviceId.replacingOccurrences(of: ".", with: "_") |
| 58 | +let newDeviceIdSnapshotTesting = newDeviceId.replacingOccurrences(of: ".", with: "-") |
| 59 | +let oldDeviceIdSnapshotTesting = oldDeviceId.replacingOccurrences(of: ".", with: "-") |
| 60 | + |
| 61 | +func ksdiff(oldFilePath: String, newFilePath: String, index: Int) throws { |
| 62 | + guard newFilePath.contains(newDeviceIdFBSnapshotTestCase) || newFilePath.contains(newDeviceIdSnapshotTesting) else { |
| 63 | + print("Skipping \(newFilePath) since there is it is of an unknown version") |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + print("Showing diff for \(newFilePath) (\(index+1) of \(snapshotImageList.count))") |
| 68 | + |
| 69 | + try execute( |
| 70 | + commandPath: "/usr/local/bin/ksdiff", |
| 71 | + arguments: ["--wait", oldFilePath, newFilePath] |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +for (index, newFilePath) in snapshotImageList.enumerated() { |
| 76 | + let oldFilePathInRepo = String( |
| 77 | + newFilePath |
| 78 | + .replacingOccurrences(of: newDeviceIdFBSnapshotTestCase, with: oldDeviceIdFBSnapshotTestCase) |
| 79 | + .replacingOccurrences(of: newDeviceIdSnapshotTesting, with: oldDeviceIdSnapshotTesting) |
| 80 | + ) |
| 81 | + |
| 82 | + do { |
| 83 | + try ksdiff(oldFilePath: oldRepoPath + "/" + oldFilePathInRepo, newFilePath: String(newFilePath), index: index) |
| 84 | + } catch { |
| 85 | + print("Failed to diff \(newFilePath)") |
| 86 | + } |
| 87 | +} |
0 commit comments