Skip to content

Commit

Permalink
fix: add missing class in swift
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed May 5, 2022
1 parent c584e9d commit ff80a4d
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 57 deletions.
10 changes: 9 additions & 1 deletion ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
50ADFFA42020D75100D50D53 /* Capacitor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50ADFFA52020D75100D50D53 /* Capacitor.framework */; };
50ADFFA82020EE4F00D50D53 /* CapacitorUpdaterPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 50ADFFA72020EE4F00D50D53 /* CapacitorUpdaterPlugin.m */; };
50E1A94820377CB70090CE1A /* CapacitorUpdaterPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E1A94720377CB70090CE1A /* CapacitorUpdaterPlugin.swift */; };
863289042823450E0057FC90 /* VersionInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 863289032823450E0057FC90 /* VersionInfo.swift */; };
863289062823453C0057FC90 /* VersionStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 863289052823453C0057FC90 /* VersionStatus.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -41,6 +43,8 @@
50ADFFA72020EE4F00D50D53 /* CapacitorUpdaterPlugin.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CapacitorUpdaterPlugin.m; sourceTree = "<group>"; };
50E1A94720377CB70090CE1A /* CapacitorUpdaterPlugin.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapacitorUpdaterPlugin.swift; sourceTree = "<group>"; };
5E23F77F099397094342571A /* Pods-Plugin.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.debug.xcconfig"; sourceTree = "<group>"; };
863289032823450E0057FC90 /* VersionInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionInfo.swift; sourceTree = "<group>"; };
863289052823453C0057FC90 /* VersionStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionStatus.swift; sourceTree = "<group>"; };
91781294A431A2A7CC6EB714 /* Pods-Plugin.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.release.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.release.xcconfig"; sourceTree = "<group>"; };
96ED1B6440D6672E406C8D19 /* Pods-PluginTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PluginTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PluginTests/Pods-PluginTests.debug.xcconfig"; sourceTree = "<group>"; };
F65BB2953ECE002E1EF3E424 /* Pods-PluginTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PluginTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PluginTests/Pods-PluginTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +98,8 @@
children = (
50E1A94720377CB70090CE1A /* CapacitorUpdaterPlugin.swift */,
2F98D68124C9AAE400613A4C /* CapacitorUpdater.swift */,
863289032823450E0057FC90 /* VersionInfo.swift */,
863289052823453C0057FC90 /* VersionStatus.swift */,
50ADFF8B201F53D600D50D53 /* CapacitorUpdaterPlugin.h */,
50ADFFA72020EE4F00D50D53 /* CapacitorUpdaterPlugin.m */,
50ADFF8C201F53D600D50D53 /* Info.plist */,
Expand Down Expand Up @@ -192,7 +198,7 @@
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1160;
ORGANIZATIONNAME = "Max Lynch";
ORGANIZATIONNAME = "Capgo";
TargetAttributes = {
50ADFF87201F53D600D50D53 = {
CreatedOnToolsVersion = 9.2;
Expand Down Expand Up @@ -307,7 +313,9 @@
buildActionMask = 2147483647;
files = (
50E1A94820377CB70090CE1A /* CapacitorUpdaterPlugin.swift in Sources */,
863289062823453C0057FC90 /* VersionStatus.swift in Sources */,
2F98D68224C9AAE500613A4C /* CapacitorUpdater.swift in Sources */,
863289042823450E0057FC90 /* VersionInfo.swift in Sources */,
50ADFFA82020EE4F00D50D53 /* CapacitorUpdaterPlugin.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
82 changes: 55 additions & 27 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ extension Bundle {
enum CustomError: Error {
// Throw when an unzip fail
case cannotUnzip
case cannotUnflat
case cannotCreateDirectory
case cannotDeleteDirectory

// Throw in all other cases
case unexpected(code: Int)
Expand All @@ -52,6 +55,21 @@ extension CustomError: LocalizedError {
"The file cannot be unzip",
comment: "Invalid zip"
)
case .cannotCreateDirectory:
return NSLocalizedString(
"The folder cannot be created",
comment: "Invalid folder"
)
case .cannotDeleteDirectory:
return NSLocalizedString(
"The folder cannot be deleted",
comment: "Invalid folder"
)
case .cannotUnzip:
return NSLocalizedString(
"The file cannot be unflat",
comment: "Invalid folder"
)
case .unexpected(_):
return NSLocalizedString(
"An unexpected error occurred.",
Expand All @@ -63,21 +81,29 @@ extension CustomError: LocalizedError {

@objc public class CapacitorUpdater: NSObject {

private var versionBuild = Bundle.main.releaseVersionNumber ?? ""
private var versionCode = Bundle.main.buildVersionNumber ?? ""
private var versionOs = ProcessInfo().operatingSystemVersion.getFullVersion()
private let versionBuild = Bundle.main.releaseVersionNumber ?? ""
private let versionCode = Bundle.main.buildVersionNumber ?? ""
private let versionOs = ProcessInfo().operatingSystemVersion.getFullVersion()
private let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private let libraryDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
private let bundleDirectoryHot = "versions"
private let bundleDirectory = "NoCloud/ionic_built_snapshots"
private let DOWNLOADED_SUFFIX = "_downloaded"
private let NAME_SUFFIX = "_name"
private let STATUS_SUFFIX = "_status"
private let FALLBACK_VERSION = "pastVersion"
private let NEXT_VERSION = "nextVersion"

private var lastPathHot = ""
private var lastPathPersist = ""
private let basePathHot = "versions"
private let basePathPersist = "NoCloud/ionic_built_snapshots"
private let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private let libraryUrl = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!

public let TAG = "✨ Capacitor-updater:";
public let pluginVersion = "3.2.0"
public var statsUrl = ""
public var appId = ""
public var deviceID = UIDevice.current.identifierForVendor?.uuidString ?? ""

public var notifyDownload: (Int) -> Void = { _ in }
public var pluginVersion = "3.2.0"

private func calcTotalPercent(percent: Int, min: Int, max: Int) -> Int {
return (percent * (max - min)) / 100 + min;
Expand All @@ -97,7 +123,8 @@ extension CustomError: LocalizedError {
do {
try FileManager.default.createDirectory(atPath: source.path, withIntermediateDirectories: true, attributes: nil)
} catch {
print("✨ Capacitor-updater: Cannot createDirectory \(source.path)")
print("\(self.TAG) Cannot createDirectory \(source.path)")
throw CustomError.cannotCreateDirectory
}
}
}
Expand All @@ -106,7 +133,8 @@ extension CustomError: LocalizedError {
do {
try FileManager.default.removeItem(atPath: source.path)
} catch {
print("✨ Capacitor-updater: File not removed. \(source.path)")
print("\(self.TAG) File not removed. \(source.path)")
throw CustomError.cannotDeleteDirectory
}
}

Expand All @@ -122,15 +150,15 @@ extension CustomError: LocalizedError {
return false
}
} catch {
print("✨ Capacitor-updater: File not moved. source: \(source.path) dest: \(dest.path)")
return true
print("\(self.TAG) File not moved. source: \(source.path) dest: \(dest.path)")
throw CustomError.cannotUnflat
}
}

private func saveDownloaded(sourceZip: URL, version: String, base: URL) throws {
prepareFolder(source: base)
let destHot = base.appendingPathComponent(version)
let destUnZip = documentsUrl.appendingPathComponent(randomString(length: 10))
let destUnZip = documentsDir.appendingPathComponent(randomString(length: 10))
if (!SSZipArchive.unzipFile(atPath: sourceZip.path, toDestination: destUnZip.path)) {
throw CustomError.cannotUnzip
}
Expand Down Expand Up @@ -170,7 +198,7 @@ extension CustomError: LocalizedError {
latest.message = message
}
case let .failure(error):
print("✨ Capacitor-updater: Error getting Latest", error )
print("\(self.TAG) Error getting Latest", error )
}
semaphore.signal()
}
Expand Down Expand Up @@ -201,17 +229,17 @@ extension CustomError: LocalizedError {
self.notifyDownload(71);
version = self.randomString(length: 10)
do {
try self.saveDownloaded(sourceZip: fileURL, version: version, base: self.documentsUrl.appendingPathComponent(self.basePathHot))
try self.saveDownloaded(sourceZip: fileURL, version: version, base: self.documentsDir.appendingPathComponent(self.bundleDirectoryHot))
self.notifyDownload(85);
try self.saveDownloaded(sourceZip: fileURL, version: version, base: self.libraryUrl.appendingPathComponent(self.basePathPersist))
try self.saveDownloaded(sourceZip: fileURL, version: version, base: self.libraryDir.appendingPathComponent(self.bundleDirectory))
self.notifyDownload(100);
self.deleteFolder(source: fileURL)
} catch {
print("✨ Capacitor-updater: download unzip error", error)
print("\(self.TAG) download unzip error", error)
mainError = error as NSError
}
case let .failure(error):
print("✨ Capacitor-updater: download error", error)
print("\(self.TAG) download error", error)
mainError = error as NSError
}
}
Expand All @@ -226,38 +254,38 @@ extension CustomError: LocalizedError {
}

public func list() -> [String] {
let dest = documentsUrl.appendingPathComponent(basePathHot)
let dest = documentsDir.appendingPathComponent(bundleDirectoryHot)
do {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
return files
} catch {
print("✨ Capacitor-updater: No version available \(dest.path)")
print("\(self.TAG) No version available \(dest.path)")
return []
}
}

public func delete(version: String, versionName: String) -> Bool {
let destHot = documentsUrl.appendingPathComponent(basePathHot).appendingPathComponent(version)
let destPersist = libraryUrl.appendingPathComponent(basePathPersist).appendingPathComponent(version)
let destHot = documentsDir.appendingPathComponent(bundleDirectoryHot).appendingPathComponent(version)
let destPersist = libraryDir.appendingPathComponent(bundleDirectory).appendingPathComponent(version)
do {
try FileManager.default.removeItem(atPath: destHot.path)
} catch {
print("✨ Capacitor-updater: Hot Folder \(destHot.path), not removed.")
print("\(self.TAG) Hot Folder \(destHot.path), not removed.")
}
do {
try FileManager.default.removeItem(atPath: destPersist.path)
} catch {
print("✨ Capacitor-updater: Folder \(destPersist.path), not removed.")
print("\(self.TAG) Folder \(destPersist.path), not removed.")
return false
}
sendStats(action: "delete", version: versionName)
return true
}

public func set(version: String, versionName: String) -> Bool {
let destHot = documentsUrl.appendingPathComponent(basePathHot).appendingPathComponent(version)
let destHot = documentsDir.appendingPathComponent(bundleDirectoryHot).appendingPathComponent(version)
let indexHot = destHot.appendingPathComponent("index.html")
let destHotPersist = libraryUrl.appendingPathComponent(basePathPersist).appendingPathComponent(version)
let destHotPersist = libraryDir.appendingPathComponent(bundleDirectory).appendingPathComponent(version)
let indexPersist = destHotPersist.appendingPathComponent("index.html")
if (destHot.isDirectory && destHotPersist.isDirectory && indexHot.exist && indexPersist.exist) {
UserDefaults.standard.set(destHot.path, forKey: "lastPathHot")
Expand Down Expand Up @@ -307,7 +335,7 @@ extension CustomError: LocalizedError {

DispatchQueue.global(qos: .background).async {
let _ = AF.request(self.statsUrl, method: .post,parameters: parameters, encoder: JSONParameterEncoder.default)
print("✨ Capacitor-updater: Stats send for \(action), version \(version)")
print("\(self.TAG) Stats send for \(action), version \(version)")
}
}

Expand Down

0 comments on commit ff80a4d

Please sign in to comment.