ClockExtension
Sample¶The ClockExtension
sample walks you through how to create a simple .NET Interactive extension and then package it using NuGet.
If you opened this notebook so that its working directory is in the ClockExtension
project directory, then the following cell will work. Otherwise, you should first switch your working directory to the directory containing this notebook.
# 1. Build the project
dotnet build ClockExtension
# Clear any older versions of this extension package from your NuGet cache
rm ~/.nuget/packages/ClockExtension -Force -Recurse -ErrorAction Ignore
# Pack up the NuGet package.
dotnet pack ClockExtension /p:PackageVersion=1.0.0
# 3. Check that the package is there
Get-ChildItem -Recurse ClockExtension*.nupkg
MSBuild version 17.5.0-preview-22620-02+a6f6699d1 for .NET Determining projects to restore... All projects are up-to-date for restore. C:\Program Files\dotnet\sdk\7.0.200-preview.22628.1\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\dev\interactive\samples\extensions\ClockExtension\ClockExtension.csproj] ClockExtension -> C:\dev\interactive\samples\extensions\ClockExtension\bin\Debug\net7.0\ClockExtension.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.97 MSBuild version 17.5.0-preview-22620-02+a6f6699d1 for .NET Determining projects to restore... All projects are up-to-date for restore. C:\Program Files\dotnet\sdk\7.0.200-preview.22628.1\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\dev\interactive\samples\extensions\ClockExtension\ClockExtension.csproj] ClockExtension -> C:\dev\interactive\samples\extensions\ClockExtension\bin\Debug\net7.0\ClockExtension.dll Directory: C:\dev\interactive\samples\extensions\ClockExtension\bin\Debug Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/19/2023 3:34 PM 9214 ClockExtension.1.0.0.nupkg -a--- 1/19/2023 3:41 PM 9210 ClockExtension.1.0.1.nupkg
Now we're ready to install the extension that packaged up in the NuGet package we just built. We can use the #i
directive to add the build output folder to our NuGet sources.
First, let's make sure the file is there like we expect after the build.
using System.IO;
// Create an absolute path since #i doesn't like
var debugOutputFolder = new DirectoryInfo(@".\ClockExtension\bin\Debug\").FullName;
if (File.Exists(Path.Combine(debugOutputFolder, "ClockExtension.1.0.0.nupkg")))
{
"✅ The package is there!".Display();
}
else
{
"❌ Something must have gone wrong with the build. The package isn't there.".Display();
}
var nugetSource = $"nuget:{debugOutputFolder}";
✅ The package is there!
If the package is there, we can include its location as a NuGet source using the #i
directive. The following syntax shares the #i
argument including the nuget:
prefix and the computed fully-qualified path.
#i @csharp:nugetSource
Now that the package source is added, we can use #r
to install the package.
#r "nuget:ClockExtension,1.0.0"
Loading extension script from `C:\Users\josequ\.nuget\packages\clockextension\1.0.0\interactive-extensions\dotnet\extension.dib`
ClockExtension
is loaded. It adds visualizations for System.DateTime
and System.DateTimeOffset
. Try it by running: DateTime.Now
or #!clock -h
As you can see from the output above, the extension is able to explain a bit about what it does. So now we can try it out.
It adds a custom formatter for System.DateTime
:
DateTime.Now
The extension output also advised us to run #!clock -h
. Extensions can add magic commands and all magic commands can provide help.
#!clock -h
Description: Displays a clock showing the current or specified time. Usage: #!clock [options] Options: -o, --hour <hour> The position of the hour hand -m, --minute <minute> The position of the minute hand -s, --second <second> The position of the second hand -?, -h, --help Show help and usage information
The #!clock
magic command help explains how to use options to set the position of the hands on the clock:
#!clock --hour 12 -m 34 -s 56