Skip to content

Instantly share code, notes, and snippets.

View mclausen's full-sized avatar
Coding

Martin Humlund Clausen mclausen

Coding
View GitHub Profile
@mclausen
mclausen / Get-GitDiffBetweenMainBranchAndCurrent.ps1
Created September 10, 2024 07:33
Create a git diff file between HEAD on your current branch and HEAD on main branch. Which can be used to generate Pull Requests Descriptions using ChatGPT (See: https://dev.to/martinhc/productivity-hack-of-the-day-creating-pull-requests-descriptions-from-git-diff-1pm0)
# Check if the git command is available
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Host "Git is not installed or not available in the PATH." -ForegroundColor Red
exit
}
# Ensure we're in a git repository
$gitStatus = git rev-parse --is-inside-work-tree 2>&1
if ($gitStatus -ne "true") {
Write-Host "This is not a git repository." -ForegroundColor Red
@mclausen
mclausen / Error.cs
Created June 15, 2024 07:50
Functional error handling in C# with Result, ResultExtensions classes, and Error, ErrorType enums for clean, maintainable code.
public class Error(ErrorType ErrorType, string Description)
{
public static Error Empty => new(ErrorType.Default, string.Empty);
public static Error ValidationError => new(ErrorType.ValidationError, "This is a validation error");
public static Error DomainLogicError => new(ErrorType.Failure, "This is a domain logic error");
}
@mclausen
mclausen / import-secrets-in-notebook.dib
Created December 27, 2021 11:11
Loads .env into a .net interactive notebooks
#r "nuget: dotenv.net, 3.0.0"
using dotenv.net;
using System.IO;
var currentDir = Directory.GetCurrentDirectory();
var secretsFileLocation = Path.Combine(currentDir, "secrets.env");
DotEnv.Load(options: new DotEnvOptions(envFilePaths: new[] { secretsFileLocation }, ignoreExceptions: false));
var mySecret = System.Environment.GetEnvironmentVariable("mysecret");
@mclausen
mclausen / Handlers.cs
Last active September 24, 2021 20:19
Adds an extension method to Umbraco to help registering all notifications handlers at once from within a provided assembly.
public interface IA {}
public interface IB {}
public interface IC<in THandle> where THandle : IA{ }
public class ContentNotificationAsyncHandler : INotificationAsyncHandler<ContentPublishedNotification>
{
private readonly ILogger<ContentNotificationAsyncHandler> _logger;
@mclausen
mclausen / IsExternalInit.cs
Created September 5, 2021 09:50
External init, allowing implicit record property setters
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
@mclausen
mclausen / NamedPropertyWithValueSpecimenBuilder.cs
Last active June 11, 2021 20:48
Generic SpecimenBuilder for AutoFixture to configure value for known Property name
private class NamedPropertyWithValueSpecimenBuilder<TValue> : ISpecimenBuilder
{
private readonly string _parameterName;
private readonly TValue _value;
public NamedPropertyWithValueSpecimenBuilder(string parameterName, TValue value)
{
_parameterName = parameterName;
_value = value;
}
@mclausen
mclausen / NamedParameterWithValueSpecimenBuilder.cs
Last active June 11, 2021 20:48
Generic SpecimenBuilder for AutoFixture to configure value for known constructor parameter name
public class NamedParameterWithValueSpecimenBuilder<TValue> : ISpecimenBuilder
{
private readonly string _parameterName;
private readonly TValue _value;
public NamedParameterWithValueSpecimenBuilder(string parameterName, TValue value)
{
_parameterName = parameterName;
_value = value;
}
@mclausen
mclausen / AutoDataWithCustomizationAttribute.cs
Created June 7, 2021 10:39
AutoData attribute to enable additiona AutoFixturel Customization per Test
public sealed class AutoDataWithCustomizationAttribute : AutoDataAttribute
{
public AutoDataWithCustomizationAttribute(params Type[] customizations) : base(() => new Fixture()
.Customize(new CompositeCustomization(customizations.Select(customization => (ICustomization)Activator.CreateInstance(customization)))))
{
}
}
@mclausen
mclausen / StringExtensions.cs
Created September 16, 2019 07:57
String extensions to interrogate a sql connection string for information
public static class StringExtensions
{
static readonly string[] serverAliases =
{
"server", "host", "data source", "datasource", "address",
"addr", "network address"
};
static readonly string[] databaseAliases = {"database", "initial catalog"};
static readonly string[] usernameAliases = {"user id", "uid", "username", "user name", "user"};
@mclausen
mclausen / VersionTagAzurePipeline.ps1
Created June 15, 2019 08:20
When used in Azure Pipelines the script creates a tag based on the $(Build.BuildNumber) and the $(Build.SourceBranch)
<#
.Synopsis
Creates a tag based on the $(Build.BuildNumber) and the $(Build.SourceBranch)
.Description
This purpose of this script is to create -beta tag if the source branch is different from master branch
We can use this version to tag nuget packages, and docker images with a -beta, so we can distinquish
between live and beta builds
.Example
1. Include this script as the first step in a build pipeline
2. Make sure that you provide a reference name under 'Output variables' in the powershell task