Extensions/GitRemoteItemType.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
Register-EnvironmentModuleRequiredItemType "GIT_REMOTE" {
    param([System.IO.DirectoryInfo] $Directory, [EnvironmentModuleCore.RequiredItem] $Item)

    if([string]::IsNullOrEmpty($Item.Value)) {
        Write-Warning "Required git remote without value specified"
    }

    Write-Verbose "Searching for remote $Item.Value in $Directory.FullName"

    Push-Location
    Set-Location $Directory.FullName
    $remotes = $null
    try {
        $remotes = git remote -v
    }
    catch {
        Write-Warning "Error executing git"
    }
    Pop-Location

    if([string]::IsNullOrEmpty($remotes)) {
        return $false
    }

    Write-Verbose "Found the following remotes in '$Directory': $remotes"
    $matchResult = $remotes -match $Item.Value
    if(($null -eq $matchResult) -or ($matchResult.Length -eq 0)) {
        Write-Verbose "'$($Item.Value)' does not match"
        return $false
    }

    Write-Verbose "'$($Item.Value)' does match"
    return $true
}