nsis打包脚本

最近在打包一个WEB程序,运行环境是PHP+Apache2+Mysql。打包过程比较顺利,注册服务、启动都能成功。遇到一个问题,就是修改配置文件的问题。既然是打包程序,当然要求打包的程序可以安装在任意位置都能够正常运行,修改apache服务器的httpd.conf文件费了我不少功夫,因为对NSIS并不熟悉。查找了一些资料,找到一些代码后面成功了。把这些代码贴出来,也许对遇到我同样问题的人有所帮助。

http://nsis.sourceforge.net/More_advanced_replace_text_in_file

 

替换文件中字符的函数:

Nsis代码 复制代码
  1. /*替换文字function*/   
  2. Function AdvReplaceInFile   
  3. Exch $0 ;file to replace in   
  4. Exch   
  5. Exch $1 ;number to replace after   
  6. Exch   
  7. Exch 2  
  8. Exch $2 ;replace and onwards   
  9. Exch 2  
  10. Exch 3  
  11. Exch $3 ;replace with   
  12. Exch 3  
  13. Exch 4  
  14. Exch $4 ;to replace   
  15. Exch 4  
  16. Push $5 ;minus count   
  17. Push $6 ;universal   
  18. Push $7 ;end string   
  19. Push $8 ;left string   
  20. Push $9 ;right string   
  21. Push $R0 ;file1   
  22. Push $R1 ;file2   
  23. Push $R2 ;read   
  24. Push $R3 ;universal   
  25. Push $R4 ;count (onwards)   
  26. Push $R5 ;count (after)   
  27. Push $R6 ;temp file name   
  28.   
  29.   GetTempFileName $R6   
  30.   FileOpen $R1 $0 r ;file to search in   
  31.   FileOpen $R0 $R6 w ;temp file   
  32.    StrLen $R3 $4  
  33.    StrCpy $R4 -1  
  34.    StrCpy $R5 -1  
  35.   
  36. loop_read:   
  37.  ClearErrors   
  38.  FileRead $R1 $R2 ;read line   
  39.  IfErrors exit   
  40.   
  41.    StrCpy $5 0  
  42.    StrCpy $7 $R2   
  43.   
  44. loop_filter:   
  45.    IntOp $5 $5 - 1  
  46.    StrCpy $6 $7 $R3 $5 ;search   
  47.    StrCmp $6 "" file_write2   
  48.    StrCmp $6 $4 0 loop_filter   
  49.   
  50. StrCpy $8 $7 $5 ;left part   
  51. IntOp $6 $5 + $R3   
  52. IntCmp $6 0 is0 not0   
  53. is0:   
  54. StrCpy $9 ""  
  55. Goto done   
  56. not0:   
  57. StrCpy $9 $7 "" $6 ;right part   
  58. done:   
  59. StrCpy $7 $8$3$9 ;re-join   
  60.   
  61. IntOp $R4 $R4 + 1  
  62. StrCmp $2 all file_write1   
  63. StrCmp $R4 $2 0 file_write2   
  64. IntOp $R4 $R4 - 1  
  65.   
  66. IntOp $R5 $R5 + 1  
  67. StrCmp $1 all file_write1   
  68. StrCmp $R5 $1 0 file_write1   
  69. IntOp $R5 $R5 - 1  
  70. Goto file_write2   
  71.   
  72. file_write1:   
  73.  FileWrite $R0 $7 ;write modified line   
  74. Goto loop_read   
  75.   
  76. file_write2:   
  77.  FileWrite $R0 $R2 ;write unmodified line   
  78. Goto loop_read   
  79.   
  80. exit:   
  81.   FileClose $R0   
  82.   FileClose $R1   
  83.   
  84.    SetDetailsPrint none   
  85.   Delete $0  
  86.   Rename $R6 $0  
  87.   Delete $R6   
  88.    SetDetailsPrint both   
  89.   
  90. Pop $R6   
  91. Pop $R5   
  92. Pop $R4   
  93. Pop $R3   
  94. Pop $R2   
  95. Pop $R1   
  96. Pop $R0   
  97. Pop $9  
  98. Pop $8  
  99. Pop $7  
  100. Pop $6  
  101. Pop $5  
  102. Pop $0  
  103. Pop $1  
  104. Pop $2  
  105. Pop $3  
  106. Pop $4  
  107. FunctionEnd   
  108. /*替换文字function*/  

 

    /*替换apache2 httpd.conf文件文字*/

Nsis代码 复制代码
  1. Push "C:/Program Files/Apache2" #text to be replaced   
  2. Push "$INSTDIR" #replace with   
  3. Push all #replace all occurrences   
  4. Push all #replace all occurrences   
  5. Push "$INSTDIR/apache2/conf/httpd.conf" #file to replace in   
  6. Call AdvReplaceInFile    

 

同样的方法,替换php.ini文件和my.ini文件中的相关字符即可

 

   /*注册apache服务器, -n MyWebServer表示apache2注册服务名为MyWebServer,默认服务名为Apache2 */

Nsis代码 复制代码
  1. nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k install -n MyWebServer'  

 

  /*注册MySQL服务器, MyDBServer表示MySQL注册服务名为MyDBServer,默认服务名为MySQL*/

Nsis代码 复制代码
  1. nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/mysql5/bin/mysqld-nt.exe" -install MyDBServer'  

 启动apache服务和MySQL服务

Nsis代码 复制代码
  1. nsExec::ExecToLog 'cmd.exe /c "$INSTDIR/apache2/bin/httpd.exe" -k start -n MyWebServer'  
  2. 或者   
  3. nsExec::ExecToLog 'cmd.exe /c net start MyWebServer'  
  4. nsExec::ExecToLog 'cmd.exe /c net start MyDBServer' 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值