@@ -356,10 +356,9 @@ async function checkAndInstallBinaries(forceInstall?: boolean): Promise<boolean>
356356
357357 const resManager = ResManager . GetInstance ( ) ;
358358
359- const eideCfg = resManager . getAppConfig < any > ( ) ;
360359 const binFolder = resManager . GetBinDir ( ) ;
361-
362- let localVersion = eideCfg [ 'binary_min_version' ] ;
360+ const eideCfg = resManager . getAppConfig < any > ( ) ;
361+ const minReqVersion = eideCfg [ 'binary_min_version' ] ;
363362
364363 // !! for compatibility with offline-package !!
365364 // if we found eide binaries in plug-in root folder, move it
@@ -380,40 +379,60 @@ async function checkAndInstallBinaries(forceInstall?: boolean): Promise<boolean>
380379 platform . DeleteDir ( binFolder ) ;
381380 }
382381
383- // if 'bin' dir is existed , we exit, if not, we need install eide-binaries
382+ // if binaries is installed , we try check update from remote repo after x sec delay
384383 else if ( checkBinFolder ( binFolder ) ) {
385- // if user enabled auto-update, we try get new version from
386- // github, and install it at background after 1 min delay
387- //if (SettingManager.GetInstance().isEnableAutoUpdateEideBinaries()) {
384+
385+ // 5sec delay
388386 setTimeout ( async ( ) => {
387+
388+ let localVersion : string | undefined ;
389+
389390 // get local binary version from disk
391+ // check binaries Main_Ver (<Main_Ver>.xx.xx <=> <Main_Ver>.xx.xx)
390392 const verFile = File . fromArray ( [ binFolder . path , 'VERSION' ] ) ;
391393 if ( verFile . IsFile ( ) ) {
392394 const cont = verFile . Read ( ) . trim ( ) ;
393395 if ( utility . isVersionString ( cont ) ) {
394396 localVersion = cont ;
397+ const mainLocalVersion = parseInt ( localVersion . split ( '.' ) [ 0 ] ) ;
398+ const mainMinReqVersion = parseInt ( minReqVersion . split ( '.' ) [ 0 ] ) ;
399+ if ( mainMinReqVersion > mainLocalVersion ) { // local Main verson < min Main version
400+ localVersion = undefined ; // local binaries is invalid, force update
401+ }
395402 }
396403 }
404+
397405 // try update
398- const done = await tryUpdateBinaries ( binFolder , localVersion ) ;
399- if ( ! done ) {
400- const msg = `Update eide-binaries failed, please restart vscode !` ;
401- const sel = await vscode . window . showErrorMessage ( msg , 'Restart' , 'Cancel' ) ;
402- if ( sel == 'Restart' ) {
403- vscode . commands . executeCommand ( 'workbench.action.reloadWindow' ) ;
406+ if ( localVersion ) {
407+ const done = await tryUpdateBinaries ( binFolder , localVersion ) ;
408+ if ( ! done ) {
409+ const msg = `Update eide-binaries failed, please restart vscode !` ;
410+ const sel = await vscode . window . showErrorMessage ( msg , 'Restart' , 'Cancel' ) ;
411+ if ( sel == 'Restart' ) {
412+ vscode . commands . executeCommand ( 'workbench.action.reloadWindow' ) ;
413+ }
404414 }
405415 }
416+
417+ // binaries folder is existed, but can not get local binaries version,
418+ // we need to force install it
419+ else {
420+ checkAndInstallBinaries ( true ) ;
421+ }
422+
406423 } , 5 * 1000 ) ;
407- //}
424+
408425 return true ;
409426 }
410427
411- return await tryUpdateBinaries ( binFolder , localVersion , true ) ;
428+ // not found binaries folder, install it
429+ return await tryUpdateBinaries ( binFolder , undefined , true ) ;
412430}
413431
414- async function tryUpdateBinaries ( binFolder : File , localVer : string , notConfirm ?: boolean ) : Promise < boolean > {
432+ async function tryUpdateBinaries ( binFolder : File , localVer ? : string , notConfirm ?: boolean ) : Promise < boolean > {
415433
416- let binVersion : string = localVer ;
434+ const eideCfg = ResManager . GetInstance ( ) . getAppConfig < any > ( ) ;
435+ const minReqVersion = eideCfg [ 'binary_min_version' ] ;
417436
418437 const getVersionFromRepo = async ( ) : Promise < string | Error | undefined > => {
419438 try {
@@ -429,42 +448,81 @@ async function tryUpdateBinaries(binFolder: File, localVer: string, notConfirm?:
429448 }
430449 } ;
431450
432- // get new version number from repo
433- const newVersion = await getVersionFromRepo ( ) ;
434- if ( typeof newVersion == 'string' ) {
435- const remotVer = newVersion . trim ( ) ;
436- if ( utility . isVersionString ( remotVer ) ) {
437- const localMainVer : string = localVer . split ( '.' ) [ 0 ] ;
438- const remotMainVer : string = remotVer . split ( '.' ) [ 0 ] ;
439- if ( localMainVer == remotMainVer && // main version number must be equal
440- utility . compareVersion ( remotVer , localVer ) > 0 ) { // remote version > local version ?
441- binVersion = remotVer ;
451+ const getAvailableBinariesVersions = async ( ) : Promise < string [ ] | Error | undefined > => {
452+ try {
453+ const url = `https://api-github.em-ide.com/repos/github0null/eide-resource/contents/binaries/${ platformType } ` ;
454+ const fList = await utility . readGithubRepoFolder ( url ) ;
455+ if ( fList instanceof Error ) throw fList ;
456+ return fList . filter ( f => f . name . startsWith ( 'bin-' ) )
457+ . map ( f => f . name . replace ( 'bin-' , '' ) . replace ( '.7z' , '' ) )
458+ . filter ( vStr => utility . isVersionString ( vStr ) ) ;
459+ } catch ( error ) {
460+ return error ;
461+ }
462+ } ;
463+
464+ let preinstallVersion : string | undefined ;
465+
466+ // compare version if local version is available
467+ if ( localVer ) {
468+ const newVersion = await getVersionFromRepo ( ) ;
469+ if ( typeof newVersion == 'string' ) {
470+ const remotVer = newVersion . trim ( ) ;
471+ if ( utility . isVersionString ( remotVer ) ) {
472+ const localMainVer : string = localVer . split ( '.' ) [ 0 ] ;
473+ const remotMainVer : string = remotVer . split ( '.' ) [ 0 ] ;
474+ if ( localMainVer == remotMainVer && // main version number must be equal
475+ utility . compareVersion ( remotVer , localVer ) > 0 ) { // remote version > local version ?
476+ preinstallVersion = remotVer ; // local binaries need update
477+ }
442478 }
443479 }
444480 }
445481
446- // check bin folder
447- if ( checkBinFolder ( binFolder ) ) {
482+ // can not match version, get version list from repo
483+ // select the latest version for min version requirment
484+ else {
485+ const vList = await getAvailableBinariesVersions ( ) ;
486+ if ( vList && Array . isArray ( vList ) ) {
487+ const minMainVer : string = minReqVersion . split ( '.' ) [ 0 ] ;
488+ const validVerList = vList . filter ( ver => ver . split ( '.' ) [ 0 ] == minMainVer ) ;
489+ if ( validVerList . length > 0 ) {
490+ preinstallVersion = validVerList [ 0 ] ;
491+ for ( const ver of validVerList ) {
492+ if ( utility . compareVersion ( ver , preinstallVersion ) > 0 ) {
493+ preinstallVersion = ver ;
494+ }
495+ }
496+ }
497+ }
498+ }
448499
449- // not need update, exit now
450- if ( binVersion == localVer ) {
451- return true ;
500+ // check version
501+ if ( localVer ) { // binaries is installed, found local version
502+ if ( preinstallVersion == undefined ) {
503+ return true ; // local version is latested, not need update
504+ }
505+ } else { // binaries is not installed
506+ if ( preinstallVersion == undefined ) {
507+ throw new Error ( `Can not fetch binaries version from remote github repo, Check your network and retry !` ) ;
452508 }
509+ }
453510
454- // if we found a new version, delete old bin Folder
455- else {
456- // show notify to user and request a confirm
457- if ( ! notConfirm ) {
458- const msg = `New update for eide binaries, version: '${ binVersion } ', [ChangeLog](https://github.com/github0null/eide-resource/pulls?q=is%3Apr+is%3Aclosed), install now ?` ;
459- const sel = await vscode . window . showInformationMessage ( msg , 'Yes' , 'Later' ) ;
460- if ( sel != 'Yes' ) { return true ; } // user canceled
461- }
462- // del old bin folder before install
463- platform . DeleteDir ( binFolder ) ;
511+ // check bin folder
512+ // show notify to user and request a confirm
513+ if ( checkBinFolder ( binFolder ) && preinstallVersion ) {
514+
515+ if ( ! notConfirm ) {
516+ const msg = `New update for eide binaries, version: '${ preinstallVersion } ', [ChangeLog](https://github.com/github0null/eide-resource/pulls?q=is%3Apr+is%3Aclosed), install now ?` ;
517+ const sel = await vscode . window . showInformationMessage ( msg , 'Yes' , 'Later' ) ;
518+ if ( sel != 'Yes' ) { return true ; } // user canceled
464519 }
520+
521+ // del old bin folder before install
522+ platform . DeleteDir ( binFolder ) ;
465523 }
466524
467- return await tryInstallBinaries ( binFolder , binVersion ) ;
525+ return await tryInstallBinaries ( binFolder , preinstallVersion ) ;
468526}
469527
470528async function tryInstallBinaries ( binFolder : File , binVersion : string ) : Promise < boolean > {
0 commit comments