Skip to content

Instantly share code, notes, and snippets.

View refactorsaurusrex's full-sized avatar

refactorsaurusrex

View GitHub Profile
@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 / 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 / JsonDeserializer.cs
Last active October 15, 2018 17:09
Debugging RestSharp Deserialization Errors http://wp.me/p4mdOw-1f9
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
using RestSharp.Deserializers;
namespace MyAwesomeProject
{
public class JsonDeserializer : IDeserializer
{
@refactorsaurusrex
refactorsaurusrex / nuget-update-fails.md
Last active March 13, 2018 20:41
Nuget 'update' command fails: 'Microsoft.CSharp.Core.targets' was not found

When running nuget update 'some.solution.sln' -id 'Some.Package.Id' yesterday, I got the following error:

Scanning for projects...
MSBuild auto-detection: using msbuild version '15.4.8.50001' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\
Enterprise\MSBuild\15.0\bin'.
The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Roslyn\
Microsoft.CSharp.Core.targets" was not found. Confirm that the path in the <Import> declaration is correct, 
and that the file exists on disk.  C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\
Microsoft.CSharp.CurrentVersion.targets
@refactorsaurusrex
refactorsaurusrex / DeleteLocalGitBranches.ps1
Last active August 18, 2016 14:31
Delete all local git branches
function Delete-LocalBranches ($Commit = 'HEAD', [switch]$Force) {
git branch |
? { $_ -notmatch '(^\*)|(^. master$)' } |
% { git branch $(if($Force) { '-D' } else { "-d" }) $_.Substring(2) }
}
@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 / Program.cs
Last active April 23, 2016 02:51
Lists all currently running IIS applications, along with their app pool names and process ids.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListAllWebApps
{
class Program
@refactorsaurusrex
refactorsaurusrex / pie.cs
Created December 12, 2015 03:31
C# Pie!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Confections
{
public class Pie<T> : IReadOnlyCollection<Slice<T>>
{
readonly List<Slice<T>> slices;
@refactorsaurusrex
refactorsaurusrex / GetFiles.cs
Created November 2, 2015 22:26
Recursively navigate the file system, skipping any files or directories that are not accessible.
public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
Stack<string> pending = new Stack<string>();
pending.Push(root);
while (pending.Count != 0)
{
var path = pending.Pop();
string[] next = null;
try
{
@refactorsaurusrex
refactorsaurusrex / selectMenu.html
Created July 4, 2015 19:03
How To Bind A JQueryUI SelectMenu Widget To KnockOut
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/sunny/jquery-ui.css">
<script>
$(document).ready(function() {