Public/Iterating/Foreach-GitRepository.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
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    Example of how to use this cmdlet
.EXAMPLE
    Another example of how to use this cmdlet
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
.NOTES
    General notes
.COMPONENT
    The component this cmdlet belongs to
.ROLE
    The role this cmdlet belongs to
.FUNCTIONALITY
    The functionality that best describes this cmdlet
#>

function ForEach-GitRepository {
    [CmdletBinding(
        HelpUri = 'http://www.microsoft.com/',
        ConfirmImpact = 'Low')]
    [Alias("fgr")]
    Param (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [scriptblock]
        $Callback, 
        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [scriptblock]
        $Predicate = { $true },
        [switch]
        $Parallel
    )
    
    begin {
    }
    
    end {
        $path = Join-Path -Path $IndexDirectoryName -ChildPath $IndexFileName
        $gitRepoInfo = Get-Content $path | ConvertFrom-Json -Depth 10

        $reposToIterate = $gitRepoInfo.Repositories | Where-Object $Predicate 

        $invokeCallback = {
            param($cb, $path)
            if ($cb) {
                # Change to GIT repo directory
                try {
                    Push-Location
                    Set-Location $path
                    $cb.InvokeWithContext(@{ }, @(New-Object "PSVariable" @("_", $_)))
                }
                finally {
                    Pop-Location
                }
            }
        }
        
        if ($Parallel) {
            $jobs = $reposToIterate | ForEach-Object -Process { 
                Start-Job { 
                    param($cmd, $cb, $path) 
                    # Script block parameters are projected as strings, therefore we've to recreate the scriptblock objects
                    & ([Scriptblock]::Create($cmd)) -cb ([Scriptblock]::Create($cb)) `
                        -path $path
                } -ArgumentList $invokeCallback, $Callback, $_.Path
            }

            # Wait for the jobs and receive theirs results
            $null = Wait-Job -Job $jobs
            Receive-Job -Job $jobs
            $null = Remove-Job -Job $jobs
        }
        else {
            # Do actions sequential
            $reposToIterate | ForEach-Object -Process {
                & $invokeCallback -cb $Callback -path $_.Path
            }
        }
    }
}