Purpose of Startup.pps, valid usage scenarios

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Purpose of Startup.pps, valid usage scenarios

Post by ALIENQuake »

Hi,

It's not clear for me what's is the purpose of Startup.pps?

Which of below scenarios are valid usage scenarios to be executed from this file:
- prevent launching of the app if application directory is blacklisted
- checking if new app version is available
- updating application before main form is displayed
- downloading extra tools
- handling launching the app with commaindline (case of file association)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Purpose of Startup.pps, valid usage scenarios

Post by jvierra »

Sapien documentation defines "startup.pss" this way:
An empty project contains a single file called Startup.pss. It is used to create a script application. This script executes when the project is executed and is a great place do any preparatory work before calling other scripts in the project.
This executes as the first code before any other code in a project. What you use it for depends only on the overall design of your project.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Purpose of Startup.pps, valid usage scenarios

Post by jvierra »

To expand on the above (since I couldn't find a Sapien blog on this) the following is how the code gets assembled when a project is executed or built.

This is the startup.pss code that is generated:

Code: Select all

#region Source: Startup.pss
#----------------------------------------------
#region Import Assemblies
#----------------------------------------------
#endregion Import Assemblies

#Define a Param block to use custom parameters in the project
#Param ($CustomParameter)

function Main {
<#
    .SYNOPSIS
        The Main function starts the project application.
    
    .PARAMETER Commandline
        $Commandline contains the complete argument string passed to the script packager executable.
    
    .NOTES
        Use this function to initialize your script and to call GUI forms.
		
    .NOTES
        To get the console output in the Packager (Forms Engine) use: 
		$ConsoleOutput (Type: System.Collections.ArrayList)
#>
    Param ([String]$Commandline)
    
    #--------------------------------------------------------------------------
    #TODO: Add initialization script here (Load modules and check requirements)
    
    
    #--------------------------------------------------------------------------
    
    if ((Show-MainForm_psf) -eq 'OK') {
        
    }
    
    $script:ExitCode = 0 #Set the exit code for the Packager
}

#endregion Source: Startup.pss

Note that it becomes a function called "Main". This is the first executable line of the generated script:

Code: Select all

#Start the application
Main ($CommandLine)
Prior to that the "globals.ps1" file is inserted at the "global" scope so the code in "globals.ps1" is executed before "startup.ps1" and all code defined in the script run or exist at a global scope. Normally "globals.ps1" is used to define global variables and functions. Variables and functions defined in "startup.ps1" will not become global unless they are defined as global.

"startup.pss" also calls the startup script for the project which would be the main form in a forms project. In a script project it calls any initial script or sequence of scripts you choose to define in the file.
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Re: Purpose of Startup.pps, valid usage scenarios

Post by ALIENQuake »

jvierra wrote: Wed Jul 31, 2019 10:14 am Prior to that the "globals.ps1" file is inserted at the "global" scope so the code in "globals.ps1" is executed before "startup.ps1" and all code defined in the script run or exist at a global scope.
Are you sure? The default "Build Order" for new From Project is:
Startup.pps - 0
Global.ps1 - 1
MainForm.psf - 2

Very simple debbuging via

Code: Select all

[System.Windows.Forms.MessageBox]::Show("startup - before main - $ScriptDirectory")
shows that $ScriptDirectory is empty.

I had to change default "Build Order" preference in order to get the 'global.ps1 first'.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Purpose of Startup.pps, valid usage scenarios

Post by jvierra »

That will not change the final component. "Globals" will always be built at the root. Dumping the PS1 will show that to always be true no matter which one you set to be processed first. All scripts and globals will b e inserted into the script and, lastly, "main" will be called. This behavior cannot be changed.

"$ScriptDirectory" will only be set after main is called.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked