1. 程式人生 > >NSIS安裝打包程式的版本比較方便做後續邏輯處理

NSIS安裝打包程式的版本比較方便做後續邏輯處理

因為當時公司的專案打包解除安裝前期出現了一些問題,只能做個補丁來彌補,考慮到不影響後期的專案,分隔版本號,以1.0.12為分割線,判斷new or old,然後分別做出不同的解除安裝邏輯。

1.VersionCompare:

*比較版本數字

語法:

    ${VersionCompare} "[Version1]" "[Version2]" $var
    "[Version1]"        ; First version
    "[Version2]"        ; Second version
    $var                ; Result:
                        ;    $var=0  Versions are equal
                        ;    $var=1  Version1 is newer
                        ;    $var=2  Version2 is newer

例子:

    Section
        ${VersionCompare} "1.1.1.9" "1.1.1.01" $R0
        ; $R0="1"
    SectionEnd

2.VersionConvert

*將版本號轉換成數字格式然後再比較。

語法:

${VersionConvert} "[Version]" "[CharList]" $var
"[Version]"         ; Version
                    ;
"[CharList]"        ; List of characters, which will be replaced by numbers
                    ; "abcdefghijklmnopqrstuvwxyz" (default)
                    ;
$var                ; Result: converted version

注意:

Converted letters are separated with dot

If character is non-digit and not in list then it will be converted to dot

例子1:

Section
    ${VersionConvert} "9.0a" "" $R0
    ; $R0="9.0.01"

    ${VersionConvert} "9.0c" "" $R1
    ; $R1="9.0.03"

    ${VersionCompare} "$R0" "$R1" $R2
    ; $R2="2"   version2 is newer
SectionEnd

例子2:

Section
    ${VersionConvert} "0.15c-9m" "" $R0
    ; $R0="0.15.03.9.13"

    ${VersionConvert} "0.15c-1n" "" $R1
    ; $R1="0.15.03.1.14"

    ${VersionCompare} "$R0" "$R1" $R2
    ; $R2="1"   version1 is newer
SectionEnd

例子3:

Section
    ${VersionConvert} "0.15c+" "abcdefghijklmnopqrstuvwxyz+" $R0
    ; $R0="0.15.0327"

    ${VersionConvert} "0.15c" "abcdefghijklmnopqrstuvwxyz+" $R1
    ; $R1="0.15.03"

    ${VersionCompare} "$R0" "$R1" $R2
    ; $R2="1"   version1 is newer
SectionEnd

一般不論是VersionCompare還是VersionConvert都會有返回值,我們可以根據返回值判斷,做出對應的邏輯處理,提示給安裝使用者。

提示:用這兩種方法一定記得 !include "WordFunc.nsh"

   ${If} $R0 == "2" ;1.012之前的版本

     StrCpy $OLD_PATH $UNINSTALL_PROG -10

     ****1.012之前的版本解除安裝邏輯處理**

 ${Else}
     ****1.012之後的版本解除安裝邏輯處理**

 ${EndIf}