Skip to content

Instantly share code, notes, and snippets.

Avatar

Alastair Smith alastairs

View GitHub Profile
@alastairs
alastairs / Expand-AllArchives
Created Dec 20, 2010
PowerShell one-liner to extract all archives in a directory (requires the PSCX).
View Expand-AllArchives
ls |% { pushd $_; ls *.zip |%{ Expand-Archive $_; rm $_} popd }
View MethodMultipleResponsibilities.cs
public ActionResult Record(int id)
{
Record record = repository.Load(id);
var viewModel = new { record.Id, record.Name, record.Description, record.Url /* etc. */ };
return View(viewModel);
}
View gist:1142957
Write a function that returns true or false depending on whether its input integer is a leap year or not.
A leap year is divisible by 4, but is not otherwise divisible by 100 unless it is also divisible by 400.
2001 is a typical common year
1996 is a typical leap year
1900 is an atypical common year
2000 is an atypical leap year
@alastairs
alastairs / acceptance_tests.cs
Created Aug 14, 2011
Leap Year Calculator First Attempt
View acceptance_tests.cs
[TestFixture]
public class LeapYearCalculatorAcceptanceTests
{
[Test]
public void TestThat_IsLeapYear_ShouldReturnTrue_WhenTheGivenYearIsATypicalLeapYear()
{
const int typicalLeapYear = 1996;
var isLeapYear = LeapYearCalculator.IsLeapYear(typicalLeapYear);
@alastairs
alastairs / FirstImpl.cs
Created Aug 14, 2011
Leap Year Calculator Second Attempt
View FirstImpl.cs
public static bool IsLeapYear(int year)
{
return true;
}
@alastairs
alastairs / gist:1152445
Created Aug 17, 2011
Pretty-print JSON
View gist:1152445
public string GetPrettyPrintedJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
@alastairs
alastairs / get-scriptdirectory.ps1
Created Sep 13, 2011
Function to get the directory in which this script resides.
View get-scriptdirectory.ps1
function Get-ScriptDirectory {
$invocation = (Get-Variable MyInvocation -Scope 1).Value
$script = [IO.FileInfo] $invocation.MyCommand.Path
if ([IO.File]::Exists($script)) {
return (Split-Path $script.Fullname)
} else {
return $null
}
}
@alastairs
alastairs / gist:1219663
Created Sep 15, 2011
Initialise a collection to an extension of another collection
View gist:1219663
private static readonly IEnumerable<Foo> BaseCollectionOfFoo = new List<Foo>
{
new Foo(1),
new Foo(2),
new Foo(3)
};
private static readonly IEnumerable<Foo> AllFoo = new List<Foo>(BaseCollectionOfFoo)
{
new Foo(4)
View Complete a unit of work.cs
using (var session = documentStore.OpenSession())
{
session.Load<Concert>(1)
}
View remove-buildagents.ps1
param([string]$XenServerPoolMaster)
Add-PSSnapIn XenServerPSSnapin
. ".\credentials.ps1"
. ".\teamcity.ps1"
Connect-XenServer -Server $XenServerPoolMaster -Creds (Get-XenServerCredential) -NoWarnNewCertificates -NoWarnCertificates
Get-XenServer:VM -Server $XenServerPoolMaster |? { $_.tags -contains 'active' } |? { $_.name_label -ne $env:COMPUTERNAME } |% {