forked from dfinke/InstallModuleFromGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallModuleFromGitHub.psm1
More file actions
144 lines (132 loc) · 4.82 KB
/
InstallModuleFromGitHub.psm1
File metadata and controls
144 lines (132 loc) · 4.82 KB
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
function Install-ModuleFromGitHub
{
[CmdletBinding()]
param(
$GitHubRepo,
$Branch = 'master',
[Parameter(ValueFromPipelineByPropertyName)]
$ProjectUri,
$DestinationPath,
$SSOToken,
$ModuleName,
[ValidateSet('CurrentUser', 'AllUsers')]
[string] $Scope = 'AllUsers'
)
Process
{
if($PSBoundParameters.ContainsKey('ProjectUri'))
{
$GitHubRepo = $null
if($ProjectUri.OriginalString.StartsWith('https://github.com'))
{
$GitHubRepo = $ProjectUri.AbsolutePath
}
else
{
$Name = $ProjectUri.LocalPath.split('/')[-1]
Write-Host -ForegroundColor Red ('Module [{0}]: not installed, it is not hosted on GitHub ' -f $Name)
}
}
if($GitHubRepo)
{
Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch)
$url = 'https://api.github.com/repos/{0}/zipball/{1}' -f $GitHubRepo, $Branch
if ($ModuleName)
{
$targetModuleName = $ModuleName
}
else
{
$targetModuleName = $GitHubRepo.split('/')[-1]
}
Write-Debug "targetModuleName: $targetModuleName"
$tmpDir = [System.IO.Path]::GetTempPath()
$OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip"
Write-Debug "OutFile: $OutFile"
if ($SSOToken)
{
$headers = @{'Authorization' = "token $SSOToken" }
}
#enable TLS1.2 encryption
if (-not ($IsLinux -or $IsMacOS))
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
Invoke-RestMethod $url -OutFile $OutFile -Headers $headers
if (-not ([System.Environment]::OSVersion.Platform -eq 'Unix'))
{
Unblock-File $OutFile
}
$fileHash = $(Get-FileHash -Path $OutFile).hash
$tmpDir = "$tmpDir/$fileHash"
Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force
$unzippedArchive = Get-ChildItem "$tmpDir"
Write-Debug "targetModule: $targetModule"
if($IsLinux -or $IsMacOS)
{
if ($Scope -eq 'CurrentUser')
{
$DestinationPath = Join-Path -Path $HOME -ChildPath '.local/share/powershell/Modules'
}
else
{
$DestinationPath = '/usr/local/share/powershell/Modules'
}
}
else
{
switch ($Scope)
{
'CurrentUser'
{
$DestinationPath = "$([Environment]::GetFolderPath('MyDocuments'))\WindowsPowerShell\Modules"
}
'AllUsers'
{
$DestinationPath = "$($env:ProgramFiles)\WindowsPowerShell\Modules"
}
}
}
$DestinationPath = Join-Path -Path $DestinationPath -ChildPath $targetModuleName
Write-Verbose "The module will be saved to '$DestinationPath'."
if($IsLinux -or $IsMacOS)
{
$psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse
}
else
{
$psd1 = Get-ChildItem (Join-Path -Path $tmpDir -ChildPath $unzippedArchive) -Include *.psd1 -Recurse
}
if($psd1)
{
$ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion
$DestinationPath = Join-Path -Path $DestinationPath -ChildPath $ModuleVersion
try
{
$null = New-Item -ItemType directory -Path $DestinationPath -Force -ErrorAction Stop
}
catch
{
Write-Error "Unable to create the folder '$DestinationPath'. Try again running as Administrator."
break
}
}
if($IsLinux -or $IsMacOS)
{
$null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $DestinationPath -Force -Recurse
}
else
{
try
{
$null = Copy-Item "$(Join-Path -Path $tmpDir -ChildPath $unzippedArchive\*)" $DestinationPath -Force -Recurse -ErrorAction Stop
}
catch
{
Write-Output 'Unable to copy files.'
break
}
}
}
}
}