var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.checkSdk=exports.checkAndConfigureSdks=exports.checkAndConfigureWebosSdks=exports.checkAndConfigureTizenSdks=exports.checkAndConfigureAndroidSdks=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));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");

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')]};


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

};

var checkAndConfigureAndroidSdks=function checkAndConfigureAndroidSdks(c){var _c$buildConfig,_c$buildConfig$sdks;var sdk;return _regenerator.default.async(function checkAndConfigureAndroidSdks$(_context){while(1){switch(_context.prev=_context.next){case 0:
sdk=(_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$sdks=_c$buildConfig.sdks)==null?void 0:_c$buildConfig$sdks.ANDROID_SDK;
(0,_logger.logTask)('checkAndConfigureAndroidSdks',"("+sdk+")");

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

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

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

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

}else{
_logSdkWarning(c);
}case 3:case"end":return _context.stop();}}},null,null,null,Promise);};exports.checkAndConfigureAndroidSdks=checkAndConfigureAndroidSdks;


var checkAndConfigureTizenSdks=function checkAndConfigureTizenSdks(c){var _c$buildConfig2,_c$buildConfig2$sdks;var sdk;return _regenerator.default.async(function checkAndConfigureTizenSdks$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)("checkAndConfigureTizenSdks:"+c.platform);
sdk=(_c$buildConfig2=c.buildConfig)==null?void 0:(_c$buildConfig2$sdks=_c$buildConfig2.sdks)==null?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"+(
_utils.isSystemWin?'.bat':'')));


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

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

}else{
_logSdkWarning(c);
}case 3:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.checkAndConfigureTizenSdks=checkAndConfigureTizenSdks;


var checkAndConfigureWebosSdks=function checkAndConfigureWebosSdks(c){var _c$buildConfig3,_c$buildConfig3$sdks;var sdk;return _regenerator.default.async(function checkAndConfigureWebosSdks$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)("checkAndConfigureWebosSdks:"+c.platform);
sdk=(_c$buildConfig3=c.buildConfig)==null?void 0:(_c$buildConfig3$sdks=_c$buildConfig3.sdks)==null?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"+(_utils.isSystemWin?'.cmd':'')));

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

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

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

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


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


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

}else{
_logSdkWarning(c);
}case 3:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.checkAndConfigureWebosSdks=checkAndConfigureWebosSdks;


var checkAndConfigureSdks=function checkAndConfigureSdks(c){return _regenerator.default.async(function checkAndConfigureSdks$(_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();}}},null,null,null,Promise);};exports.checkAndConfigureSdks=checkAndConfigureSdks;



var _getCurrentSdkPath=function _getCurrentSdkPath(c){var _c$buildConfig4,_c$buildConfig4$sdks;return(_c$buildConfig4=c.buildConfig)==null?void 0:(_c$buildConfig4$sdks=_c$buildConfig4.sdks)==null?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){
var subDir=_path.default.join(dir,subDirName);
var foundSubDir=_findFolderWithFile(subDir,fileToFind);
if(foundSubDir){
foundDir=foundSubDir;
}
});
return foundDir;
};

var _attemptAutoFix=function _attemptAutoFix(c,sdkPlatform,sdkKey,traverseUntilFoundFile){var _process$env,ANDROID_SDK_HOME,ANDROID_SDK_ROOT,ANDROID_HOME,ANDROID_SDK_ENV,result,subResult,confirmSdk,_await$inquirer$promp,confirm,setupInstance;return _regenerator.default.async(function _attemptAutoFix$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('_attemptAutoFix');_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;

result=[].concat((0,_toConsumableArray2.default)(SDK_LOCATIONS[sdkPlatform]),[ANDROID_SDK_HOME,ANDROID_SDK_ROOT,ANDROID_HOME,ANDROID_SDK_ENV]).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=26;break;}
(0,_logger.logSuccess)("Found existing "+
c.platform+" SDK location at "+(0,_logger.chalk)().white(
result));


confirmSdk=true;if(
c.program.ci){_context5.next=13;break;}_context5.next=10;return _regenerator.default.awrap(
_inquirer.default.prompt({
type:'confirm',
name:'confirm',
message:'Do you want to use it?'}));case 10:_await$inquirer$promp=_context5.sent;confirm=_await$inquirer$promp.confirm;

confirmSdk=confirm;case 13:if(!


confirmSdk){_context5.next=26;break;}_context5.prev=14;

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

(0,_configManager.generateBuildConfig)(c);_context5.next=20;return _regenerator.default.awrap(
checkAndConfigureSdks(c));case 20:_context5.next=25;break;case 22:_context5.prev=22;_context5.t0=_context5["catch"](14);

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


true);case 26:



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

setupInstance=(0,_setupManager.default)(c);_context5.next=30;return _regenerator.default.awrap(
setupInstance.askToInstallSDK(sdkPlatform));case 30:
(0,_configManager.generateBuildConfig)(c);return _context5.abrupt("return",
true);case 32:case"end":return _context5.stop();}}},null,null,[[14,22]],Promise);};


var checkSdk=function checkSdk(c){return _regenerator.default.async(function checkSdk$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('checkSdk');if(
_isSdkInstalled(c)){_context6.next=12;break;}
(0,_logger.logWarning)(

c.platform+" requires SDK to be installed. Your SDK path in "+
(0,_logger.chalk)().white(
c.paths.workspace.config)+" does not exist: "+
(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 _regenerator.default.awrap(_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:return _context6.abrupt("return",


true);case 13:case"end":return _context6.stop();}}},null,null,null,Promise);};exports.checkSdk=checkSdk;
//# sourceMappingURL=installer.js.map