[server] add module
This commit is contained in:
83
src/app/haxe/ru/m/update/Updater.hx
Normal file
83
src/app/haxe/ru/m/update/Updater.hx
Normal file
@@ -0,0 +1,83 @@
|
||||
package ru.m.update;
|
||||
|
||||
import hw.net.JsonLoader;
|
||||
import openfl.Lib;
|
||||
import openfl.net.URLRequest;
|
||||
import promhx.Promise;
|
||||
import ru.m.Device;
|
||||
import ru.m.update.Version;
|
||||
|
||||
@:enum abstract PackageType(String) from String to String {
|
||||
var APK = "apk";
|
||||
var DEB = "deb";
|
||||
var EXE = "exe";
|
||||
var ARCHIVE = "archive";
|
||||
}
|
||||
|
||||
typedef PackageInfo = {
|
||||
var platform:Platform;
|
||||
var type:PackageType;
|
||||
var path:String;
|
||||
var filename:String;
|
||||
var url:String;
|
||||
var version:String;
|
||||
}
|
||||
|
||||
typedef PackagesBundle = {
|
||||
var name:String;
|
||||
var version:String;
|
||||
var packages:Array<PackageInfo>;
|
||||
}
|
||||
|
||||
class Updater {
|
||||
|
||||
private static inline var TAG = "Update";
|
||||
|
||||
public var type(get, null):PackageType;
|
||||
public var bundle(get, null):Promise<PackagesBundle>;
|
||||
|
||||
private function get_bundle():Promise<PackagesBundle> {
|
||||
if (bundle == null) {
|
||||
bundle = new JsonLoader().GET(url);
|
||||
}
|
||||
return bundle;
|
||||
}
|
||||
|
||||
private var url:String;
|
||||
private var version:Version;
|
||||
|
||||
public function new(version:Version, url:String) {
|
||||
this.url = url;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
private function get_type():PackageType {
|
||||
return switch Device.platform {
|
||||
case ANDROID: APK;
|
||||
case LINUX: DEB;
|
||||
case WINDOWS: EXE;
|
||||
case _: null;
|
||||
}
|
||||
}
|
||||
|
||||
public function check():Promise<Null<PackageInfo>> {
|
||||
return bundle.then((bundle:PackagesBundle) -> Lambda.find(bundle.packages, item -> (
|
||||
item.platform == Device.platform &&
|
||||
item.type == type &&
|
||||
version < Version.fromString(item.version)
|
||||
)));
|
||||
}
|
||||
|
||||
public function download():Promise<Bool> {
|
||||
return bundle.then((bundle:PackagesBundle) -> {
|
||||
for (item in bundle.packages) {
|
||||
if (item.platform == Device.platform && item.type == type) {
|
||||
L.i(TAG, 'download: ${item.url}');
|
||||
Lib.getURL(new URLRequest(item.url));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
52
src/app/haxe/ru/m/update/Version.hx
Normal file
52
src/app/haxe/ru/m/update/Version.hx
Normal file
@@ -0,0 +1,52 @@
|
||||
package ru.m.update;
|
||||
|
||||
abstract Version(Array<Int>) {
|
||||
|
||||
public function new(mayor:Int, minor:Int, patch:Int, snapshot:Bool = false) {
|
||||
this = [
|
||||
mayor,
|
||||
minor,
|
||||
patch,
|
||||
snapshot ? -1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
@:arrayAccess public inline function get(key:Int) return this[key];
|
||||
|
||||
public function compare(other:Version):Int {
|
||||
if (other == null) {
|
||||
return 1;
|
||||
}
|
||||
for (i in 0...4) {
|
||||
var d = this[i] - other[i];
|
||||
if (d != 0) {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@:op(A > B) static function gt(a:Version, b:Version):Bool return a.compare(b) > 0;
|
||||
|
||||
@:op(A == B) static function eq(a:Version, b:Version):Bool return a.compare(b) == 0;
|
||||
|
||||
@:op(A < B) static function lt(a:Version, b:Version):Bool return a.compare(b) < 0;
|
||||
|
||||
@:from public static function fromString(value:String):Version {
|
||||
var r = ~/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?/;
|
||||
return if (r.match(value)) {
|
||||
new Version(
|
||||
Std.parseInt(r.matched(1)),
|
||||
Std.parseInt(r.matched(2)),
|
||||
Std.parseInt(r.matched(3)),
|
||||
r.matched(4) != null
|
||||
);
|
||||
} else {
|
||||
new Version(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@:to public function toString():String {
|
||||
return '${this[0]}.${this[1]}.${this[2]}${this[3] < 0 ? "-SNAPSHOT" : ""}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user