Skip to content

Commit

Permalink
feat: add methods to decouple update
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 22, 2021
1 parent 5627e21 commit a710f96
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 33 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ updateApp('URL_TO_S3_OR ANY_PLACE')
<docgen-index>

* [`download(...)`](#download)
* [`setVersion(...)`](#setversion)
* [`set(...)`](#set)
* [`delete(...)`](#delete)
* [`list()`](#list)
* [`load()`](#load)

</docgen-index>
Expand All @@ -43,6 +45,8 @@ updateApp('URL_TO_S3_OR ANY_PLACE')
download(options: { url: string; }) => any
```

download new version from url

| Param | Type |
| ------------- | ----------------------------- |
| **`options`** | <code>{ url: string; }</code> |
Expand All @@ -52,12 +56,14 @@ download(options: { url: string; }) => any
--------------------


### setVersion(...)
### set(...)

```typescript
setVersion(options: { version: string; }) => any
set(options: { version: string; }) => any
```

set version as current version

| Param | Type |
| ------------- | --------------------------------- |
| **`options`** | <code>{ version: string; }</code> |
Expand All @@ -67,12 +73,44 @@ setVersion(options: { version: string; }) => any
--------------------


### delete(...)

```typescript
delete(options: { version: string; }) => any
```

delete version in storage

| Param | Type |
| ------------- | --------------------------------- |
| **`options`** | <code>{ version: string; }</code> |

**Returns:** <code>any</code>

--------------------


### list()

```typescript
list() => any
```

get all avaible verisions

**Returns:** <code>any</code>

--------------------


### load()

```typescript
load() => any
```

load current version

**Returns:** <code>any</code>

--------------------
Expand Down
62 changes: 44 additions & 18 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,23 @@ extension URL {
}

@objc public func download(url: URL) -> String? {
print("URL " + url)
print("URL " + url.path)
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destZip = documentsUrl.appendingPathComponent(randomString(length: 10))
let dest = documentsUrl.appendingPathComponent(randomString(length: 10))
let index = dest.appendingPathComponent("index.html")
let version = randomString(length: 10)
let dest = documentsUrl.appendingPathComponent("versions").appendingPathComponent(version)
let r = Just.get(url)
if r.ok {
if (FileManager.default.createFile(atPath: destZip.path, contents: r.content, attributes: nil)) {
print("File created successfully.", destZip.path)
SSZipArchive.unzipFile(atPath: destZip.path, toDestination: dest.path)
do {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
if (files.count == 1 && dest.appendingPathComponent(files[0]).isDirectory && !FileManager.default.fileExists(atPath: index.path)) {
return files[0]
}
} catch {
print("FILE NOT AVAILABLE" + index.path)
return nil
}
do {
try FileManager.default.removeItem(atPath: dest.path)
try FileManager.default.removeItem(atPath: destZip.path)
} catch {
print("File not removed.")
return nil
}
return dest
return version
} else {
print("File not created.")
}
Expand All @@ -53,11 +44,46 @@ extension URL {
return nil
}

@objc public func setVersion(version: String) -> Bool {
@objc public func list() -> [String] {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dest = documentsUrl.appendingPathComponent("versions")
do {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
return files
} catch {
print("NO version available" + dest.path)
return []
}
}

@objc public func delete(version: String) -> Bool {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destFolder = documentsUrl.appendingPathComponent(version)
if (destFolder.isDirectory) {
lastPath = destFolder.path
let dest = documentsUrl.appendingPathComponent("versions").appendingPathComponent(version)
do {
try FileManager.default.removeItem(atPath: dest.path)
} catch {
print("File not removed.")
return false
}
return true
}

@objc public func set(version: String) -> Bool {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let dest = documentsUrl.appendingPathComponent("versions").appendingPathComponent(version)
let index = dest.appendingPathComponent("index.html")
if (dest.isDirectory) {
do {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
if (files.count == 1 && dest.appendingPathComponent(files[0]).isDirectory && !FileManager.default.fileExists(atPath: index.path)) {
lastPath = dest.appendingPathComponent(files[0]).path
} else {
lastPath = dest.path
}
} catch {
print("FILE NOT AVAILABLE" + dest.path)
return false
}
return true
}
return false
Expand Down
4 changes: 3 additions & 1 deletion ios/Plugin/CapacitorUpdaterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(CapacitorUpdaterPlugin, "CapacitorUpdater",
CAP_PLUGIN_METHOD(download, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(updateApp, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(set, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(list, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(delete, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(load, CAPPluginReturnNone);
)
39 changes: 33 additions & 6 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,57 @@ public class CapacitorUpdaterPlugin: CAPPlugin {


@objc func download(_ call: CAPPluginCall) {
guard let bridge = self.bridge else { return }
let url = URL(string: call.getString("url") ?? "")

let res = implementation.download(url: url!)
if ((res) != nil) {
call.resolve([
"version": res
"version": res!
])
} else {
call.reject("download failed")
}
}

@objc func setVersion(_ call: CAPPluginCall) {
guard let bridge = self.bridge else { return }
let version = URL(string: call.getString("version") ?? "")
let res = implementation.setVersion(version: version)
@objc func set(_ call: CAPPluginCall) {
let version = call.getString("version") ?? ""
let res = implementation.set(version: version)

if (res) {
// guard let bridge = self.bridge else { return call.reject("bridge missing") }
//
// if let vc = bridge.viewController as? CAPBridgeViewController {
// let path = implementation.getLastPath()
// if (path != "") {
// vc.setServerBasePath(path: path)
// let defaults = UserDefaults.standard
// defaults.set(path, forKey: "serverBasePath")
// call.resolve()
// }
// }
call.resolve()
} else {
call.reject("update failed, version don't exist")
}
}

@objc func delete(_ call: CAPPluginCall) {
let version = call.getString("version") ?? ""
let res = implementation.delete(version: version)
if (res) {
call.resolve()
} else {
call.reject("delete failed, version don't exist")
}
}

@objc func list(_ call: CAPPluginCall) {
let res = implementation.list()
call.resolve([
"versions": res
])
}

@objc func load(_ call: CAPPluginCall) {
guard let bridge = self.bridge else { return }

Expand Down
27 changes: 26 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
export interface CapacitorUpdaterPlugin {
/**
* download new version from url
* @returns {Promise<{version: string}>} an Promise with version name of the downloaded version
* @param url The url where download the version, it can be S3 github tag or whatever, it should be a zip file
*/
download(options: { url: string }): Promise<{ version: string }>;
setVersion(options: { version: string }): Promise<void>;
/**
* set version as current version
* @returns {Promise<void>} an empty Promise
* @param version The version name to set as current version
*/
set(options: { version: string }): Promise<void>;
/**
* delete version in storage
* @returns {Promise<void>} an empty Promise
* @param version The version name to delete
*/
delete(options: { version: string }): Promise<void>;
/**
* get all avaible verisions
* @returns {Promise<{version: string[]}>} an Promise witht the version list
*/
list(): Promise<{ versions: string[] }>;
/**
* load current version
* @returns {Promise<void>} an empty Promise
*/
load(): Promise<void>;
}
15 changes: 11 additions & 4 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ export class CapacitorUpdaterWeb
extends WebPlugin
implements CapacitorUpdaterPlugin {
async download(options: { url: string }): Promise<{ version: string }> {
console.log('Cannot download in web', options);
console.log('Cannot download version in web', options);
return { version: ""};
}
async setVersion(options: { version: string }): Promise<void> {
console.log('Cannot setVersion in web', options);
async set(options: { version: string }): Promise<void> {
console.log('Cannot set version in web', options);
}
async delete(options: { version: string }): Promise<void> {
console.log('Cannot delete version in web', options);
}
async list(): Promise<{ versions: string[] }> {
console.log('Cannot list version in web');
return { versions: []};
}
async load(): Promise<void> {
console.log('Cannot load in web');
console.log('Cannot load version in web');
}
}

0 comments on commit a710f96

Please sign in to comment.