Changes between Initial Version and Version 1 of Programming/PowerShell/DotSourcingScriptsQuirks


Ignore:
Timestamp:
Feb 19, 2016, 12:57:30 PM (9 years ago)
Author:
Vijay Varadan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Programming/PowerShell/DotSourcingScriptsQuirks

    v1 v1  
     1= !PowerShell: Dot Sourcing Scripts Quirks =
     2Jan 05 2016
     3
     4[[Image(htdocs:images/powershell/PowerShell_5.0_icon.png, alt="PowerShell", align=center)]]
     5
     6I spent the last couple of days of 2015 porting my [Programming/Python/OrganizationOfBuildEnvironmentScripts build environment scripts] (recommend that you read that article first) over to !PowerShell. I ran into some rather quirky and inconsistent behavior when dot-sourcing scripts.
     7
     8I wanted to stick to the same organization and file layout that I had for the MSYS and Linux platforms. Essentially, something like this:
     9{{{#!html
     10<pre>~vijay
     11    |-<span style="color: #800000;">$PROFILE ![1]</span>
     12    |-work
     13      |-repos
     14        |-0
     15        |-1
     16          |-private
     17            |-bin
     18              |-<span style="color: #0000ff;">devenv.ps1 ![3]</span>
     19            |-developer
     20              |-bin
     21              | |-<span style="color: #800080;">devenv.ps1 ![4]</span>
     22              |-project1
     23                |-<span style="color: #ff6600;">project1.ps1 ![5]</span>
     24            |-artist
     25              |-bin
     26              | |-devenv.ps1
     27              |-project1
     28                |-project1.ps1
     29            |-vijay
     30              |-bin
     31                |-<span style="color: #008000;">devenv.ps1 ![2]</span></pre>
     32}}}
     33
     341. [[span(style=color: #800000, $PROFILE ![1])]] contains the Start-Dev() function
     351. [[span(style=color: #008000, devenv.ps1 ![2])]] user specific overrides go here
     361. [[span(style=color: #0000ff, devenv.ps1 ![3])]] common to all projects, users and roles, like build / install directories
     371. [[span(style=color: #800080, devenv.ps1 ![4])]] role specific settings go here
     381. [[span(style=color: #ff6600, project1.ps1 ![5])]] project and sub-project (if any) specific settings go here
     39
     40So, I started off with the 1:1 port attempt with necessary adjustments to naming conventions (camel case for variables, title case for functions, etc). So, the [[span(style=color: #800000, user profile script ![1])]] has a `Start-Dev()` function which is the equivalent of `startdev()` in .bashrc. Just like it works for the MSYS platform, this function in turn dot-sources the user specific [[span(style=color: #008000, devenv.ps1 ![2])]]. [[span(style=color: #008000, devenv.ps1 ![2])]] dot-sources the common [[span(style=color: #0000ff, devenv.ps1 ![3])]] and the role specific (in this case, developer) [[span(style=color: #800080, devenv.ps1 ![4])]], which dot-sources the project specific [[span(style=color: #ff6600, project1.sh ![5])]] for that role.
     41
     42I expected things would work fine, but they didn't. It was a tad difficult to debug initially, since the environment variables were getting set and I could see them when I invoked the `Start-Dev()` function, but functions and aliases that were setup in the dot-sourced PS1 files were missing from the !PowerShell environment. The inconsistency flummoxed me for a bit. So, I threw in some Write-Host statements immediately following the lines where the functions and aliases were defined and they were fine. Similarly, they existed in the caller, cascading upwards all the way to the initial dot-sourcing that happens in the `Start-Dev()` function.
     43
     44First I tried replacing the dot-sourcing with `Invoke-Expression`, but as I expected based on the documentation, that didn't work either. The behavior was inconsistent - environment variables were set / updated correctly, but function and alias definitions just disappeared, poof! I tried !PowerShell modules instead of scripts to see if that fixed the problem, but no dice. The solution I ended up with was to create a `Start-Dev.ps1` script with the contents of the function dumped into the script's global space. I dot-sourced the script like this:
     45{{{
     46PS C:\Users\vijay> . Start-Dev 1 project1
     47}}}
     48That did the trick. All functions and aliases defined in the dot-sourced files became defined and available in the !PowerShell environment. I wrapped up the porting work by adding an alias to simplify invocation of the Start-Dev.ps1 script.
     49