How to load a file's contents into a variable/define in NSIS?
Asked Answered
P

1

5

I have a file 'releaseVersionNumber.txt' which I read during my build process; currently its read for my Mac build but I want to read it in my Windows NSIS build to reduce the number of edit locations (duplication being evil)...

So I'm trying to replace:

!define VERSION 1.2.3

with something like

FileOpen $4 "..\releaseVersionNumber.txt" r
FileRead $4 $1
FileClose $4
!define VERSION ${1}

But I get an error command FileOpen not valid outside Section or Function. Wrapping it in a function I produces command call not valid outside Section or Function so I can't seem to do this in the installer setup, only at runtime.

Is there a way to achieve what I'm after?!

Paravane answered 26/3, 2013 at 8:8 Comment(2)
Getting another issue now after using !define /file - this doesn't work: VIProductVersion "${VERSION}.0" gives me Error: invalid VIProductVersion format, should be X.X.X.X ...Paravane
Turns out the variable isn't being set as expected - the !define /file is loading "1.2?" - no idea what's up with that but adding 4/5 blanks at the end fixes it.Paravane
B
13

All commands begining with ! are compile time commands, so they are processed at compile time, much before your program runs.

  • You can try declaring VERSION as a Variable instead of a define:

    Var VERSION
    FileOpen $4 "..\releaseVersionNumber.txt" r
    FileRead $4 $VERSION
    FileClose $4
    
  • If you need VERSION to be a define, then you can try the /file parameter in !define.

    !define /file VERSION "..\releaseVersionNumber.txt"
    
  • I like to have a version.nsh file with just the define:

    !define VERSION "2013-03-25:16:23:50"
    

    And then, I include it:

    !include /NONFATAL version.nsh
    # Default value in case no version.nsh is present
    !ifndef VERSION
        !define /date VERSION "%Y-%m-%d %H:%M:%S"
    !endif
    
Buckram answered 26/3, 2013 at 9:7 Comment(1)
!define /file is the one for me!Paravane

© 2022 - 2024 — McMap. All rights reserved.