|
| 1 | +// |
| 2 | +// InterfaceBuilderFileMigrator.swift |
| 3 | +// ThunderBasics |
| 4 | +// |
| 5 | +// Created by Simon Mitchell on 18/09/2020. |
| 6 | +// Copyright © 2020 threesidedcube. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// A class responsible for migrating an Interface Builder to fix issues |
| 12 | +/// when upgrading to `ThunderBasics` v2.0.0 |
| 13 | +final class InterfaceBuilderFileMigrator { |
| 14 | + |
| 15 | + static let directlyMappableAttributes: [(String, String)] = [ |
| 16 | + ("color", "borderColor"), |
| 17 | + ("number", "borderWidth"), |
| 18 | + ("number", "cornerRadius"), |
| 19 | + ("color", "shadowColor"), |
| 20 | + ("number", "shadowOpacity"), |
| 21 | + ("number", "shadowRadius") |
| 22 | + ] |
| 23 | + |
| 24 | + static let removedCustomClasses: [String] = [ |
| 25 | + "TSCTextView", |
| 26 | + "TSCView", |
| 27 | + "TSCImageView" |
| 28 | + ] |
| 29 | + |
| 30 | + /// Represents the current contents of the interface builder file |
| 31 | + /// |
| 32 | + /// - Note: This will be changed by calling the `migrate` function to |
| 33 | + /// represent the fixed version of the file! |
| 34 | + var string: String |
| 35 | + |
| 36 | + convenience init?(filePath: String) { |
| 37 | + let fileURL = URL(fileURLWithPath: filePath) |
| 38 | + guard let data = try? Data(contentsOf: fileURL) else { |
| 39 | + return nil |
| 40 | + } |
| 41 | + guard let fileString = String(data: data, encoding: .utf8) else { |
| 42 | + return nil |
| 43 | + } |
| 44 | + self.init(string: fileString) |
| 45 | + } |
| 46 | + |
| 47 | + init(string: String) { |
| 48 | + self.string = string |
| 49 | + } |
| 50 | + |
| 51 | + /// Performs migration of the current file, storing the result in `string` |
| 52 | + func migrate() { |
| 53 | + migrateUserDefinedRuntimeAttributes() |
| 54 | + migrateRemovedCustomClasses() |
| 55 | + } |
| 56 | + |
| 57 | + private func migrateRemovedCustomClasses() { |
| 58 | + |
| 59 | + Self.removedCustomClasses.forEach { (customClass) in |
| 60 | + |
| 61 | + // Have to catch all of this in one redux as could appear in either order but if we do one check before the other |
| 62 | + // we may miss/break other cases! |
| 63 | + // |
| 64 | + // check for customModule="ThunderBasics" ... "customClass"=customClass || "customClass"=customClass ... customModule="ThunderBasics" |
| 65 | + string = string.replacingOccurrences( |
| 66 | + of: "(customModule=\"ThunderBasics\"([^>]*))?\\s*customClass=\"\(customClass)\"\\s*(([^>]*)customModule=\"ThunderBasics\")?", |
| 67 | + with: "$2$4", // We will only either have $2 or $4 so we don't need a space between them! |
| 68 | + options: .regularExpression, |
| 69 | + range: nil |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private func migrateUserDefinedRuntimeAttributes() { |
| 75 | + |
| 76 | + // These use the same type as `CALayer` equivalents, so can be |
| 77 | + // mapped simply using the same regex |
| 78 | + Self.directlyMappableAttributes.forEach { (attribute) in |
| 79 | + |
| 80 | + string = string.replacingOccurrences( |
| 81 | + of: "<userDefinedRuntimeAttribute(\\s+)type=\"\(attribute.0)\"(\\s+)keyPath=\"(\(attribute.1))\">", |
| 82 | + with: "<userDefinedRuntimeAttribute$1type=\"\(attribute.0)\"$2keyPath=\"layer.$3\">", |
| 83 | + options: .regularExpression, |
| 84 | + range: nil |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + // shadowOffset is a bit more complex because we used `CGPoint` but |
| 89 | + // `CALayer` uses `CGSize` |
| 90 | + |
| 91 | + // Note this regex isn't perfect as it allows for x,y to take the form 1.2.1.3.1.2 e.t.c |
| 92 | + // however this should be impossible to enter in Interface Builder and it makes replacing it |
| 93 | + // far simpler so we will leave be for now! |
| 94 | + string = string.replacingOccurrences( |
| 95 | + of: "<userDefinedRuntimeAttribute(\\s+)type=\"point\"(\\s+)keyPath=\"shadowOffset\">(\\s+)(<point(\\s)+key=\"value\"(\\s)+x=\"([.\\d]+)\"(\\s+)y=\"([.\\d]+)\"\\/>)", |
| 96 | + with: "<userDefinedRuntimeAttribute$1type=\"size\"$2keyPath=\"layer.shadowOffset\">$3<size$5key=\"value\"$6width=\"$7\"$8height=\"$9\"\\/>", |
| 97 | + options: .regularExpression, |
| 98 | + range: nil |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
0 commit comments