Functions/Get-RepoInfo.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
<#
    .SYNOPSIS
        Gets an object describing the full details of a repo based on its name and hosting service.
 
    .DESCRIPTION
        Generates an object which includes information about a repo including its expected location on
        your local filesystem, the webpage on which it may be viewed, the URL which should be used by
        a git clone operation and more.
 
    .PARAMETER Repo
        The name of the repository which you would like to describe.
 
    .PARAMETER Service
        The remote hosting provider for the repository in question.
     
    .PARAMETER Path
        The directory within which all of your repositories are stored. Defaults to the value of $GitTool.Directory
        if not specified.
     
    .EXAMPLE
        Get-RepoInfo sierrasoftworks/git-tool
 
        Name Value
        ---- -----
        WebURL https://github.com/SierraSoftworks/git-tool
        Service github.com
        Repo SierraSoftworks/git-tool
        Path C:\dev\github.com\SierraSoftworks\git-tool
        Exists True
        GitURL git@github.com:SierraSoftworks/git-tool.git
#>

function Get-RepoInfo {
    param(
        [string]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "The name of the repository that you wish to synchronize (e.g. Namespace/RepoName)")]
        $Repo,

        [string]
        [Parameter(HelpMessage = "The service hosting your repository (e.g. github.com)")]
        $Service = $GitTool.Service,

        [string]
        [Parameter(HelpMessage = "The directory within which your repositories will be checked out (e.g. /src/)")]
        $Path = $GitTool.Directory
    )

    $repoPath = [System.IO.Path]::Combine($Path, $Service, $Repo.Replace('/', [System.IO.Path]::DirectorySeparatorChar))
    
    $repoNameSplit = $Repo -split '/'

    if ($repoNameSplit.Length -ne ($GitTool.Services[$Service].NamespaceDepth + 1)) {
        Write-Error -Category InvalidArgument -Message "The service $Service expects a repository name with $($GitTool.Services[$Service].NamespaceDepth + 1) path segments, you provided $($repoNameSplit.Length) segments." -RecommendedAction "Please enter a name with $($GitTool.Services[$Service].NamespaceDepth + 1) path segments each separated by a '/'."
        return
    }

    $repoNamespace = $repoNameSplit[0..($GitTool.Services[$Service].NamespaceDepth - 1)] -join '/'
    $repoName = $repoNameSplit[$GitTool.Services[$Service].NamespaceDepth]

    $repoUrl = [System.String]::Format($GitTool.Services[$Service].WebURLFormat, $repoNamespace, $repoName)
    $gitUrl = [System.String]::Format($GitTool.Services[$Service].GitURLFormat, $repoNamespace, $repoName)

    return @{
        Repo      = "$repoNamespace/$repoName";
        Namespace = $repoNamespace;
        Name      = $repoName;
        Service   = $Service;
        Path      = $repoPath;
        WebURL    = $repoUrl;
        GitURL    = $gitUrl;
        Exists    = $(Test-Path -Path $repoPath -PathType Container);
    }
}