-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwshrun.psm1
154 lines (132 loc) · 3.98 KB
/
pwshrun.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
. "$PSScriptRoot/command.ps1"
$modules = @{}
$settingsPath = "~\.pwshrun.json"
if (!(Test-Path $settingsPath)) {
Write-Host "PwshRun: Initializing..."
Write-Host "PwshRun: Creating $settingsPath"
@{} | ConvertTo-Json | Set-Content $settingsPath
}
function PrePromptWithHooks {
$global:PwshRunPrompt.hooks.Values | ForEach-Object { & $_ }
}
function PromptWithHooks {
PrePromptWithHooks
& $global:PwshRunPrompt.oldPrompt
}
function Create-PromptHooks {
$promptConfig = Get-Variable -Name "PwshRunPrompt" -Scope Global -ValueOnly -ErrorAction SilentlyContinue
if (!$promptConfig) {
$promptConfig = @{
"hooks" = @{}
}
Set-Variable -Name "PwshRunPrompt" -Value $promptConfig -Scope Global
if (Test-Path "variable:PrePrompt") {
# Cmder sets up its own prompt with a "PrePrompt" script block that we can use
$global:PrePrompt = { PrePromptWithHooks }
} else {
$promptConfig.oldPrompt = (Get-Item -Path "function:prompt").ScriptBlock
Set-Item -Path "function:prompt" -Value PromptWithHooks
}
}
}
function Reset-PromptHooks {
if ($global:PwshRunPrompt) {
if ($global:PwshRunPrompt.oldPrompt) {
Set-Item -Path "function:prompt" -Value $global:PwshRunPrompt.oldPrompt
}
Remove-Variable -Name "PwshRunPrompt" -Scope Global
}
}
<#
.Synopsis
Loads the PwshRun settings file that contains all the runner definitions
#>
function Load-Settings {
$settings = @{}
if (!(Test-Path -Path $settingsPath)) {
Write-Error "Missing settings file $settingsPath"
} else {
$settings = Get-Content $settingsPath | ConvertFrom-Json -AsHashtable
}
return $settings
}
<#
.Synopsis
Creates dynamic runner modules - one module for each runner - with the pwshrun-bootstrap.ps1 script
loading the runner tasks.
#>
function Create-Modules {
Create-PromptHooks
$settings = Load-Settings
$settings.Keys | ForEach-Object {
$alias = $_
$options = $settings[$alias]
$moduleName = "pwshrun-$alias"
$module = New-Module -Name $moduleName -ArgumentList @($alias, $options) -ScriptBlock {
Param(
[string] $alias,
$options
)
. "$PSScriptRoot/pwshrun-bootstrap.ps1"
}
Import-Module -Global -Force $module
$modules[$moduleName] = $module
}
}
<#
.Synopsis
Creates a new PwshRun runner with the given name by adding a new runner definition to the settings file.
#>
function New-PwshRunner {
Param(
[string] $alias
)
$settings = Load-Settings
$settings[$alias] = @{
"load" = @("`$PWSHRUN_HOME\utility")
}
$settings | ConvertTo-Json | Set-Content $settingsPath
$runnerSettingsPath = "~/.pwshrun.$alias.json"
@{
"locations" = @{
"windir" = $env:WINDIR
}
} | ConvertTo-Json | Set-Content $runnerSettingsPath
Invoke-Expression $runnerSettingsPath
Reset-PwshRunModules
}
<#
.Synopsis
Removes all runner modules from the current session.
#>
function Uninstall-PwshRunModules {
$modules.Keys | ForEach-Object {
Remove-Module $_
}
$modules = @{}
Reset-PromptHooks
}
<#
.Synopsis
Reloads all runner modules, refreshing the settings for each.
#>
function Reset-PwshRunModules {
Uninstall-PwshRunModules
Create-Modules
}
function Invoke-PwshRunCommand {
param(
[Parameter(Mandatory=$true)]
[PSObject] $command
)
if ($command.Runner) {
& "Invoke-PwshRunCommandIn$((Get-Culture).TextInfo.ToTitleCase($command.Runner))" $command
} else {
Invoke-PwshRunCommandInternal $command
}
}
Export-ModuleMember -Function Uninstall-PwshRunModules,Reset-PwshRunModules,New-PwshRunner,New-PwshRunCommand,Invoke-PwshRunCommand,Push-PwshRunCommand,Pop-PwshRunCommand
$ExecutionContext.SessionState.Module.OnRemove += {
Uninstall-PwshRunModules
}
Create-Modules