Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
fixing autoupdater
Browse files Browse the repository at this point in the history
  • Loading branch information
gerwinbrunner committed Mar 8, 2017
2 parents 1695de9 + 10d4ddb commit adc3919
Show file tree
Hide file tree
Showing 15 changed files with 477 additions and 478 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-meteor-webapp",
"version": "1.3.0",
"version": "1.4.1",
"description": "Cordova plugin that serves a Meteor web app through a local server and implements hot code push",
"cordova": {
"id": "cordova-plugin-meteor-webapp",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-plugin-meteor-webapp"
version="1.3.0">
version="1.4.0">
<name>Meteor Webapp</name>
<description>Cordova plugin that serves a Meteor web app through a local server and implements hot code push</description>
<keywords>cordova,meteor</keywords>
Expand All @@ -22,7 +22,7 @@

<platform name="ios">
<hook type="after_plugin_install" src="scripts/iosAddBridgingHeader.js" />
<hook type="before_prepare" src="scripts/iosSetLastSwiftUpdateCheck.js" />
<hook type="before_prepare" src="scripts/iosSetSwiftVersion.js" />

<config-file target="config.xml" parent="/*">
<feature name="WebAppLocalServer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,30 @@ module.exports = function(context) {
var projectPath = path.join(platformRoot, projectName + '.xcodeproj/project.pbxproj');
var xcodeProject = xcode.project(projectPath);

xcodeProject.parse(function(error) {
if (error) {
console.log('Error: ' + JSON.stringify(error));
} else {
var pbxProjectSection = xcodeProject.pbxProjectSection();
var firstProjectUUID = Object.keys(pbxProjectSection)[0];
var firstProject = pbxProjectSection[firstProjectUUID];
// Xcode 7.2
firstProject.attributes['LastSwiftUpdateCheck'] = '0720';
fs.writeFileSync(projectPath, xcodeProject.writeSync());
xcodeProject.parseSync();

var configurations, buildSettings;
configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection());
Object.keys(configurations).forEach(function (config) {
buildSettings = configurations[config].buildSettings;
buildSettings.SWIFT_VERSION = '3.0';
});

fs.writeFileSync(projectPath, xcodeProject.writeSync());
}

// Extracted from https://github.com/alunny/node-xcode/blob/master/lib/pbxProject.js

COMMENT_KEY = /_comment$/;

function nonComments(obj) {
var keys = Object.keys(obj), newObj = {}, i = 0;

for (i; i < keys.length; i++) {
if (!COMMENT_KEY.test(keys[i])) {
newObj[keys[i]] = obj[keys[i]];
}
});
}

return newObj;
}
2 changes: 1 addition & 1 deletion src/android/WebAppLocalServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private void revertToLastKnownGoodVersion() {
}

// Else, revert to the initial asset bundle, unless that is what we are currently serving
if (!currentAssetBundle.equals(assetBundleManager.initialAssetBundle)) {
else if (!currentAssetBundle.equals(assetBundleManager.initialAssetBundle)) {
pendingAssetBundle = assetBundleManager.initialAssetBundle;
}

Expand Down
17 changes: 8 additions & 9 deletions src/ios/Asset.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
struct Asset {
let bundle: AssetBundle
let filePath: String
var fileURL: NSURL {
return bundle.directoryURL.URLByAppendingPathComponent(filePath,
isDirectory: false)
var fileURL: URL {
return bundle.directoryURL.appendingPathComponent(filePath, isDirectory: false)
}
let URLPath: String
let urlPath: String
let fileType: String?
let cacheable: Bool
let hash: String?
let sourceMapURLPath: String?

init(bundle: AssetBundle, filePath: String, URLPath: String, fileType: String? = nil,
init(bundle: AssetBundle, filePath: String, urlPath: String, fileType: String? = nil,
cacheable: Bool, hash: String? = nil, sourceMapURLPath: String? = nil) {
self.bundle = bundle
self.filePath = filePath
self.URLPath = URLPath
self.urlPath = urlPath
self.fileType = fileType
self.cacheable = cacheable
self.hash = hash
Expand All @@ -25,15 +24,15 @@ struct Asset {

extension Asset: CustomStringConvertible {
var description: String {
return URLPath
return urlPath
}
}

extension Asset: Hashable, Equatable {
var hashValue: Int { return ObjectIdentifier(bundle).hashValue ^ URLPath.hashValue }
var hashValue: Int { return ObjectIdentifier(bundle).hashValue ^ urlPath.hashValue }
}

func ==(lhs: Asset, rhs: Asset) -> Bool {
return ObjectIdentifier(lhs.bundle) == ObjectIdentifier(rhs.bundle)
&& lhs.URLPath == rhs.URLPath
&& lhs.urlPath == rhs.urlPath
}
62 changes: 33 additions & 29 deletions src/ios/AssetBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ private let configJSONRegEx = try! NSRegularExpression(

/// Load the runtime config by extracting and parsing
/// `__meteor_runtime_config__` from index.html
func loadRuntimeConfigFromIndexFileAtURL(fileURL: NSURL) throws -> AssetBundle.RuntimeConfig {
func loadRuntimeConfigFromIndexFileAtURL(_ fileURL: URL) throws -> AssetBundle.RuntimeConfig {
do {
let indexFileString = try NSString(contentsOfURL: fileURL, encoding: NSUTF8StringEncoding)
let indexFileString = try NSString(contentsOf: fileURL, encoding: String.Encoding.utf8.rawValue)
guard
let match = configJSONRegEx.firstMatchInString(indexFileString as String),
let configString = (indexFileString.substringWithRange(match.rangeAtIndex(1)) as NSString).stringByRemovingPercentEncoding,
let configData = configString.dataUsingEncoding(NSUTF8StringEncoding)
else { throw WebAppError.UnsuitableAssetBundle(reason: "Couldn't load runtime config from index file", underlyingError: nil) }
return AssetBundle.RuntimeConfig(JSON: try NSJSONSerialization.JSONObjectWithData(configData, options: []) as! JSONObject)
let configString = (indexFileString.substring(with: match.rangeAt(1)) as NSString).removingPercentEncoding,
let configData = configString.data(using: String.Encoding.utf8)
else { throw WebAppError.unsuitableAssetBundle(reason: "Couldn't load runtime config from index file", underlyingError: nil) }
return AssetBundle.RuntimeConfig(json: try JSONSerialization.jsonObject(with: configData, options: []) as! JSONObject)
} catch {
throw WebAppError.UnsuitableAssetBundle(reason: "Couldn't load runtime config from index file", underlyingError: error)
throw WebAppError.unsuitableAssetBundle(reason: "Couldn't load runtime config from index file", underlyingError: error)
}
}

final class AssetBundle {
private(set) var directoryURL: NSURL
private(set) var directoryURL: URL

let version: String
let cordovaCompatibilityVersion: String
Expand All @@ -33,13 +33,13 @@ final class AssetBundle {
return Array(ownAssetsByURLPath.values)
}

convenience init(directoryURL: NSURL, parentAssetBundle: AssetBundle? = nil) throws {
let manifestURL = directoryURL.URLByAppendingPathComponent("program.json")
convenience init(directoryURL: URL, parentAssetBundle: AssetBundle? = nil) throws {
let manifestURL = directoryURL.appendingPathComponent("program.json")
let manifest = try AssetManifest(fileURL: manifestURL)
try self.init(directoryURL: directoryURL, manifest: manifest, parentAssetBundle: parentAssetBundle)
}

init(directoryURL: NSURL, manifest: AssetManifest, parentAssetBundle: AssetBundle? = nil) throws {
init(directoryURL: URL, manifest: AssetManifest, parentAssetBundle: AssetBundle? = nil) throws {
self.directoryURL = directoryURL
self.parentAssetBundle = parentAssetBundle

Expand All @@ -53,7 +53,7 @@ final class AssetBundle {
let asset = Asset(
bundle: self,
filePath: entry.filePath,
URLPath: URLPath,
urlPath: URLPath,
fileType: entry.fileType,
cacheable: entry.cacheable,
hash: entry.hash,
Expand All @@ -67,47 +67,51 @@ final class AssetBundle {
let sourceMap = Asset(
bundle: self,
filePath: sourceMapPath,
URLPath: sourceMapURLPath,
urlPath: sourceMapURLPath,
fileType: "json",
cacheable: true)
addAsset(sourceMap)
}
}
}

let indexFile = Asset(bundle: self, filePath: "index.html", URLPath: "/", fileType: "html", cacheable: false, hash: nil)
let indexFile = Asset(bundle: self, filePath: "index.html", urlPath: "/", fileType: "html", cacheable: false, hash: nil)
addAsset(indexFile)
self.indexFile = indexFile
}

func addAsset(asset: Asset) {
ownAssetsByURLPath[asset.URLPath] = asset
func addAsset(_ asset: Asset) {
ownAssetsByURLPath[asset.urlPath] = asset
}

func assetForURLPath(URLPath: String) -> Asset? {
func assetForURLPath(_ URLPath: String) -> Asset? {
return ownAssetsByURLPath[URLPath] ?? parentAssetBundle?.assetForURLPath(URLPath)
}

func cachedAssetForURLPath(URLPath: String, hash: String? = nil) -> Asset? {
if let asset = ownAssetsByURLPath[URLPath]
func cachedAssetForURLPath(_ URLPath: String, hash: String? = nil) -> Asset? {
if let asset = ownAssetsByURLPath[URLPath],
// If the asset is not cacheable, we require a matching hash
where (asset.cacheable || asset.hash != nil) && asset.hash == hash {
(asset.cacheable || asset.hash != nil) && asset.hash == hash {
return asset
} else {
return nil
}
}

struct RuntimeConfig {
private let JSON: JSONObject
private let json: JSONObject

init(json: JSONObject) {
self.json = json
}

var appId: String? {
return JSON["appId"] as? String
return json["appId"] as? String
}

var rootURL: NSURL? {
if let rootURLString = JSON["ROOT_URL"] as? String {
return NSURL(string: rootURLString)
var rootURL: URL? {
if let rootURLString = json["ROOT_URL"] as? String {
return URL(string: rootURLString)
} else {
return nil
}
Expand All @@ -124,7 +128,7 @@ final class AssetBundle {
/* Patch for AutoupdateServer - End */

var autoupdateVersionCordova: String? {
return JSON["autoupdateVersionCordova"] as? String
return json["autoupdateVersionCordova"] as? String
}
}

Expand All @@ -133,7 +137,7 @@ final class AssetBundle {
guard let indexFile = self.indexFile else { return nil }

do {
return try loadRuntimeConfigFromIndexFileAtURL(indexFile.fileURL)
return try loadRuntimeConfigFromIndexFileAtURL(indexFile.fileURL as URL)
} catch {
NSLog("\(error)")
return nil
Expand All @@ -144,7 +148,7 @@ final class AssetBundle {
return runtimeConfig?.appId
}

var rootURL: NSURL? {
var rootURL: URL? {
return runtimeConfig?.rootURL
}

Expand All @@ -154,7 +158,7 @@ final class AssetBundle {
}
/* Patch for AutoupdateServer - End */

func didMoveToDirectoryAtURL(directoryURL: NSURL) {
func didMoveToDirectoryAtURL(_ directoryURL: URL) {
self.directoryURL = directoryURL
}
}
Loading

0 comments on commit adc3919

Please sign in to comment.