summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/ios/cordova/lib/versions.js
blob: e22e499ae1ba98f61cf3e4a315d9a085f1c30b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node

/*
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/

var child_process = require('child_process'),
    Q = require('q');

exports.get_apple_ios_version = function() {
    var d = Q.defer();
    child_process.exec('xcodebuild -showsdks', function(error, stdout, stderr) {
        if (error) {
            d.reject(stderr);
        }
        else {
            d.resolve(stdout);
        }
    });

    return d.promise.then(function(output) {
        var regex = /[0-9]*\.[0-9]*/,
            versions = [],
            regexIOS = /^iOS \d+/;
        output = output.split('\n');
        for (var i = 0; i < output.length; i++) {
            if (output[i].trim().match(regexIOS)) {
                versions[versions.length] = parseFloat(output[i].match(regex)[0]);
                }
        }
        versions.sort();
        console.log(versions[0]);
        return Q();
    }, function(stderr) {
        return Q.reject(stderr);
    });
};

exports.get_apple_osx_version = function() {
    var d = Q.defer();
    child_process.exec('xcodebuild -showsdks', function(error, stdout, stderr) {
        if (error) {
            d.reject(stderr);
        }
        else {
            d.resolve(stdout);
        }
    });

    return d.promise.then(function(output) {
        var regex = /[0-9]*\.[0-9]*/,
            versions = [],
            regexOSX = /^OS X \d+/;
        output = output.split('\n');
        for (var i = 0; i < output.length; i++) {
            if (output[i].trim().match(regexOSX)) {
                versions[versions.length] = parseFloat(output[i].match(regex)[0]);
            }
        }
        versions.sort();
        console.log(versions[0]);
        return Q();
    }, function(stderr) {
        return Q.reject(stderr);
    });
};

exports.get_apple_xcode_version = function() {
    var d = Q.defer();
    child_process.exec('xcodebuild -version', function(error, stdout, stderr) {
        var versionMatch = /Xcode (.*)/.exec(stdout);
        if (error || !versionMatch) {
            d.reject(stderr);
        } else {
            d.resolve(versionMatch[1]);
        }
    });
    return d.promise;
};

/**
 * Gets ios-deploy util version
 * @return {Promise} Promise that either resolved with ios-deploy version
 *                           or rejected in case of error
 */
exports.get_ios_deploy_version = function() {
    var d = Q.defer();
    child_process.exec('ios-deploy --version', function(error, stdout, stderr) {
        if (error) {
            d.reject(stderr);
        } else {
            d.resolve(stdout);
        }
    });
    return d.promise;
};

/**
 * Gets ios-sim util version
 * @return {Promise} Promise that either resolved with ios-sim version
 *                           or rejected in case of error
 */
exports.get_ios_sim_version = function() {
    var d = Q.defer();
    child_process.exec('ios-sim --version', function(error, stdout, stderr) {
        if (error) {
            d.reject(stderr);
        } else {
            d.resolve(stdout);
        }
    });
    return d.promise;
};

/**
 * Gets specific tool version
 * @param  {String} toolName Tool name to check. Known tools are 'xcodebuild', 'ios-sim' and 'ios-deploy'
 * @return {Promise}         Promise that either resolved with tool version
 *                                   or rejected in case of error
 */
exports.get_tool_version = function (toolName) {
    switch (toolName) {
        case 'xcodebuild': return exports.get_apple_xcode_version();
        case 'ios-sim': return exports.get_ios_sim_version();
        case 'ios-deploy': return exports.get_ios_deploy_version();
        default: return Q.reject(toolName + ' is not valid tool name. Valid names are: \'xcodebuild\', \'ios-sim\' and \'ios-deploy\'');
    }
};

/**
 * Compares two semver-notated version strings. Returns number
 * that indicates equality of provided version strings.
 * @param  {String} version1 Version to compare
 * @param  {String} version2 Another version to compare
 * @return {Number}          Negative number if first version is lower than the second,
 *                                    positive otherwise and 0 if versions are equal.
 */
exports.compareVersions = function (version1, version2) {
    function parseVer (version) {
        return version.split('.').map(function (value) {
            // try to convert version segment to Number
            var parsed = Number(value);
            // Number constructor is strict enough and will return NaN
            // if conversion fails. In this case we won't be able to compare versions properly
            if (isNaN(parsed)) {
                throw 'Version should contain only numbers and dots';
            }
            return parsed;
        });
    }
    var parsedVer1 = parseVer(version1);
    var parsedVer2 = parseVer(version2);

    // Compare corresponding segments of each version
    for (var i = 0; i < Math.max(parsedVer1.length, parsedVer2.length); i++) {
        // if segment is not specified, assume that it is 0
        // E.g. 3.1 is equal to 3.1.0
        var ret = (parsedVer1[i] || 0) - (parsedVer2[i] || 0);
        // if segments are not equal, we're finished
        if (ret !== 0) return ret;
    }
    return 0;
};