-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dev-Assist.psm1
172 lines (146 loc) · 5 KB
/
Dev-Assist.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<#
.SYNOPSIS
Retrieves the root cert from a website
.PARAMETER Url
The Url of the website
.EXAMPLE
$cert = Get-SiteRootCert -Url 'https://www.google.com'
#>
function Get-SiteRootCert
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]
$Url
)
$webRequest = [Net.WebRequest]::Create($Url)
try
{
$webRequest.GetResponse().Dispose()
}
catch [System.Net.WebException]
{
}
$cert = $webRequest.ServicePoint.Certificate
$chain = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Chain
$null = $chain.Build($cert)
$chain.ChainElements |Foreach-Object {$_.Certificate} |Select-Object -Last 1
}
<#
.SYNOPSIS
Adds a X509Certificate to git trusted root certs
.PARAMETER Certificate
The certificate to add
.PARAMETER CrtPath
The path to the crt file to update
.EXAMPLE
$cert = Get-SiteRootCert -Url 'https://www.google.com'
Add-CertToGit -Certificate $cert
.EXAMPLE
$cert = Get-SiteRootCert -Url 'https://www.google.com'
Add-CertToGit -Certificate $cert -CrtPath 'C:\Certs\MyCerts.crt'
#>
function Add-CertToGit
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[X509Certificate]
$Certificate,
[Parameter()]
[ValidateScript({Test-Path -Path $_ })]
[ValidateScript({(Get-Item -Path $_).Extension -eq '.crt'})]
[string]
$CrtPath
)
if(Test-Path 'C:\Program Files\Git')
{
$x64 = $true
}
if(Test-Path 'C:\Program Files (x86)\Git')
{
$x32 = $true
}
if( -not $x32 -and -not $x64)
{
throw 'This command requires Git'
}
if($CrtPath)
{
$crtInfo = Get-Item $CrtPath
}
else
{
if($x64)
{
$crtInfo = Copy-Item 'C:\Program Files\Git\mingw64\ssl\certs\ca-bundle.crt' -Destination $Env:USERPROFILE -PassThru
}
elseif($x32)
{
$crtInfo = Copy-Item 'C:\Program Files (x86)\Git\mingw32\ssl\certs\ca-bundle.crt' -Destination $Env:USERPROFILE -PassThru
}
}
$certBegin = '-----BEGIN CERTIFICATE-----'
$rootBase64 = [convert]::ToBase64String($Certificate.RawData)
$certEnd = '-----END CERTIFICATE-----'
Add-Content -Path $crtInfo.FullName -Value "`n$certBegin`n$rootBase64`n$certEnd" -NoNewline
git config --global http.sslCAInfo $crtInfo.FullName
}
<#
.SYNOPSIS
Ensures that environment Path variables are unique and properly formatted
.EXAMPLE
$null = Optimize-PathVariables
#>
function Optimize-PathVariables
{
# System
$vars = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
$pathSplit = $vars -split ';' | Foreach-Object {$_.trim()} | Where-Object {$_} | Sort-Object -Unique
[Environment]::SetEnvironmentVariable('Path', $pathSplit -join ';', [System.EnvironmentVariableTarget]::Machine)
# User
$vars = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::User)
$pathSplit = $vars -split ';' | Foreach-Object {$_.trim()} | Where-Object {$_} | Sort-Object -Unique
[Environment]::SetEnvironmentVariable('Path', $pathSplit -join ';', [System.EnvironmentVariableTarget]::User)
# Session
$pathVar = $env:Path
$pathSplit = $pathVar -split ';' | Foreach-Object {$_.trim()} | Where-Object {$_} | Sort-Object -Unique
$env:Path = $pathSplit -join ';'
}
<#
.SYNOPSIS
Returns current packages that do not support the target framework
.PARAMETER RepoRootPath
The root path to the repository under test
.PARAMETER TargetFramework
The destination framework for the upgrade
.EXAMPLE
$uP = Get-UnsupportedPackages -RepoRootPath 'C:\MyRepo' -TargetFramework 'netstandard2.0'
#>
function Get-UnsupportedPackages
{
param
(
[Parameter(Mandatory = $true)]
$RepoRootPath,
[Parameter(Mandatory = $true)]
[string]
$TargetFramework
)
#Find the Packages Folder
$packagesFolder = (Get-ChildItem $RepoRootPath -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -eq "Packages"}).FullName
#Find all package.configs in the repo
$packageConfigs = (Get-ChildItem $RepoRootPath -recurse | Where-Object {$_.Name -eq "Packages.config"}).FullName
#Get package ids (e.g. Serilog.2.7.1) to build folder paths from
$packageIds = ($packageConfigs | ForEach-Object {[xml](Get-Content $_) | ForEach-Object {$_.packages.ChildNodes | ForEach-Object {"$($_.id).$($_.version)"}}}) | select -Unique
#Return packages that don't support the target framework
$packageIds | ForEach-Object -Process {
if(Test-Path -Path (Join-Path -Path $packagesFolder -ChildPath $_))
{
if(-not (Test-Path -Path (Join-Path -Path $packagesFolder -ChildPath "$($_)\lib\$TargetFramework"))) { $_}
}
}
}