diff --git a/scripts/Common/Software/aliae/Constants.ps1 b/scripts/Common/Software/aliae/Constants.ps1 new file mode 100644 index 00000000..0ab6f188 --- /dev/null +++ b/scripts/Common/Software/aliae/Constants.ps1 @@ -0,0 +1,15 @@ +<# + .SYNOPSIS + Gets a powershell expression which points to the global `aliae` configuration. +#> +function Get-GlobalConfigExpression { + return "`"$($IsWindows ? "`$env:ProgramData" : "/etc")/aliae/aliae.yml`""; +} + +<# + .SYNOPSIS + Gets the path to the global `aliae` configuration. +#> +function Get-GlobalConfigPath { + return & ([scriptblock]::Create((Get-GlobalConfigExpression))); +} diff --git a/scripts/Common/Software/aliae/Main.ps1 b/scripts/Common/Software/aliae/Main.ps1 index ffd1fe07..6419a760 100644 --- a/scripts/Common/Software/aliae/Main.ps1 +++ b/scripts/Common/Software/aliae/Main.ps1 @@ -3,13 +3,15 @@ param( [hashtable] $Arguments ) +. "$PSScriptRoot/Constants.ps1"; . "$PSScriptRoot/../PowerShell/Profile.ps1"; . "$PSScriptRoot/../../Scripts/Software.ps1"; Start-SoftwareInstaller @PSBoundParameters ` -Configurator { - $pathExpression = "`"$($IsWindows ? "`$env:ProgramData" : "/etc")/aliae/aliae.yml`""; - $path = & ([scriptblock]::Create($pathExpression)); + . "$PSScriptRoot/Constants.ps1"; + $pathExpression = Get-GlobalConfigExpression; + $path = Get-GlobalConfigPath; $null = New-Item -Force -ItemType Directory (Split-Path -Parent $path); Copy-Item -Force "$PSScriptRoot/aliae.yml" $path; diff --git a/scripts/Common/Software/aliae/Manage.ps1 b/scripts/Common/Software/aliae/Manage.ps1 new file mode 100644 index 00000000..4b65d3f5 --- /dev/null +++ b/scripts/Common/Software/aliae/Manage.ps1 @@ -0,0 +1,98 @@ +$null = New-Module { + . "$PSScriptRoot/Constants.ps1"; + + <# + .SYNOPSIS + Adds an alias to an existing `aliae` configuration. + + .PARAMETER Name + The name of the alias to add. + + .PARAMETER Value + The script the alias should point to. + + .PARAMETER User + The user to add the alias to. + #> + function Add-Alias { + param( + [string] $Name, + [string] $Value, + [string] $User + ) + + Edit-Config ` + -Variables @{ + Name = "$Name"; + Value = "$Value"; + } ` + ".alias |= . + [{ name: env.Name, value: env.Value }]" ` + -User $User; + } + + <# + .SYNOPSIS + Adds an environment variable to an existing `aliae` configuration. + + .PARAMETER Name + The name of the variable to add. + + .PARAMETER Value + The value of the variable. + #> + function Add-EnvironmentVariable { + param( + [string] $Name, + [string] $Value, + [string] $User + ) + + Edit-Config ` + -Variables @{ + Name = "$Name"; + Value = "$Value"; + } ` + ".env |= . + [{ name: env.Name, value: env.Value }]" ` + -User $User; + } + + <# + .SYNOPSIS + Edits the underlying `aliae` configuration. + + .PARAMETER Script + The yq script to run over the configuration. + + .PARAMETER User + The user to edit the configuration for. + #> + function Edit-Config { + param( + [string] $Script, + [hashtable] $Variables, + [string] $User + ) + + if ($User) { + $path = "$($IsWindows ? "~" : "$(sudo -u $User bash -c "realpath ~")")/.aliae.yaml"; + } else { + $path = Get-GlobalConfigPath; + } + + Start-Job { + $file = New-TemporaryFile; + $variables = $using:Variables; + + foreach ($key in $variables.Keys) { + Set-Item "Env:\$key" $variables[$key]; + } + + sudo -u $using:User cp $using:path $file; + yq -yi $using:Script $file; + sudo -u $using:User cp $file $using:path; + Remove-Item $file; + } | Receive-Job -Wait; + } + + Export-ModuleMember -Function Add-Alias; +};