MSH: Script to Search Files Checked into Team Foundation Version Control

Scripts like this, no matter what version control system you use, are very handy.  This particular one is designed to work with Microsoft’s Team Foundation Version Control.  It search recursively from whatever directory you run it from.
 
# ———————————————————————
# Utility functions for searching the source code
# ———————————————————————
function tfgrepItem {
    param([string]$item, [string]$pattern)
   
    if (test-path $item -Type Container) {
        $origDir = get-location
        set-location $item
        "–> $item"
        $items = $(tf dir $item | tail +2)
        for ($ndx = 0; $ndx -lt ($items.Length – 2); $ndx++) {
            $leafItem = $items[$ndx]
            if ($leafItem.Trim() -eq "") { continue }
            if ($leafItem[0] -eq "$") {
                $leafItem = $leafItem.Substring(1)
            }
            tfgrepItem $item\$leafItem $pattern
        }
        set-location $origDir
    }
    else {
        $ext = [System.IO.Path]::GetExtension($item)
        # You could use "tf properties" and look at file type to see
        # if it is binary but this slows down the script considerably.
        if ($ext -notmatch "^(dll)|(exe)|(lib)|(png)|(bmp)|(ico)|(jpg)|(jpeg)|(msi)|(msm)$") {
            match-string -Fullname $item -Pattern $pattern -CaseSensitive |
                foreach { "{0}:{1} {2}" -f $_.Filename, $_.LineNumber,`
                          $_.Line.Trim() }
        }
    }
}
function tfgrep {
    if ($args.Length -ne 1) {
        write-host "`nusage: tfgrep <pattern>`n"
        return
    }
   
    if ($(tf dir 2>&1) -match "Unable to determine*") {
        write-host "`nThe dir `"$(get-location)`" is not mapped to a Team Foundation project.`n"
        return
    }
    "Searching source in:"
    tfgrepItem $(get-location) $args[0]
}
Advertisements
This entry was posted in Monad. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s