nsis打包指令碼
阿新 • • 發佈:2019-01-02
最近在打包一個WEB程式,執行環境是PHP+Apache2+Mysql。打包過程比較順利,註冊服務、啟動都能成功。遇到一個問題,就是修改配置檔案的問題。既然是打包程式,當然要求打包的程式可以安裝在任意位置都能夠正常執行,修改apache伺服器的httpd.conf檔案費了我不少功夫,因為對NSIS並不熟悉。查找了一些資料,找到一些程式碼後面成功了。把這些程式碼貼出來,也許對遇到我同樣問題的人有所幫助。
http://nsis.sourceforge.net/More_advanced_replace_text_in_file
替換檔案中字元的函式:
Nsis程式碼- /*替換文字function*/
- Function AdvReplaceInFile
- Exch $0 ;file to replace in
- Exch
- Exch $1 ;number to replace after
- Exch
- Exch 2
- Exch $2 ;replace and onwards
- Exch 2
- Exch 3
- Exch $3 ;replace with
- Exch 3
- Exch 4
- Exch $4 ;to replace
- Exch 4
- Push $5 ;minus count
- Push $6 ;universal
- Push $7 ;end string
- Push $8 ;left string
- Push $9 ;right string
- Push $R0 ;file1
- Push $R1 ;file2
- Push $R2 ;read
- Push $R3 ;universal
- Push $R4 ;count (onwards)
- Push $R5 ;count (after)
- Push $R6 ;temp file name
- GetTempFileName $R6
- FileOpen $R1 $0 r ;file to search in
- FileOpen $R0 $R6 w ;temp file
- StrLen $R3 $4
- StrCpy $R4 -1
- StrCpy $R5 -1
- loop_read:
- ClearErrors
- FileRead $R1 $R2 ;read line
- IfErrors exit
- StrCpy $50
- StrCpy $7 $R2
- loop_filter:
- IntOp $5 $5 - 1
- StrCpy $6 $7 $R3 $5 ;search
- StrCmp $6"" file_write2
- StrCmp $6 $40 loop_filter
- StrCpy $8 $7 $5 ;left part
- IntOp $6 $5 + $R3
- IntCmp $60 is0 not0
- is0:
- StrCpy $9""
- Goto done
- not0:
- StrCpy $9 $7"" $6 ;right part
- done:
- StrCpy $7 $8$3$9 ;re-join
- IntOp $R4 $R4 + 1
- StrCmp $2 all file_write1
- StrCmp $R4 $20 file_write2
- IntOp $R4 $R4 - 1
- IntOp $R5 $R5 + 1
- StrCmp $1 all file_write1
- StrCmp $R5 $10 file_write1
- IntOp $R5 $R5 - 1
- Goto file_write2
- file_write1:
- FileWrite $R0 $7 ;write modified line
- Goto loop_read
- file_write2:
- FileWrite $R0 $R2 ;write unmodified line
- Goto loop_read
- exit:
- FileClose $R0
- FileClose $R1
- SetDetailsPrint none
- Delete $0
- Rename $R6 $0
- Delete $R6
- SetDetailsPrint both
- Pop $R6
- Pop $R5
- Pop $R4
- Pop $R3
- Pop $R2
- Pop $R1
- Pop $R0
- Pop $9
- Pop $8
- Pop $7
- Pop $6
- Pop $5
- Pop $0
- Pop $1
- Pop $2
- Pop $3
- Pop $4
- FunctionEnd
- /*替換文字function*/
/*替換apache2 httpd.conf檔案文字*/
Nsis程式碼- Push "C:/Program Files/Apache2" #text to be replaced
- Push "$INSTDIR" #replace with
- Push all #replace all occurrences
- Push all #replace all occurrences
- Push "$INSTDIR/apache2/conf/httpd.conf" #file to replace in
- Call AdvReplaceInFile
同樣的方法,替換php.ini檔案和my.ini檔案中的相關字元即可
/*註冊apache伺服器, -n MyWebServer表示apache2註冊服務名為MyWebServer,預設服務名為Apache2 */
Nsis程式碼- nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k install -n MyWebServer'
/*註冊MySQL伺服器, MyDBServer表示MySQL註冊服務名為MyDBServer,預設服務名為MySQL*/
Nsis程式碼- nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/mysql5/bin/mysqld-nt.exe" -install MyDBServer'
啟動apache服務和MySQL服務
Nsis程式碼- nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k start -n MyWebServer'
- 或者
- nsExec::ExecToLog 'cmd.exe /c net start MyWebServer'
- nsExec::ExecToLog 'cmd.exe /c net start MyDBServer'