Skip to content

Instantly share code, notes, and snippets.

@AlexVallat
AlexVallat / msbuild.csproj
Created September 7, 2021 09:38
Publish only the appsettings.environment.json for the publish profile
<ItemGroup Condition="'$(WebPublishProfileFile)' != ''">
<!-- When publishing, publish only the app settings that match the profile being published -->
<Content Update="appsettings.*.json" CopyToPublishDirectory="Never" />
<Content Update="appsettings.$([System.IO.Path]::GetFileNameWithoutExtension('$(WebPublishProfileFile)')).json" CopyToPublishDirectory="Always" />
</ItemGroup>
@AlexVallat
AlexVallat / README.md
Created April 2, 2021 09:29
Force enable "Accept the Risk and Continue" button for HSTS

HSTS says that the browser must not allow exceptions.

As a user I say that the browser must allow whatever I tell it to allow.

Add this to your userContent.css file to get the button back

meme

@AlexVallat
AlexVallat / Directory.Build.targets
Created March 4, 2021 18:33
Auto override Nugets with local assembly references for local development
<Project>
<ItemGroup>
<!-- Customise this section to list all the locally developed nugets and which locally built assembly they should be replaced by -->
<_ReplacePackageReference Include="Some.Locally.Developed.Nuget" HintPath="C:\Dev\SomeLocalDevFolder\bin\Debug\netstandard2.1\Some.Locally.Developed.Nuget.dll" />
<_ReplacePackageReference Include="Another.Package" HintPath="C:\Dev\SomeOtherDevFolder\bin\Debug\netstandard2.1\Another.dll" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'DEBUG'">
<_UnwantedReplacements Include="@(_ReplacePackageReference)" Exclude="@(PackageReference)" />
<_PackagesToReplace Include="@(_ReplacePackageReference)" Exclude="@(_UnwantedReplacements)" />
@AlexVallat
AlexVallat / msbuild-itemgroup-intersection.md
Created March 4, 2021 18:20
MSBuild ItemGroup Intersection

It's not obvious how to produce an intersection of two itemgroups in MSBuild. There's this brainteaser solution based on tricky metadata behaviour, but it only works inside a <target> due to it's use of metadata.

There is another way. Algebra tells us that A ∩ B = A ∖ (A∖B): intersection of A and B is A excluding (A excluding B). This can be expressed in MSBuild as:

<ItemGroup>
	<A Include="1" />
	<A Include="2" />
	<A Include="3" />
@AlexVallat
AlexVallat / async-patterns.md
Last active January 18, 2021 17:57
Async patterns

Useful patterns I have found and used when writing Async code

Note: .ConfigureAwait(false) is omitted from all examples here, but due to poor design decisions in C# should be added to every await when writing library code.

Initialisation

Stephen Cleary, whose blog is an excellent resource for Async, describes The Asynchronous Initialization Pattern which is useful when your class has to do something async on construction. Contruction itself can't be async, but after construction (either by explicit new or being passed an potentially newly constructed instance through DI), you await the initialisation before using it. Note that the initialisation code starts executing on construction.

interface IAsyncInitialization
{
  Task Initialization { get; }
@AlexVallat
AlexVallat / SetTeamsDownloadLocation.ps1
Last active September 5, 2020 15:29
Unpacks electron.asar, patches it to insert an app.setPath('downloads',...) line to override the downloads path, then repacks it.
$DownloadLocation = "$env:TEMP\TeamsDownload"
$ErrorActionPreference = 'Stop'
$DownloadLocation = $DownloadLocation -replace '\\', '/' #Electron wants paths with /
$teamsElectronAsar = "$env:LOCALAPPDATA\Microsoft\Teams\current\resources\electron.asar"
$tempFolderPath = Join-Path $env:TEMP $(New-Guid)
New-Item -Type Directory -Path $tempFolderPath | Out-Null
Push-Location $tempFolderPath
npm install asar --no-fund --no-audit --no-progress --loglevel=error
.\node_modules\.bin\asar extract $teamsElectronAsar .\electron
@AlexVallat
AlexVallat / Open Command Prompt Here.reg
Created January 12, 2019 11:25
Adds "Open command prompt" to folder context menu, and "Open elevated command prompt" to extended folder context menu (Shift+Right Click)
Windows Registry Editor Version 5.00
; Adds "Open command prompt" to folder context menu,
; and "Open elevated command prompt" to extended folder context menu (Shift+Right Click)
;
; For elevated command prompt, requires NirCmd <http://www.nirsoft.net/utils/nircmd.html>
; to be installed somewhere on the system path.
;
; Author: Alex Vallat
@AlexVallat
AlexVallat / ObjectValue.ts
Last active October 18, 2018 08:41
Type-safe object property value references
export interface IObjectValue<T> {
set(value: T): void;
get(): T;
}
export class ObjectValue<K extends keyof T, T> implements IObjectValue<T[K]> {
constructor(private readonly object: T, private readonly key: K) {}
set(value: T[K]) { this.object[this.key] = value; }
get() { return this.object[this.key]; }
}