windowsbatch-fileinstallationscriptingsilent-installer

How to Run Post-Install Commands Silently Using Inno Setup?


I'm using Inno Setup to create an installer for my application. Part of the installation process includes installing the Ollama binary and running a couple of commands to set up models. However, the commands do not seem to run as expected. Here is my Inno Setup script:

[Setup]
AppName=Chat2Find
AppVersion=1.0
DefaultDirName={pf}\Chat2Find
DefaultGroupName=Chat2Find
OutputDir=.
OutputBaseFilename=Chat2FindInstaller
Compression=lzma
SolidCompression=yes
DiskSpanning=yes

[Files]
; Include the executable
Source: "C:\Users\presh\c2f\dist\Chat2Find.exe"; DestDir: "{app}"; Flags: ignoreversion

; Include the GGUF files to be extracted to a permanent location
Source: "C:\Users\presh\c2f\dist\Mistral-7B-Instruct-v0.3.Q4_K_M.gguf"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\Phi-3-mini-4k-instruct-q4.gguf"; DestDir: "{app}"; Flags: ignoreversion

; Include the Ollama setup binary
Source: "C:\Users\presh\c2f\dist\OllamaSetup.exe"; DestDir: "{tmp}"; Flags: ignoreversion deleteafterinstall

; Include other resources
Source: "C:\Users\presh\c2f\dist\menu.png"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\close.png"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\new_note.jpg"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\copy.png"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\send_icon.png"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\presh\c2f\dist\chat2find.jpeg"; DestDir: "{app}"; Flags: ignoreversion

[Icons]
Name: "{group}\Chat2Find"; Filename: "{app}\Chat2Find.exe"
Name: "{commondesktop}\Chat2Find"; Filename: "{app}\Chat2Find.exe"; Tasks: desktopicon

[Tasks]
Name: "desktopicon"; Description: "Create a &desktop icon"; GroupDescription: "Additional icons:"; Flags: checkedonce

[Run]
Filename: "{app}\Chat2Find.exe"; Description: "Launch Chat2Find"; Flags: nowait postinstall skipifsilent
; Run Ollama setup silently
Filename: "{tmp}\OllamaSetup.exe"; Description: "Install Ollama"; Flags: waituntilterminated

; Run Ollama commands silently using a script
Filename: "cmd.exe"; Parameters: "/C {app}\run_ollama_commands.bat"; Flags: runhidden postinstall

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
  GGUFDir: String;
  MistralDef, Phi3Def, OllamaScript: String;
begin
  if CurStep = ssPostInstall then
  begin
    GGUFDir := ExpandConstant('{app}');
    
    if not DirExists(GGUFDir) then
    begin
      CreateDir(GGUFDir);
    end;
    
    MistralDef := GGUFDir + '\mistral_definition';
    SaveStringToFile(MistralDef,
      'FROM ' + GGUFDir + '\Mistral-7B-Instruct-v0.3-Q4_K_M.gguf' + #13#10 +
      'TEMPLATE """[INST] {{ if .System }}{{ .System }} {{ end }}{{ .Prompt }} [/INST]"""' + #13#10 +
      'PARAMETER stop "[INST]"' + #13#10 +
      'PARAMETER stop "[/INST]"', False);

    Phi3Def := GGUFDir + '\phi3_definition';
    SaveStringToFile(Phi3Def,
      'FROM ' + GGUFDir + '\Phi-3-mini-4k-instruct-q4.gguf' + #13#10 +
      'TEMPLATE """<s>{{ if .Prompt }}' + #13#10 +
      '{{ .Prompt }}' + #13#10 +
      '{{ end }}' + #13#10 +
      '{{ .Response }}"""' + #13#10 +
      'PARAMETER stop' + #13#10 +
      'PARAMETER stop' + #13#10 +
      'PARAMETER stop' + #13#10 +
      'PARAMETER num_ctx 4096', False);

    OllamaScript := GGUFDir + '\run_ollama_commands.bat';
    SaveStringToFile(OllamaScript,
      'ollama create C2F_mistral -f "' + MistralDef + '"' + #13#10 +
      'ollama create C2F_phi3 -f "' + Phi3Def + '"', False);
  end;
end;

Problem:

The commands in run_ollama_commands.bat do not seem to execute as expected. How can I ensure that these commands run silently after the installation of the Ollama binary?

Additional Context:

The OllamaSetup.exe installs the Ollama binary correctly.

I need the models to be created using the GGUF files and templates provided during the installation.

The batch file (run_ollama_commands.bat) should run silently without showing a command prompt window.

Any insights or suggestions on how to correctly execute these post-install commands would be greatly appreciated!


Solution

  • If you're using Inno Setup and need to execute post-install commands silently, follow these steps to ensure everything runs smoothly. Here’s a refined solution:

    1. Update Your Inno Setup Script:

    Remove the [Code] section that creates the definition files and batch script dynamically. Instead, manually prepare these files and include them in your installer. Here’s how you should modify your Inno Setup script:

    [Setup]
    AppName=Chat2Find
    AppVersion=1.0
    DefaultDirName={pf}\Chat2Find
    DefaultGroupName=Chat2Find
    OutputDir=.
    OutputBaseFilename=Chat2FindInstaller
    Compression=lzma
    SolidCompression=yes
    DiskSpanning=yes
    
    [Files]
    ; Include the executable
    Source: "C:\Users\presh\c2f\dist\Chat2Find.exe"; DestDir: "{app}"; Flags: ignoreversion
    
    ; Include the GGUF files
    Source: "C:\Users\presh\c2f\dist\Mistral-7B-Instruct-v0.3.Q4_K_M.gguf"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\Phi-3-mini-4k-instruct-q4.gguf"; DestDir: "{app}"; Flags: ignoreversion
    
    ; Include pre-created definition files
    Source: "C:\Users\presh\c2f\dist\mistral_definition"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\phi3_definition"; DestDir: "{app}"; Flags: ignoreversion
    
    ; Include the Ollama setup binary
    Source: "C:\Users\presh\c2f\dist\OllamaSetup.exe"; DestDir: "{tmp}"; Flags: ignoreversion deleteafterinstall
    
    ; Include the batch file
    Source: "C:\Users\presh\c2f\dist\run_ollama_commands.bat"; DestDir: "{app}"; Flags: ignoreversion
    
    ; Include other resources
    Source: "C:\Users\presh\c2f\dist\menu.png"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\close.png"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\new_note.jpg"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\copy.png"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\send_icon.png"; DestDir: "{app}"; Flags: ignoreversion
    Source: "C:\Users\presh\c2f\dist\chat2find.jpeg"; DestDir: "{app}"; Flags: ignoreversion
    
    [Icons]
    Name: "{group}\Chat2Find"; Filename: "{app}\Chat2Find.exe"
    Name: "{commondesktop}\Chat2Find"; Filename: "{app}\Chat2Find.exe"; Tasks: desktopicon
    
    [Tasks]
    Name: "desktopicon"; Description: "Create a &desktop icon"; GroupDescription: "Additional icons:"; Flags: checkedonce
    
    [Run]
    Filename: "{tmp}\OllamaSetup.exe"; Description: "Install Ollama"; Flags: waituntilterminated runhidden
    
    ; Run Ollama commands silently
    Filename: "{app}\run_ollama_commands.bat"; Parameters: ""; Flags: waituntilterminated runhidden
    
    Filename: "{app}\Chat2Find.exe"; Description: "Launch Chat2Find"; Flags: postinstall
    
    1. Prepare the Batch File:

    Create a run_ollama_commands.bat file with the following content. This file should be included in your installer:

    @echo off
    cd /d %~dp0
    echo Running Ollama commands...
    
    ollama create C2F_mistral -f mistral_definition
    if %errorlevel% neq 0 (
        echo Failed to create C2F_mistral model
        exit /b %errorlevel%
    )
    
    ollama create C2F_phi3 -f phi3_definition
    if %errorlevel% neq 0 (
        echo Failed to create C2F_phi3 model
        exit /b %errorlevel%
    )
    
    echo Ollama commands executed successfully. Deleting GGUF files...
    
    del /f /q "Mistral-7B-Instruct-v0.3.Q4_K_M.gguf"
    del /f /q "Phi-3-mini-4k-instruct-q4.gguf"
    
    echo GGUF files deleted.
    exit
    

    Explanation:

    The @echo off command suppresses the command output to avoid clutter. (Inno Setup also runs it silently) cd /d %~dp0 changes the directory to where the batch file is located. Commands are run with error checking, and any errors are reported. GGUF files are deleted after successful command execution. By following these instructions, you'll ensure that your post-install commands run silently and efficiently.