Skip to content

Instantly share code, notes, and snippets.

@james-world
Created October 20, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james-world/95e45539fc9e04d74817cff2fb742eb9 to your computer and use it in GitHub Desktop.
Save james-world/95e45539fc9e04d74817cff2fb742eb9 to your computer and use it in GitHub Desktop.
Powershell command to create a .gitignore file from .gitignore.io
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