You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by om...@apache.org on 2016/04/21 19:23:55 UTC

cordova-medic git commit: CB-10475 (cordova-plugin-contacts) Grant 'Contacts' permissions while running on iOS simulator

Repository: cordova-medic
Updated Branches:
  refs/heads/master f8d07291a -> f02a5b05b


CB-10475 (cordova-plugin-contacts) Grant 'Contacts' permissions while running on iOS simulator

 This closes #94


Project: http://git-wip-us.apache.org/repos/asf/cordova-medic/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-medic/commit/f02a5b05
Tree: http://git-wip-us.apache.org/repos/asf/cordova-medic/tree/f02a5b05
Diff: http://git-wip-us.apache.org/repos/asf/cordova-medic/diff/f02a5b05

Branch: refs/heads/master
Commit: f02a5b05b2b42282517b95d5c5b0481dee212f6f
Parents: f8d0729
Author: Omar Mefire <om...@gmail.com>
Authored: Tue Apr 19 11:54:25 2016 -0700
Committer: Omar Mefire <om...@gmail.com>
Committed: Thu Apr 21 10:23:38 2016 -0700

----------------------------------------------------------------------
 lib/medicIOSPermissions.js |  42 +++++++++++++++
 lib/util.js                | 110 ++++++++++++++++++++++++++++++++++------
 medic/medic-run.js         |  30 +++++++++++
 package.json               |   3 +-
 4 files changed, 168 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/f02a5b05/lib/medicIOSPermissions.js
----------------------------------------------------------------------
diff --git a/lib/medicIOSPermissions.js b/lib/medicIOSPermissions.js
new file mode 100644
index 0000000..573f2fd
--- /dev/null
+++ b/lib/medicIOSPermissions.js
@@ -0,0 +1,42 @@
+#!/usr/bin/env node
+
+/* jshint node: true */
+var path     = require('path');
+var fs       = require('fs');
+var shelljs  = require("shelljs");
+var util     = require('./util');
+
+var TCC_FOLDER_PERMISSION = 0777;
+
+function MedicIOSPermissions(appName, tccDbPath) {
+    this.appName = appName;
+    this.tccDbPath = tccDbPath;
+}
+
+MedicIOSPermissions.prototype.updatePermissions = function(serviceList){
+    var simulatorsFolder   = util.getSimulatorsFolder();
+    var simId              = util.getSimId();
+    util.medicLog('Sim Id is: ' + simId);
+    var destinationTCCFile = path.join(simulatorsFolder, simId, '/data/Library/TCC/TCC.db');
+
+    if(!util.doesFileExist(destinationTCCFile)) {
+        // No TCC.db file exists by default. So, Copy the new TCC.db file
+        var destinationTCCFolder = path.join(simulatorsFolder, simId, '/data/Library/TCC');
+        if(!util.doesFileExist(destinationTCCFolder)){
+            fs.mkdir(destinationTCCFolder, TCC_FOLDER_PERMISSION);
+        }
+        var command = "cp " + this.tccDbPath + " " + destinationTCCFolder;
+        util.medicLog("Running Command: " + command);
+        shelljs.exec(command, {silent: true, async: false});
+    }
+
+    for(var i = 0; i < serviceList.length; i++) {
+        var command = util.getSqlite3InsertionCommand(destinationTCCFile, serviceList[i], this.appName);
+        util.medicLog("Running Command: " + command);
+        // If the service has an entry already, the insert command will fail.
+        // But, such a failure is intentionally not handled here.
+        shelljs.exec(command, {silent: true, async: false});
+    }
+}
+
+module.exports = MedicIOSPermissions;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/f02a5b05/lib/util.js
----------------------------------------------------------------------
diff --git a/lib/util.js b/lib/util.js
index 06da676..110cf7d 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -21,15 +21,17 @@
 
 "use strict";
 
-module.exports = (function () {
+module.exports = (function() {
 
     var os = require("os");
     var shelljs = require("shelljs");
+    var util = require('util');
+    var path = require('path-extra');
 
     // constants
-    var ESCAPE    = String.fromCharCode(27);
+    var ESCAPE = String.fromCharCode(27);
     var RED_COLOR = ESCAPE + "[31m";
-    var NO_COLOR  = ESCAPE + "[m";
+    var NO_COLOR = ESCAPE + "[m";
     var DEVICE_ROW_PATTERN = /(emulator|device|host)/m;
     var HEADING_LINE_PATTERN = /List of devices/m;
 
@@ -41,7 +43,7 @@ module.exports = (function () {
         return collection.indexOf(item) !== (-1);
     }
 
-    function isWindows () {
+    function isWindows() {
         // NOTE:
         //      - including "^" because otherwise "Darwin" matches
         //      - only "win" and not "windows" because "win32" should also match
@@ -53,10 +55,15 @@ module.exports = (function () {
         process.exit(1);
     }
 
-    function secToMin (seconds) {
+    function secToMin(seconds) {
         return Math.ceil(seconds / 60);
     }
 
+    function getSimulatorsFolder() {
+        var simulatorsFolderPath = path.join(path.homedir(), "Library", "Developer", "CoreSimulator", "Devices");
+        return simulatorsFolderPath;
+    }
+
     function countAndroidDevices() {
         var listCommand = "adb devices";
 
@@ -64,8 +71,8 @@ module.exports = (function () {
         medicLog("    " + listCommand);
 
         var numDevices = 0;
-        var result = shelljs.exec(listCommand, {silent: false, async: false});
-        result.output.split('\n').forEach(function (line) {
+        var result = shelljs.exec(listCommand, { silent: false, async: false });
+        result.output.split('\n').forEach(function(line) {
             if (!HEADING_LINE_PATTERN.test(line) && DEVICE_ROW_PATTERN.test(line)) {
                 numDevices += 1;
             }
@@ -74,24 +81,95 @@ module.exports = (function () {
         return numDevices;
     }
 
+    function getSimId() {
+        var findSimCommand = "cordova run --list --emulator | grep ^iPhone | tail -n1";
+
+        medicLog("running:");
+        medicLog("    " + findSimCommand);
+
+        var findSimResult = shelljs.exec(findSimCommand, { silent: true, async: false });
+
+        if (findSimResult.code > 0) {
+            medicLog("Failed to find simulator we deployed to");
+            return;
+        }
+
+        var split = findSimResult.output.split(", ");
+
+        // Format of the output is "iPhone-6s-Plus, 9.1"
+        // Extract the device name and the version number
+        var device = split[0].replace(/-/g, " ").trim();
+        var version = split[1].trim();
+        medicLog("device: " + device);
+
+        // Next, figure out the ID of the simulator we found
+        var instrCommand = "instruments -s devices | grep ^iPhone";
+        medicLog("running:");
+        medicLog("    " + instrCommand);
+
+        var instrResult = shelljs.exec(instrCommand, { silent: true, async: false });
+
+        if (instrResult.code > 0) {
+            medicLog("Failed to get the list of simulators");
+            return;
+        }
+
+        // This matches <device> (<version>) [<simulator-id>]
+        // ... or <device> (<version>) [<simulator-id>] (Simulator) in Xcode 7.3
+        var simIdRegex = /^([a-zA-Z\d ]+) \(([\d.]+)\) \[([a-zA-Z\d\-]*)\].*$/;
+
+
+        var simId = null;
+        var lines = instrResult.output.split(/\n/);
+        lines.forEach(function(line) {
+            var simIdMatch = simIdRegex.exec(line);
+            if (simIdMatch && simIdMatch.length === 4 && simIdMatch[1] === device && simIdMatch[2] === version) {
+                simId = encodeURIComponent(simIdMatch[3]);
+            }
+        });
+
+        return simId;
+    }
+
+    function doesFileExist(filePath) {
+        var fileExists = false;
+        try {
+            stats = fs.statSync(filePath);
+            fileExists = true;
+        } catch (e) {
+            fileExists = false;
+        }
+        return fileExists;
+    }
+
+    function getSqlite3InsertionCommand(destinationTCCFile, service, appName) {
+        return util.format('sqlite3 %s "insert into access'
+            + '(service, client, client_type, allowed, prompt_count, csreq) values(\'%s\', \'%s\', '
+            + '0,1,1,NULL)"', destinationTCCFile, service, appName);
+    }
+
     return {
 
         // constants
-        ANDROID:    "android",
+        ANDROID: "android",
         BLACKBERRY: "blackberry10",
-        IOS:        "ios",
-        WINDOWS:    "windows",
+        IOS: "ios",
+        WINDOWS: "windows",
 
         DEFAULT_ENCODING: "utf-8",
         DEFAULT_LOG_TIME: 15,
         DEFAULT_LOG_TIME_ADDITIONAL: 2,
 
         // functions
-        fatal:     fatal,
+        fatal: fatal,
         isWindows: isWindows,
-        medicLog:  medicLog,
-        contains:  contains,
-        secToMin:  secToMin,
-        countAndroidDevices: countAndroidDevices
+        medicLog: medicLog,
+        contains: contains,
+        secToMin: secToMin,
+        countAndroidDevices: countAndroidDevices,
+        getSimulatorsFolder: getSimulatorsFolder,
+        getSimId: getSimId,
+        doesFileExist: doesFileExist,
+        getSqlite3InsertionCommand: getSqlite3InsertionCommand
     };
-}());
+} ());

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/f02a5b05/medic/medic-run.js
----------------------------------------------------------------------
diff --git a/medic/medic-run.js b/medic/medic-run.js
index 984c102..7b57898 100644
--- a/medic/medic-run.js
+++ b/medic/medic-run.js
@@ -33,6 +33,7 @@ var request  = require("request");
 
 var util     = require("../lib/util");
 var testwait = require("../lib/testwait");
+var MedicIOSPermissions = require("../lib/medicIOSPermissions");
 
 // constants
 var CORDOVA_MEDIC_DIR         = "cordova-medic";
@@ -49,6 +50,11 @@ var SERVER_RESPONSE_TIMEOUT   = 15000; // in milliseconds
 var MAX_NUMBER_OF_TRIES       = 3;
 var WAIT_TIME_TO_RETRY_CONNECTION  = 15000; // in milliseconds
 
+// Used to grant appropriate iOS permissions 
+var IOS_APPS_TO_GRANT_PERMISSIONS = ['kTCCServiceAddressBook'];
+var IOS_SIM_FOLDER = "/Users/buildbot/Library/Developer/CoreSimulator/Devices/";
+var IOS_TCC_DB_FOLDER = "/Users/buildbot/Library/Application\ Support/com.apple.TCC/";
+
 // helpers
 function currentMillisecond() {
     return new Date().valueOf();
@@ -243,6 +249,23 @@ function windowsSpecificPreparation(argv) {
     return extraArgs;
 }
 
+function iOSSpecificPreparation(argv) {
+    var extraArgs = "";
+    
+    util.medicLog("Granting iOS permissions: ");
+    
+    var appName = argv.app;
+    var simulatorsFolder = argv.simulatorsFolder;
+    var tccDbPath = argv.tccDbPath;
+    
+    if(appName && simulatorsFolder && tccDbPath) {
+        var medicPerms = new MedicIOSPermissions(appName, simulatorsFolder, tccDbPath);
+        medicPerms.updatePermissions(IOS_APPS_TO_GRANT_PERMISSIONS);
+    }
+    
+    return extraArgs;
+}
+
 function getLocalCLI() {
     if (util.isWindows()) {
         return "cordova.bat";
@@ -331,6 +354,8 @@ function main() {
         .default("app", DEFAULT_APP_PATH)
         .default("timeout", DEFAULT_TIMEOUT).describe("timeout", "timeout in seconds")
         .default("winvers", DEFAULT_WINDOWS_VERSION).describe("winvers", "[" + WINDOWS_VERSION_CHOICES.join("|") + "]")
+        .default("simulatorsFolder", IOS_SIM_FOLDER)
+        .default("tccDbPath", IOS_TCC_DB_FOLDER)
         .argv;
 
     var platform   = argv.platform;
@@ -363,6 +388,11 @@ function main() {
             platformArgs = androidSpecificPreparation(argv);
         } else if (platform === util.WINDOWS) {
             platformArgs = windowsSpecificPreparation(argv);
+        } else if (platform === util.IOS) {
+            // Note: (hack) we need to be in a cordova project before we can run iOS specific preparations
+            shelljs.pushd(appPath);
+            platformArgs = iOSSpecificPreparation(argv);
+            shelljs.popd(appPath);
         }
 
         // enter the app directory

http://git-wip-us.apache.org/repos/asf/cordova-medic/blob/f02a5b05/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index f947f43..c4a43e8 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,8 @@
     "tree-kill": "^1.0.0",
     "unorm": "^1.4.1",
     "wd": "^0.3.11",
-    "mkdirp": "^0.5.1"
+    "mkdirp": "^0.5.1",
+    "path-extra": "^3.0.0"
   },
   "subdomain": "cordova.medic",
   "engines": {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org