1
0
Fork 0

use ini file modification to persist config values

This commit is contained in:
Wilfried Goesgens 2017-05-24 16:33:55 +02:00
parent 50876d0d3f
commit 9936899caf
3 changed files with 151 additions and 140 deletions

View File

@ -0,0 +1,139 @@
!define StrTrimNewLines "!insertmacro StrTrimNewLines"
!macro StrTrimNewLines ResultVar String
Push "${String}"
Call StrTrimNewLines
Pop "${ResultVar}"
!macroend
Function StrTrimNewLines
/*After this point:
------------------------------------------
$R0 = String (input)
$R1 = TrimCounter (temp)
$R2 = Temp (temp)*/
;Get input from user
Exch $R0
Push $R1
Push $R2
;Initialize trim counter
StrCpy $R1 0
loop:
;Subtract to get "String"'s last characters
IntOp $R1 $R1 - 1
;Verify if they are either $\r or $\n
StrCpy $R2 $R0 1 $R1
${If} $R2 == `$\r`
${OrIf} $R2 == `$\n`
Goto loop
${EndIf}
;Trim characters (if needed)
IntOp $R1 $R1 + 1
${If} $R1 < 0
StrCpy $R0 $R0 $R1
${EndIf}
/*After this point:
------------------------------------------
$R0 = ResultVar (output)*/
;Return output to user
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function SplitFirstStrPart
Exch $R0
Exch
Exch $R1
Push $R2
Push $R3
StrCpy $R3 $R1
StrLen $R1 $R0
IntOp $R1 $R1 + 1
loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 -$R1
StrCmp $R1 0 exit0
StrCmp $R2 $R3 exit1 loop
exit0:
StrCpy $R1 ""
Goto exit2
exit1:
IntOp $R1 $R1 - 1
StrCmp $R1 0 0 +3
StrCpy $R2 ""
Goto +2
StrCpy $R2 $R0 "" -$R1
IntOp $R1 $R1 + 1
StrCpy $R0 $R0 -$R1
StrCpy $R1 $R2
exit2:
Pop $R3
Pop $R2
Exch $R1 ;rest
Exch
Exch $R0 ;first
FunctionEnd
Function ReadINIFileKeys
Exch $R0 ;INI file to write
Exch
Exch $R1 ;INI file to read
Push $R2
Push $R3
Push $R4 ;uni var
Push $R5 ;uni var
Push $R6 ;last INI section
FileOpen $R2 $R1 r
Loop:
FileRead $R2 $R3 ;get next line into R3
IfErrors Exit
Push $R3
Call StrTrimNewLines
Pop $R3
StrCmp $R3 "" Loop ;if blank line, skip
StrCpy $R4 $R3 1 ;get first char into R4
StrCmp $R4 ";" Loop ;check it for semicolon and skip line if so(ini comment)
StrCpy $R4 $R3 "" -1 ;get last char of line into R4
StrCmp $R4 "]" 0 +6 ;if last char is ], parse section name, else jump to parse key/value
StrCpy $R6 $R3 -1 ;get all except last char
StrLen $R4 $R6 ;get str length
IntOp $R4 $R4 - 1 ;subtract one from length
StrCpy $R6 $R6 "" -$R4 ;copy all but first char to trim leading [, placing the section name in R6
Goto Loop
Push "=" ;push delimiting char
Push $R3
Call SplitFirstStrPart
Pop $R4
Pop $R5
WriteINIStr $R0 $R6 $R4 $R5
Goto Loop
Exit:
FileClose $R2
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd

View File

@ -1,135 +0,0 @@
!define StrRep "!insertmacro StrRep"
!macro StrRep output string old new
Push `${string}`
Push `${old}`
Push `${new}`
!ifdef __UNINSTALL__
Call un.StrRep
!else
Call StrRep
!endif
Pop ${output}
!macroend
!macro Func_StrRep un
Function ${un}StrRep
Exch $R2 ;new
Exch 1
Exch $R1 ;old
Exch 2
Exch $R0 ;string
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9
StrCpy $R3 0
StrLen $R4 $R1
StrLen $R6 $R0
StrLen $R9 $R2
loop:
StrCpy $R5 $R0 $R4 $R3
StrCmp $R5 $R1 found
StrCmp $R3 $R6 done
IntOp $R3 $R3 + 1 ;move offset by 1 to check the next character
Goto loop
found:
StrCpy $R5 $R0 $R3
IntOp $R8 $R3 + $R4
StrCpy $R7 $R0 "" $R8
StrCpy $R0 $R5$R2$R7
StrLen $R6 $R0
IntOp $R3 $R3 + $R9 ;move offset by length of the replacement string
Goto loop
done:
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Push $R0
Push $R1
Pop $R0
Pop $R1
Pop $R0
Pop $R2
Exch $R1
FunctionEnd
!macroend
!insertmacro Func_StrRep ""
!insertmacro Func_StrRep "un."
Function RIF
ClearErrors ; want to be a newborn
Exch $0 ; REPLACEMENT
Exch
Exch $1 ; SEARCH_TEXT
Exch 2
Exch $2 ; SOURCE_FILE
Push $R0 ; SOURCE_FILE file handle
Push $R1 ; temporary file handle
Push $R2 ; unique temporary file name
Push $R3 ; a line to sar/save
Push $R4 ; shift puffer
IfFileExists $2 +1 RIF_error ; knock-knock
FileOpen $R0 $2 "r" ; open the door
GetTempFileName $R2 ; who's new?
FileOpen $R1 $R2 "w" ; the escape, please!
RIF_loop: ; round'n'round we go
FileRead $R0 $R3 ; read one line
IfErrors RIF_leaveloop ; enough is enough
RIF_sar: ; sar - search and replace
Push "$R3" ; (hair)stack
Push "$1" ; needle
Push "$0" ; blood
Call StrRep ; do the bartwalk
StrCpy $R4 "$R3" ; remember previous state
Pop $R3 ; gimme s.th. back in return!
StrCmp "$R3" "$R4" +1 RIF_sar ; loop, might change again!
FileWrite $R1 "$R3" ; save the newbie
Goto RIF_loop ; gimme more
RIF_leaveloop: ; over'n'out, Sir!
FileClose $R1 ; S'rry, Ma'am - clos'n now
FileClose $R0 ; me 2
Delete "$2.old" ; go away, Sire
Rename "$2" "$2.old" ; step aside, Ma'am
Rename "$R2" "$2" ; hi, baby!
ClearErrors ; now i AM a newborn
Goto RIF_out ; out'n'away
RIF_error: ; ups - s.th. went wrong...
SetErrors ; ...so cry, boy!
RIF_out: ; your wardrobe?
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $2
Pop $0
Pop $1
FunctionEnd
!macro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
Push "${SOURCE_FILE}"
Push "${SEARCH_TEXT}"
Push "${REPLACEMENT}"
Call RIF
!macroend

View File

@ -5,7 +5,6 @@
!addplugindir '@CPACK_PLUGIN_PATH@/SharedMemory/Plugins'
!addincludedir '@CPACK_PLUGIN_PATH@/UAC-plug-in-NSIS'
!addincludedir '@CPACK_PLUGIN_PATH@/'
!include "StrRep.nsh"
!include "OpenLink.nsh"
;--------------------------------
@ -59,6 +58,7 @@ Var ADD_TO_PATH_CURRENT_USER ; x bool
; Variables for definition of instdir
; posible values: SingleUser | AllUsers | Service
VAR TRI_INSTALL_TYPE ; x
Var newCfgValues ; keep new config file values
!define TEMP1 $R0 ;Temp variable
Var DATADIR
@ -88,7 +88,7 @@ RequestExecutionLevel highest
!include "AddToPath.nsh"
!include "AddToRegistry.nsh"
!include "WaitForService.nsh"
!include "ReadINIFileKeys.nsh"
Function disableBackButton
GetDlgItem $0 $HWNDParent 3
@ -479,10 +479,17 @@ Section "-Core installation"
!insertmacro AddToRegistry "DATADIR" "$DATADIR"
!insertmacro _ReplaceInFile "$INSTDIR\etc\arangodb3\arangod.conf" "storage-engine = auto" "storage-engine = $STORAGE_ENGINE"
!insertmacro _ReplaceInFile "$INSTDIR\etc\arangodb3\arangod.conf" "directory = @CPACK_ROOTDIR@var/lib/arangodb3" "directory = $DATADIR"
; Create a file containing the settings we want to be overwritten:
StrCpy $newCfgValues "[database]$\r$\ndirectory = $DATADIR$\r$\n[server]$\r$\nstorage-engine = $STORAGE_ENGINE$\r$\n"
FileOpen $4 "$INSTDIR\etc\arangodb3\newValues.ini" w
FileWrite $4 "$newCfgValues"
FileClose $4
; Alter the shipped file and insert the values from above:
push "$INSTDIR\etc\arangodb3\newValues.ini"
push "$INSTDIR\etc\arangodb3\arangod.conf"
call ReadINIFileKeys
System::Call 'Kernel32::SetEnvironmentVariable(t, t)i ("ARANGODB_DEFAULT_ROOT_PASSWORD", "$PASSWORD").r0'
System::Call 'Kernel32::SetEnvironmentVariable(t, t)i ("ARANGODB_DEFAULT_ROOT_PASSWORD", "$PASSWORD").r0'
StrCmp $0 0 error
ExecWait "$INSTDIR\${SBIN_DIR}\arangod.exe --database.init-database --server.rest-server false --server.statistics false --foxx.queues false"
Goto done