Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Func: Program Update Checking - open for critiquing

$
0
0
I wanted to post this here before putting it in the Examples forum to see if it's worth it, and also get some input since i'm no expert.

I have been using this for a long time but just recently cleaned it up a bit in preperation for public scrutiny :)

This is a somewhat elaborate function to check for a newer version of your (compiled) script, but it's looks decent (i think) and works well.

Procedure...
1) reads a file containing version information on remote server
2) compares it with local script version
3) offers to download file if newer one is available
4) if file is an installer, user can run the installer, else Explore the folder

Requirements...
* a web server
* a file on the server that contains nothing more than the version number of the newest version of your script
* a file to download (exe, installer, zip, etc.)

There is an example script at the bottom.

[ autoit ]         
#include-once #include <Misc.au3> ;#include <EditConstants.au3> ;#include <GUIConstantsEx.au3> ;#include <StaticConstants.au3> ;#include <WindowsConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ProgramUpdateCheck ; Description ...: Check for a program update by comparing script version with version information contained in a text file on a ;                        remote server ; Syntax ........: _ProgramUpdateCheck($sName, $sVersionFile, $sReleaseNotes, $sFile, $iInstaller[, $iGui = 1]) ; Parameters ....: $sName            - Friendly program name (ex. My Program) ;                 $sVersionFile    - network path to version file ;                 $sReleaseNotes    - network path to release notes ;                 $sFile            - network path to program file ;                 $iInstaller        - [1|0] Whether the file to be downloaded is an installer package. ;                 $iGui            - [optional] Whether to show the GUI with release notes and other information or update ;                                        silently. Default is 1. ; Return values .: Returns version string from remote server ; Author ........: iCode ; Modified ......: 04-DEC-2012 ; Remarks .......: $sVersionFile should be a text file and must contain only a version string (example: 1.0.0.0). ;                   $sReleaseNotes is a text file containing whatever info you want about the new version. ;                   $sFile can be an installer or any other file type. If it is not an installer, option will not be given to ;                        install and, instead, user can open the folder containing the downloaded file in Explorer. ;                   $iGui - if 1, the GUI will shown along with other information. If 0, only the version information will be ;                        returned. ; ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _ProgramUpdateCheck($sName, $sVersionFile, $sReleaseNotes, $sFile, $iInstaller, $iGui = 1)     If $iGui = 1 Then         Local Const $ES_READONLY = 2048         Local Const $GUI_DOCKRIGHT = 0x0004         Local Const $GUI_DOCKTOP = 0x0020         Local Const $GUI_DOCKBOTTOM = 0x0040         Local Const $GUI_DOCKWIDTH = 0x0100         Local Const $GUI_DOCKLEFT = 0x0002         Local Const $GUI_DOCKHEIGHT = 0x0200         Local Const $GUI_ENABLE = 64         Local Const $GUI_DISABLE = 128         Local Const $GUI_EVENT_CLOSE = -3         Local Const $SS_CENTER = 0x1         Local Const $WS_MINIMIZEBOX = 0x00020000         Local Const $WS_CAPTION = 0x00C00000         Local Const $WS_POPUP = 0x80000000         Local Const $WS_SYSMENU = 0x00080000         Local Const $WS_MAXIMIZEBOX = 0x00010000         Local Const $WS_SIZEBOX = 0x00040000         Local Const $WS_THICKFRAME = 0x00040000         Local Const $WS_TABSTOP = 0x00010000         Local Const $WS_VSCROLL = 0x00200000         Local Const $GUI_SS_DEFAULT_GUI = BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU)         Local $hInet = 0, $count = 0, $iInfo, $sInet, $sFileName, $dirSavePath         Local $appVersion = FileGetVersion(@ScriptFullPath)         Local $GUIOnEventMode = Opt("GUIOnEventMode")         Local $GUIEventOptions = Opt("GUIOnEventMode")         Opt("GUIOnEventMode", 0)         Opt("GUIEventOptions", 0)         #region ### GUI ###         Local $Form_Update = GUICreate($sName & " - Program Update Check", 775, 293, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))         GUISetFont(10, 400, 0, "Arial")         GUISetBkColor(0xFFFFFF)         Local $Label_Status = GUICtrlCreateLabel("Checking for a newer version...", 8, 12, 501, 40, $SS_CENTER)         GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT)         Local $Button_Cancel = GUICtrlCreateButton("Cancel", 520, 9, 79, 38)         GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)         GUICtrlSetState(-1, $GUI_DISABLE)         GUICtrlSetCursor(-1, 0)         Local $Button_Download = GUICtrlCreateButton("Download", 604, 9, 79, 38)         GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)         GUICtrlSetState(-1, $GUI_DISABLE)         GUICtrlSetCursor(-1, 0)         Local $Button_Install = GUICtrlCreateButton("Install", 688, 9, 79, 38)         GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKWIDTH + $GUI_DOCKHEIGHT)         GUICtrlSetState(-1, $GUI_DISABLE)         GUICtrlSetCursor(-1, 0)         Local $Edit_ReleaseNotes = GUICtrlCreateEdit("", 8, 56, 766, 233, BitOR($ES_READONLY, $WS_VSCROLL), 0)         GUICtrlSetData(-1, "Retrieving release notes...")         GUICtrlSetFont(-1, 10, 400, 0, "Courier New")         GUICtrlSetColor(-1, 0x696969)         GUICtrlSetBkColor(-1, 0xFEFFFF)         GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)         GUICtrlCreateLabel("", 0, 55, 774, 1)         GUICtrlSetBkColor(-1, 0xC0C0C0)         GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT)         Local $Form_Update_AccelTable[1][2] = [["{ESC}", $Button_Cancel]]         GUISetAccelerators($Form_Update_AccelTable)         #endregion ### GUI ###         ; set install button text according to file type         If $iInstaller = 1 Then             GUICtrlSetData($Button_Install, "Install")         Else             GUICtrlSetData($Button_Install, "Locate")         EndIf         GUISetState(@SW_SHOW, $Form_Update)     EndIf     Local $inetVersion = BinaryToString(InetRead($sVersionFile, 3)) ; read version file, ignore cached, ignore SSL errors     If $iGui = 0 Then Return $inetVersion ; if we're updating silently, then no need to go further     $sInet = BinaryToString(InetRead($sReleaseNotes, 3)) ; grab release notes     If $sInet <> "" Then         $sInet = StringRegExpReplace($sInet, "\n", @CRLF)         GUICtrlSetData($Edit_ReleaseNotes, $sInet)     Else         GUICtrlSetData($Edit_ReleaseNotes, "Failed to retrieve the release notes." & @CRLF & @CRLF & _                 "There may be a temporary network problem or the server may not be responding. Please try again later.")     EndIf     GUICtrlSetState($Button_Cancel, $GUI_ENABLE)     If $inetVersion = "" Then         GUICtrlSetData($Label_Status, "Unable to retrieve the version information!")     ElseIf _VersionCompare($inetVersion, $appVersion) = 1 Then         GUICtrlSetData($Label_Status, "A newer version of " & $sName & " is available!" & @LF & "Your version: " & $appVersion & " Newest version: " & $inetVersion)         GUICtrlSetState($Button_Download, $GUI_ENABLE)     ElseIf $appVersion = $inetVersion Then         GUICtrlSetData($Label_Status, "You already have the latest version of " & $sName & "!")     ElseIf _VersionCompare($inetVersion, $appVersion) = -1 Then         GUICtrlSetData($Label_Status, "Version out of range error!" & @LF & "Are you running a development build of " & $sName & " possibly?")     Else         GUICtrlSetData($Label_Status, "The Update check failed for an unspecified reason!")     EndIf     While 1         Switch GUIGetMsg()             Case $GUI_EVENT_CLOSE, $Button_Cancel                 ExitLoop             Case $Button_Download                 $dirSavePath = FileSelectFolder("Select a location to save the file...", "", 3, "", $Form_Update)                 If Not FileExists($dirSavePath) Then ExitLoop                 GUICtrlSetState($Button_Download, $GUI_DISABLE)                 $sFileName = StringRegExpReplace($sFile, ".*/", "")                 GUICtrlSetData($Label_Status, "Preparing to download " & $sFileName & "...")                 $hInet = InetGet($sFile, $dirSavePath & "\" & $sFileName, 3, 1)             Case $Button_Install                 If $iInstaller = 1 Then                     Run($dirSavePath & "\" & $sFileName)                     Exit                 Else                     ShellExecute("explorer.exe", $dirSavePath)                     GUIDelete($Form_Update)                     Exit                 EndIf         EndSwitch         If $hInet > 0 Then             Sleep(10)             $count += 1             If $count = 25 Then ; we'll update the status label roughly every 250 ms                 $count = 0                 ; InetGetInfo return values                 ; 0 - Bytes read so far (this is updated while the download progresses).                 ; 1 - The size of the download (this may not always be present).                 ; 2 - Set to True if the download is complete, False if the download is still ongoing.                 ; 3 - True if the download was successful. If this is False then the next data member will be non-zero.                 ; 4 - The error value for the download. The value itself is arbitrary. Testing that the value is non-zero is sufficient for determining if an error occurred.                 $iInfo = InetGetInfo($hInet)                 GUICtrlSetData($Label_Status, "Downloading file: " & $sFileName & @LF & "Bytes downloaded: " & $iInfo[0])                 If $iInfo[2] = True Then                     InetClose($hInet)                     $hInet = 0 ; prevent running "If $hInet > 0 Then" again                     If $iInfo[3] = True Then                         If FileExists($dirSavePath & "\" & $sFileName) Then                             GUICtrlSetState($Button_Install, $GUI_ENABLE)                             If $iInstaller = 1 Then                                 GUICtrlSetData($Label_Status, "Download complete!" & @LF & "Click Install to install the new version.")                             Else                                 GUICtrlSetData($Label_Status, "Download complete!" & @LF & "Click Locate to open the folder containing " & $sFileName & ".")                             EndIf                         Else                             GUICtrlSetData($Label_Status, "The file that was downloaded could not be found!")                         EndIf                     Else                         GUICtrlSetData($Label_Status, "An error was encountered while downloading " & $sFileName & "!")                     EndIf                 EndIf             EndIf         EndIf     WEnd     GUIDelete($Form_Update)     Opt("GUIOnEventMode", $GUIOnEventMode)     Opt("GUIEventOptions", $GUIEventOptions)     Return $inetVersion EndFunc   ;==>_ProgramUpdateCheck

Example...

[ autoit ]         
; this script provides examples for both manual and automatic update checking ; run this script twice... ; - on the first exit an update version number will be written to config.ini ; - on the next startup, you will be notified if a newer version was found #include "ProgramUpdateCheck.au3" #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> ; VARIABLES/CONSTANTS Global Const $appName = "My Stellar App" Global Const $appVersion = FileGetVersion(@ScriptFullPath) ; CONFIG.INI Global Const $configIni = @ScriptDir & "\config.ini" ; update checking Global $AutoUpdate = IniRead($configIni, "CONFIG", "AutoUpdate", 1) Global $UpdateVersion = IniRead($configIni, "CONFIG", "UpdateVersion", "") ; UPDATE CHECKING If $AutoUpdate = 1 And _VersionCompare($UpdateVersion, $appVersion) = 1 Then ; if auto-update is enabled and an update was found on last exit     _UpdateCheck(1) ; [0|1] whether to show update GUI when updating, or update silently EndIf ; GUI Global $Form_Test = GUICreate($appName & " - " & $appVersion, 300, 100, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW,$WS_EX_TOPMOST,$WS_EX_WINDOWEDGE)) Global $Button_Check = GUICtrlCreateButton("Update Check", 8, 8, 285, 85) GUISetState(@SW_SHOW) While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             GUIDelete($Form_Test)             ; auto check for updates if enabled             If $AutoUpdate = 1 Then                 TraySetState(2)                 _UpdateCheck(0) ; [0|1] whether to show update GUI when updating, or update silently             EndIf             Exit         Case $Button_Check             GUISetState(@SW_HIDE, $Form_Test)             _UpdateCheck(1) ; [0|1] whether to show update GUI when updating, or update silently             GUISetState(@SW_SHOW, $Form_Test)     EndSwitch WEnd Func _UpdateCheck($iGui) ; [0|1] whether to show update GUI when updating, or update silently     Local $sName = $appName     Local $sVersionFile = "http://yourserver.org/my_stellar_app/version"     Local $sReleaseNotes = "http://yourserver.org/my_stellar_app/releasenotes.txt"     Local $sFile = "http://yourserver.org/my_stellar_app/my_stellar_app_installer.exe"     ;Local $sFile = "http://yourserver.org/my_stellar_app/my_stellar_app.zip"     Local $iInstaller = 1 ; the file is an installer package and we want to allow user to run the installler     ; _UpdateCheck returns the string from the version file on the server     ; the version file does not have to have an extension... at least not on a *nix server.  it must contain *only* the version number (1.2.3.4)     ; we'll write the version string to our ini file so we can check it on startup if the user enables $AutoUpdate     ; _ProgramUpdateCheck parms     ; 1st parm - Friendly program name     ; 2nd parm - http path to version file     ; 3rd parm - http path to release notes     ; 4th parm - http path to program file     ; 5th parm - Whether the file to be downloaded is an installer package     ; 6th parm - [optional] Whether to show GUI with release notes and other information - 1=yes, 0=no - defaults to 1     Local $iVersion = _ProgramUpdateCheck($sName, $sVersionFile, $sReleaseNotes, $sFile, $iInstaller, $iGui)     If IsNumber($iVersion) Then IniWrite($configIni, 'CONFIG', 'UpdateVersion', $iVersion) EndFunc   ;==>_UpdateCheck Func _Exit()     ; UPDATE CHECK     If $AutoUpdate = 1 Then         TraySetState(2) ; hide tray icon         _UpdateCheck(0) ; [0|1] whether to show update GUI when updating, or update silently     EndIf     Exit EndFunc

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>