-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTInstallModule.ps1
More file actions
189 lines (142 loc) · 4.77 KB
/
DTInstallModule.ps1
File metadata and controls
189 lines (142 loc) · 4.77 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<#PSScriptInfo
.VERSION 1.1.1
.GUID d4e95bd6-ba9c-4024-8de2-0f6d1b8439b4
.AUTHOR daniznf
.COMPANYNAME znflabs
.COPYRIGHT (c) 2022 daniznf. All rights reserved.
.TAGS Install module
.LICENSEURI https://www.gnu.org/licenses/gpl-3.0.txt
.PROJECTURI https://github.com/daniznf/DTInstallModule
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
param (
[string]
$ModuleDir,
[string]
$PSModuleDir
)
if ($ModuleDir -and (-not (Test-Path $ModuleDir)))
{
Write-Host "Directory $ModuleDir does not exist!"
$ModuleDir = $null
}
$InvocationPath = $MyInvocation.MyCommand.Path
if (-not $ModuleDir) { $ModuleDir = Split-Path $InvocationPath -Parent }
$ModuleDir = Resolve-Path $ModuleDir
# Find this script's actual name
$InstallScriptName = $MyInvocation.MyCommand.Name
$InstallScriptName = $InstallScriptName.Remove($InstallScriptName.LastIndexOf("."))
# If this is in subdirectory of the module to install, go up of one directory. The module to install should be there.
$ModuleName = Split-Path $ModuleDir -Leaf
if ($ModuleName -eq $InstallScriptName)
{
$ModuleDir = Split-Path $ModuleDir -Parent
$ModuleName = Split-Path $ModuleDir -Leaf
}
Write-Host "This installation script is going to install $ModuleName from $ModuleDir."
if (-not ($PSModuleDir))
{
# Modules should be installed in ModulePath. This should be an array of paths.
$ModulePaths = $env:PSModulePath.Split(";")
if ($ModulePaths.Length -gt 1)
{
Write-Host ""
Write-host "Recognized module paths are:"
for ($i = 0; $i -lt $ModulePaths.Length; $i++)
{
Write-Host ("{0}: {1}" -f $i, $ModulePaths[$i])
}
[int] $Choice = -1
while ($Choice -notin (0..($ModulePaths.Length-1)))
{
Write-Host ""
$Choice = Read-Host -Prompt ("Please choose where you want to install module [{0}-{1}]" -f 0, ($ModulePaths.Length-1) )
Write-Host ""
}
$PSModuleDir = $ModulePaths[$Choice]
}
else # $ModulesPaths is string
{
$PSModuleDir = $ModulesPaths[0]
}
}
$ModuleInstallDir = Join-Path $PSModuleDir $ModuleName
Write-Host "Module $ModuleName will be installed in $ModuleInstallDir."
[string] $Choice = Read-Host -Prompt ("Continue? [y-n]")
if (($Choice -ne "y") -and ($Choice -ne "yes"))
{
Write-Host ""
Write-Host "Installation aborted."
Write-Host ""
Write-Host "Press return."
$null = Read-Host
exit 1
}
try
{
Remove-Module $ModuleName -ErrorAction Ignore
Write-Host ""
Write-Host "Copying module files..."
$DirExclude = "git|vscode|$InstallScriptName"
$FileInclude = "\.psd1|\.psm1|README|LICENSE"
# get files, recursively, excluding directories that match $DirExclude and including only files that match $FileInclude
$Files = Get-ChildItem -Path $ModuleDir -Recurse -File |
Where-Object { ($_.DirectoryName -notmatch $DirExclude) -and ($_.FullName -match $FileInclude) }
for ($i = 0; $i -lt $Files.Length; $i++)
{
$File = $Files[$i]
$Source = $File.FullName
$DestinationDir = $ModuleInstallDir
# if file is in subdirectory, add that subdirectory to destination
if ($File.DirectoryName -ne $ModuleDir)
{
$RelativeDir = $File.DirectoryName.Replace($ModuleDir, "")
$DestinationDir = Join-Path $ModuleInstallDir $RelativeDir
}
if (-not (Test-Path $DestinationDir))
{
Write-Host ""
Write-Host "Creating $DestinationDir..."
$null = New-Item -Path $DestinationDir -ItemType Directory -ErrorAction Stop
}
$Destination = Join-Path $DestinationDir $File.Name
write-host "Copying $Destination..."
Copy-Item -Path $Source -Destination $Destination -ErrorAction Stop -Force
}
Write-Host ""
Write-Host "Installation completed!"
Write-Host ""
Write-Host "Press return."
$null = Read-Host
exit 0
}
catch
{
Write-Host ""
Write-Host $_
Write-Host "Installation failed."
Write-Host "Press return."
$null = Read-Host
exit 1
}
<#
.SYNOPSIS
Installs module.
.DESCRIPTION
This script installs module(s) found in containing directory, or parent directory, by copying necessary files into modules directory.
The script is intended to be run inside the directory of the module to install, but can also be run from anywhere passing necessary parameters.
.PARAMETER ModuleDir
Directory of the module to install.
.PARAMETER PSModuleDir
One of directories in $env:PSModulePath.
.EXAMPLE
.\DTInstallModule.ps1
Installs module contained in local or parent directory
.EXAMPLE
.\DTInstallModule.ps1 -ModuleDir C:\my_module
Installs module contained in C:\my_module
#>