Created
December 27, 2011 16:52
-
-
Save drlongnecker/1524336 to your computer and use it in GitHub Desktop.
Finding TODOs and Reporting in PowerShell
This file contains hidden or 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
#full details now available: http://tiredblogger.wordpress.com/2011/12/28/finding-todos-and-reporting-in-powershell/ | |
param( | |
#this is appalling; there has to be a better way to get the raw name of "this" directory. | |
[string]$DirectoryMask = (get-location), | |
[array]$Include = | |
@("*.spark","*.cs","*.js","*.coffee","*.rb"), | |
[array]$Exclude = | |
@("fubu-content","packages","build","release"), | |
[switch]$Html, | |
[string]$FileName = "todoList.html") | |
$originalFiles = | |
gci -Recurse -Include $Include -Exclude $Exclude -Path $DirectoryMask; | |
$withoutExcludes = $originalFiles | ? { | |
if ($Exclude.count -gt 0) { | |
#need moar regex | |
[regex] $ex = | |
# 1: case insensitive and start of string | |
# 2: join our array, using RegEx to escape special characters | |
# the * allow wildcards so directory names filter out | |
# and not just exact paths. | |
# 3: end of string | |
# 1 2 3 | |
'(?i)^(.*'+(($Exclude|%{[regex]::escape($_)}) -join ".*|.*")+'.*)$' | |
$_ -notmatch $ex | |
} | |
else { | |
$_ | |
} | |
} | |
$todos = $withoutExcludes | | |
% { select-string $_ -pattern "TODO:" } | | |
select-object LineNumber, FileName, Line; | |
$todoReplacer = '(#|//).TODO:.' | |
$formattedOutput = $todos | select-object ` | |
@{Name='LineNumber'; Expression={$_.LineNumber}}, ` | |
@{Name='FileName'; Expression={$_.FileName}}, ` | |
@{Name='TODO'; Expression={[regex]::replace($_.Line.Trim(),$todoReplacer,'')}} | |
if ($Html) { | |
$head = @' | |
<style> | |
body{ font-family:Segoe UI; background-color:white;} | |
table{ border-width: 1px;border-style: solid; | |
border-color: black;border-collapse: collapse;width:100%;} | |
th{ font-family:Segoe Print;font-size:1.0em; border-width: 1px; | |
padding: 2px;border-style: solid;border-color:black; | |
background-color:lightblue;} | |
td{ border-width: 1px;padding: 2px;border-style: solid; | |
border-color: black;background-color:white;} | |
</style> | |
'@ | |
$solutionName = | |
(Get-Location).ToString().Split("\")[(Get-Location).ToString().Split("\").Count-1]; | |
$header = "<h1>"+$solutionName +" TODO List</h1><p>Generated on "+ [DateTime]::Now +".</p>" | |
$title = $solutionName +" TODO List" | |
$formattedOutput | | |
ConvertTo-HTML -head $head -body $header -title $title | | |
Out-File $FileName | |
} | |
else { | |
$formattedOutput | |
} |
o_0 ... must learn moar powershell
Updated the TODO replacement finder to handle '# TODO: ' for ruby and coffeescript files.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the ? (where) -notmatch because -Excludes is full of fail in PowerShell 2.0 still. =/