diff --git a/.github/workflows/powershell-validation.yml b/.github/workflows/powershell-validation.yml index 7ef7af4..8484d67 100644 --- a/.github/workflows/powershell-validation.yml +++ b/.github/workflows/powershell-validation.yml @@ -17,6 +17,7 @@ jobs: run: | ./tests/Test-Source.ps1 ./tests/Test-Workspaces.ps1 + ./tests/Test-SessionContext.ps1 ./tests/Test-CatalogueBridge.ps1 validate-powershell-7: @@ -30,4 +31,5 @@ jobs: run: | ./tests/Test-Source.ps1 ./tests/Test-Workspaces.ps1 + ./tests/Test-SessionContext.ps1 ./tests/Test-CatalogueBridge.ps1 diff --git a/ProjectShell.Workspaces.ps1 b/ProjectShell.Workspaces.ps1 index 248d554..94d52a3 100644 --- a/ProjectShell.Workspaces.ps1 +++ b/ProjectShell.Workspaces.ps1 @@ -421,6 +421,7 @@ function Start-ProfileLaunchable($Profile) { Write-Host 'A standard profile launch cannot run from an elevated session.' -ForegroundColor Yellow return } + Set-ProjectShellLaunchContext $Profile Invoke-SavedTool (Get-ProfileLaunchable $Profile) ([string]$Profile.StartDirectory) } diff --git a/ProjectShell.ps1 b/ProjectShell.ps1 index 17a8b59..e86107b 100644 --- a/ProjectShell.ps1 +++ b/ProjectShell.ps1 @@ -900,6 +900,17 @@ function Set-ProjectTitle([string]$Name) { try { $Host.UI.RawUI.WindowTitle = $Name } catch { } } +function Set-ProjectShellLaunchContext($entry) { + # Process-local handoff only. Descendant tools (for example GPB) can offer + # this saved starting folder as an editable default even after cd elsewhere. + # Do NOT persist a user/machine environment variable or disclose profiles.json. + $folder = [string]$entry.StartDirectory + if (-not (Test-Path -LiteralPath $folder -PathType Container)) { + throw ('ProjectShell configured starting folder is missing: ' + $folder) + } + $env:PROJECTSHELL_START_DIRECTORY = [IO.Path]::GetFullPath($folder) +} + function Invoke-ProjectTask($entry) { Set-ProjectTitle $entry.Name if (-not (Test-Path -LiteralPath $entry.StartDirectory -PathType Container)) { @@ -907,6 +918,7 @@ function Invoke-ProjectTask($entry) { return } Set-Location -LiteralPath $entry.StartDirectory + Set-ProjectShellLaunchContext $entry Write-Host '' Write-Host '============================================================' -ForegroundColor Cyan Write-Host (' ' + $entry.Name) -ForegroundColor Cyan @@ -947,6 +959,7 @@ function Invoke-ProjectTask($entry) { function Show-OpenProjectMenu([string]$Id) { while ($true) { $entry = Get-ProfileById $Id + Set-ProjectShellLaunchContext $entry Set-ProjectTitle $entry.Name Write-Host '' Write-Host (' ' + $entry.Name + ' | SESSION MENU') -ForegroundColor Cyan diff --git a/README.md b/README.md index 5d5f60b..59a1699 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,8 @@ ProjectShell keeps your settings on your own computer: | Optional Catalogue location and generated broker requests | `%LOCALAPPDATA%\ProjectShell\catalogue-integration.json` and `dependency-requests\` | | Created project shortcuts | The Windows desktop folder chosen during installation | +When a ProjectShell project session starts, it now exports the profile's saved `StartDirectory` as the **process-local** environment variable `PROJECTSHELL_START_DIRECTORY`. Child applications such as GitHub Package Builder can use this as an editable default even if you change the working directory. It is **not** a system-wide or persistent environment setting, and ProjectShell does not expose private profile JSON to applications. Older ProjectShell versions still set the initial working directory, which applications may use as a fallback. + Updating ProjectShell keeps your saved profiles, workspace registry, and startup commands in place. These personal files aren't meant to go in the public GitHub repository. **Don't upload your profiles, settings, logs, backups, or personal `.lnk` files**, since they may reveal local folders or commands. ## If a shortcut isn't working diff --git a/tests/Test-SessionContext.ps1 b/tests/Test-SessionContext.ps1 new file mode 100644 index 0000000..14540a5 --- /dev/null +++ b/tests/Test-SessionContext.ps1 @@ -0,0 +1,50 @@ +# Test only the process-local ProjectShell starting-folder handoff, not UI/install. +$ErrorActionPreference = 'Stop' +$sourcePath = Join-Path (Split-Path -Parent $PSScriptRoot) 'ProjectShell.ps1' +$tokens = $null +$errors = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile( + $sourcePath, [ref]$tokens, [ref]$errors +) +if ($errors.Count -ne 0) { throw 'Cannot parse ProjectShell source.' } +$fn = $ast.Find({ + param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $node.Name -eq 'Set-ProjectShellLaunchContext' +}, $true) +if (-not $fn) { throw 'ProjectShell session-context function is absent.' } + +# Import only this one function, avoiding loading profiles or invoking the manager. +. ([scriptblock]::Create($fn.Extent.Text)) +$key = 'PROJECTSHELL_START_DIRECTORY' +$before = [Environment]::GetEnvironmentVariable($key, 'Process') +$userBefore = [Environment]::GetEnvironmentVariable($key, 'User') +$machineBefore = [Environment]::GetEnvironmentVariable($key, 'Machine') + +try { + $target = Split-Path -Parent $PSScriptRoot + Set-ProjectShellLaunchContext ([pscustomobject]@{ StartDirectory = $target }) + $actual = [Environment]::GetEnvironmentVariable($key, 'Process') + if ([IO.Path]::GetFullPath($actual).TrimEnd('\') -ne [IO.Path]::GetFullPath($target).TrimEnd('\')) { + throw 'Process-local starting-folder handoff does not match the saved profile.' + } + $nonexistent = Join-Path $target ('No-Such-ProjectShell-Folder-' + [guid]::NewGuid().ToString('N')) + try { + Set-ProjectShellLaunchContext ([pscustomobject]@{ StartDirectory = $nonexistent }) + throw 'Invalid starting folder did not fail closed.' + } + catch { + if ($_.Exception.Message -match 'Invalid starting folder did not fail closed') { throw } + } + if ([Environment]::GetEnvironmentVariable($key, 'Process') -ne $actual) { + throw 'Invalid folder unexpectedly changed the existing process context.' + } + if ([Environment]::GetEnvironmentVariable($key, 'User') -ne $userBefore -or + [Environment]::GetEnvironmentVariable($key, 'Machine') -ne $machineBefore) { + throw 'Profile handoff wrote persistent User/Machine environment variables.' + } + Write-Host 'PROJECTSHELL_PROCESS_STARTDIRECTORY_HANDOFF_PASSED' -ForegroundColor Green +} +finally { + [Environment]::SetEnvironmentVariable($key, $before, 'Process') +} diff --git a/tests/Test-Source.ps1 b/tests/Test-Source.ps1 index 588dc1a..6600fc3 100644 --- a/tests/Test-Source.ps1 +++ b/tests/Test-Source.ps1 @@ -34,6 +34,7 @@ $checks = @( @{ Name='Catalogue preview only'; Pass=($catalogue.Contains('schema_version=1') -and $catalogue.Contains('dependency-request') -and -not $catalogue.Contains('--interactive-apply') -and -not $catalogue.Contains('Start-Process')) }, @{ Name='Workspace module installed'; Pass=($installer.Contains('ProjectShell.Workspaces.ps1') -and $app.Contains('. $script:WorkspaceModule')) }, @{ Name='Session tool launch and update menu'; Pass=($app.Contains('Start-ProfileLaunchable $entry') -and $app.Contains('Set-ProfileLaunchable $entry')) }, + @{ Name='Process-local ProjectShell folder handoff'; Pass=($app.Contains('function Set-ProjectShellLaunchContext') -and $app.Contains('$env:PROJECTSHELL_START_DIRECTORY') -and $app.Contains('Set-ProjectShellLaunchContext $entry') -and $workspace.Contains('Set-ProjectShellLaunchContext $Profile')) }, @{ Name='Workspace source transfer approval'; Pass=($workspace.Contains('Assert-CopiedFolder') -and $workspace.Contains('Confirm original deletion after verifying copied content?')) }, @{ Name='Tools stored independently from app'; Pass=($workspace.Contains('workspaces.json') -and $workspace.Contains('Save-WorkspaceStore')) }, @{ Name='Installers never auto-run'; Pass=($workspace.Contains('not executed') -and $workspace.Contains('Invoke-SavedTool')) }