Skip to content

Instantly share code, notes, and snippets.

@wgross
wgross / Format-Json.ps1
Created January 27, 2017 13:57
Format-Json witthout Newtonsoft.Json
# Taken from: https://github.com/PowerShell/PowerShell/issues/2736
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split '\n' |
% {
if ($_ -match '[\}\]]') {
# This line contains ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent * 2) + $_.TrimStart().Replace(': ', ': ')
@wgross
wgross / LoggerExtensions.cs
Created February 19, 2017 02:33
Adding Callerinfo to Serilog Context
public static class LoggerExtensions
{
public static ILogger WithCallerInfo(this ILogger logger,
[CallerMemberName]string callerMemberName = null,
[CallerFilePath]string callerFilePath = null,
[CallerLineNumber]int callerLineNumber = 0)
{
return logger
// substituting 'Caller' with 'Source' because 'ForContext<T>()' adds property 'SourceContext'
.ForContext("SourceMemberName", callerMemberName)
@wgross
wgross / Measure-Coverage.ps1
Last active February 21, 2017 19:07
Runs OpenCover and ReportGenerator on a dotnet core test project
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[ValidateScript({Test-Path -Path $_})]
$ProjectToMeasure = $PWD, # Allows to pipe in project directiories
[Parameter()]
[switch]$ShowResult
)
begin {
@wgross
wgross / htmlGenerator.ps1
Created July 20, 2017 18:30
powershell html generator syntax experiment
using namespace System.Xml.Linq
@(
"html"
"head"
"body"
)|Foreach-Object -Process {
@"
function global:$_ {
[CmdletBinding()]
@wgross
wgross / SimpleLazy.cs
Last active July 23, 2017 15:54
Most simple lazy implementation. (Contains a Linqad Main methods for verification)
void Main()
{
var lazy = new SimpleLazy<string>(() =>
{
"calculating".Dump();
return "hello";
});
lazy.Value.Dump("1");
lazy.Value.Dump("2");
# https://en.wikipedia.org/wiki/Box-drawing_character
$lightBox = @{
vert = [char]::ConvertFromUtf32(9474)
horz = [char]::ConvertFromUtf32(9472)
leftup = [char]::ConvertFromUtf32(9484)
leftdown = [char]::ConvertFromUtf32(9492)
rightdown = [char]::ConvertFromUtf32(9496)
rightup = [char]::ConvertFromUtf32(9488)
}
@wgross
wgross / AnsiEscapeFormat.ps1
Created June 14, 2018 07:41
Format text with ansi sequences in powershell
# https://github.com/bozho/AnsiColorOut/blob/master/AnsiColorOut/Console.cs
# https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
$esc=[char]0x001b
filter esc {
param(
[ValidateSet("underline","italic")]
[string[]]$Format
)
@wgross
wgross / powershell_context_concept.ps1
Last active October 30, 2018 09:37
POC for powershell delegateish scriptblock with context. Possible use as some kind of builder pattern
class Context {
$Data
}
filter require_context {
if( $_ -is [Context]) {
$_
} else {
throw "missing context"
}
@wgross
wgross / dyn_param.ps1
Created July 24, 2019 17:45
POC: replicate parameters of a called script as parameters of a calling script
using namespace System.Management.Automation
using namespace System.Collections.ObjectModel
function inner {
param(
[int]$Value
)
$Value
}
@wgross
wgross / ClassLogger.cs
Last active February 28, 2020 07:34
Logger facade with using and Caller-attributes
public class ClassLogger
{
private readonly string typeName;
public static ClassLogger Make<T>(T instance)
{
return new ClassLogger(typeof(T).Name);
}
public ClassLogger(string typeName)