|
| 1 | + |
| 2 | +//runner. |
| 3 | + |
| 4 | +/** |
| 5 | + * Modal functions |
| 6 | + */ |
| 7 | +function binaryAdd() { |
| 8 | + var file = dialog.showOpenDialog({ |
| 9 | + "title": i18n.__("Find PHP binary") |
| 10 | + }); |
| 11 | + |
| 12 | + if (file && file[0]) { |
| 13 | + // Get path |
| 14 | + var path = file[0]; |
| 15 | + |
| 16 | + // Get version |
| 17 | + // We cannot save data with dots with "configstore" package :( |
| 18 | + // Workaround = replace . with : (the "true" is for returning replaced) |
| 19 | + var version = binaryGetVersion(path, true); |
| 20 | + |
| 21 | + // Oops! Invalid PHP binary! |
| 22 | + if (!version) |
| 23 | + dialog.showErrorBox(i18n.__("Error"), i18n.__("Invalid PHP binary")); |
| 24 | + |
| 25 | + // Save new version to config |
| 26 | + conf.set("php.versions." + version, path); |
| 27 | + |
| 28 | + // Update list |
| 29 | + binaryUpdateList(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function binaryRemove() { |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +function binaryMakeDefault(version) { |
| 38 | + conf.set("php.default", binaryConvertVersionToSave(version)); |
| 39 | +} |
| 40 | + |
| 41 | +function binaryConvertVersionToSave(version) { |
| 42 | + return version.replace(/\./g, ":"); |
| 43 | +} |
| 44 | + |
| 45 | +function binaryConvertVersionToShow(version) { |
| 46 | + return version.replace(/\:/g, "."); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * replaced = replacing . with : (configstore workaround) |
| 51 | + */ |
| 52 | +function binaryGetVersion(path, replaced) { |
| 53 | + var response = runner.execSync(path + " --version", {"encoding": "utf8"}); |
| 54 | + |
| 55 | + // Is this PHP? |
| 56 | + if (/^PHP/.test(response)) { |
| 57 | + // Get PHP version |
| 58 | + var result = response.match(/^PHP ([0-9\.]+)/); |
| 59 | + if (result && result[1]) { |
| 60 | + if (replaced) |
| 61 | + return binaryConvertVersionToSave(result[1]); |
| 62 | + else |
| 63 | + return result[1]; |
| 64 | + } |
| 65 | + return false; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Binary list functions |
| 71 | + */ |
| 72 | +function binaryUpdateList() { |
| 73 | + $("#binary-list").empty(); |
| 74 | + |
| 75 | + var versions = conf.get("php.versions"); |
| 76 | + var inUse = conf.get("php.default"); |
| 77 | + |
| 78 | + for (var v in versions) { |
| 79 | + $("#binary-list").append(binaryLineGetTemplate(v, versions[v], (inUse == v))); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function binaryLineGetTemplate(version, path, inUse) { |
| 84 | + return [ |
| 85 | + '<tr ' + (inUse ? 'class="info"' : '') + '>', |
| 86 | + ' <td>' + binaryConvertVersionToShow(version) + '</td>', |
| 87 | + ' <td>' + path + '</td>', |
| 88 | + ' <td class="text-right">', |
| 89 | + ' <div class="btn-group">', |
| 90 | + ' <button class="btn btn-default btn-xs" onclick="makeDefaultVersion(\'' + version + '\')">', |
| 91 | + ' <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>', |
| 92 | + ' </button>', |
| 93 | + ' <button class="btn btn-default btn-xs" onclick="removeVersion(\'' + version + '\')">', |
| 94 | + ' <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>', |
| 95 | + ' </button>', |
| 96 | + ' </div>', |
| 97 | + ' </td>', |
| 98 | + '</tr>' |
| 99 | + ].join("\n"); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Commands from modal |
| 104 | + */ |
| 105 | +function makeDefaultVersion(version) { |
| 106 | + binaryMakeDefault(version); |
| 107 | + binaryUpdateList(); |
| 108 | + php_path = conf.get("php.versions." + conf.get("php.default")); |
| 109 | +} |
| 110 | + |
| 111 | +function removeVersion(version) { |
| 112 | + var opt = dialog.showMessageBox({ |
| 113 | + "type": "question", |
| 114 | + "title": i18n.__("Are you sure?"), |
| 115 | + "message": i18n.__("Removing {{version}} version. Are you sure?", {"version": binaryConvertVersionToShow(version)}), |
| 116 | + "buttons": [i18n.__("Yes"), i18n.__("No")], |
| 117 | + }); |
| 118 | + |
| 119 | + console.log(opt); |
| 120 | +} |
0 commit comments