PSDependScripts/GitHub.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<#
    .SYNOPSIS
        Installs a module from a GitHub repository.

    .DESCRIPTION
        Installs a module from a GitHub repository.

        Relevant Dependency metadata:
            DependencyName (Key): The key for this dependency is used as Name, if none is specified
            Name: Used to specify the GitHub repository name to download
            Version: Used to identify existing installs meeting this criteria, and as RequiredVersion for installation. Defaults to 'latest'
            Target: The folder to download repo to. Created if it doesn't exist.
                "AllUsers" resolves to:
                    Windows: the system's program files folder.
                    Other: the platform's SHARED_MODULES folder.
                "CurrentUser" resolves to:
                    Windows: the user's (My)Documents folder.
                    Other: the platform's USER_MODULES folder.
                It defaults to "AllUsers" on Windows in an elevated session and to "CurrentUser" otherwise.
                PowerShell uses the "WindowsPowerShell\Modules" folder hierarchy while PowerShell Core uses "PowerShell\Modules".


    .NOTES
        Initial idea and some code by Doug Finke. Rewrite by Jonas Thelemann for tag support and PowerShell Core compatibility.
        A huge thanks to both!
            https://github.com/dfinke/InstallModuleFromGitHub
            https://github.com/dargmuesli

    .PARAMETER PSDependAction
        Test, install or import the module. Defaults to "Install".

        Test: Return true or false on whether the dependency is in place.
        Install: Install the dependency.
        Import: Import the dependency.

    .PARAMETER ExtractPath
        Limit extraction of file(s) and folder(s).

    .PARAMETER ExtractProject
        Parse the GitHub repository for a common PowerShell project hierarchy and extract only the project folder.

        Example: ramblingcookiemonster/psslack looks like this:
                  PSSlack/ Repo root
                    PSSlack/ Module root
                      PSSlack.psd1 Module manifest
                  Tests/

                  In this case, we would extract PSSlack/PSSlack only.

        Example: bundyfx/vamp looks like this:
                  vamp/ Repo root (also, module root)
                    vamp.psd1 Module manifest

                  In this case, we would extract the whole root vamp folder.

    .PARAMETER TargetType
        How the target is interpreted:
            Standard: Extract to "target\name" [Default].
            Exact: Extract to "target\".
            Parallel: Extract to "target\name\version" or "target\name\branch\name" depending on the version specified.

    .PARAMETER Force
        If specified, delete an already existing target folder (as defined by TargetType).
        Default: False / not specified. Files are copied to the target folder without any file or folder removal.

    .EXAMPLE
        Imagine a GitHub repository containing a PowerShell module with git tags named "1.0.0" and "0.1.0".

        @{
            'Dargmuesli/powershell-lib' = '1.0.0'
        }
        @{
            'Dargmuesli/powershell-lib' = 'latest'
        }
        @{
            'Dargmuesli/powershell-lib' = ''
        }
        These download version 1.0.0 to "powershell-lib\1.0.0"

        @{
            'Dargmuesli/powershell-lib' = '0.1.0'
        }
        This downloads version 0.1.0 to "powershell-lib\0.1.0"

        @{
            'Dargmuesli/powershell-lib' = 'master'
        }
        This downloads branch "master" (most recent commit version) to "powershell-lib"

    .EXAMPLE
        Imagine a GitHub repository containing a PowerShell module with no git tags.

        @{
            'Dargmuesli/powershell-lib' = 'latest'
        }
        @{
            'Dargmuesli/powershell-lib' = ''
        }
        @{
            'Dargmuesli/powershell-lib' = 'master'
        }
        These download branch "master" (most recent commit version) to "powershell-lib"

        @{
            'Dargmuesli/powershell-lib' = @{
                Version = 'latest'
                Parameters @{
                    TargetType = 'Parallel'
                }
            }
        }
        @{
            'Dargmuesli/powershell-lib' = @{
                Parameters @{
                    TargetType = 'Parallel'
                }
            }
        }
        @{
            'Dargmuesli/powershell-lib' = @{
                Version = 'master'
                Parameters @{
                    TargetType = 'Parallel'
                }
            }
        }
        These download branch "master" (most recent commit version) to "powershell-lib\master\powershell-lib"

        @{
            'Dargmuesli/powershell-lib' = @{
                Version = 'feature'
                Parameters @{
                    TargetType = 'Parallel'
                }
            }
        }
        This downloads branch "feature" (most recent commit version) to "powershell-lib\feature\powershell-lib"

    .EXAMPLE
        @{
            'powershell/demo_ci' = @{
                Version = 'latest'
                DependencyType = 'GitHub'
                Target = 'C:\T'
                Parameters = @{
                    ExtractPath = 'Assets/DscPipelineTools',
                                  'InfraDNS/Configs/DNSServer.ps1'
                    TargetType = 'Exact'
                }
            }
        }

        # This downloads the latest version of demo_ci by powershell from GitHub.
        # Then it extracts "repo-root/Assets/DscPipelineTools" and "repo-root/InfraDNS/Configs/DNSServer.ps1" to the target.
#>

[cmdletbinding()]
param(
    [PSTypeName('PSDepend.Dependency')]
    [psobject[]]$Dependency,

    [ValidateSet('Test', 'Install', 'Import')]
    [string[]]$PSDependAction = @('Install'),

    [string[]]$ExtractPath,

    [bool]$ExtractProject = $true,

    [ValidateSet('Parallel', 'Standard', 'Exact')]
    [string]$TargetType = 'Standard',

    [switch]$Force
)

$script:IsWindows = (-not (Get-Variable -Name "IsWindows" -ErrorAction "Ignore")) -or $IsWindows
$script:IsCoreCLR = $PSVersionTable.ContainsKey("PSEdition") -and $PSVersionTable.PSEdition -eq "Core"

Write-Verbose -Message "Am I on Windows? [$script:IsWindows]! Am I PS Core? [$script:IsCoreCLR]!"

# Extract data from dependency
$DependencyID = $Dependency.DependencyName
$DependencyVersion = $Dependency.Version
$DependencyTarget = $Dependency.Target
$DependencyName = $DependencyID.Split("/")[1]

# Translate "" to "latest"
if($DependencyVersion -eq "")
{
    $DependencyVersion = "latest"
}

# Check if the version that should be used is a version number
if($DependencyVersion -match "^\d+(?:\.\d+)+$")
{
    $DependencyVersion = New-Object "System.Version" $DependencyVersion
}

if ($script:IsCoreCLR) {
    $ModuleChildPath = "PowerShell\Modules"
}
else
{
    $ModuleChildPath = "WindowsPowerShell\Modules"
}

# Get system installation path
if($script:IsWindows)
{
    $AllUsersPath = Join-Path -Path $env:ProgramFiles -ChildPath $ModuleChildPath
}
else
{
    $AllUsersPath = [System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')
}

# Check if the MyDocuments folder path is accessible
try
{
    $MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")
}
catch
{
    $MyDocumentsFolderPath = $null
}

# Get user installation path
if($script:IsWindows)
{
    if($MyDocumentsFolderPath)
    {
        $CurrentUserPath = Join-Path -Path $MyDocumentsFolderPath -ChildPath $ModuleChildPath
    }
    else
    {
        $CurrentUserPath = Join-Path -Path $HOME -ChildPath "Documents\$ModuleChildPath"
    }
}
else
{
    $CurrentUserPath = [System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')
}

# Set target path
if($DependencyTarget)
{
    # Resolve scope keywords
    if($DependencyTarget -Eq "CurrentUser")
    {
        $TargetPath = $CurrentUserPath
    }
    elseif($DependencyTarget -Eq "AllUsers")
    {
        $TargetPath = $AllUsersPath
    }
    else
    {
        $TargetPath = $DependencyTarget
    }
}
else
{
    # Set default target depending on admin permissions
    if(($script:IsWindows) -And (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")))
    {
        $TargetPath = $AllUsersPath
    }
    else
    {
        $TargetPath = $CurrentUserPath
    }
}

Write-Verbose -Message "Dependency id: [$DependencyID]"
Write-Verbose -Message "Dependency version: [$DependencyVersion]"
Write-Verbose -Message "Dependency target: [$DependencyTarget]"
Write-Verbose -Message "Dependency name: [$DependencyName]"
Write-Verbose -Message "Target Path: [$TargetPath]"

# Search for an already existing version of the dependency
$Module = Get-Module -ListAvailable -Name $DependencyName -ErrorAction SilentlyContinue
$ModuleExisting = $null
$ModuleExistingMatches = $false
$ExistingVersions = $null
$ShouldInstall = $false
$RemoteAvailable = $false
$URL = $null

if($Module)
{
    $ModuleExisting = $true
}
else
{
    $ModuleExisting = $false
}

if($ModuleExisting)
{
    Write-Verbose "Found existing module [$DependencyName]"
    $ExistingVersions = $Module | Select-Object -ExpandProperty "Version"

    # Check if the version that is should be used is a version number
    if($DependencyVersion -match "^\d+(?:\.\d+)+$")
    {
        :versionslocal foreach($ExistingVersion in $ExistingVersions)
        {
            switch($ExistingVersion.CompareTo($DependencyVersion))
            {
                {@(-1, 1) -contains $_} {
                    Write-Verbose "For [$DependencyName], the version you specified [$DependencyVersion] does not match the already existing version [$ExistingVersion]"
                    $ShouldInstall = $true
                    break
                }
                0 {
                    Write-Verbose "For [$DependencyName], the version you specified [$DependencyVersion] matches the already existing version [$ExistingVersion]"
                    $ShouldInstall = $false
                    $ModuleExistingMatches = $true
                    break versionslocal
                }
            }
        }
    }
    else
    {
        # The version that is to be used is probably a GitHub branch name
        $ShouldInstall = $true
    }
}
else
{
    Write-Verbose "Did not find existing module [$DependencyName]"
    $ShouldInstall = $true
}

# Skip the case when the version that is to be used already exists
if($ShouldInstall)
{
    # API-fetch the tags on GitHub
    $GitHubVersion = $null
    $GitHubTag = $null
    $Page = 0

    try
    {
        :nullcheck while($GitHubVersion -Eq $null)
        {
            $Page++
            [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
            $GitHubTags = Invoke-RestMethod -Uri "https://api.github.com/repos/$DependencyID/tags?per_page=100&page=$Page"

            if($GitHubTags)
            {
                foreach($GitHubTag in $GitHubTags)
                {
                    if($GitHubTag.name -match "^\d+(?:\.\d+)+$" -and ($DependencyVersion -match "^\d+(?:\.\d+)+$" -or $DependencyVersion -eq "latest"))
                    {
                        $GitHubVersion = New-Object "System.Version" $GitHubTag.name

                        if($DependencyVersion -Eq "latest")
                        {
                            $DependencyVersion = $GitHubVersion
                        }

                        switch($DependencyVersion.CompareTo($GitHubVersion))
                        {
                            -1 {
                                # Version is older compared to the GitHub version, continue searching
                                break
                            }
                            0 {
                                Write-Verbose "For [$DependencyName], a matching version [$DependencyVersion] has been found in the GitHub tags"
                                $RemoteAvailable = $true
                                break nullcheck
                            }
                            1 {
                                # Version is newer compared to the GitHub version, which means we can stop searching (given version history is reasonable)
                                break nullcheck
                            }
                        }
                    }
                }
            }
            else
            {
                break nullcheck
            }
        }
    }
    catch
    {
        # Repository does not seem to exist or a branch is the target
        $ShouldInstall = $false
        Write-Warning "Could not find module on GitHub: $_"
    }

    if($RemoteAvailable)
    {
        # Use the tag's link
        $URL = $GitHubTag.zipball_url
        if($ExistingVersions)
        {
            :versionsremote foreach($ExistingVersion in $ExistingVersions)
            {
                # Because a remote and a local version exist
                # Prevent a module from getting installed twice
                switch($ExistingVersion.CompareTo($GitHubVersion))
                {
                    {@(-1, 1) -contains $_} {
                        Write-Verbose "For [$DependencyName], you have a different version [$ExistingVersion] compared to the version available on GitHub [$GitHubVersion]"
                        break
                    }
                    0 {
                        Write-Verbose "For [$DependencyName], you already have the version [$ExistingVersion]"
                        $ModuleExistingMatches = $true
                        $ShouldInstall = $false
                        break versionsremote
                    }
                }
            }
        }
    }
    else
    {
        Write-Verbose "[$DependencyID] has no tags on GitHub or [$DependencyVersion] is a branchname"
        # Translate version "latest" to "master"
        if($DependencyVersion -eq "latest")
        {
            $DependencyVersion = "master"
        }

        # Link for a .zip archive of the repository's branch
        $URL = "https://api.github.com/repos/$DependencyID/zipball/$DependencyVersion"
        $ShouldInstall = $true
    }
}

if ($TargetType -ne 'Exact')
{
    $TargetPath = Join-Path $TargetPath $DependencyName
}

# Install action needs to be wanted and logical
if(($PSDependAction -contains 'Install') -and $ShouldInstall)
{
    # Create a temporary directory and download the repository to it
    $OutPath = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().guid)
    New-Item -ItemType Directory -Path $OutPath -Force | Out-Null
    $OutFile = Join-Path $OutPath "$DependencyVersion.zip"
    Invoke-RestMethod -Uri $URL -OutFile $OutFile

    if(-not (Test-Path $OutFile))
    {
        Write-Error "Could not download [$URL] to [$OutFile]. See error details and verbose output for more information"
        return
    }

    # Extract the zip file
    if($script:IsWindows)
    {
        $ZipFile = (New-Object -com shell.application).NameSpace($OutFile)
        $ZipDestination = (New-Object -com shell.application).NameSpace($OutPath)
        $ZipDestination.CopyHere($ZipFile.Items())
    }
    else
    {
        # If not on Windows "Expand-Archive" should be available as PS version 6 is considered minimum.
        Expand-Archive $OutFile -DestinationPath $OutPath
    }

    # Remove the zip file
    Remove-Item $OutFile -Force -Confirm:$false

    $OutPath = (Get-ChildItem -Path $OutPath)[0].FullName
    $OutPath = (Rename-Item -Path $OutPath -NewName $DependencyName -PassThru).FullName

    if($ExtractPath)
    {
        # Filter only the contents wanted
        [string[]]$ToCopy = foreach($RelativePath in $ExtractPath)
        {
            $AbsolutePath = Join-Path $OutPath $RelativePath
            if(-not (Test-Path $AbsolutePath))
            {
                Write-Warning "Expected ExtractPath [$RelativePath], did not find at [$AbsolutePath]"
            }
            else
            {
                $AbsolutePath
            }
        }
    }
    elseif($ExtractProject)
    {
        # Filter only the project contents
        $ProjectDetails = Get-ProjectDetail -Path $OutPath
        [string[]]$ToCopy = $ProjectDetails.Path
    }
    else
    {
        # Use the standard download path
        [string[]]$ToCopy = $OutPath
    }

    Write-Verbose "Contents that will be copied: $ToCopy"

    # Copy the contents to their target
    if(-not (Test-Path $TargetPath))
    {
        New-Item $TargetPath -ItemType "directory" -Force
    }

    $Destination = $null

    if($TargetType -eq 'Exact')
    {
        $Destination = $TargetPath
    }
    elseif($DependencyVersion -match "^\d+(?:\.\d+)+$" -and $PSVersionTable.PSVersion -ge '5.0'  )
    {
        # For versioned GitHub tags
        $Destination = Join-Path $TargetPath $DependencyVersion
    }
    elseif(($DependencyVersion -eq "latest") -and ($RemoteAvailable) -and $PSVersionTable.PSVersion -ge '5.0' )
    {
        # For latest GitHub tags
        $Destination = Join-Path $TargetPath $GitHubVersion
    }
    elseif($PSVersionTable.PSVersion -ge '5.0' -and $TargetType -eq 'Parallel')
    {
        # For GitHub branches
        $Destination = Join-Path $TargetPath $DependencyVersion
        $Destination = Join-Path $Destination $DependencyName
    }
    else
    {
        $Destination = $TargetPath
    }

    if($Force -and (Test-Path -Path $Destination))
    {
        Remove-Item -Path $Destination -Force -Recurse
    }

    Write-Verbose "Copying [$($ToCopy.Count)] items to destination [$Destination] with`nTarget [$TargetPath]`nName [$DependencyName]`nVersion [$DependencyVersion]`nGitHubVersion [$GitHubVersion]"

    foreach($Item in $ToCopy)
    {
        Copy-Item -Path $Item -Destination $Destination -Force -Recurse
    }

    # Delete the temporary folder
    Remove-Item (Get-Item $OutPath).parent.FullName -Force -Recurse
    $ModuleExisting = $true
}

# Conditional import
if($ModuleExisting)
{
    Import-PSDependModule -Name $TargetPath -Action $PSDependAction
}
elseif($PSDependAction -contains 'Import')
{
    Write-Warning "[$DependencyName] at [$TargetPath] should be imported, but does not exist"
}

# Return true or false if Test action is wanted
if($PSDependAction -contains 'Test')
{
    return $ModuleExistingMatches
}

# Otherwise return null
return $null