-
Notifications
You must be signed in to change notification settings - Fork 2
/
Install.ps1
174 lines (162 loc) · 6.53 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
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
173
174
<#
.SYNOPSIS
Installs module from Git clone or directly from GitHub.
File must not have BOM for GitHub deploy to work.
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param (
# Path to install the module to, if not provided -Scope used.
[Parameter(Mandatory, ParameterSetName = 'ModulePath')]
[ValidateNotNullOrEmpty()]
[String]$ModulePath,
# Path to install the module to, PSModulePath "CurrentUser" or "AllUsers", if not provided "CurrentUser" used.
[Parameter(Mandatory, ParameterSetName = 'Scope')]
[ValidateSet('CurrentUser', 'AllUsers')]
[string]
$Scope = 'CurrentUser',
# Get module from GitHub instead of local Git clone, for example "https://raw.githubusercontent.com/ili101/Module.Template/master/Install.ps1"
[ValidateNotNullOrEmpty()]
[Uri]$FromGitHub
)
# Set Files and Folders patterns to Include/Exclude.
$IncludeFiles = @(
'*.dll',
'*.psd1',
'*.psm1',
'*.ps1',
'BinFolder*'
)
$ExcludeFiles = @(
'Install.ps1'
)
function Invoke-MultiLike {
[alias("LikeAny")]
[CmdletBinding()]
param
(
$InputObject,
[Parameter(Mandatory)]
[String[]]$Filters,
[Switch]$Not
)
$FiltersRegex = foreach ($Filter In $Filters) {
$Filter = [regex]::Escape($Filter)
if ($Filter -match "^\\\*") {
$Filter = $Filter.Remove(0, 2)
}
else {
$Filter = '^' + $Filter
}
if ($Filter -match "\\\*$") {
$Filter = $Filter.Substring(0, $Filter.Length - 2)
}
else {
$Filter = $Filter + '$'
}
$Filter
}
if ($Not) {
$InputObject -notmatch ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
}
else {
$InputObject -match ($FiltersRegex -join '|').replace('\*', '.*').replace('\?', '.')
}
}
Try {
Write-Verbose -Message 'Module installation started'
if (!$ModulePath) {
if ($Scope -eq 'CurrentUser') {
$ModulePathIndex = 0
}
else {
$ModulePathIndex = 1
}
if ($IsLinux -or $IsMacOS) {
$ModulePathSeparator = ':'
}
else {
$ModulePathSeparator = ';'
}
$ModulePath = ($env:PSModulePath -split $ModulePathSeparator)[$ModulePathIndex]
}
# Get $ModuleName, $TargetPath, [$Links]
if ($FromGitHub) {
# Fix Could not create SSL/TLS secure channel
#$SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$WebClient = [System.Net.WebClient]::new()
$GitUri = $FromGitHub.AbsolutePath.Split('/')[1, 2] -join '/'
$GitBranch = $FromGitHub.AbsolutePath.Split('/')[3]
$Links = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GitUri/contents" -Body @{ref = $GitBranch }) | Where-Object { (LikeAny $_.name $IncludeFiles) -and (LikeAny $_.name $ExcludeFiles -Not) }
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension(($Links | Where-Object { $_.name -like '*.psm1' }).name)
$ModuleVersion = (. ([Scriptblock]::Create((Invoke-WebRequest -Uri ($Links | Where-Object { $_.name -eq "$ModuleName.psd1" }).download_url)))).ModuleVersion
}
else {
$ModuleName = [System.IO.Path]::GetFileNameWithoutExtension((Get-ChildItem -File -Filter *.psm1 -Name -Path $PSScriptRoot))
$ModuleVersion = (. ([Scriptblock]::Create((Get-Content -Path (Join-Path $PSScriptRoot "$ModuleName.psd1") | Out-String)))).ModuleVersion
}
$TargetPath = Join-Path -Path $ModulePath -ChildPath $ModuleName
$TargetPath = Join-Path -Path $TargetPath -ChildPath $ModuleVersion
# Create Directory
if (-not (Test-Path -Path $TargetPath)) {
$null = New-Item -Path $TargetPath -ItemType Directory -ErrorAction Stop
Write-Verbose -Message ('Created module folder: "{0}"' -f $TargetPath)
}
# Copy Files
if ($FromGitHub) {
foreach ($Link in $Links) {
$TargetPathItem = Join-Path -Path $TargetPath -ChildPath $Link.name
if ($Link.type -ne 'dir') {
$WebClient.DownloadFile($Link.download_url, $TargetPathItem)
Write-Verbose -Message ('Installed module file: "{0}"' -f $Link.name)
}
else {
if (-not (Test-Path -Path $TargetPathItem)) {
$null = New-Item -Path $TargetPathItem -ItemType Directory -ErrorAction Stop
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathItem
}
$SubLinks = (Invoke-RestMethod -Uri $Link.git_url -Body @{recursive = '1' }).tree
foreach ($SubLink in $SubLinks) {
$TargetPathSub = Join-Path -Path $TargetPathItem -ChildPath $SubLink.path
if ($SubLink.'type' -EQ 'tree') {
if (-not (Test-Path -Path $TargetPathSub)) {
$null = New-Item -Path $TargetPathSub -ItemType Directory -ErrorAction Stop
Write-Verbose -Message 'Created module folder: "{0}"' -f $TargetPathSub
}
}
else {
$WebClient.DownloadFile(
('https://raw.githubusercontent.com/{0}/{1}/{2}/{3}' -f $GitUri, $GitBranch, $Link.name, $SubLink.path),
$TargetPathSub
)
}
}
}
}
}
else {
Get-ChildItem -Path $PSScriptRoot -Exclude $ExcludeFiles | Where-Object { LikeAny $_.Name $IncludeFiles } | ForEach-Object {
if ($_.Attributes -ne 'Directory') {
Copy-Item -Path $_ -Destination $TargetPath
Write-Verbose -Message ('Installed module file "{0}"' -f $_)
}
else {
Copy-Item -Path $_ -Destination $TargetPath -Recurse -Force
Write-Verbose -Message ('Installed module folder "{0}"' -f $_)
}
}
}
# Import Module
Write-Verbose -Message "$ModuleName module installation successful to $TargetPath"
Import-Module -Name $ModuleName -Force
Write-Verbose -Message "Module installed"
}
Catch {
throw ('Failed installing module "{0}". Error: "{1}" in Line {2}' -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber)
}
finally {
#if ($FromGitHub) {
# [Net.ServicePointManager]::SecurityProtocol = $SecurityProtocol
#}
Write-Verbose -Message 'Module installation end'
}