-
Notifications
You must be signed in to change notification settings - Fork 9
/
install.ps1
89 lines (65 loc) · 2.85 KB
/
install.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
;$moduleToLoad=$module;
$RepoName = 'PowershellScripts';
$tempFile= "$env:TEMP\$RepoName";
$ProfileModulePath = $env:PSModulePath.split(';')[0];
if (!(Test-Path $ProfileModulePath)) {
New-Item -ItemType Directory -Path $ProfileModulePath;
}
$moduleName= Split-Path $moduleToLoad -leaf
$moduleFolder= Join-Path $ProfileModulePath $moduleName
if (Test-Path $moduleFolder) {
throw "Unable to install module ''$moduleName''.
Directory with the same name alredy exist in the Profile directory.
Please rename exisitng module folder and try again.
"
}
$client = New-Object System.Net.WebClient
$url = [uri]"https://github.com/stadub/$RepoName/archive/master.zip"
$file = "${tempFile}.zip"
try {
$progressEventArgs = @{
InputObject = $client
EventName = 'DownloadProgressChanged'
SourceIdentifier = 'ModuleDownload'
Action = {
Write-Progress -Activity "Module Installation" -Status `
("Downloading Module: {0} of {1}" -f $eventargs.BytesReceived, $eventargs.TotalBytesToReceive) `
-PercentComplete $eventargs.ProgressPercentage
}
}
$completeEventArgs = @{
InputObject = $client
EventName = 'DownloadFileCompleted'
SourceIdentifier = 'ModuleDownloadCompleted'
}
Register-ObjectEvent @progressEventArgs
Register-ObjectEvent @completeEventArgs
$client.DownloadFileAsync($url, $file)
Wait-Event -SourceIdentifier ModuleDownloadCompleted
}
catch [System.Net.WebException]
{
Write-Host("Cannot download $url")
}
finally {
$client.dispose()
Unregister-Event -SourceIdentifier ModuleDownload
Unregister-Event -SourceIdentifier ModuleDownloadCompleted
}
#avoid errors on already existing file
try {
Unblock-File -Path "${tempFile}.zip";
Write-Progress -Activity "Module Installation" -Status "Unpacking Module" -PercentComplete 0
Add-Type -AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::ExtractToDirectory("${tempFile}.zip", "${tempFile}");
}
catch { }
Write-Progress -Activity "Module Installation" -Status "Unpacking Module" -PercentComplete 40
Write-Progress -Activity "Module Installation" -Status "Copy Module to PowershellModules folder" -PercentComplete 50
Move-Item -Path "${tempFile}\$RepoName-master\$moduleToLoad" -Destination "$ProfileModulePath"
Write-Progress -Activity "Module Installation" -Status "Copy Module to PowershellModules folder" -PercentComplete 60
Write-Progress -Activity "Module Installation" -Status "Finishing Installation and Cleanup " -PercentComplete 80
Remove-Item "${tempFile}*" -Recurse -ErrorAction SilentlyContinue;
Write-Progress -Activity "Module Installation" -Status "Module installed sucessaful "
Write-Host "Module installation complete"
Write-Host "Use ''Import-Module $module'' to start using module"