Created
October 20, 2018 14:55
-
-
Save james-world/95e45539fc9e04d74817cff2fb742eb9 to your computer and use it in GitHub Desktop.
Powershell command to create a .gitignore file from .gitignore.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-GitIgnore { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string[]] | |
$Targets, | |
[string] | |
$OutputDirectory = "$PWD", | |
[string] | |
$Filename = ".gitignore", | |
[switch]$NoClobber | |
) | |
foreach ($target in $Targets) { | |
if ($target -notmatch "[a-zA-Z][a-zA-Z0-9\.]*") { | |
Write-Error -ErrorAction Stop -Message "Incorrect target format, " + ` | |
"must start with a letter, and use only letters, numbers and periods." | |
} | |
} | |
$url = "https://www.gitignore.io/api/$($Targets -join `",`")" | |
$gitIgnoreResponse = Invoke-WebRequest -Uri $url | |
if ($gitIgnoreResponse.StatusCode -ne 200) { | |
Write-Error -ErrorAction Stop -Message "gitignore.io call returned error" | |
} | |
$outputPath = Join-Path -Path $OutputDirectory -ChildPath $Filename | |
$outputPath | |
if ((Test-Path -Path $outputPath) -And ($NoClobber -eq $true)) { | |
Write-Error -ErrorAction Stop -Message "File already exists and NoClobber was specified." | |
} | |
if ((Test-Path $OutputDirectory) -ne $true) { | |
New-Item -ItemType Directory -Path $OutputDirectory | |
} | |
$content = $gitIgnoreResponse.Content | |
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False | |
[System.IO.File]::WriteAllLines($outputPath, $content, $Utf8NoBomEncoding) | |
Write-Output "GitIgnore file created at $outputPath" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment