Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Created September 19, 2013 11:41
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 janikvonrotz/6622205 to your computer and use it in GitHub Desktop.
Save janikvonrotz/6622205 to your computer and use it in GitHub Desktop.
PowerShell: Sync GitHub Gists #PowerShell #EmbededPost
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
function Sync-GitHubGists{
param(
[string[]]$Url,
[string]$Path
)
# settings
$TagFileName = "Tags.txt"
$GithubGists = $null
$Url | %{
# get json
$WebClient = New-Object System.Net.WebClient
$webClient.Headers.Add("user-agent", "PowerShell Scripts")
$WebResponse = $WebClient.DownloadString($_)
# json to object
$JavaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$GitHubGists += $JavaScriptSerializer.DeserializeObject($WebResponse)
}
# get repos
$GitHubGists = $GitHubGists | select `
@{L="Name";E={($_.description).split("#")[0] -replace "`n","" -replace "`r","" -replace "`r`n",""}}, `
@{L="Tags";E={($_.description).split("#")[1..100] | %{"#$_" -replace "`n","" -replace "`r","" -replace "`r`n",""}}}, `
@{L="git_pull_url";E={$_.git_pull_url}}
# update or create local repos
$GitHubGists | %{
$LocalGitPath = Join-Path $Path ($_.Name -replace ":","")
if(Test-Path $LocalGitPath){
cd ($LocalGitPath)
Write-Host "Update GitHubGist: $($_.Name)"
git pull
}else{
$LocalGitClonePath = "`"$LocalGitPath`"" -replace "`n",""
Write-Host "Cloning GitHubGist: $($_.Name)"
git clone ($_.git_pull_url) $LocalGitClonePath
}
$TagFilePath = Join-Path $LocalGitPath $TagFileName
if(-not (Test-Path $TagFilePath)){New-Item -Path $TagFilePath -ItemType File}
Set-Content -Value $_.Tags -Path $TagFilePath
}
# delete old repos
$GitHubGistNames = $GitHubGists | %{"$($_.Name)" -replace ":",""}
Get-ChildItem -Path $Path | where{$_.psiscontainer -and $GitHubGistNames -notcontains $_.Name} | %{
Write-Host "Delete GitHubGist: $($_.Name)"
$_ | Remove-Item -Force -Recurse
}
}
Sync-GitHubGists -Url "https://api.github.com/users/janikvonrotz/gists?page=1", `
"https://api.github.com/users/janikvonrotz/gists?page=2", `
"https://api.github.com/users/janikvonrotz/gists?page=3", `
"https://api.github.com/users/janikvonrotz/gists?page=4", `
"https://api.github.com/users/janikvonrotz/gists?page=5", `
"https://api.github.com/users/janikvonrotz/gists?page=6", `
"https://api.github.com/users/janikvonrotz/gists?page=7", `
"https://api.github.com/users/janikvonrotz/gists?page=8", `
"https://api.github.com/users/janikvonrotz/gists?page=9", `
"https://api.github.com/users/janikvonrotz/gists?page=10" `
-Path (Get-Path "$($PSscripts.Path)/GitHubGist")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment