- NSIS Discussion
- Replace string in text file at compile time
Archive: Replace string in text file at compile time
qwertymodo
8th April 2009 06:15 UTC
Replace string in text file at compile time
I need to replace a string inside of a text file at compile time, in the same way that the ReplaceInFile header does. I know that I COULD write a separate script that does it via the command-line, then call
!system '"${NSISDIR}\makensis.exe" "textreplacescript.nsi"' = 0
!system '"textreplace.exe" "commandlineargs"' = 0
!delfile "textreplace.exe"
Is there an easier way?
jpderuiter
8th April 2009 14:54 UTC
http://nsis.sourceforge.net/TextReplace_plugin
Sorry, I was too quick...
This will not work at compile time.
qwertymodo
9th April 2009 02:35 UTC
Hmm... well in the mean time I'll try the other way...
qwertymodo
12th April 2009 10:56 UTC
So, the whole point of this question was that I have portable-ized a few programs for the U3 platform, and anyone who has ever perused PortableApps.com knows how useful NSIS is for creating portable application launchers. Unfortunately, I did have to go the route I had indicated, but it ended up not being so bad. I actually added a few more functions in this manner as well. My one gripe was having to use separate helper scripts, which just made the source folder all ugly.
Then I discovered the wonders of !system 'echo ... >> mytempfile'
Well, one thing led to another, and it was just too cool using runtime commands at compile time, and then I was like "hey I'll bet I could take all of these helper scripts I'm writing and merge them all into one!"
Yes, I did actually managed to write a very useful script whose output file does NOTHING.
...I get the feeling that no one else has done this before...
...perhaps with good reason...
...I scare myself sometimes...
{_trueparuex^}
12th April 2009 12:04 UTC
Try this.
!define /file BUNCHASTUFF somefile.txt
!searchreplace NEWSTUFF "${BUNCHASTUFF}" "foo" "bar"
...
!delfile somefile.txt
!appendfile somefile.txt "${NEWSTUFF}"
Also you could use the NSIS !echo command instead of using !system command to call echo in your script.
PaR
{_trueparuex^}
12th April 2009 18:51 UTC
I just realized that code probably won't work with script files or with any other files that breaks the parameter quoting. :(
PaR
qwertymodo
14th April 2009 06:12 UTC
Hmm... I'll have to try that. Can !echo be used with >> the same as the system echo can? I mean, what is the actual difference? And as for breaking parameter quoting, I can usually get around that with $\"
kichik
10th May 2009 13:19 UTC
There's no !echo, only !appendfile and it works on all platforms and doesn't depend on the existence of echo or platform dependent command line parsing.
qwertymodo
10th May 2009 21:28 UTC
Well, there is an !echo command, I guess it just outputs only to the compiler, but thanks for pointing me at !appendfile, it's exactly what I needed.
...also, I'm not sure what you mean by platform-independent, as I'm pretty sure NSIS is Windows only anyways...
Afrow UK
11th May 2009 00:38 UTC
NSIS can build on other platforms other than Windows (but only build installers that run on Windows).
Stu
qwertymodo
11th May 2009 04:57 UTC
Oh, right, I knew that... just for some reason I wasn't thinking about the fact that it's a compile-time command, which would depend on the compiling system :P Anyway, problem solved, so thanks for the input :)