forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chocolateyInstall.ps1
148 lines (119 loc) · 4.41 KB
/
chocolateyInstall.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
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
[CmdletBinding()]
param ( )
end
{
$modulePath = Join-Path $env:ProgramFiles WindowsPowerShell\Modules
$targetDirectory = Join-Path $modulePath Pester
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$sourceDirectory = Join-Path $scriptRoot Tools
Update-Directory -Source $sourceDirectory -Destination $targetDirectory
if ($PSVersionTable.PSVersion.Major -lt 4)
{
$modulePaths = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') -split ';'
if ($modulePaths -notcontains $modulePath)
{
Write-Verbose "Adding '$modulePath' to PSModulePath."
$modulePaths = @(
$modulePath
$modulePaths
)
$newModulePath = $modulePaths -join ';'
[Environment]::SetEnvironmentVariable('PSModulePath', $newModulePath, 'Machine')
$env:PSModulePath += ";$modulePath"
}
}
}
begin
{
function Update-Directory
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $Source,
[Parameter(Mandatory = $true)]
[string] $Destination
)
$Source = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Source)
$Destination = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Destination)
if (-not (Test-Path -LiteralPath $Destination))
{
$null = New-Item -Path $Destination -ItemType Directory -ErrorAction Stop
}
try
{
$sourceItem = Get-Item -LiteralPath $Source -ErrorAction Stop
$destItem = Get-Item -LiteralPath $Destination -ErrorAction Stop
if ($sourceItem -isnot [System.IO.DirectoryInfo] -or $destItem -isnot [System.IO.DirectoryInfo])
{
throw 'Not Directory Info'
}
}
catch
{
throw 'Both Source and Destination must be directory paths.'
}
$sourceFiles = Get-ChildItem -Path $Source -Recurse |
Where-Object { -not $_.PSIsContainer }
foreach ($sourceFile in $sourceFiles)
{
$relativePath = Get-RelativePath $sourceFile.FullName -RelativeTo $Source
$targetPath = Join-Path $Destination $relativePath
$sourceHash = Get-FileHash -Path $sourceFile.FullName
$destHash = Get-FileHash -Path $targetPath
if ($sourceHash -ne $destHash)
{
$targetParent = Split-Path $targetPath -Parent
if (-not (Test-Path -Path $targetParent -PathType Container))
{
$null = New-Item -Path $targetParent -ItemType Directory -ErrorAction Stop
}
Write-Verbose "Updating file $relativePath to new version."
Copy-Item $sourceFile.FullName -Destination $targetPath -Force -ErrorAction Stop
}
}
$targetFiles = Get-ChildItem -Path $Destination -Recurse |
Where-Object { -not $_.PSIsContainer }
foreach ($targetFile in $targetFiles)
{
$relativePath = Get-RelativePath $targetFile.FullName -RelativeTo $Destination
$sourcePath = Join-Path $Source $relativePath
if (-not (Test-Path $sourcePath -PathType Leaf))
{
Write-Verbose "Removing unknown file $relativePath from module folder."
Remove-Item -LiteralPath $targetFile.FullName -Force -ErrorAction Stop
}
}
}
function Get-RelativePath
{
param ( [string] $Path, [string] $RelativeTo )
return $Path -replace "^$([regex]::Escape($RelativeTo))\\?"
}
function Get-FileHash
{
param ([string] $Path)
if (-not (Test-Path -LiteralPath $Path -PathType Leaf))
{
return $null
}
$item = Get-Item -LiteralPath $Path
if ($item -isnot [System.IO.FileSystemInfo])
{
return $null
}
$stream = $null
try
{
$sha = New-Object System.Security.Cryptography.SHA256CryptoServiceProvider
$stream = $item.OpenRead()
$bytes = $sha.ComputeHash($stream)
return [convert]::ToBase64String($bytes)
}
finally
{
if ($null -ne $stream) { $stream.Close() }
if ($null -ne $sha) { $sha.Clear() }
}
}
}