Skip to content

Instantly share code, notes, and snippets.

@derantell
derantell / Job.cs
Last active November 8, 2017 08:43
EPiServer scheduled job template
using EPiServer;
using EPiServer.Logging.Compatibility;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.ServiceLocation;
using System;
using System.Collections.Generic;
namespace <namespace> {
// The docs: http://world.episerver.com/documentation/developer-guides/CMS/scheduled-jobs/
@derantell
derantell / Logger snippet
Last active August 22, 2016 16:11
Visual studio snippet for EPiServer log field
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>EPiServer class logger</Title>
<Description>Adds a static EPiServer Log logger field</Description>
<Shortcut>log</Shortcut>
</Header>
<Snippet>
@derantell
derantell / bumpversion.ps1
Last active June 3, 2016 08:38
Powershell semver bump function
function bumpVersion {
param ( $v, $part = 'patch' )
$v -match '^(\d+)\.(\d+)\.(\d+)(.*)$' | out-null
$major, $minor, $patch, $rest = [int]$matches[1], [int]$matches[2], [int]$matches[3], $matches[4]
switch ($part) {
'major' { $major += 1; $minor = 0; $patch = 0 }
'minor' { $minor += 1; $patch = 0 }
'patch' { $patch += 1 }
@derantell
derantell / OgMetaFetcher.cs
Created September 9, 2015 10:24
Fetch og metadata with CsQuery
// Fetch meta data from a web page
// Depends on CsQuery [https://github.com/jamietre/CsQuery]
// Nuget: install-package csquery
using CsQuery;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@derantell
derantell / gist:5df4df934a1da2259f99
Last active August 29, 2015 14:02
Get formatted byte size from long value
public static string FriendlyFileSize( long size) {
const double @base = 1024d;
const string format = "{0:0.#} {1}";
if (size == 0L) {
return "0 b";
}
var magnitudes = new Dictionary<int, string> {
{0, "B" }, {1, "kB"}, {2, "MB"},
@derantell
derantell / Profiler.cs
Created May 29, 2013 14:36
Simple profiler class for .Net apps.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public static class Profiler {
public static ProfilerTimer Profile(string name) {
return new ProfilerTimer(name);
}
@derantell
derantell / ProcessIndicator.cs
Created May 20, 2013 21:01
C# console process indicator
class ProcessIndicator : IDisposable {
public ProcessIndicator(string message, int count, int step = 1, Func<string, int, int, int, string> getIndicator = null) {
_count = count;
_step = step;
_counter = 0;
_getIndicator = getIndicator ?? Bar;
Console.Write(message + " ");
}
@derantell
derantell / StringRegexExtensions
Created February 18, 2013 15:13
C# string extensions methods wrapping common regex tasks.
public class StringRegexExtensions{
/// <summary>
/// Matches a string against a regular expression.
/// </summary>
/// <param name="self">The string self.</param>
/// <param name="pattern">The regular expression.</param>
/// <returns>True if it is match, otherwise false.</returns>
public static bool IsMatch(this string self, string pattern) {
return Regex.IsMatch(self, pattern);
@derantell
derantell / SubstringExtensions
Created February 18, 2013 15:10
C# substring extensions inspired by XPath's substring-before and substring-after functions.
public static class SubstringExtensions {
/// <summary>
/// Gets the substring after the first occurence of the specified character.
/// </summary>
/// <param name="self">The string self.</param>
/// <param name="ch">The character.</param>
/// <returns>The substring after the first occurrence of the specified character. If
/// The character does not occur in the string, the empty string is returned.</returns>
public static string SubstringAfter(this string self, char ch) {
int index = self == null ? -1 : self.IndexOf(ch);