Skip to content

Instantly share code, notes, and snippets.

View jamescrowley's full-sized avatar

James Crowley jamescrowley

View GitHub Profile
@jamescrowley
jamescrowley / UpgradeTentacle.ps1
Created February 5, 2014 14:00
Powershell script for updating Octopus Tentacles to 2.x
function Uninstall-OldTentacle {
Write-Output "Uninstalling the 1.0 Tentacle"
$app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name = 'Octopus Deploy Tentacle' AND Version < 2.0"
$app.Uninstall()
& sc.exe delete "Octopus Tentacle"
}
function Upgrade-Tentacle
@jamescrowley
jamescrowley / gulpfile.js
Last active August 29, 2015 13:56
Output a file per folder in gulp
var fs = require('fs');
var path = require('path');
var es = require('event-stream');
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var scriptsPath = './src/scripts/';
@jamescrowley
jamescrowley / orderedDirectory.js
Created February 21, 2014 11:09
Get files in folder in specific order
function readJson(file) {
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
function getFilesInFolder(dir) {
console.log("Exploring " + dir);
return fs.readdirSync(dir)
.map(function(fileName) {
var filePath = path.join(dir, fileName);
var stat = fs.statSync(filePath);
@jamescrowley
jamescrowley / GetCookie.ps1
Last active August 29, 2015 13:56
Get ASP.NET auth cookie for site with CSRF protection enabled using PowerShell
function Get-SessionCookie($url,$username,$password,$cookieName)
{
$loginResponse = Invoke-WebRequest $url -SessionVariable ws -UseBasicParsing
$requestVerificationToken = ($loginResponse.InputFields | Where { $_.name -eq "__RequestVerificationToken" }).value
$body = @{
"__RequestVerificationToken" = $requestVerificationToken;
"UserName" = $username;
"Password" = $password;
}
$loggedInResponse = Invoke-WebRequest $url -Body $body -Method POST -WebSession $ws -UseBasicParsing
@jamescrowley
jamescrowley / HtmlRequestValidationModelBinder.cs
Created February 22, 2014 13:14
Return validation errors instead of an exception for HttpRequestValidation errors
public class HtmlRequestValidationModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
try
{
return base.BindModel(controllerContext, bindingContext);
}
catch (HttpRequestValidationException)
{
@jamescrowley
jamescrowley / AntiForgeryTokenFilter.cs
Last active August 29, 2015 13:56
Global CSRF checking
public class AntiForgeryTokenFilter : IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (IsHttpPostRequest(filterContext) && !SkipCsrfCheck(filterContext))
AntiForgery.Validate();
}
private static bool IsHttpPostRequest(AuthorizationContext filterContext)
{
@jamescrowley
jamescrowley / DateTimeExtensions.cs
Created March 27, 2014 10:43
Calculate working days (per country)
public static DateTime AddWorkingDays(this DateTime date, int workingDaysToAdd, string countryCode = null)
{
return AddWorkingDays(date, workingDaysToAdd, (dateToTest) => IsWorkingDay(dateToTest, countryCode));
}
public static DateTime AddWorkingDays(this DateTime date, int workingDaysToAdd, Func<DateTime,bool> isWorkingDay)
{
var sign = Math.Sign(workingDaysToAdd);
var unsignedDays = Math.Abs(workingDaysToAdd);
for (var i = 0; i < unsignedDays; i++)
@jamescrowley
jamescrowley / Day1.hs
Last active August 29, 2015 13:58
Haskell - Seven languages in Seven Weeks
-- Question 1
allEven :: [Integer] -> [Integer]
allEven [] = []
allEven (h:t) = if even h then h:allEven t else allEven t
allEven2 :: [Integer] -> [Integer]
allEven2 list = [a | a <- list, even a]
-- Question 2
Target "TestCoverage" (fun _ ->
let filters = "-:*.Tests;" # exclude test assemblies from coverage stats
# run NUnit tests via dotCover
!! testAssemblies
|> DotCoverNUnit (fun p -> { p with
Output = artifactsDir @@ "NUnitDotCover.snapshot"
Filters = filters }) nunitOptions
# run the MSpec tests via dotCover
!! testAssemblies
@jamescrowley
jamescrowley / ndepend.fsx
Created April 22, 2014 21:15
NDepend F# make
module Fake.NDepend
open Fake
open System
open System.IO
open System.Text
let getWorkingDir workingDir =
Seq.find isNotNullOrEmpty [workingDir; environVar("teamcity.build.workingDir"); "."]
|> Path.GetFullPath