-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloki.ps1
113 lines (94 loc) · 2.87 KB
/
loki.ps1
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
$Global:__loki = @{}
function Register-LokiFile {
function Get-LokiFile {
$currentDir = Get-Location
$lokiFiles = @(Resolve-Path (Join-Path $currentDir ".loki*") -ErrorAction SilentlyContinue)
if($lokiFiles.Length -eq 0) { return $null }
return $lokiFiles[0]
}
$lokiFile = Get-LokiFile
if(!$lokiFile) { return }
if($Global:__loki.current -and !($Global:__loki.current -eq $lokiFile)) {
if($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) {
Write-Host "Removing loki config file: $($Global:__loki.current)"
}
Remove-Module -Name $Global:__loki.current -Force -ErrorAction SilentlyContinue
$Global:__loki.current = $null
}
$Global:__loki.current = "$lokiFile"
if($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) {
Write-Host "Importing loki config file: $lokiFile"
}
$module = New-Module -Name "$lokiFile" -ScriptBlock {}
. $module $lokiFile
Import-Module $module
}
function Add-LokiFile {
[CmdletBinding()]
param([Parameter()] [string] $path)
$filePath = Join-Path $path ".loki.ps1"
if(Test-Path $filePath) {
Write-Output "Loki file already exists"
return
}
$contents = @"
function Out-HelloWorld {
Write-Host "Hello, World!"
}
Export-ModuleMember -Function Out-HelloWorld
"@
$contents | Out-File $filePath -Encoding utf8
}
<#
.ForwardHelpTargetName Set-Location
.ForwardHelpCategory Cmdlet
#>
function Set-Location {
[CmdletBinding(DefaultParameterSetName='Path', SupportsTransactions=$true, HelpUri='http://go.microsoft.com/fwlink/?LinkID=113397')]
param(
[Parameter(ParameterSetName='Path', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
${Path},
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath')]
[string]
${LiteralPath},
[switch]
${PassThru},
[Parameter(ParameterSetName='Stack', ValueFromPipelineByPropertyName=$true)]
[string]
${StackName})
begin
{
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Set-Location', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
Register-LokiFile
}
}