Skip to content

Instantly share code, notes, and snippets.

Avatar

James Crowley jamescrowley

View GitHub Profile
@jamescrowley
jamescrowley / UpgradeTentacle.ps1
Created Feb 5, 2014
Powershell script for updating Octopus Tentacles to 2.x
View UpgradeTentacle.ps1
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 Aug 29, 2015
Output a file per folder in gulp
View gulpfile.js
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 Feb 21, 2014
Get files in folder in specific order
View orderedDirectory.js
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 Aug 29, 2015
Get ASP.NET auth cookie for site with CSRF protection enabled using PowerShell
View GetCookie.ps1
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 Feb 22, 2014
Return validation errors instead of an exception for HttpRequestValidation errors
View HtmlRequestValidationModelBinder.cs
public class HtmlRequestValidationModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
try
{
return base.BindModel(controllerContext, bindingContext);
}
catch (HttpRequestValidationException)
{
View AntiForgeryTokenFilter.cs
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 Mar 27, 2014
Calculate working days (per country)
View DateTimeExtensions.cs
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 Aug 29, 2015
Haskell - Seven languages in Seven Weeks
View Day1.hs
-- 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
View build.fsx
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
View ndepend.fsx
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