2013-10-05 110 views
1

我正在尋找能夠使用最新版本的安裝程序自行更新的NSIS示例。NSIS自動更新安裝程序本身的示例?

邏輯很簡單:檢查發佈URL是否比當前可執行文件更新,並下載它並使用相同的參數重新調用它自己。

主要是我會像「wget --timestamp URL」,但到目前爲止我無法找到任何NSIS文件下載擴展名,只有在更新的情況下才能下載文件。

回答

0

INetC ::頭將抓住剛剛從服務器的HTTP標頭但恕我直言與下載的版本信息從服務器是一個較好的.ini文件...

0

這裏的東西我寫的,可能是對你有所幫助。我沒有徹底地測試過,但它在我跑過的第一對測試中確實有效。這只是我爲我正在進行的一個項目而拼在一起的一個例子。希望它對你有用。 =)

VersionCompare功能

;=# 
;= Taken and modified from: 
;= http://nsis.sourceforge.net/VersionCompare 
Function VersionCompare 
    !define VersionCompare `!insertmacro _VersionCompare` 
    !macro _VersionCompare _VER1 _VER2 _RESULT 
     Push `${_VER1}` 
     Push `${_VER2}` 
     Call VersionCompare 
     Pop ${_RESULT} 
    !macroend 
    Exch $1 
    Exch 
    Exch $0 
    Exch 
    Push $2 
    Push $3 
    Push $4 
    Push $5 
    Push $6 
    Push $7 
    BEGIN: 
    StrCpy $2 -1 
    IntOp $2 $2 + 1 
    StrCpy $3 $0 1 $2 
    StrCmp $3 '' +2 
    StrCmp $3 '.' 0 -3 
    StrCpy $4 $0 $2 
    IntOp $2 $2 + 1 
    StrCpy $0 $0 '' $2 
    StrCpy $2 -1 
    IntOp $2 $2 + 1 
    StrCpy $3 $1 1 $2 
    StrCmp $3 '' +2 
    StrCmp $3 '.' 0 -3 
    StrCpy $5 $1 $2 
    IntOp $2 $2 + 1 
    StrCpy $1 $1 '' $2 
    StrCmp $4$5 '' EQUAL 
    StrCpy $6 -1 
    IntOp $6 $6 + 1 
    StrCpy $3 $4 1 $6 
    StrCmp $3 '0' -2 
    StrCmp $3 '' 0 +2 
    StrCpy $4 0 
    StrCpy $7 -1 
    IntOp $7 $7 + 1 
    StrCpy $3 $5 1 $7 
    StrCmp $3 '0' -2 
    StrCmp $3 '' 0 +2 
    StrCpy $5 0 
    StrCmp $4 0 0 +2 
    StrCmp $5 0 BEGIN NEWER2 
    StrCmp $5 0 NEWER1 
    IntCmp $6 $7 0 NEWER1 NEWER2 
    StrCpy $4 '1$4' 
    StrCpy $5 '1$5' 
    IntCmp $4 $5 BEGIN NEWER2 NEWER1 
    EQUAL: 
    StrCpy $0 0 
    Goto FINISH 
    NEWER1: 
    StrCpy $0 1 
    Goto FINISH 
    NEWER2: 
    StrCpy $0 2 
    FINISH: 
    Pop $7 
    Pop $6 
    Pop $5 
    Pop $4 
    Pop $3 
    Pop $2 
    Pop $1 
    Exch $0 
FunctionEnd 

UpdateCheck的功能

;=# 
;= Author: demon.devin 
;= http://softables.tk/ 
Function UpdateCheck 
    !define UpdateCheck `!insertmacro _UpdateCheck` 
    !macro _UpdateCheck _NAME _LINK _URL _VERSION 
     Push `${_URL}` 
     Push `${_VERSION}` 
     Call UpdateCheck 
     Pop `${_NAME}` 
     Pop `${_LINK}` 
    !macroend 
    Exch $1 
    Exch 
    Exch $0 
    Push $0 
    Push $1 
    DetailPrint "Current Version: $1" 
    GetTempFileName $R1 
    NSISdl::download $0 $R1 
    ReadINIStr $R0 "$R1" "PAFInfo" "Version" 
    DetailPrint "Latest Version: $R0" 
    ${VersionCompare} $1 $R0 $R4 
    ${If} $R4 == 0 
    ${OrIf} $R4 == 1 
     ReadINIStr $R3 "$R1" "PAFInfo" "Name" 
     StrCpy $File `$R3` 
     StrCpy $R2 "" 
     StrCpy $Link `` 
    ${Else} 
     ReadINIStr $R2 "$R1" "PAFInfo" "DownloadLink" 
     StrCpy $Link `$R2` 
     ReadINIStr $R3 "$R1" "PAFInfo" "Filename" 
     StrCpy $File `$R3` 
    ${EndIf} 
    SetDetailsPrint none 
    Delete $R1 
    SetDetailsPrint lastused 
    Pop $1 
    Pop $0 
    Exch $R2 
    Exch 
    Exch $R3 
FunctionEnd 

WGetDownload功能

;=# 
;= Author: demon.devin 
;= http://softables.tk/ 
Function WGetDownload 
    !define WGetDownload "!insertmacro _WGetDownload" 
    !macro _WGetDownload _RETURN _TEMPFILE _URL _FILENAME 
     Push ${_URL} 
     Push ${_FILENAME} 
     Call WGetDownload 
     Pop ${_TEMPFILE} 
     Pop ${_RETURN} 
    !macroend 
    Exch $1 
    Exch 
    Exch $2 
    Push $1 
    Push $2 
    !define WGET  `$PLUGINSDIR\wget.exe` 
    !define WGETCMD `"${WGET}" --html-extension --referer="http://www.google.com" --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" -T 10 --no-check-certificate "$Link" --output-document=$File` 
    MessageBox MB_OK|MB_USERICON|MB_TOPMOST "Package: $File$\r$\nLink: $Link$\r$\n$\r$\nClick OK to start download." 
    DetailPrint "Downloading: $Link" 
    SetOutPath `$PLUGINSDIR` 
    File `${NSISDIR}\Contrib\7zip\wget.exe` 
    WGET: 
    SetOutPath `$PLUGINSDIR` 
    ExecDos::Exec /TOSTACK `${WGETCMD}` 
    Pop $0 
    Pop $1 
    ${If} $0 = 1 
     StrCmp $2 1 +3 
     StrCpy $2 1 
     Goto WGET 
     Goto FAIL 
    ${EndIf} 
    ${If} ${FileExists} "$PLUGINSDIR\$File" 
     ClearErrors 
     StrCpy $R5 true 
     StrCpy $R7 "$PLUGINSDIR\$File" 
     ${GetSize} "$PLUGINSDIR" "/M=$File /S=0K" $0 $1 $2 
     IfErrors 0 +2 
     StrCpy $0 $0 * 1024 
     IntCmp $0 100 +3 0 +3 
     IntCmp $0 0 +2 +2 0 
     IntOp $0 103 + 0 
     IntOp $0 $0 * 10 
     IntOp $0 $0/1024 
     StrCpy $1 "$0" "" -1 
     IntCmp $0 9 +3 +3 0 
     StrCpy $0 "$0" -1 "" 
     Goto +2 
     StrCpy $0 "0" 
     MessageBox MB_OK|MB_USERICON|MB_TOPMOST "Your file $File was successfully downloaded! ($0.$1 MiB)" 
     Goto DONE 
    ${Else} 
     FAIL: 
     StrCpy $R5 "Your file $File has failed to download using the WGet utility!" 
     Goto CANCEL 
    ${EndIf} 
    CANCEL: 
    MessageBox MB_OK|MB_ICONSTOP|MB_TOPMOST "ERROR!$\r$\n$\r$\n$R5" 
    StrCpy $R5 false 
    StrCpy $R7 "" 
    SetErrors 
    DONE: 
    Pop $2 
    Pop $1 
    Exch $R5 
    Exch 
    Exch $R7 
FunctionEnd 

OpenWebPage宏

;=# 
;= Author: demon.devin 
;= http://softables.tk/ 
!define OpenWebPage '!insertmacro "_OpenWebPage"' 
!macro _OpenWebPage _URL 
    ExecShell open "${_URL}" 
!macroend 

實例:

;=# 
;= Author: demon.devin 
;= More NSIS help: 
;= http://softables.tk/docs 
Section "MAIN" 
    ClearErrors 
    MessageBox MB_USERICON|MB_YESNO|MB_TOPMOST `Would you like to check for any possible upgrades?` IDYES CHECK IDNO NOCHECK 
    NOCHECK: 
    DetailPrint "What?! How can you improve if you don't check for upgrades?" 
    Goto DONE 
    CHECK: 
    DetailPrint "Checking for new version..." 
    ${UpdateCheck} $0 $1 "http://softables.tk/repository/docs/manifests.ini" "6.25.11.0" 
    StrCmp $1 "" 0 +5 
    DetailPrint "You have the most recent version..." 
    MessageBox MB_USERICON|MB_OK|MB_TOPMOST `$0 is up to date!` 
    DetailPrint "Continuing with the rest of the operation..." 
    Goto DONE 
    DetailPrint "There's a new version available..." 
    MessageBox MB_ICONEXCLAMATION|MB_YESNO|MB_TOPMOST "New version of $File available.$\r$\nDownload now?" IDYES YES IDNO NO 
    NO: 
    DetailPrint "Fine! Stick with the old stuff. See if I care..." 
    Goto DONE 
    YES: 
    DetailPrint "Great! Downloading new updates now..." 
    ${WGetDownload} $R0 $R1 "$Link" "$File" 
    ${If} $R0 == true 
    ${AndIf} $R1 != "" 
     DetailPrint "Downloaded: $R1" 
     DetailPrint "Awesome! The download was successful..." 
     CopyFiles /SILENT `$R1` `$INSTDIR\WGET\$R1` 
     DetailPrint "Extracting the contents now..." 
     File '/oname=$PLUGINSDIR\7z.dll' `${NSISDIR}\Contrib\7zip\7z.dll` 
     File '/oname=$PLUGINSDIR\7z.exe' `${NSISDIR}\Contrib\7zip\7z.exe` 
     ExecDos::Exec /TOSTACK `"$PLUGINSDIR\7z.exe" x "$R1" -o"$INSTDIR" -aoa -y` "" "" 
     Pop $0 
     DetailPrint "All done! Cleaning up temporary files..." 
     Delete `$R1` 
     DetailPrint "Finished. Enjoy..." 
    ${Else} 
     DetailPrint "Something went wrong! The download was unsuccessful..." 
     MessageBox MB_ICONEXCLAMATION|MB_YESNO|MB_TOPMOST "You can manually download the upgrades by visiting:$\r$\n$Link$\r$\n$\r$\nGo there now?" IDYES WEB IDNO DONE 
     WEB: 
     DetailPrint "Launching web page now... " 
     SetDetailsPrint none 
     ${OpenWebPage} "$Link" 
     SetDetailsPrint lastused 
    ${EndIf} 
    DONE: 
    DetailPrint "Moving right along..." 
SectionEnd