Skip to content

Instantly share code, notes, and snippets.

@cbilson
Last active June 6, 2023 09:20
Show Gist options
  • Save cbilson/6309229 to your computer and use it in GitHub Desktop.
Save cbilson/6309229 to your computer and use it in GitHub Desktop.
Current Profile. Contains a few TFS hacks...for if I ever need to use TFS again.
Set-StrictMode -version Latest
function Test-FileDep($a,$b) {
$bExists = test-path $b
$aTs = (Get-Item $a).LastWriteTime
return $bExists -and $aTs -le (Get-Item $b).LastWriteTime
}
function Update-Path($val) {
if (!$ENV:PATH.contains($val)) {
$ENV:PATH += ";$val"
}
}
function Write-Info($name) {
write-host " $name" -ForegroundColor Green -NoNewLine
}
#
# VisualStudio
#
function Set-VisualStudioEnvVars {
$psvsvarFile = "~\Documents\WindowsPowerShell\vs2010.ps1"
$VSDir = 'C:\Program Files (x86)\Microsoft Visual Studio 11.0'
if (!(test-path $VSDir)) {
write-host "no VS found"
return
}
$depA = -not (Test-FileDep "$VSDir\Common7\Tools\VsDevCmd.bat" $psvsvarFile)
$depB = -not (Test-FileDep $MyInvocation.ScriptName $psvsvarFile)
if ($depA -or $depB) {
Write-Host -ForegroundColor yellow "regen vsvars"
if (Test-path $psvsvarFile ) {
Remove-Item -Force $psvsvarFile
}
pushd "$VSDir\VC"
cmd /c "vcvarsall.bat&set" |
foreach {
if ($_ -match "=") {
$v = $_.split("="); "set-item -force -path 'ENV:\$($v[0])' -value '$($v[1])'" | Out-File -Append -FilePath $psvsvarFile
}
}
popd
}
Write-Info "VS2010"
. $psvsvarFile
}
Set-VisualStudioEnvVars
Update-Path 'C:\bin'
Update-Path 'C:\Tools\gittfs'
Update-Path 'C:\Program Files\Microsoft SQL Server\110\Tools\Binn'
Update-Path 'C:\Program Files\Microsoft SQL Server\110\DTS\Binn'
Update-Path 'c:\strawberry\perl\bin'
Update-Path 'c:\strawberry\bin'
Update-Path 'C:\Program Files\Java\jre7\bin'
function Reload-Profile {
@(
$Profile.AllUsersAllHosts,
$Profile.AllUsersCurrentHost,
$Profile.CurrentUserAllHosts,
$Profile.CurrentUserCurrentHost
) | % {
if(Test-Path $_){
Write-Verbose "Running $_"
. $_
}
}
}
function Is-PathRooted($p = (Get-Location)) {
[io.Path]::GetPathRoot($p) -eq [io.Path]::GetFullPath($p)
}
#
# TFS
#
function Get-TfsShelvedSets($owner = '*') {
tf shelvesets /owner:$owner | ForEach-Object {
$_.SubString(0, 64).Trim()
}
}
function Get-TfsShelvedSet($name, $owner='*') {
tf shelvesets /owner:$owner /format:Detailed $name
}
#
# SQL Server
#
function New-SqlServerAlias($name, $server='.', $protocol='tcp', $port='1433') {
$options = New-Object System.Management.ConnectionOptions
$options.Context.Add('__ProviderArchitecture', 64)
$path = '\\.\root\microsoft\sqlserver\computermanagement11'
$scope = New-Object System.Management.ManagementScope($path, $options)
$scope.Connect()
$managementPath = New-Object System.Management.ManagementPath('SqlServerAlias')
$class = New-Object System.Management.ManagementClass($scope, $managementPath, $null)
$alias = $class.CreateInstance()
$alias.SetPropertyValue('AliasName', $name)
$alias.SetPropertyValue('ServerName', $server)
$alias.SetPropertyValue('ProtocolName', $protocol)
$alias.SetPropertyValue('ConnectionString', $port)
$alias.Put()
}
#
# Development Workflow
#
function Find-DominatingFile($name) {
$dir = Get-Location
while ($true) {
$target = Join-Path $dir $name
if (Test-Path $target) {
return $target
}
$dir = [io.Path]::GetDirectoryName($dir)
if (Is-PathRooted $dir) {
return $null
}
}
}
$UsingProject = ''
function Set-Project($name) {
switch -regex ($name) {
'(Riverwalk|4\.6|46|Services)' {
$env:TFSROOT = "http://tfs.calicoenergy.local:8080/tfs/calico"
$global:TFSROOT = $env:TFSROOT
$global:UsingProject = 'Services'
}
'(Phoenix|Eng|Engineering|4.7|4.8)' {
$env:TFSROOT = "http://hqvm32.calicoenergy.local:8080/tfs/Calico"
$global:TFSROOT = $env:TFSROOT
$global:UsingProject = 'Engineering'
}
default { 'Unknow project name.' }
}
}
function Get-DominatingSolutionFile {
$git = Find-DominatingFile '.git'
$fullPathToSln = if ($git) {
$sln = switch ($global:UsingProject) {
'Services' { 'BusinessLayer\EIS.Calico.Full.sln' }
'Engineering' { 'EISPrimary\EISPrimary.sln' }
}
$root = [io.Path]::GetDirectoryName($git)
return (Join-Path $root $sln)
} else {
$sln = switch ($global:UsingProject) {
'Services' { 'C:\Calico\EISPrimary-Release4.6\BusinessLayer\EIS.Calico.Full.sln' }
'Enginerring' { 'C:\Calico\Dev\EISPrimary\EISPrimary.sln' }
}
if (-not (Test-Path $sln)) { throw "Couldn't find the solution." }
return $sln
}
}
function Get-SolutionRoot() {
$sln = Get-DominatingSolutionFile
[io.Path]::GetDirectoryName($sln)
}
function Get-Feature($kw, $root = (Get-SolutionRoot)) {
if ($global:UsingProject -ne 'Engineering') { throw 'This only works in the Engineering repo.' }
$acceptanceTestProjects = Get-ChildItem -Path $root `
| Where-Object { $_.Name -match 'Acceptance' }
$featureFiles = ForEach ($project in $acceptanceTestProjects) {
Get-ChildItem -Path $project.FullName -Include *.feature -Recurse
}
ForEach ($featureFile in $featureFiles) {
Get-Content $featureFile `
| Where-Object { $_ -match "Scenario.*$($kw)" } `
| ForEach { "$($featureFile.Name): $_" }
}
}
function Get-AllSteps($kw, $root = 'C:\Calico\EIS\Dev\EISPrimary') {
if ($global:UsingProject -ne 'Engineering') { throw 'This only works in the Engineering repo.' }
$acceptanceTestProjects = Get-ChildItem -Path $root `
| Where-Object { $_.Name -match 'Acceptance' }
$stepFiles = ForEach ($project in $acceptanceTestProjects) {
$stepsPath = "$($project.FullName)\Steps"
if (Test-Path $stepsPath) {
Get-ChildItem -Path $stepsPath -Include *.cs -Recurse
}
}
ForEach ($stepFile in $stepFiles) {
Get-Content $stepFile `
| Where-Object { $_ -match "\[Given\(@\`"(.*)\]" } `
| ForEach { "$($stepFile.Name): $($matches[1].TrimEnd('`"\]'))" }
}
}
function Publish-Database($configuration = 'Dev') {
if ($global:UsingProject -ne 'Engineering') { throw 'This only works in the Engineering repo.' }
$solutionFile = Get-DominatingSolutionFile
$solutionDir = [io.Path]::GetDirectoryName($solutionFile)
$databaseProjectDir = Join-Path $solutionDir 'EIS.Database'
Push-Location $databaseProjectDir
msbuild '/t:Build;Publish' `
"/p:SqlPublishProfilePath=EIS.Database.$configuration.publish.xml" `
EIS.Database.sqlproj `
/verbosity:quiet
Pop-Location
}
function Get-BranchName {
(git symbolic-ref HEAD).Substring(11)
}
function update {
$branch = Get-BranchName
if ($branch -ne 'master') {
git add --all
git cim "WIP"
}
git co master
git tfr
if ($branch -ne 'master') {
git co $branch
git rebase master
if (-not $?) {
git reset HEAD~1
}
}
}
function Tag-And-Archive ($name) {
git tag $name $name
git branch -D $name
}
function Clone-TFS($path, [switch] $branches) {
if ($branches) {
git tfs clone $TFSROOT ('$/{0}' -f $path) --with-branches
} else {
git tfs clone $TFSROOT ('$/{0}' -f $path)
}
}
function Get-SafeShelveSetName($name = $(Read-Host -prompt "Branch")) {
$shelveSetName = $name -replace '/', '_'
if ($shelveSetName.Length -gt 64) { $shelveSetName = $shelveSetName.SubString(0, 64) }
$shelveSetName
}
function Get-SafeShelveSetNameForCurrentBranch($prefix = '') {
$name = "$($prefix)$(Get-SafeShelveSetName (Get-BranchName))"
if ($name.Length -gt 64) { $name = $name.Substring(0, 64) }
$name
}
function Create-ShelveSet($prefix = '', [switch] $verbose = $false) {
$shelveSetName = Get-SafeShelveSetNameForCurrentBranch $prefix
$output = git tfs shelve $shelveSetName -f | Out-String
$success = $?
if ($verbose) { Write-Debug "$output`n" }
if ($success) { Write-Info "Created shelveset $shelveSetName`n" }
}
function Create-ShelveSetForCodeReview([switch] $verbose = $false) {
Create-ShelveSet 'CR_'
}
function Build-SandboxBuild($shelveSetName = $null, [switch] $shelve = $false) {
if ($global:UsingProject -ne 'Engineering') { throw 'This only works in the Engineering repo.' }
if ([string]::IsNullOrWhitespace($shelveSetName)) {
$shelveSetName = Get-SafeShelveSetNameForCurrentBranch
}
if ($shelve) { Create-ShelveSet }
$output = TFSBuild.exe start /collection:$env:TFSROOT `
/builddefinition:"\EIS\Sandbox Bld [For MTM with private binaries]" `
/shelveset:$shelveSetName | Out-String
if (-not $?) {
throw "Build failed: $output"
}
if ($output -match 'Build number: (\d+)') {
return $matches[1]
}
}
function Get-SandboxBuildDir($buildNumber) {
if ($global:UsingProject -ne 'Engineering') { throw 'This only works in the Engineering repo.' }
"\\hqdevvm213\TFSBuilds\BuildDrop\Sandbox Bld [For MTM with private binaries]\$buildNumber"
}
function Bulid-SandboxBuildAndCopyThings($thing) {
$buildNumber = Build-SandboxBuild
Copy-Item -Path thing -Destination (Get-SandboxBuildDir $buildNumber)
}
function Open-TheSolution {
$sln = Get-DominatingSolutionFile
if ($sln) { Start-Process $sln }
else { Write-Error "Can't find project root." }
}
function Blame($filenamePattern) {
$matchingFiles = Get-SolutionRoot | Get-ChildItem -Recurse -Include $filenamePattern
foreach ($file in $matchingFiles) {
Write-Host "Annotating $($file):"
git blame ($file.FullName)
}
}
function Log($filenamePattern, [Switch] $source) {
$matchingFiles = Get-SolutionRoot | Get-ChildItem -Recurse -Include $filenamePattern
foreach ($file in $matchingFiles) {
Write-Host "Log for $($file):"
git lg ($file.FullName) $(if ($source) { '--source' })
}
}
function Go($where) {
switch -regex ($where) {
'root' { Set-Location (Get-SolutionRoot) }
'temp' { Set-Location 'C:\temp' }
'~' { Set-Location $env:HOME }
'docs' { Set-Location "$env:HOME\Documents" }
'downloads' { Set-Location "$env:HOME\Downloads" }
'desktop' { Set-Location "$env:HOME\Desktop" }
}
}
function Uml-Graph([io.FileInfo] $sourceFile, [string] $format = 'png', [string] $classNames = $null, [switch] $whatIf) {
$classpath = @('C:\Tools\umlgraph-5.6.6.jar',
'C:\Program Files\Java\jdk1.7.0_45\lib\Tools.jar') `
-join ';'
$entryPoint = 'org.umlgraph.doclet.UmlGraph'
$sourceFileName = Resolve-Path $sourceFile
$outputFileName = $sourceFile.BaseName + '.' + $format
$dotFormat = switch($format) {
'png' { '-Tpng' }
'pdf' { '-Tpdf' }
'txt' { '-Tplain-ext' }
'svg' { '-Tsvg' }
}
$outputEncoding = if ($format -eq 'svg') { '-outputencoding utf8' } else { '' }
$hideOption = if ($classNames) { -hide } else { '' }
if ($whatIf) {
Write-Output "java -classpath `"$classpath`" $entryPoint $outputEndcoding -package $hideOption -output - `"$sourceFileName`""
Write-Output "`"C:\tools\graphviz-2.34\release\bin\dot.exe`" $dotFormat -o `"$outputFileName`""
return
}
& java `
-classpath $classpath $entryPoint `
$outputEncoding `
-package `
$hideOption `
-output - $sourceFileName `
| & C:\tools\graphviz-2.34\release\bin\dot.exe `
$dotFormat `
-o $outputFileName
Write-Output (Resolve-Path $outputFileName)
}
function rmf { Remove-Item -Force -Recurse $args }
function gg { git gui }
New-Alias -Force -Name 'll' -Value 'Get-ChildItem'
New-Alias -Force -Name 'ss' -Value 'Select-String'
New-Alias -Force -Name 'e' -Value 'runemacs'
New-Alias -Force -Name 'bsb' -Value 'Bulid-SandboxBuild'
New-Alias -Force -Name 'bcomp' -Value 'C:\Program Files (x86)\Beyond Compare 3\BComp.exe'
Set-Project Eng
@edwinakatosh
Copy link

Thanks. This is very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment