|
| 1 | +#!/usr/bin/env xcrun --sdk macosx swift |
| 2 | + |
| 3 | +import Foundation |
| 4 | + |
| 5 | +do { |
| 6 | + guard (CommandLine.argc == 2) else { |
| 7 | + throw ScriptError.arguments |
| 8 | + } |
| 9 | + |
| 10 | + let path = CommandLine.arguments[1] |
| 11 | + |
| 12 | + try cleanupLicenses(atPath: path) |
| 13 | +} catch { |
| 14 | + // The extra white lines in the string are there to make the description & usage more visible |
| 15 | + fatalError(""" |
| 16 | +
|
| 17 | +
|
| 18 | + ----------------------------------- |
| 19 | + CONTEXT: Script made for the HiDrive project in order to clean up the Pods-acknowledgements.plist. It should be run in the Podfile after the pod installation. |
| 20 | + See https://smartmobilefactory.atlassian.net/browse/STRFRAMEWORK-2640 |
| 21 | +
|
| 22 | + SCRIPT DESCRIPTION: Cleans up the Pod-acknowledgements.plist file from: |
| 23 | + - Pods without a licence |
| 24 | + - Pods with closed source |
| 25 | + - Internal SMF HiDrive-iOS framework |
| 26 | + Moreover, it also removes "SMF" or "SMF-" when set as a prefix on a Pod name |
| 27 | + At the end, it sorts the Pods by name alphabetically, and override the file at the path given as argument |
| 28 | +
|
| 29 | + USAGE: `cleanLicences.swift *path*` |
| 30 | + - path: Absolute path to the Pod-acknowledgements.plist to clean up |
| 31 | +
|
| 32 | + ERROR: \(error) |
| 33 | + ----------------------------------- |
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | + """) |
| 39 | +} |
| 40 | + |
| 41 | +// ----- Script ends here, below is the model and code used in the script |
| 42 | + |
| 43 | +enum ScriptError: Error { |
| 44 | + case arguments |
| 45 | +} |
| 46 | + |
| 47 | +struct LicenseContainer: Codable { |
| 48 | + |
| 49 | + let licenses: [License] |
| 50 | + |
| 51 | + enum CodingKeys: String, CodingKey { |
| 52 | + case licenses = "PreferenceSpecifiers" |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +struct License: Codable { |
| 57 | + |
| 58 | + let title: String |
| 59 | + let license: String? |
| 60 | + let footer: String? |
| 61 | + |
| 62 | + enum CodingKeys: String, CodingKey { |
| 63 | + case title = "Title" |
| 64 | + case license = "License" |
| 65 | + case footer = "FooterText" |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Cleans up the Pod-acknowledgements.plist file from: |
| 70 | +/// - Pods without a licence |
| 71 | +/// - Pods with closed source |
| 72 | +/// - Internal SMF HiDrive-iOS framework |
| 73 | +/// Moreover, it also removes "SMF" or "SMF-" when set as a prefix on a Pod name (see https://smartmobilefactory.atlassian.net/browse/STRFRAMEWORK-2640) |
| 74 | +/// At the end, it sorts the Pods by name alphabetically, and override the file at `path` |
| 75 | +func cleanupLicenses(atPath path: String) throws { |
| 76 | + let url = URL(fileURLWithPath: path) |
| 77 | + let data = try Data(contentsOf: url) |
| 78 | + let decoder = PropertyListDecoder() |
| 79 | + let container = try decoder.decode(LicenseContainer.self, from: data) |
| 80 | + |
| 81 | + let licenses = container.licenses |
| 82 | + .filter { (license: License) -> Bool in |
| 83 | + guard let licenseString = license.license else { |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + guard (licenseString.isEmpty == false) else { |
| 88 | + return false |
| 89 | + } |
| 90 | + |
| 91 | + guard (licenseString.starts(with: "Closed Source") == false) else { |
| 92 | + return false |
| 93 | + } |
| 94 | + |
| 95 | + guard (license.title.starts(with: "HiDrive-iOS-") == false) else { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + return true |
| 100 | + } |
| 101 | + .map { (license: License) -> License in |
| 102 | + if let range = license.title.range(of: "SMF-") { |
| 103 | + var title = license.title |
| 104 | + title.replaceSubrange(range, with: "") |
| 105 | + return License(title: title, license: license.license, footer: license.footer) |
| 106 | + } else if let range = license.title.range(of: "SMF") { |
| 107 | + var title = license.title |
| 108 | + title.replaceSubrange(range, with: "") |
| 109 | + return License(title: title, license: license.license, footer: license.footer) |
| 110 | + } else { |
| 111 | + return license |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + let sortedLicences = licenses.sorted { $0.title < $1.title } |
| 116 | + let sortedContainer = LicenseContainer(licenses: sortedLicences) |
| 117 | + |
| 118 | + let encoder = PropertyListEncoder() |
| 119 | + encoder.outputFormat = .xml |
| 120 | + let sortedData = try encoder.encode(sortedContainer) |
| 121 | + try sortedData.write(to: url) |
| 122 | +} |
0 commit comments