xmlnsisconfiguration-files

How to use NSISXML to modify a existing config file upon successful installation


I am currently working on modifying an installer that will ask the user for the name of a server that will be used to edit a value in a config (XML file) file post successful installation.

I have managed to modify the existing NSIS to get the server name from the user during installation,

Function nsDialogsServerPage
SetOutPath $INSTDIR

nsDialogs::Create 1018
Pop $Dialog

${If} $Dialog == error
    Abort
${EndIf}

${NSD_CreateLabel} 0 0 100% 12u "Please Enter the name server URL :"
Pop $Label1

${NSD_CreateText} 0 13u 100% 12u ""
Pop $MVMSPathURL

nsDialogs::Show

FunctionEnd

But the part I am having problems with is the one that modifies the config file at the end of a successful installation,

$AppDataPath is the file path to where all config files are to be install (C:\ProgramData\MyApp)

Function .onInstSuccess
DetailPrint ".onInstSuccess"

${NSD_GetText} $MVMSPathURL $4
nsisXML::create
nsisXML::load '"$AppDataPath\server.config"' #open the file.
nsisXML::select '/configuration/appSettings/Update[@server_address="localhost"]'
nsisXML::setAttribute "server_address" '$4'

#Free memory
nsisXML::release $4
nsisXML::release $2
nsisXML::release $1

#Save the JSON file 
nsisXML::save '"$AppDataPath\server.config"'

#free last memory 
nsisXML::release $0
##################

FunctionEnd

`

Here is the content of the config file that I want to modify (it is part of the package and is automatically copied to destination before the end of installation). I am trying to modify the content of server_address in the update field with the code above

<?xml version="1.0" encoding="utf-8" ?\>
<configuration>
    <appSettings>
    <log level="2" size="50MB" />
    <service name="MyApp" instance="" startup="auto" start="y"/>
    <Update 
        use_updater="1" 
        use_notifier="1" 
        server_protocol="http" 
        server_address="localhost" 
        server_port="80" 
        agent_id="" />

    </appSettings>
</configuration>

the content of the "server_address" is set to the value that was given before installation was started at the end of a successful installation.


Solution

  •     Section 
        # Prepare example
        InitPluginsDir
        Var /Global AppDataPath
        StrCpy $AppDataPath $pluginsdir
        FileOpen $0 "$pluginsdir\server.config" w
        FileWrite $0 '<?xml version="1.0" encoding="UTF-8"?><configuration><appSettings><log level="2" size="50MB" />'
        FileWrite $0 '<service name="MyApp" instance="" startup="auto" start="y"/>'
        FileWrite $0 '<Update use_updater="1" use_notifier="1" server_protocol="http" server_address="localhost" server_port="80" agent_id="" />'
        FileWrite $0 '</appSettings></configuration>'
        FileClose $0
        
        # Begin
        nsisXML::create
        nsisXML::load '$AppDataPath\server.config' #open the file.
    
        nsisXML::select '/configuration/appSettings/Update[@server_address="localhost"]'
        StrCpy $4 "example.com"
        nsisXML::setAttribute "server_address" '$4'
        nsisXML::release $2 # we are done with this node
        
        #Save the JSON file 
        nsisXML::save '$AppDataPath\server.config'
        nsisXML::release $0 # done with the document
        SectionEnd