Created
December 16, 2016 06:07
-
-
Save BrandynThornton/72faef5845875b611d27394e189f28c0 to your computer and use it in GitHub Desktop.
A few Powershell functions that are helpful to have in psake build scripts. Some have .net in mind and assume a bin folder or NuGet package directory is desired.
This file contains 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
function Find-PackagePath { | |
[CmdletBinding()] | |
param( | |
[Parameter(Position=0, Mandatory=1)]$packagesPath, | |
[Parameter(Position=1, Mandatory=1)]$packageName | |
) | |
return (Get-ChildItem ($packagesPath + "\" + $packageName + "*")).FullName | Sort-Object $_ | select -Last 1 | |
} | |
function Prepare-Tests { | |
[CmdletBinding()] | |
param( | |
[Parameter(Position=0, Mandatory=1)]$testRunnerName, | |
[Parameter(Position=1, Mandatory=1)]$publishedTestsDirectory, | |
[Parameter(Position=2, Mandatory=1)]$testResultsDirectory | |
) | |
$projects = Get-ChildItem $publishedTestsDirectory -Filter *Tests | |
Write-Host "$($projects.Count) $testRunnerName project(s) have been found:" | |
Write-Host ($prjects | Select $_.Name) | |
# Create the test results directory if needed | |
if(!(Test-Path $testResultsDirectory)) | |
{ | |
Write-Host "Creating test results directory located at $testResultsDirectory" | |
mkdir $testResultsDirectory | Out-Null | |
} | |
# Get the list of test DLLs | |
$testAssembliesPaths = $projects | ForEach-Object {$_.FullName + "\bin\" + $_.Name + ".dll" } | |
$testAssemblies = [string]::Join(" ", $testAssembliesPaths) | |
return $testAssemblies | |
} | |
function Find-NuGetPackagePaths { | |
[CmdletBinding()] | |
param( | |
[Parameter(Position=0, Mandatory=1)]$repositoriesPath | |
) | |
Write-Host $(Get-Location) | |
if(!(Test-Path $repositoriesPath)) | |
{ | |
throw [System.IO.FileNotFoundException] "$repositoriesPath not found." | |
} | |
[xml]$repositoryConfig = Get-Content -Path $repositoriesPath | |
return $repositoryConfig.repositories.repository.path | |
} | |
function Get-GitCommit { | |
$gitLog = git log --oneline -1 | |
return $gitLog.Split(' ')[0] | |
} | |
function Get-GitBranchName { | |
return git rev-parse --abbrev-ref HEAD | |
} | |
function CopyDirectory($source, $destination, [regex]$exclude="a^", [regex]$include=".", [switch]$Recurse, [switch]$Flatten) { | |
if(!(Test-Path $destination)) | |
{ | |
mkdir $destination | |
} | |
$source = Resolve-Path $source | |
Get-ChildItem $source -Recurse:$Recurse | ?{ $_.FullName -notmatch $exclude } | ?{ $_.FullName -match $include } ` | |
| %{ | |
if($Flatten) | |
{ | |
$newDestination = Join-Path $destination $_.Name | |
} | |
else | |
{ | |
$newDestination = Join-Path $destination $_.FullName.Substring($source.path.length) | |
} | |
if($_.PSIsContainer) | |
{ | |
Copy-Item $_.FullName -Destination $newDestination -Force | |
} | |
else | |
{ | |
New-Item -ItemType File -Path $newDestination -Force | |
Copy-Item $_.FullName $newDestination -Force | |
} | |
} | |
} | |
function CopyFiles($source, $destination, [regex]$exclude="a^", [regex]$include=".", [switch]$Recurse, [switch]$Flatten) { | |
if(!(Test-Path $destination)) | |
{ | |
mkdir $destination | |
} | |
$source = Resolve-Path $source | |
Get-ChildItem $source -Recurse:$Recurse | ?{ ! $_.PSIsContainer } | ?{ $_.FullName -notmatch $exclude } | ?{ $_.FullName -match $include } ` | |
| %{ | |
if($Flatten) | |
{ | |
$newDestination = Join-Path $destination $_.Name | |
} | |
else | |
{ | |
$relativePath = $_.FullName.Substring($source.path.length) | |
if([string]::IsNullOrEmpty($relativePath)) | |
{ | |
#We passed in a file source, we should pass in a file destination | |
$newDestination = $destination | |
} | |
else | |
{ | |
$newDestination = Join-Path $destination $_.FullName.Substring($source.path.length) | |
} | |
} | |
New-Item -ItemType File -Path $newDestination -Force | |
Copy-Item $_.FullName $newDestination -Force | |
} | |
} | |
function ReplaceInFile($source, $oldVal, $newVal) { | |
if(!(Test-Path $source)) | |
{ | |
return | |
} | |
Write-Host "Replacing $source" | |
$try = 0 | |
$limit = 3 | |
while (1) | |
{ | |
$try++ | |
try | |
{ | |
#Set-Content $source throws 'Exception: Stream was not readable.' unreliably | |
#might switch to %{[System.IO.File]::WriteAllLines($source,$_)} but messes with newlines | |
(Get-Content $source) -Replace $oldVal, $newVal | Set-Content $source | |
break; | |
} | |
catch | |
{ | |
if ($try -lt $limit) | |
{ | |
Sleep -Seconds 1 | |
Write-Host "retrying..." -ForegroundColor Red | |
} | |
else | |
{ | |
throw | |
} | |
} | |
} | |
} | |
function EnsureDirExists($path) { | |
if(!(Test-Path $path)) | |
{ | |
mkdir $path | |
} | |
} | |
function Remove-Directory($path, [switch]$Recurse, [switch]$Force) { | |
Get-ChildItem -Path $path -Recurse:$Recurse -Force:$Force | Where-Object { -not ($_.psiscontainer) } | Remove-Item –Force | |
Remove-Item -Recurse:$Recurse -Force:$Force $path | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment