-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHelperFunctions.ps1
103 lines (97 loc) · 3.39 KB
/
HelperFunctions.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
function Dump-InstalledPackages {
param($InstalledPackages)
$InstalledPackages | ConvertTo-Json |
Out-File $script:InstalledPackagesPath -Force
}
function Dump-RegisteredPackageSources {
$script:RegisteredPackageSources | Select * -ExcludeProperty Headers | ConvertTo-Json |
Out-File $script:RegisteredPackageSourcesPath -Force
}
function Get-PackageSources {
param(
[Parameter(Mandatory)]
$request
)
$Sources = if ($request.PackageSources) {
$script:RegisteredPackageSources | ? Name -in $request.PackageSources
} else { $script:RegisteredPackageSources }
$Sources | ? {-not $_.Headers} | % {
if ($request.Credential) {
Set-PackageSourcePrivateToken -Source $_.Name -Credential $request.Credential
} else {
$msg = "Credentials are required for source $($_.Name)"
Write-Error -Message $msg -ErrorId CredentialsNotSpecified -Category InvalidOperation -TargetObject $_.Name
}
}
$Sources
}
function Set-PackageSourcePrivateToken {
param(
[Parameter(Mandatory)]
[string[]] $Source,
[Parameter(Mandatory)]
[pscredential] $Credential
)
$Source | % {
$PackageSource = $script:RegisteredPackageSources | ? Name -eq $_
if (-not $PackageSource.Headers) {
if ($Credential.UserName -eq 'AuthToken') {
$PrivateToken = $Credential.GetNetworkCredential().Password
} else {
$Auth = @{
login = $Credential.UserName
password = $Credential.GetNetworkCredential().Password
}
$Location = $PackageSource.Location.TrimEnd('/')
$PrivateToken = (Invoke-RestMethod -Uri ($Location + '/session') -Method Post -Body $Auth).'private_token'
}
$Headers = @{
'PRIVATE-TOKEN' = $PrivateToken
#'SUDO' = 'root'
}
$PackageSource | Add-Member -MemberType NoteProperty -Name Headers -Value $Headers -TypeName hashtable
}
}
}
function ConvertTo-Hashtable {
param(
[Parameter(Mandatory,ValueFromPipeline)]
[array] $Object,
[int] $Depth = 3
)
Process {
foreach ($obj in $Object) {
if (!$Depth) { return $obj }
$ht = [ordered]@{}
if ($obj -as [hashtable]) {
($obj -as [hashtable]).GetEnumerator() | % {
if ($_.Value -is [PSCustomObject]) {
$ht[$_.Key] = ConvertTo-Hashtable ($_.Value) ($Depth - 1)
} else {
$ht[$_.Key] = $_.Value
}
}
return $ht
} elseif ($obj.GetType().Name -eq 'PSCustomObject') {
$obj | Get-Member -MemberType Properties | % {
$ht[$_.Name] = ConvertTo-Hashtable (,$obj.($_.Name)) ($Depth - 1)
}
return $ht
} elseif ($obj -as [array]) {
return ,$obj
} else {
return $obj
}
}
}
}
function Get-GitSubmodules {
param(
[Parameter(Mandatory)]
[ValidateScript({Test-Path $_})]
$Path
)
(Get-Content $Path -Raw).Split('[]') -ne '' | % -Begin { $i = 0 } {
if ($i++ % 2) { [PSCustomObject](ConvertFrom-StringData $_) }
}
}