Creating a tsexec command

August 21, 2020typscriptbatch

I want to use TypeScript as a scripting language for my machine and so I want to be able just to execute TypeScript files directly. There are npm packages like ts-node that do this already but I wanted to have a crack at implementing it myself. So this is the batch file I've come up with so far.

Read More

Checking for uncommited git changes in a batch program

July 22, 2020batchgit

Sometimes it's helpful to exit out of a batch script if there are pending changes to a repo. This is done in batch script in a non intuitive way.

@ECHO OFF
PUSHD %~dp0

SET __HAS_CHANGES=0

REM The loop won't be executed if 'git status -s' doesn't produce any output.
FOR /f "tokens=*" %%i IN ('git status -s') DO ( SET __HAS_CHANGES=1 )

REM Check if the loop was executed and goto EXIT if it was.
IF "%__HAS_CHANGES%" NEQ "0" (
    ECHO There are currently uncommitted changes.
    GOTO EXIT
)

REM Do the work you want to do here.

:EXIT
POPD

Enum Extension Methods

Did you know you can create extension methods for enums?

Read More

Disable C# null checks for a field or property

January 2, 2020.netc#nrtnullable

The new nullable reference types in C# are great but sometimes you might need to disable it for a single field or property. You can do that by assigning the value to null and using the ! operator.

Read More

Use nameof() to set a string variable to its name

December 17, 2019.netc#

This is one of those things that should have just been obvious to me but once I saw it I wondered why I had never thought to use it myself. nameof() can be used when declaring a variable to set the value of the variable to the name of the variable:

public const string MyVariable1 = nameof(MyVariable1);

public static void Main()
{
    Console.WriteLine(MyVariable1);
}
Read More