One of my favorite features of Azure DevOps is its package management feature where you can publish NuGet packages to your own organization’s feed. Packages in your feed can be referenced by other projects in the CI/CD pipeline or through Visual Studio. In this walkthrough I’ll give a simple example of how to use it. I’ll assume you’re comfortable with Azure DevOps. If you’re not familiar with the tool see my four-part series on the subject.
Let’s say we want a package that returns the day of the week (where Saturday = 0, Sunday = 1 … Friday = 6) on the Gregorian or Julian calendar for any date we pass in. And suppose further I want to implement this using Zeller’s congruence algorithm:
So I first create a new .NET Core 2.x class library project in Visual Studio and add a Date class. Then I implement a static function to accept a DateTime and return an integer representing the value of h:
using System; namespace SquareWidget.DayOfWeek.Core { public static class Date { /// <summary> /// Return the day of week for any date using Zeller's congruence /// </summary> /// <param name="date"></param> /// <returns>0=Sat, 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri</returns> public static int DayOfWeek(DateTime date) { int q = date.Day; int month = date.Month; int year = date.Year; if (month == 1) { month = 13; year--; } if (month == 2) { month = 14; year--; } int m = month; int J = year / 100; int K = year % 100; return (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + (5 * J)) % 7; } } }
Then I make sure that my build produces a NuGet package:
Ok good to go. I check in my code to an Azure DevOps project called SquareWidget.DayOfWeek.Core and then go to the DevOps portal. Once there I have to set up a feed. On the left-hand menu click Artifacts and then the New Feed button:I called mine SquareWidgetFeed. The new feed is empty right now. So it’s time to set up a build to automate the NuGet push into the feed. Here’s my build:
No surprises here. The only thing different from a typical CI/CD pipeline is the NuGet push task:
You can see I’m invoking the push command to send the nupkg file to the new feed. After the build runs I can go to the Artifacts tab and see the NuGet package that has been published there:
Now the package is available for my other projects. In Visual Studio I can add a new feed to my NuGet package manager settings called SquareWidgetFeed. (This is under Options > NuGet Package Manager > Package Sources.) In Azure DevOps you click on the “Connect to feed” button at the top to get the URL to the feed. Then I can call Install-Package or search for it through the menu. Once the package is added to my console app I can call the function:
using SquareWidget.DayOfWeek.Core; using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var date = new DateTime(1517, 10, 31); // Luther's "95 Theses" disputation var h = Date.DayOfWeek(date); Console.WriteLine(date.ToShortDateString() + " was on a " + GetDayOfWeek(h)); Console.ReadLine(); } static string GetDayOfWeek(int d) { switch (d) { case 0: return "Saturday"; case 1: return "Sunday"; case 2: return "Monday"; case 3: return "Tuesday"; case 4: return "Wednesday"; case 5: return "Thursday"; case 6: return "Friday"; default: return "Unknown day of week!"; } } } }
I get back a 4 (Wednesday). I always pictured Luther nailing the theses to the church door on a Sunday for some reason. I guess history doesn’t always have a flair for the dramatic.