Functions/New-Repo.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
<#
    .SYNOPSIS
        Creates a new local repository with the provided name.
 
    .DESCRIPTION
        Used to quickly bootstrap a local repository by automatically creating the correct
        folder structure, configuring your Git remotes and optionally adding a relevant gitignore
        file. Upon completion, you may switch to the location of the newly created repo by either
        passing the -Open flag or by using Open-Repo.
 
    .PARAMETER Repo
        The name of the repository which you would like to fetch.
 
    .PARAMETER Service
        The remote hosting provider from which you would like to fetch the repository.
     
    .PARAMETER Path
        The directory within which all of your repositories are stored. Defaults to the value of $GitTool.Directory
        if not specified.
 
    .PARAMETER Open
        Whether you would like to reconfigure the local repository to use the updated remote address in situations where that has changed.
 
    .PARAMETER GitIgnore
        The language or tool for which you would like to fetch a gitignore file. If not provided, defaults to your globally configured
        $GitTool.GitIgnore.Default option.
     
    .EXAMPLE
        New-Repo sierrasoftworks/git-tool
 
        Running git init
        Running git remote add origin git@github.com:SierraSoftworks/git-tool.git
 
        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
     
    .EXAMPLE
        New-Repo sierrasoftworks/git-tool -GitIgnore powershell
 
        Running git init
        Running git remote add origin git@github.com:SierraSoftworks/git-tool.git
        Adding .gitignore file
 
        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 New-Repo {
    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,

        [switch]
        [Parameter(HelpMessage = "Open the repo once it has been created.")]
        $Open = $false,

        [string]
        [Parameter(HelpMessage = "The language or tool for which you would like to fetch a gitignore file")]
        $GitIgnore = $GitTool.GitIgnore.Default
    )

    $info = Get-RepoInfo -Repo $Repo -Service $Service -Path $Path

    Write-Host -NoNewline "Creating "
    Write-Host -ForegroundColor Blue $info.WebURL
    Write-Host -NoNewline " - Git URL: "
    Write-Host -ForegroundColor Red $info.GitURL
    Write-Host -NoNewline " - Target: "
    Write-Host -ForegroundColor Green $info.Path

    if (-not $info.Exists) {
        New-Item -Path $info.Path -ItemType Container | Out-Null
    }

    Write-Host ""

    Push-Location -Path $info.Path
    try {
        if (Test-Path -PathType Container -Path ".git") {
            Write-Error -Category ResourceExists -Message "The repository $Service/$Repo already exists." -RecommendedAction "Please use git to manage this repository directly."
            return
        }
        
        Write-Host -NoNewline "Running "
        Write-Host -ForegroundColor DarkGray "git init"
        git.exe init | Out-Null

        Write-Host -NoNewline "Running "
        Write-Host -ForegroundColor DarkGray "git remote add origin ${info.GitURL}"
        git.exe remote add origin $info.GitURL | Out-Null

        if ($null -ne $GitIgnore) {
            Write-Host "Adding .gitignore file"
            Get-GitIgnore -Language $GitIgnore | Set-Content .gitignore
        }

        if (($null -ne $GitTool.Services[$Service]) -and ($null -ne $GitTool.Services[$Service].AutoCreate.Enabled) -and (Get-Module -Name "PowerShellForGitHub")) {
            Write-Host "Creating GitHub repository"
            New-GitHubRepository -OrganizationName $info.Namespace -RepositoryName $info.Name -Private:$GitTool.Services["github.com"].AutoCreate.Private
        }
    }
    finally {
        Pop-Location
    }

    if ($Open) {
        Open-Repo -Repo $Repo -Service $Service -Path $Path
    }

    return $info
}