Skip to content

Commit

Permalink
replace bundle id in all Info.plist files found
Browse files Browse the repository at this point in the history
  • Loading branch information
ned committed Jul 2, 2019
1 parent f8c5d87 commit 4aa9921
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions appdb/Tabs/Downloads/Library/IPAFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ struct IPAFileManager {

// MARK: - Change bundle id and return eventual new filename

func changeBundleId(for file: LocalIPAFile, to newBundleId: String, overwriteFile: Bool) -> String? {
func changeBundleId(for file: LocalIPAFile, from oldBundleId: String, to newBundleId: String, overwriteFile: Bool) -> String? {

let randomName = Global.randomString(length: 5)
let tmp = documentsDirectoryURL().appendingPathComponent(randomName, isDirectory: true)
Expand All @@ -229,9 +229,6 @@ struct IPAFileManager {
}
do {
let ipaUrl = documentsDirectoryURL().appendingPathComponent(file.filename)

debugLog(ipaUrl.absoluteString)

guard FileManager.default.fileExists(atPath: ipaUrl.path) else { return exit("IPA Not found") }
if FileManager.default.fileExists(atPath: tmp.path) { try FileManager.default.removeItem(atPath: tmp.path) }
try FileManager.default.createDirectory(atPath: tmp.path, withIntermediateDirectories: true)
Expand All @@ -240,13 +237,24 @@ struct IPAFileManager {
guard FileManager.default.fileExists(atPath: payload.path) else { return exit("IPA is missing Payload folder") }
let contents = try FileManager.default.contentsOfDirectory(at: payload, includingPropertiesForKeys: nil)
guard let dotApp = contents.first(where: { $0.pathExtension == "app" }) else { return exit("IPA is missing .app folder") }
let infoPlist = dotApp.appendingPathComponent("Info.plist", isDirectory: false)
guard FileManager.default.fileExists(atPath: infoPlist.path) else { return exit("IPA is missing Info.plist file") }

guard let dict = NSMutableDictionary(contentsOfFile: infoPlist.path) else { return exit("Unable to read contents of Info.plist file") }

dict.setValue(newBundleId, forKey: "CFBundleIdentifier")
dict.write(to: infoPlist, atomically: true)
// Search recursively in .app folder for files named 'Info.plist'
let enumerator = FileManager.default.enumerator(atPath: dotApp.path)
if let filePaths = enumerator?.allObjects as? [String] {
let infoPlists = filePaths.filter { $0.contains("Info.plist") }

// For each file found, replace old bundle id with new one
for plist in infoPlists {
let infoPlist = dotApp.appendingPathComponent(plist, isDirectory: false)
if FileManager.default.fileExists(atPath: infoPlist.path), let dict = NSMutableDictionary(contentsOfFile: infoPlist.path) {
if let oldValue = dict["CFBundleIdentifier"] as? String, oldValue.contains(oldBundleId) {
let newValue = oldValue.replacingOccurrences(of: oldBundleId, with: newBundleId)
dict.setValue(newValue, forKey: "CFBundleIdentifier")
dict.write(to: infoPlist, atomically: true)
}
}
}
}

var destinationUrl: URL!

Expand Down
2 changes: 1 addition & 1 deletion appdb/Tabs/Downloads/Library/Library+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ extension Library {

delay(0.2) {
cell.updateText(ipa.size)
if let filename = IPAFileManager.shared.changeBundleId(for: ipa, to: newBundleId, overwriteFile: overwriteFile) {
if let filename = IPAFileManager.shared.changeBundleId(for: ipa, from: bundleId, to: newBundleId, overwriteFile: overwriteFile) {
upload(filename: filename)
}
}
Expand Down

0 comments on commit 4aa9921

Please sign in to comment.