Skip to content

Instantly share code, notes, and snippets.

View refactorsaurusrex's full-sized avatar

refactorsaurusrex

View GitHub Profile
@refactorsaurusrex
refactorsaurusrex / maybe.cs
Created May 22, 2016 20:46
Maybe<T>, inspired by Mark Seemann
public class Maybe<T> : IEnumerable<T>
{
readonly IEnumerable<T> values;
public Maybe()
{
values = new T[0];
}
public Maybe(T value)
@refactorsaurusrex
refactorsaurusrex / wordBoundaries.cs
Created May 26, 2016 20:02
A better way to add line endings at specific positions in a long text string.
var text = "A long string....";
var words = text.Split(' ');
var allLines = words.Skip(1).Aggregate(words.Take(1).ToList(), (lines, word) =>
{
if (lines.Last().Length + word.Length >= 80)
lines.Add(word);
else
lines[lines.Count - 1] += " " + word;
return lines;
});
@refactorsaurusrex
refactorsaurusrex / jenkinsfile.txt
Last active January 7, 2020 18:55
Jenkinsfile: How to get the author name for the HEAD commit
AUTHOR_NAME = bat (
script: "git show -s --format='%%an' HEAD",
returnStdout: true
).split('\r\n')[2].trim()
echo "The last commit was written by ${AUTHOR_NAME}."
echo "PS - Jenkins sucks giant monkey balls."
@refactorsaurusrex
refactorsaurusrex / git.md
Last active April 13, 2024 16:31
How to retroactively apply gitignore to a repo
  1. Add or update a .gitignore file
  2. Rebase your repo such that the .gitignore from step one is positioned in history prior to the naughty file(s) being commited.
  3. Run this command, where <commit> is the hash for the commit respresented by step 1: git rebase -ix 'git rm -r --cache . && git add . && git commit --amend --no-edit' <commit>
  4. Resolve conflicts as necessary.

Note that if there are only a few file types or directories or individual files, filter-branch is a simpler route to take. These steps are only necessary for large numbers of files / file types. Also note that there may be better ways still of doing this. :-)

@refactorsaurusrex
refactorsaurusrex / jenkinsfile.groovy
Created May 22, 2017 23:07
Jenkins Pipeline: How to get the status of the previous build
node('whatever') {
stage('blah') {
echo currentBuild.getPreviousBuild().result
}
}
// ref: https://support.cloudbees.com/hc/en-us/articles/217591038-How-to-Iterate-Through-the-Last-Successful-Builds-in-Pipeline-Job
@refactorsaurusrex
refactorsaurusrex / zero29.bat
Last active January 7, 2020 18:49
Install zero29 with Chocolatey!
choco install zero29 -source https://nuget.org/api/v2/
@refactorsaurusrex
refactorsaurusrex / appendAllLines.ps1
Last active August 1, 2023 12:53
How to call 'File.AppendAllLines' with PowerShell
$path = 'C:\text.txt'
$output = 'This is an output string'
# This works...
[System.IO.File]::WriteAllLines($path, $output)
# But this doesn't. WTF!
[System.IO.File]::AppendAllLines($path, $output)
# Result: 'Cannot find an overload for "AppendAllLines" and the argument count: "2".'
@refactorsaurusrex
refactorsaurusrex / Open-Solution.psd1
Last active February 1, 2024 03:21
PowerShell function to open the first Visual Studio solution file found within the current directory.
@{
RootModule = 'Open-Solution.psm1'
ModuleVersion = '0.1.0'
GUID = '42f8cad0-c32a-4ccd-9401-de4bdbafdd65'
Author = 'Nick Spreitzer'
FunctionsToExport = @('Open-Solution')
AliasesToExport = @('sln')
}
@refactorsaurusrex
refactorsaurusrex / prepare-commit-msg.sh
Last active December 23, 2019 14:58
git 'prepare commit msg' hook that rejects merge commits when the source branch is behind the target branch
#!/bin/sh
if [ "$2" = "merge" ]; then
githead=$(env | grep GITHEAD)
source="${githead##*=}"
target=$(git symbolic-ref HEAD)
behind=$(git rev-list --left-only --count $target...$source)
if [ $behind -gt 0 ]; then
echo "Aborting: The source branch is $behind commits behind the target branch."
exit 1
fi
@refactorsaurusrex
refactorsaurusrex / process-vs-continue.ps1
Last active July 28, 2021 05:27
PowerShell: What's the difference between ShouldContinue and ShouldProcess?
# The default is High. Setting it here for clarity.
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1
$ConfirmPreference = 'High'
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[switch]$Force
)
# Use this to prompt the user by default. Use -Force to disable prompting.