How to control addition of file in Inno Setup 6 script using the presence of a substring in a defined string - inno-setup

I have two executables which do the same thing, but one is built to target a DEV environment, and the other a PROD environment.
my-app-dev.exe
my-app-prod.exe
A VERSION parameter is always passed into the Inno build script which contains the build version in one of the following two formats:
"1.0.0"
"1.0.0-DEV"
In the [Files] section, how can I include the my-app-dev.exe if the current value of VERSION contains the DEV suffix, but include the my-app-prod.exe if not?
I figured a Check parameter would be the way to resolve this, but I can't even get the simplest case to work.
For example, the following adds the file to the build despite the Check function clearly returning False.
[Files]
Source: "my-app-dev.exe"; DestDir: {app}; Check: ShouldIncludeDev
function ShouldIncludeDev: Boolean;
begin
Result := False;
end;
Perhaps I must be missing something fundamental here...

Based on this answer (Inno Setup IDE and ISCC/ISPP passing define) you could do this:
Pass the value via command line parameter:
/DargDEV="DEV"
In your [Files] section you can then do this:
#ifdef argDEV
Source: "my-app-dev.exe"; DestDir: {app};
#else
Source: "my-app-prod.exe"; DestDir: {app};
#endif
Notes
It uses the #ifdef pre-processor directive.

Related

Configuring Visual & Installer in VS 2022 for x64

My config screen:
Why is it that I can't select x64 in the list? In the Alarm Clock row there is Add / Edit in the drop-down list. But not for AlarmClockSetup.
I am posting step by step tutorial because Configuration in Visual Studio works differently than in Inno Setup:
Lets assume user wants to build 2 installers: for 32 and 64 bit OS (two separate setup.exe-s).
Define 2 Visual Studio Configuration(s) for this using standard Visual Studio Configuration manager dialog, choose any names for them you wish, I chose: "setup32" and "setup64", the result will look like this:
For EACH configuration define some symbol (called conf in this example) in Project Properties like this: conf=$(Configuration)
It is important to have this symbol set for EACH configuration
And now double check whether you have defined this symbol for EACH configuration
If symbol is not defined for some Configuration you receive "Error on line XXX: Undeclared identifier: conf."
Inno script to test the configuration:
[Setup]
AppName=InnoSetupProject1
AppVersion=1.0
DefaultDirName={pf}\InnoSetupProject1
DefaultGroupName=InnoSetupProject1
UninstallDisplayIcon={app}\InnoSetupProject1.exe
Compression=lzma2
SolidCompression=yes
OutputDir=userdocs:Output
OutputBaseFilename=InnoSetupProject1
PrivilegesRequired=lowest
[Files]
Source: "Script.iss"; DestDir: "{app}"
[Icons]
Name: "{group}\InnoSetupProject1"; Filename: "{app}\InnoSetupProject1.exe"
[Code]
// Place your code here...
procedure InitializeWIzard();
begin
MsgBox(ExpandConstant('Configuration: {#conf}'), mbinformation,mb_ok);
#if conf == "setup32"
MsgBox('This is setup32', mbinformation,mb_ok);
#endif
#if conf == "setup64"
MsgBox('This is setup64', mbinformation,mb_ok);
#endif
end;
How it works:
When you choose "setup32" in Configuration dropdown, the $(Configuration) MSBuild variable is initialized and set to "setup32".
This $(Configuration) is mapped to Symbol conf (defined in Project Properties) that can be used anywhere in the script.
Use it for conditioning the setup behaviour using pre-processor #if, for Pascal code or anywhere as {#conf}.
So when the configuration is set to setup32 and you build the script, the Inno pre-processor excludes the inappropriate parts from the script and only the correct message box is shown.
Use the #if for include/exclude files in your script, for [Setup] section directives, for defining the Output directory or anything, choose the Configuration and rebuild the setup.

Read destination directory from ini file during install time in Inno Setup

I need to read a directory path from an ini file at install time.
Under the [Code] section I have defined a function like
function GetDirectoryFromIni: String;
begin
Result :=
GetIniString('Directories' , 'Name' ,
ExpandConstant('{app}')+'\Default_Path\' ,
ExpandConstant('{app}')+'my_ini_file.ini');
end;
How can I use this function/path/string in the [Files] section?
Something like:
[Files]
Source: "C:\Source_Directory\*.*"; DestDir: "GetDirectoryFromIni"; \
Flags: ignoreversion
The basic logic is if the user has changed the "Default_Path" from a previous installation I want to adjust to use it, otherwise it uses the default path when the program is first installed.
I cannot seem to define or set a string variable to use the GetDirectoryFromIni result. Can anyone help?
You can do this even without any Pascal Script code. There's {ini} "constant":
[Files]
Source: "C:\Source_Directory\*.*"; \
DestDir: "{ini:{app}\my_ini_file.ini,Directories,Name|{app}\Default_Path\}"; \
Flags: ignoreversion
For an answer for your expected solution, see:
Using global string script variable in Run or other section in Inno Setup
You need to refer to the function in the [Files] section like
[Files]
Source: "C:\Source_Directory\*.*"; DestDir: "{code:GetDirectoryFromIni}"; Flags: ignoreversion
Then it works.

Constant defined using Inno Setup preprocessor #define is not recognised in ExpandConstant

I'm trying to set up an install file that (optionally) installs .NET 5 if it's not already installed.
However, I'm having trouble defining the version of .NET to install in a constant.
My script is set up like this
#define DotNetVersion "5"
...
[Tasks]
Name: "dotnet"; Description: "{cm:DotNet}"; GroupDescription: "{cm:Prerequisites}"
...
[Files]
Source: "..\Dependencies\{#DotNetInstallFile}"; DestDir: {tmp}; \
Flags: deleteafterinstall; AfterInstall: InstallDotNet; \
Check: NetNotInstalled(ExpandConstant('{DotNetVersion}')); Tasks: "dotnet"
When I try running the resulting install file I get the following error:
Internal error: Expression error 'Internal error: Unknown constant "DotNetVersion"'
The function NetNotInstalled works correctly if I replace ExpandConstant('{DotNetVersion}') with '5', but I want to easily be able to change this without modifying more than the defined constants.
I don't get what's wrong here. The Inno Setup docs state that this should be valid.
Using the same constant for any other function seems to work flawlessly.
A variable defined using Inno Setup preprocessor is not Inno Setup constant. Calling ExpandConstant function on it has no effect.
To expand preprocessor variable (or any expression), you can use {#VariableOrExpression} syntax. It's an inline preprocessor directive call, where, when no directive is explicitly specified, the emit is implied. So the {#VariableOrExpression} is the same as {#emit VariableOrExpression}. And as every preprocessor construct, it's evaluated on compile time (contrary to ExpandConstant).
You actually do that correctly already with {#DotNetInstallFile}, so do the same with DotNetVersion:
Source: "..\Dependencies\{#DotNetInstallFile}"; \
DestDir: {tmp}; Flags: deleteafterinstall; AfterInstall: InstallDotNet; \
Check: NetNotInstalled('{#DotNetVersion}'); Tasks: "dotnet"
See also How to use variables \ macros with Inno Setup?

Inno Setup Runtime error while accessing CurrentFileName when installing an empty folder

I tried to access all files inside a folder through Pascal code. When I iterate through an empty folder I am getting the following error:
Exception: Internal error; An attempt was made to call the "CurrentFileName" function from outside a "Check", "BeforeInstall" or "AfterInstall" event function belonging to a "[Files]" entry
The code I used:
[Files]
Source: ".\3D_Dlls\*"; DestDir: {app}\3D_Dlls; \
Flags: ignoreversion recursesubdirs createallsubdirs skipifsourcedoesntexist; \
Check: FileBackup
[Code]
function FileBackup(): Boolean;
var FileName,Source,Target,TargetDir: String;
begin
Result := True;
Source := ExpandConstant(CurrentFileName);
end
Say for example my folder structure is like that:
3D_Dlls
- Folder 1
- Folder 2 // Empty folder and it invokes the problem
- Folder 3
I believe this is a bug in Inno Setup triggered by the createallsubdirs flag.
When the flag is specified, Inno Setup collects a separate list of empty directories that need to be explicitly created during an installation. When "installing" the empty folder, the call to the CurrentFileName simply fails.
Workarounds:
Remove the createallsubdirs flag, if possible.
Instead, you can explicitly create empty directories using the [Dirs] section.
Catch the exception:
function FileBackup: Boolean;
begin
Result := True;
try
Source := ExpandConstant(CurrentFileName);
except
Log(GetExceptionMessage);
end;
end;
I have reported this on Inno Setup newsgroup, but the newsgroup was reset since.

Script for copying Files at the end of install : Inno Setup

I explain my project : my setup will be delivered with 2 licence files beside him who they must not be include inside the concerned setup. Like this :
Folder/
----Setup.exe
----CanBTL.dat
----CanBTP.dat
And i want, if that's sure they are here, to copy this files in a folder who will be build with Setup.exe. So i'm trying to make this code :
I edit my script :
EDIT :
[Code]
function CheckForFile(CurPageID: Integer): Boolean;
begin
if (CurPageID = wpFinished) and (FileExists('CanBTL.dat' + 'CanBTP.dat')) then
begin
FileCopy(ExpandConstant('CanBTL.dat' + 'CanBTP.dat'), ExpandConstant('{cf}\Folder\'), false);
end;
end;
The goal is to copy the two .dat file next to the setup in a folder created by the setup.exe
It compile, but seems to make nothing. My files are not copied.
I'm still a beginner to the section code in Inno Setup so if anyone can help me?
Thanks
Ok. No need of code section, that's working fine using external flags and the
{src} constant to say current directory :
Source: "{src}\CanBTL.dat"; DestDir: "{cf}\Folder"; Flags: external;
Source: "{src}\CanBTP.dat"; DestDir: "{cf}\Folder"; Flags: external;
Thanks TLama

Resources