"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.checkSdk = exports.checkAndConfigureWebosSdks = exports.checkAndConfigureTizenSdks = exports.checkAndConfigureSdks = exports.checkAndConfigureAndroidSdks = void 0;var _path = _interopRequireDefault(require("path"));
var _inquirer = _interopRequireDefault(require("inquirer"));
var _constants = require("../constants");





























var _utils = require("../systemManager/utils");
var _fileutils = require("../systemManager/fileutils");
var _logger = require("../systemManager/logger");






var _setupManager = _interopRequireDefault(require("../setupManager"));
var _configManager = require("../configManager");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}

var SDK_LOCATIONS = {
  android: [
  _path["default"].join('/usr/local/android-sdk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'Library/Android/sdk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/android-sdk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/sdk'),
  _path["default"].join('Program Files (x86)/Android/android-sdk')],

  'android-ndk': [
  _path["default"].join('/usr/local/android-sdk/ndk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'Library/Android/sdk/ndk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/android-sdk/ndk'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/sdk/ndk'),
  _path["default"].join('Program Files (x86)/Android/android-sdk/ndk'),
  _path["default"].join('/usr/local/android-sdk/ndk-bundle'),
  _path["default"].join(_constants.USER_HOME_DIR, 'Library/Android/sdk/ndk-bundle'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/android-sdk/ndk-bundle'),
  _path["default"].join(_constants.USER_HOME_DIR, 'AppData/Local/Android/sdk/ndk-bundle'),
  _path["default"].join('Program Files (x86)/Android/android-sdk/ndk-bundle')],

  tizen: [
  _path["default"].join('usr/local/tizen-studio'),
  _path["default"].join(_constants.USER_HOME_DIR, 'tizen-studio'),
  _path["default"].join('C:\\tizen-studio')],

  webos: [
  _path["default"].join('/opt/webOS_TV_SDK'),
  _path["default"].join('C:\\webOS_TV_SDK')] };



var _logSdkWarning = function _logSdkWarning(c) {
  (0, _logger.logWarning)("Your ".concat(
  c.paths.workspace.config, " is missing SDK configuration object"));

};

var checkAndConfigureAndroidSdks = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c) {var _c$buildConfig, _c$buildConfig$sdks;var sdk, sdkManagerPath, avdManagerPath;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            sdk = (_c$buildConfig = c.buildConfig) === null || _c$buildConfig === void 0 ? void 0 : (_c$buildConfig$sdks = _c$buildConfig.sdks) === null || _c$buildConfig$sdks === void 0 ? void 0 : _c$buildConfig$sdks.ANDROID_SDK;
            (0, _logger.logTask)('checkAndConfigureAndroidSdks', "(".concat(sdk, ")"));if (

            sdk) {_context.next = 4;break;}return _context.abrupt("return", _logSdkWarning(c));case 4:

            sdkManagerPath = (0, _fileutils.getRealPath)(
            c,
            _path["default"].join(sdk, "cmdline-tools/latest/bin/sdkmanager".concat(_utils.isSystemWin ? '.bat' : '')));


            if (!(0, _fileutils.fsExistsSync)(sdkManagerPath)) {
              sdkManagerPath = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "tools/bin/sdkmanager".concat(_utils.isSystemWin ? '.bat' : '')));

            }

            avdManagerPath = (0, _fileutils.getRealPath)(
            c,
            _path["default"].join(sdk, "cmdline-tools/latest/bin/avdmanager".concat(_utils.isSystemWin ? '.bat' : '')));


            if (!(0, _fileutils.fsExistsSync)(avdManagerPath)) {
              avdManagerPath = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "tools/bin/avdmanager".concat(_utils.isSystemWin ? '.bat' : '')));

            }

            c.cli[_constants.CLI_ANDROID_EMULATOR] = (0, _fileutils.getRealPath)(
            c,
            _path["default"].join(sdk, "emulator/emulator".concat(_utils.isSystemWin ? '.exe' : '')));

            c.cli[_constants.CLI_ANDROID_ADB] = (0, _fileutils.getRealPath)(
            c,
            _path["default"].join(sdk, "platform-tools/adb".concat(_utils.isSystemWin ? '.exe' : '')));

            c.cli[_constants.CLI_ANDROID_AVDMANAGER] = avdManagerPath;
            c.cli[_constants.CLI_ANDROID_SDKMANAGER] = sdkManagerPath;case 12:case "end":return _context.stop();}}}, _callee);}));return function checkAndConfigureAndroidSdks(_x) {return _ref.apply(this, arguments);};}();exports.checkAndConfigureAndroidSdks = checkAndConfigureAndroidSdks;


var checkAndConfigureTizenSdks = /*#__PURE__*/function () {var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(c) {var _c$buildConfig2, _c$buildConfig2$sdks;var sdk;return regeneratorRuntime.wrap(function _callee2$(_context2) {while (1) {switch (_context2.prev = _context2.next) {case 0:
            (0, _logger.logTask)("checkAndConfigureTizenSdks:".concat(c.platform));
            sdk = (_c$buildConfig2 = c.buildConfig) === null || _c$buildConfig2 === void 0 ? void 0 : (_c$buildConfig2$sdks = _c$buildConfig2.sdks) === null || _c$buildConfig2$sdks === void 0 ? void 0 : _c$buildConfig2$sdks.TIZEN_SDK;
            if (sdk) {
              c.cli[_constants.CLI_TIZEN_EMULATOR] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(
              sdk, "tools/emulator/bin/em-cli".concat(
              _utils.isSystemWin ? '.bat' : '')));


              c.cli[_constants.CLI_TIZEN] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "tools/ide/bin/tizen".concat(_utils.isSystemWin ? '.bat' : '')));

              c.cli[_constants.CLI_SDB_TIZEN] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "tools/sdb".concat(_utils.isSystemWin ? '.exe' : '')));

            } else {
              _logSdkWarning(c);
            }case 3:case "end":return _context2.stop();}}}, _callee2);}));return function checkAndConfigureTizenSdks(_x2) {return _ref2.apply(this, arguments);};}();exports.checkAndConfigureTizenSdks = checkAndConfigureTizenSdks;


var checkAndConfigureWebosSdks = /*#__PURE__*/function () {var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(c) {var _c$buildConfig3, _c$buildConfig3$sdks;var sdk;return regeneratorRuntime.wrap(function _callee3$(_context3) {while (1) {switch (_context3.prev = _context3.next) {case 0:
            (0, _logger.logTask)("checkAndConfigureWebosSdks:".concat(c.platform));
            sdk = (_c$buildConfig3 = c.buildConfig) === null || _c$buildConfig3 === void 0 ? void 0 : (_c$buildConfig3$sdks = _c$buildConfig3.sdks) === null || _c$buildConfig3$sdks === void 0 ? void 0 : _c$buildConfig3$sdks.WEBOS_SDK;
            if (sdk) {
              c.cli[_constants.CLI_WEBOS_ARES] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "CLI/bin/ares".concat(_utils.isSystemWin ? '.cmd' : '')));

              c.cli[_constants.CLI_WEBOS_ARES_PACKAGE] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "CLI/bin/ares-package".concat(_utils.isSystemWin ? '.cmd' : '')));

              c.cli[_constants.CLI_WEBOS_ARES_INSTALL] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "CLI/bin/ares-install".concat(_utils.isSystemWin ? '.cmd' : '')));

              c.cli[_constants.CLI_WEBOS_ARES_LAUNCH] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "CLI/bin/ares-launch".concat(_utils.isSystemWin ? '.cmd' : '')));

              c.cli[_constants.CLI_WEBOS_ARES_SETUP_DEVICE] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(
              sdk, "CLI/bin/ares-setup-device".concat(
              _utils.isSystemWin ? '.cmd' : '')));


              c.cli[_constants.CLI_WEBOS_ARES_DEVICE_INFO] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(
              sdk, "CLI/bin/ares-device-info".concat(
              _utils.isSystemWin ? '.cmd' : '')));


              c.cli[_constants.CLI_WEBOS_ARES_NOVACOM] = (0, _fileutils.getRealPath)(
              c,
              _path["default"].join(sdk, "CLI/bin/ares-novacom".concat(_utils.isSystemWin ? '.cmd' : '')));

            } else {
              _logSdkWarning(c);
            }case 3:case "end":return _context3.stop();}}}, _callee3);}));return function checkAndConfigureWebosSdks(_x3) {return _ref3.apply(this, arguments);};}();exports.checkAndConfigureWebosSdks = checkAndConfigureWebosSdks;


var checkAndConfigureSdks = /*#__PURE__*/function () {var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(c) {return regeneratorRuntime.wrap(function _callee4$(_context4) {while (1) {switch (_context4.prev = _context4.next) {case 0:
            (0, _logger.logTask)('checkAndConfigureSdks');_context4.t0 =

            c.platform;_context4.next = _context4.t0 ===
            _constants.ANDROID ? 4 : _context4.t0 ===
            _constants.ANDROID_TV ? 4 : _context4.t0 ===
            _constants.FIRE_TV ? 4 : _context4.t0 ===
            _constants.ANDROID_WEAR ? 4 : _context4.t0 ===

            _constants.TIZEN ? 5 : _context4.t0 ===
            _constants.TIZEN_MOBILE ? 5 : _context4.t0 ===
            _constants.TIZEN_WATCH ? 5 : _context4.t0 ===

            _constants.WEBOS ? 6 : 7;break;case 4:return _context4.abrupt("return", checkAndConfigureAndroidSdks(c));case 5:return _context4.abrupt("return", checkAndConfigureTizenSdks(c));case 6:return _context4.abrupt("return",
            checkAndConfigureWebosSdks(c));case 7:return _context4.abrupt("return",

            true);case 8:case "end":return _context4.stop();}}}, _callee4);}));return function checkAndConfigureSdks(_x4) {return _ref4.apply(this, arguments);};}();exports.checkAndConfigureSdks = checkAndConfigureSdks;



var _getCurrentSdkPath = function _getCurrentSdkPath(c) {var _c$buildConfig4, _c$buildConfig4$sdks;return (_c$buildConfig4 = c.buildConfig) === null || _c$buildConfig4 === void 0 ? void 0 : (_c$buildConfig4$sdks = _c$buildConfig4.sdks) === null || _c$buildConfig4$sdks === void 0 ? void 0 : _c$buildConfig4$sdks[_constants.SDK_PLATFORMS[c.platform]];};

var _isSdkInstalled = function _isSdkInstalled(c) {
  (0, _logger.logTask)('_isSdkInstalled');

  if (!_constants.SDK_PLATFORMS[c.platform]) return true;

  var sdkPath = _getCurrentSdkPath(c, c.platform);

  return (0, _fileutils.fsExistsSync)((0, _fileutils.getRealPath)(c, sdkPath));
};

var _findFolderWithFile = function _findFolderWithFile(dir, fileToFind) {
  var opt = _path["default"].join(dir, fileToFind);
  if ((0, _fileutils.fsExistsSync)(opt)) {
    return dir;
  }
  var foundDir;
  (0, _fileutils.fsReaddirSync)(dir).forEach(function (subDirName) {
    // not a directory check
    if (!(0, _fileutils.fsLstatSync)(subDirName).isDirectory()) return;
    var subDir = _path["default"].join(dir, subDirName);
    var foundSubDir = _findFolderWithFile(subDir, fileToFind);
    if (foundSubDir) {
      foundDir = foundSubDir;
    }
  });
  return foundDir;
};

var _attemptAutoFix = /*#__PURE__*/function () {var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(c, sdkPlatform, sdkKey, traverseUntilFoundFile) {var locations, _process$env, ANDROID_SDK_HOME, ANDROID_SDK_ROOT, ANDROID_HOME, ANDROID_SDK_ENV, ANDROID_NDK_HOME, result, subResult, confirmSdk, _yield$inquirer$promp, confirm, setupInstance;return regeneratorRuntime.wrap(function _callee5$(_context5) {while (1) {switch (_context5.prev = _context5.next) {case 0:
            (0, _logger.logTask)('_attemptAutoFix');

            locations = SDK_LOCATIONS[sdkPlatform];

            // try common Android SDK env variables
            if (sdkKey === _constants.ANDROID_SDK) {_process$env =
              process.env, ANDROID_SDK_HOME = _process$env.ANDROID_SDK_HOME, ANDROID_SDK_ROOT = _process$env.ANDROID_SDK_ROOT, ANDROID_HOME = _process$env.ANDROID_HOME, ANDROID_SDK_ENV = _process$env.ANDROID_SDK;
              locations = locations.concat([ANDROID_SDK_HOME, ANDROID_SDK_ROOT, ANDROID_HOME, ANDROID_SDK_ENV]);
            }

            if (sdkKey === _constants.ANDROID_NDK) {
              ANDROID_NDK_HOME = process.env.ANDROID_NDK_HOME;
              locations.push(ANDROID_NDK_HOME);
            }

            result = locations.find(function (v) {return (0, _fileutils.fsExistsSync)(v);});

            if (result && traverseUntilFoundFile) {
              subResult = _findFolderWithFile(result, traverseUntilFoundFile);
              if (subResult) {
                result = subResult;
              } else {
                result = null;
              }
            }if (!

            result) {_context5.next = 29;break;}
            (0, _logger.logSuccess)("Found existing ".concat(
            c.platform, " SDK location at ").concat((0, _logger.chalk)().white(
            result)));


            confirmSdk = true;if (
            c.program.ci) {_context5.next = 15;break;}_context5.next = 12;return (
              _inquirer["default"].prompt({
                type: 'confirm',
                name: 'confirm',
                message: 'Do you want to use it?' }));case 12:_yield$inquirer$promp = _context5.sent;confirm = _yield$inquirer$promp.confirm;

            confirmSdk = confirm;case 15:if (!


            confirmSdk) {_context5.next = 29;break;}_context5.prev = 16;

            if (!c.files.workspace.config.sdks) c.files.workspace.config.sdks = {};
            c.files.workspace.config.sdks[sdkKey] = result;
            (0, _fileutils.writeFileSync)(
            c.paths.workspace.config,
            c.files.workspace.config);

            (0, _configManager.generateBuildConfig)(c);_context5.next = 23;return (
              checkAndConfigureSdks(c));case 23:_context5.next = 28;break;case 25:_context5.prev = 25;_context5.t0 = _context5["catch"](16);

            (0, _logger.logError)(_context5.t0);case 28:return _context5.abrupt("return",


            true);case 29:



            (0, _logger.logTask)("_attemptAutoFix: no sdks found. searched at: ".concat(SDK_LOCATIONS[sdkPlatform].join(', ')));

            setupInstance = (0, _setupManager["default"])(c);_context5.next = 33;return (
              setupInstance.askToInstallSDK(sdkPlatform));case 33:
            (0, _configManager.generateBuildConfig)(c);return _context5.abrupt("return",
            true);case 35:case "end":return _context5.stop();}}}, _callee5, null, [[16, 25]]);}));return function _attemptAutoFix(_x5, _x6, _x7, _x8) {return _ref5.apply(this, arguments);};}();


var checkSdk = /*#__PURE__*/function () {var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(c) {return regeneratorRuntime.wrap(function _callee6$(_context6) {while (1) {switch (_context6.prev = _context6.next) {case 0:
            (0, _logger.logTask)('checkSdk');if (
            _isSdkInstalled(c)) {_context6.next = 14;break;}
            (0, _logger.logWarning)("".concat(

            c.platform, " requires SDK to be installed. Your SDK path in ").concat(
            (0, _logger.chalk)().white(
            c.paths.workspace.config), " does not exist: ").concat(
            (0, _logger.chalk)().white(_getCurrentSdkPath(c))));_context6.t0 =


            c.platform;_context6.next = _context6.t0 ===
            _constants.ANDROID ? 6 : _context6.t0 ===
            _constants.ANDROID_TV ? 6 : _context6.t0 ===
            _constants.FIRE_TV ? 6 : _context6.t0 ===
            _constants.ANDROID_WEAR ? 6 : _context6.t0 ===


            _constants.TIZEN ? 9 : _context6.t0 ===
            _constants.TIZEN_MOBILE ? 9 : _context6.t0 ===
            _constants.TIZEN_WATCH ? 9 : _context6.t0 ===

            _constants.WEBOS ? 10 : 11;break;case 6:_context6.next = 8;return _attemptAutoFix(c, 'android', _constants.ANDROID_SDK);case 8:return _context6.abrupt("return", _attemptAutoFix(c, 'android-ndk', _constants.ANDROID_NDK, 'source.properties'));case 9:return _context6.abrupt("return", _attemptAutoFix(c, 'tizen', _constants.TIZEN_SDK));case 10:return _context6.abrupt("return",
            _attemptAutoFix(c, 'webos', _constants.WEBOS_SDK));case 11:return _context6.abrupt("return",

            true);case 12:_context6.next = 16;break;case 14:_context6.next = 16;return (


              checkAndConfigureAndroidSdks(c));case 16:return _context6.abrupt("return",

            true);case 17:case "end":return _context6.stop();}}}, _callee6);}));return function checkSdk(_x9) {return _ref6.apply(this, arguments);};}();exports.checkSdk = checkSdk;
//# sourceMappingURL=installer.js.map