Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Kuech chriskuech

  • Datum Source, ex MSFT
View GitHub Profile
View medium-fp-callbackexample.ps1
function Invoke-AfterEach {
Param(
[scriptblock[]]$ScriptBlock,
[scriptblock]$OnTaskComplete
)
$ScriptBlock | % {
Start-Job {
Param($action, $callback)
&$action
&$callback
View medium-modules-general.ps1
$modulesDir = $env:PSModulePath -split ":" | Select -First 1
Move-Item .\MyModule $modulesDir
View medium-de-shallow.ps1
function Assert-ArrayEquality($test, $expected) {
$test | Should -HaveCount $expected.Count
0..($test.Count - 1) | % {$test[$_] | Should -Be $expected[$_]}
}
function Assert-HashtableEquality($test, $expected) {
$test.Keys | Should -HaveCount $expected.Keys.Count
$test.Keys | % {$test[$_] | Should -Be $expected[$_]}
}
View tryfinally-6.ps1
function Invoke-WithStorageAccount {
Param(
[ValidateNotNullOrEmpty()]
[scriptblock]$ScriptBlock
)
$name = ("s$(New-Guid)" -replace "-").Substring(24)
$storageAccount = New-AzStorageAccount `
-ResourceGroupName "ephemeral-storages" `
-Name $name`
View tryfinally-5.ps1
Invoke-InDirectory $RepoRoot {
Invoke-InContext "Build ReactJS App" {
Invoke-InContext "Validate" {
Invoke-InContext "Lint" { yarn lint }
Invoke-InContext "Test" { yarn test }
}
Invoke-InContext "Transpile" { yarn build:ts }
Invoke-InContext "Bundle" { yarn webpack }
}
Invoke-InContext "Build Container" { docker build . }
View tryfinally-4.ps1
# ContextLogger.psm1
$ContextStack = [System.Collections.Stack]::new()
function Invoke-InContext {
Param(
[ValidateNotNullOrEmpty()]
[string] $Name,
[ValidateNotNullOrEmpty()]
[scriptblock] $ScriptBlock
View tryfinally-3.ps1
# invoke in repo root
Invoke-InDirectory "$PSScriptRoot/.." {
Invoke-InDirectory "./components" {
npm install
npm build
}
Invoke-InDirectory "./server" {
npm install
npm build
}
View tryfinally-2.ps1
function Invoke-InDirectory {
Param(
[ValidateScript( { Test-Path $_ -PathType Container } )]
[string] $Path,
[ValidateNotNullOrEmpty()]
[scriptblock] $ScriptBlock
)
try {
Push-Location $Path
View tryfinally-1.ps1
$ErrorActionPreference = "Stop"
Push-Location "./path/I/need/to/be/in"
try {
# do something here
} finally {
Pop-Location
}
View medium-de-recursivenaive.ps1
# modified from https://github.com/chriskuech/functional
function recursiveEquality($a, $b) {
# recursive step for arrays
if ($a -is [array] -and $b -is [array]) {
if ($a.Count -ne $b.Count) {
return $false
}
$inequalIndexes = 0..($a.Count - 1) | ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalIndexes.Count -eq 0
}