Skip to content

Instantly share code, notes, and snippets.

@cbadke
Last active December 13, 2015 23:18
Show Gist options
  • Save cbadke/4990286 to your computer and use it in GitHub Desktop.
Save cbadke/4990286 to your computer and use it in GitHub Desktop.
Scan files for unique guids. Generate replacements. Replace each guid in all files, committing between each guid.
param (
[string] $path
)
$oldPath = $pwd
cd (Resolve-Path $path)
$files = grep -rIlE "[0-9a-fA-F]\{8\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{12\}" * |
? { !$_.Contains('.min.js') } |
? { !$_.Contains('tiny_mce') } |
? { !$_.Contains('jquery') }
$spFiles = $files |
? { $_.EndsWith('FieldRegistry.cs') -or $_.EndsWith('Elements.xml') }
$guids = $spFiles |
% {
Get-Content $_ | % {
$guidPattern = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
$_ |
Select-String -pattern "$guidPattern" -AllMatches |
Select-Object -ExpandProperty Matches |
? {$_.Value.ToString() -ne [System.Guid]::Empty.ToString()} |
% {Write-Output $_.Value.ToLower()}
}
} |
Select-Object -Unique |
% {
New-Object System.Object |
Add-Member -type NoteProperty -name 'Original' -value $_ -PassThru |
Add-Member -type NoteProperty -name 'Replacement' -value ([System.Guid]::NewGuid().ToString()) -PassThru |
Write-Output
}
$guids | % {
$guid = $_
$files |
% {
(Get-Content $_) | %{ $_ -replace $guid.Original, $guid.Replacement } | Set-Content $_
}
git commit -am ("Automated commit for guid replacement - " + $_.Original.ToString().ToUpper() + " -> " + $_.Replacement.ToString().ToUpper())
}
cd $oldPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment