Skip to content

Instantly share code, notes, and snippets.

@tathamoddie
Last active August 29, 2015 13:56
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 tathamoddie/9224417 to your computer and use it in GitHub Desktop.
Save tathamoddie/9224417 to your computer and use it in GitHub Desktop.
Change all ng- attributes to data-ng- instead

So, you built an AngularJS app.

You used ng- attributes everywhere, and now your W3C validation is failing.

You wish you had used data-ng- instead.

This script will fix them all for you.

.\Fix-AllAngularAttributesForHtml5.ps1 c:\code\myapp\web\Views -TfvcAware -Verbose -PrefixesToQuality @('ng', 'app')

The -TfvcAware switch will tell the script to mark each changed file as a pending change in TFVC, if you're still stuck with it.

The -PrefixesToQualify argument lets you specify other custom directives that you might have used.

param (
[parameter(Mandatory=$true)]
$TargetPath,
[switch]
$TfvcAware = $false,
$PrefixesToQuality = @('ng')
)
$ErrorActionPreference = 'Stop'
if ($TfvcAware) {
$TfsSnapinAvailable = (Get-PSSnapin -Registered -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -ne $null
if (-not $TfsSnapinAvailable) {
throw "Microsoft.TeamFoundation.PowerShell snapin was not available. http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f"
}
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}
function Process-Items($Items) {
$FilesProcessed = 0
$Items | % {
Write-Progress -Status 'Processing' -Activity $_.Name -PercentComplete ($FilesProcessed / $Items.Length * 100)
Process-File $_.FullName
$FilesProcessed ++
}
Write-Progress -Status 'Processing' -Activity 'Done' -Completed
}
function Process-File($Path) {
$OriginalContent = [System.IO.File]::ReadAllText($Path)
$NewContent = Process-Content $OriginalContent
if ($NewContent -eq $OriginalContent) {
Write-Verbose "No change for $Path"
return;
}
if ($TfvcAware) {
Write-Verbose "TFVC: Recording pending change for $Path"
$null = Add-TfsPendingChange -Edit $Path
}
Write-Verbose "Updated $Path"
$NewContent | Set-Content $Path
}
function Process-Content($Content) {
$OpenTagRegex = [regex]'(?s:\<[a-z][^\>]+>)'
$AngularAttributeNameRegex = [regex]"(?<=[\s])($($PrefixesToQuality -join '|')-[\w\-]*)"
$TagCallback = {
param ([System.Text.RegularExpressions.Match]$Match)
$AngularAttributeNameRegex.Replace($Match.Value, 'data-$0')
};
$Content = $OpenTagRegex.Replace($Content, $TagCallback)
$Content
}
$AllRazorFiles = @(Get-ChildItem $TargetPath -Include *.cshtml -Recurse)
Process-Items $AllRazorFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment