Self-extracting executable (SFX) generator for AquaShell scripts

This script allows creating self-extracting executables (SFX) to easily ship AquaShell scripts as a single EXE using IExpress.

# Generate a self-extracting executable (SFX) to easily ship AquaShell scripts
#
# Features:
# - Run in interactive mode
# - Run in commandline mode
# - Standalone builds via local AquaShell installation
#
# How to run:
# - Interactive mode: aquashell sfxmaker.dnys
# - CLI mode: aquashell sfxmaker.dnys --name AppName --version x.x.x --folder path\to\build\folder --launch name_of_entrypoint_script.dnys --standalone y/n
# Hint: If you skip any of the paramters during CLI mode, the script will prompt the missing arguments.
#
# Note: Standalone builds don't require an AquaShell installation when running the EXE on the host system.
#       It is highly recommended to activate standalone mode for release builds.
#       Standalone builds require a local AquaShell installation having its path set via the PATH environment variable.
#

require "auto";
require "dialog";
require "fileio";
require "strings";

global scriptpath string;
global shellpath string;
global argval string;
global appname string;
global appver string;
global folder string;
global launch string;
global standalone string;

getscriptpath scriptpath;

function CmdHas bool(search string)
{
    result false;

    if (%argc, -gr, 1) {
        for (i, 1, %argc, -inc) {
            if ("%argv[%i]", -eq, "%search") {
                result true;
            };
        };
    };
};

function CmdPair string(key string)
{
    result "";

    if (%argc, -gr, 1) {
        for (i, 1, %argc, -inc) {
            if ("%argv[%i]", -eq, "%key") {
                local varIncr int;

                set varIncr <= %i;
                ++ varIncr;

                if (%varIncr, -ls, %argc) {
                    result "%argv[%varIncr]";
                };
            };
        };
    };
};

function get_shell_path string()
{
    local pathvar string;
    local checkfl bool;

    result "";

    sys {echo %Path%} pathvar;
    s_tokenize "%pathvar" ";" tokPaths;

    for (i, 0, %tokPaths.count, -inc) {
        if (%checkfl, -nt, true) {
            fexists "%tokPaths[%i]\dnyAquaShell.exe" checkfl;
            if (%checkfl, -eq, true) {
                result "%tokPaths[%i]";
            };
        };
    };

    s_cleartokens tokPaths;
};

function create_sed_file void()
{
    local exists bool;
    local hfile int;

    fexists "%folder/%appname.sed" exists;
    if (%exists, -eq, true) {
        fremove "%folder/%appname.sed" void;
    };
    
    fopen "%folder/%appname.sed" false hfile;
    if (%hfile, -nt, %FIO_INVALID_HANDLE) {
        fwriteline %hfile {[Version]};
        fwriteline %hfile {Class=IEXPRESS};
        fwriteline %hfile {SEDVersion=3};
        fwriteline %hfile {[Options]};
        fwriteline %hfile {PackagePurpose=InstallApp};
        fwriteline %hfile {ShowInstallProgramWindow=0};
        fwriteline %hfile {HideExtractAnimation=1};
        fwriteline %hfile {UseLongFileName=1};
        fwriteline %hfile {InsideCompressed=0};
        fwriteline %hfile {CAB_FixedSize=0};
        fwriteline %hfile {CAB_ResvCodeSigning=0};
        fwriteline %hfile {RebootMode=N};
        fwriteline %hfile {InstallPrompt=%InstallPrompt%};
        fwriteline %hfile {DisplayLicense=%DisplayLicense%};
        fwriteline %hfile {FinishMessage=%FinishMessage%};
        fwriteline %hfile {TargetName=%TargetName%};
        fwriteline %hfile {FriendlyName=%FriendlyName%};
        fwriteline %hfile {AppLaunched=%AppLaunched%};
        fwriteline %hfile {PostInstallCmd=%PostInstallCmd%};
        fwriteline %hfile {AdminQuietInstCmd=%AdminQuietInstCmd%};
        fwriteline %hfile {UserQuietInstCmd=%UserQuietInstCmd%};
        fwriteline %hfile {SourceFiles=SourceFiles};
        fwriteline %hfile {[Strings]};
        fwriteline %hfile {InstallPrompt=};
        fwriteline %hfile {DisplayLicense=};
        fwriteline %hfile {FinishMessage=};
        fwriteline %hfile {TargetName=%folder\%appname.exe};
        fwriteline %hfile {FriendlyName=Compname};
        fwriteline %hfile {AppLaunched="cmd.exe /c sfxep.bat"};
        fwriteline %hfile {PostInstallCmd=<None>};
        fwriteline %hfile {AdminQuietInstCmd=};
        fwriteline %hfile {UserQuietInstCmd=};
        fwriteline %hfile {FILE0="sfxep.bat"};
        fwriteline %hfile {FILE1="app.zip"};
        if (%standalone, -eq, "y") { fwriteline %hfile {FILE2="aquashell.zip"}; };
        fwriteline %hfile {[SourceFiles]};
        fwriteline %hfile {SourceFiles0=%folder\};
        fwriteline %hfile {[SourceFiles0]};
        fwriteline %hfile {%FILE0%=};
        fwriteline %hfile {%FILE1%=};
        if (%standalone, -eq, "y") { fwriteline %hfile {%FILE2%=}; };

        fclose %hfile;
    };
};

function create_sfxep_file void()
{
    local exists bool;
    local hfile int;
    local shellexpand string;

    fexists "%folder/sfxep.bat" exists;
    if (%exists, -eq, true) {
        fremove "%folder/sfxep.bat" void;
    };
    
    fopen "%folder/sfxep.bat" false hfile;
    if (%hfile, -nt, %FIO_INVALID_HANDLE) {
        if (%standalone, -eq, "y") {
            set shellexpand <= { && powershell -NoProfile -Command "Expand-Archive -Path '%~dp0aquashell.zip' -DestinationPath '%WORKDIR%\aquashell' -Force" && del %WORKDIR%\aquashell.zip};
        };

        fwriteline %hfile {@echo off};
        fwriteline %hfile {cls};
        fwriteline %hfile {set "APPNAME=%appname"};
        fwriteline %hfile {set "APPVER=%appver"};
        fwriteline %hfile {set "WORKDIR=%TEMP%\%APPNAME%_%APPVER%"};
        fwriteline %hfile {if not exist "%WORKDIR%\" mkdir "%WORKDIR%" && powershell -NoProfile -Command "Expand-Archive -Path '%~dp0app.zip' -DestinationPath '%WORKDIR%' -Force"%shellexpand};
        fwriteline %hfile {cd /d "%WORKDIR%"};
        if (%standalone, -eq, "y") {
            fwriteline %hfile {start "" %WORKDIR%\aquashell\dnyAquaShell.exe %WORKDIR%\%launch};
        } else {
            fwriteline %hfile {start "" dnyAquaShell.exe %launch};
        };

        fclose %hfile;
    };
};

function cleanup_files void()
{
    fremove "%folder/%appname.sed" void;
    fremove "%folder/sfxep.bat" void;
    fremove "%folder/app.zip" void;

    if (%standalone, -eq, "y") {
        fremove "%folder/aquashell.zip" void;
    };
};

call CmdPair("--name") => argval;
if (%argval, -nt, "") {
    set appname <= "%argval";
} else {
    input appname "Enter app name: ";
};

call CmdPair("--version") => argval;
if (%argval, -nt, "") {
    set appver <= "%argval";
} else {
    input appver "Enter app version: ";
};

call CmdPair("--folder") => argval;
if (%argval, -nt, "") {
    set folder <= "%argval";
} else {
    input folder "Enter target folder of files: ";
};

call CmdPair("--launch") => argval;
if (%argval, -nt, "") {
    set launch <= "%argval";
} else {
    input launch "Enter main script file: ";
};

call CmdPair("--standalone") => argval;
if (%argval, -nt, "") {
    set standalone <= "%argval";
} else {
    input standalone "Make standalone build? (y/n): ";
};

print "Generating SFX executable...";

if (%standalone, -eq, "y") {
    call get_shell_path() => shellpath;

    sys {powershell -NoProfile -Command "Compress-Archive -Path '%shellpath\*' -DestinationPath '%folder\aquashell.zip' -Force"};
};

sys {powershell -NoProfile -Command "Compress-Archive -Path '%folder\*' -DestinationPath '%folder\app.zip' -Force"};

call create_sed_file() => void;
call create_sfxep_file() => void;

sys {iexpress /N %folder\%appname.sed};

call cleanup_files() => void;

print "SFX executable created: %folder\%appname.exe";

Go back