This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinstall-provider.ps1
72 lines (58 loc) · 2.3 KB
/
install-provider.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
#.Synopsis
# Copy the provider assembly to LocalAppData
#.Description
# By default it will find the newest .*Provider.dll and copy it to the right location
# You can also specify the build name (e.g. 'debug' or 'release') to use.
[CmdletBinding()]
param(
# If specified, force to use the output in a specific build folder
[string]$build = '',
[string]$providerName = 'ChocolateyProvider',
# How many levels UP to check for output folders (defaults to 2)
$depth = 2
)
Push-Location $PSScriptRoot
while($depth-- -gt 0) {
Write-Verbose "Searching '$pwd' for '${providerName}.dll'"
$candidates = Get-ChildItem -Recurse -Filter "${providerName}.dll" |
Where-Object { $_.FullName -match "\\output\\" }
Sort-Object -Descending -Property LastWriteTime
if( $build ) {
$candidates = $candidates | Where-Object { $_.FullName -match $build }
}
$provider = $candidates | Select-Object -First 1
if( -not $provider ) {
cd ..
}
}
if( -not $provider ) {
Write-Error "Can't find assembly '${providerName}.dll' under '$Pwd' with '\output\' and '$build' somewhere in the path`n"
Pop-Location
return
}
$srcpath = $provider.Fullname
$filename = $provider.Name
$output = "${Env:LocalAppData}\PackageManagement\providerassemblies\$fileName"
if(Test-Path $output) {
Write-Warning "Found existing provider. Deleting `n '$output' `n"
# delete the old provider assembly
# even if it's in use.
$tmpName = "$filename.delete-me.$(get-random)"
$tmpPath = "$env:localappdata\PackageManagement\providerassemblies\$tmpName"
Rename-Item $output $tmpName -force -ea SilentlyContinue
Remove-Item -force -ea SilentlyContinue $tmpPath
if(Test-Path $tmpPath) {
# locked. Move to temp folder
Write-Warning "Old provider is in use, moving to `n '$env:TEMP' folder `n However, you must restart any processes using it!"
Move-Item $tmpPath $env:TEMP
}
if(Test-Path $output) {
Write-Error "Cannot remove existing provider file: `n $output `n"
Pop-Location
return
}
}
Write-Host "Copying provider `nFrom: '$srcPath'`nTo: '$output' `n"
Copy-Item -force $srcPath $output
Pop-Location