TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Mini Project: Solution, CPM & a Private Package

Mini Project: Solution, CPM & a Private Package

Time to put the course together. You'll build a small repository the way a team would set it up on day one:

  • a class library Ada.TextKit (slugs and titles for blog posts), using Humanizer;
  • a console app that uses the library through a project reference;
  • xunit tests for the library;
  • shared settings in Directory.Build.props, package versions in Directory.Packages.props (CPM with transitive pinning), lock files, and a .slnx solution.

Then the library is packed with full metadata, pushed to the local feed, and consumed by a separate app that knows nothing about the repository.

The layout

text
textkit/
├── TextKit.slnx
├── global.json
├── nuget.config
├── Directory.Build.props        shared settings (warnings as errors, lock files, package metadata)
├── Directory.Packages.props     every package version, transitive pinning
├── src/
│   ├── Ada.TextKit/             the library (packable)
│   └── TextKit.Cli/             the console app (not packable)
└── tests/
    └── Ada.TextKit.Tests/       xunit tests (not packable)

Part 1: build and test the solution

bash · try it yourself
mkdir textkit && cd textkit
git init -q
dotnet new gitignore > /dev/null
dotnet new globaljson --sdk-version 10.0.402 --roll-forward latestFeature > /dev/null
cat > nuget.config <<'XML'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="local" value="/opt/nuget-feed" />
  </packageSources>
</configuration>
XML
cat > Directory.Build.props <<'XML'
<Project>
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
    <IsPackable>false</IsPackable>
    <Authors>Ada Lovelace</Authors>
    <Company>Ada Industries</Company>
  </PropertyGroup>
</Project>
XML
cat > Directory.Packages.props <<'XML'
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
    <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Humanizer.Core" Version="2.14.1" />
    <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
    <PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
    <PackageVersion Include="xunit" Version="2.9.3" />
    <PackageVersion Include="xunit.runner.visualstudio" Version="3.1.4" />
  </ItemGroup>
</Project>
XML
mkdir -p src/Ada.TextKit src/TextKit.Cli tests/Ada.TextKit.Tests
cat > src/Ada.TextKit/Ada.TextKit.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <IsPackable>true</IsPackable>
    <PackageId>Ada.TextKit</PackageId>
    <VersionPrefix>1.0.0</VersionPrefix>
    <Description>Slugs and titles for blog posts.</Description>
    <PackageTags>slug;text;humanizer</PackageTags>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
    <PackageReadmeFile>README.md</PackageReadmeFile>
  </PropertyGroup>
  <ItemGroup>
    <None Include="README.md" Pack="true" PackagePath="/" />
    <PackageReference Include="Humanizer.Core" />
  </ItemGroup>
</Project>
XML
printf '# Ada.TextKit\n\n`Post.Slug("Hello World")` returns `hello-world`.\n' > src/Ada.TextKit/README.md
cat > src/Ada.TextKit/Post.cs <<'CS'
using Humanizer;

namespace Ada.TextKit;

public static class Post
{
    public static string Slug(string title) => title.Trim().Kebaberize();

    public static string Title(string slug) => slug.Replace('-', ' ').Humanize(LetterCasing.Title);
}
CS
cat > src/TextKit.Cli/TextKit.Cli.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="../Ada.TextKit/Ada.TextKit.csproj" />
  </ItemGroup>
</Project>
XML
cat > src/TextKit.Cli/Program.cs <<'CS'
using Ada.TextKit;

foreach (var title in args)
    Console.WriteLine(
quot;{title} -> {Post.Slug(title)} -> {Post.Title(Post.Slug(title))}"); CS cat > tests/Ada.TextKit.Tests/Ada.TextKit.Tests.csproj <<'XML' <Project Sdk="Microsoft.NET.Sdk"> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" /> <PackageReference Include="xunit" /> <PackageReference Include="xunit.runner.visualstudio" /> <ProjectReference Include="../../src/Ada.TextKit/Ada.TextKit.csproj" /> </ItemGroup> <ItemGroup> <Using Include="Xunit" /> </ItemGroup> </Project> XML cat > tests/Ada.TextKit.Tests/PostTests.cs <<'CS' using Ada.TextKit; namespace Ada.TextKit.Tests; public class PostTests { [Theory] [InlineData("Hello World", "hello-world")] [InlineData(" Build Engineering 101 ", "build-engineering-101")] public void Slug_is_kebab_case(string title, string expected) => Assert.Equal(expected, Post.Slug(title)); [Fact] public void Title_round_trips() => Assert.Equal("Hello World", Post.Title(Post.Slug("Hello World"))); } CS dotnet new sln -n TextKit > /dev/null dotnet sln add src/Ada.TextKit src/TextKit.Cli tests/Ada.TextKit.Tests > /dev/null cat TextKit.slnx echo "== restore, build, test" dotnet restore -v:q dotnet build -c Release --no-restore -nologo -v:q | grep -E 'Warn|Err' dotnet test -c Release --no-build -nologo -v:q | grep -E 'Passed!|Failed!' dotnet run --project src/TextKit.Cli -c Release --no-build -- "Hello World" "Ship It Today" echo "== what gets committed" git add -A && git status --porcelain | sed 's/^A //'
<Solution>
  <Folder Name="/src/">
    <Project Path="src/Ada.TextKit/Ada.TextKit.csproj" />
    <Project Path="src/TextKit.Cli/TextKit.Cli.csproj" />
  </Folder>
  <Folder Name="/tests/">
    <Project Path="tests/Ada.TextKit.Tests/Ada.TextKit.Tests.csproj" />
  </Folder>
</Solution>
== restore, build, test
    0 Warning(s)
    0 Error(s)
Passed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 32 ms - Ada.TextKit.Tests.dll (net10.0)
Hello World -> hello-world -> Hello World
Ship It Today -> ship-it-today -> Ship It Today
== what gets committed
.gitignore
Directory.Build.props
Directory.Packages.props
TextKit.slnx
global.json
nuget.config
src/Ada.TextKit/Ada.TextKit.csproj
src/Ada.TextKit/Post.cs
src/Ada.TextKit/README.md
src/Ada.TextKit/packages.lock.json
src/TextKit.Cli/Program.cs
src/TextKit.Cli/TextKit.Cli.csproj
src/TextKit.Cli/packages.lock.json
tests/Ada.TextKit.Tests/Ada.TextKit.Tests.csproj
tests/Ada.TextKit.Tests/PostTests.cs
tests/Ada.TextKit.Tests/packages.lock.json

Check the result against the course:

  • Every project file is short: the target framework, nullable settings, warnings-as-errors, lock files and authorship come from Directory.Build.props; no PackageReference has a version.
  • IsPackable is false by default and true only in the library, so dotnet pack on the solution will produce exactly one package.
  • dotnet sln add filed the projects into /src/ and /tests/ solution folders by itself.
  • git status lists sources, the root files and three packages.lock.json files, and nothing from bin/ or obj/.

Part 2: pack, push and consume

A fresh session, so the first command recreates the repository from Part 1 silently (setup holds exactly the files above). Then the CI-style release: locked restore, ContinuousIntegrationBuild, pack with a version from the "pipeline", push, and a consumer in a different folder.

bash · try it yourself
setup() {
  mkdir textkit && cd textkit && git init -q && dotnet new gitignore > /dev/null
  printf '<configuration><packageSources><clear /><add key="local" value="/opt/nuget-feed" /></packageSources></configuration>\n' > nuget.config
  cat > Directory.Build.props <<'XML'
<Project>
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
    <IsPackable>false</IsPackable>
    <Authors>Ada Lovelace</Authors>
    <Company>Ada Industries</Company>
  </PropertyGroup>
</Project>
XML
  cat > Directory.Packages.props <<'XML'
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
    <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Humanizer.Core" Version="2.14.1" />
  </ItemGroup>
</Project>
XML
  mkdir -p src/Ada.TextKit
  cat > src/Ada.TextKit/Ada.TextKit.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <IsPackable>true</IsPackable>
    <PackageId>Ada.TextKit</PackageId>
    <VersionPrefix>1.0.0</VersionPrefix>
    <Description>Slugs and titles for blog posts.</Description>
    <PackageTags>slug;text;humanizer</PackageTags>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
    <PackageReadmeFile>README.md</PackageReadmeFile>
  </PropertyGroup>
  <ItemGroup>
    <None Include="README.md" Pack="true" PackagePath="/" />
    <PackageReference Include="Humanizer.Core" />
  </ItemGroup>
</Project>
XML
  printf '# Ada.TextKit\n\n`Post.Slug("Hello World")` returns `hello-world`.\n' > src/Ada.TextKit/README.md
  cat > src/Ada.TextKit/Post.cs <<'CS'
using Humanizer;

namespace Ada.TextKit;

public static class Post
{
    public static string Slug(string title) => title.Trim().Kebaberize();

    public static string Title(string slug) => slug.Replace('-', ' ').Humanize(LetterCasing.Title);
}
CS
  dotnet new sln -n TextKit > /dev/null && dotnet sln add src/Ada.TextKit > /dev/null
  dotnet restore -v:q && git add -A && git commit -qm "TextKit 1.0"
}
setup
echo "== release build"
export ContinuousIntegrationBuild=true
dotnet restore --locked-mode -v:q
dotnet build -c Release --no-restore -nologo -v:q | grep -E 'Err'
dotnet pack -c Release --no-build -o artifacts -nologo -v:q -p:Version=1.0.0
unzip -Z1 artifacts/Ada.TextKit.1.0.0.nupkg | grep -E 'lib/|README'
unzip -p artifacts/Ada.TextKit.1.0.0.nupkg Ada.TextKit.nuspec | grep -E '<(id|version|authors|license|readme|repository|dependency) ?' | sed 's/^ *//; s/commit="[0-9a-f]*"/commit="…"/'
dotnet nuget push artifacts/Ada.TextKit.1.0.0.nupkg -s local
unset ContinuousIntegrationBuild
echo "== a consumer elsewhere"
cd ~ && mkdir blog && cd blog
cat > blog.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'Console.WriteLine(
quot;/posts/{Ada.TextKit.Post.Slug("Publishing To A Private Feed")}");' > Program.cs sed -i 's|<TargetFramework>|<ImplicitUsings>enable</ImplicitUsings>\n &|' blog.csproj dotnet package add Ada.TextKit | grep -o 'Installed [^ ]* [^ ]* from [^ ]*' dotnet run dotnet package list --include-transitive | grep '>'
== release build
    0 Error(s)
README.md
lib/net10.0/Ada.TextKit.dll
<id>Ada.TextKit</id>
<version>1.0.0</version>
<authors>Ada Lovelace</authors>
<license type="expression">MIT</license>
<licenseUrl>https://licenses.nuget.org/MIT</licenseUrl>
<readme>README.md</readme>
<repository type="git" commit="…" />
<dependency id="Humanizer.Core" version="2.14.1" exclude="Build,Analyzers" />
Pushing Ada.TextKit.1.0.0.nupkg to '/opt/nuget-feed'...
Your package was pushed.
== a consumer elsewhere
Installed Ada.TextKit 1.0.0 from /opt/nuget-feed
/posts/publishing-to-a-private-feed
   > Ada.TextKit          1.0.0       1.0.0
   > Humanizer.Core        2.14.1

What the second part proves:

  • The locked restore succeeded because the lock file was committed with the code; the release used exactly the reviewed versions.
  • The package contains the library for net10.0 and the README, and its nuspec carries the license, the README reference, the git commit (from the SDK's built-in source-control support, because ContinuousIntegrationBuild was on; with a GitHub origin remote it would carry the URL too), and a dependency on Humanizer.Core 2.14.1 taken from the central version.
  • The blog app lives outside the repository and only knows the feed. Restoring Ada.TextKit brought Humanizer.Core along as a transitive package, exactly like any package from nuget.org. (Only Ada.TextKit printed an Installed line: Humanizer.Core was already in this machine's global packages folder from the repository build.)

Extend it

  1. Add <GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.402" /> to Directory.Packages.props, a GitHub origin remote, and check the .nuspec repository URL and the sourcelink.json in obj/.
  2. Publish 1.1.0-beta.1 with --version-suffix, and show that blog doesn't pick it up until you reference it explicitly.
  3. Add a nuget.config with package source mapping to blog that maps Ada.* to a second folder feed, push the package there instead, and watch restore pick it up from that source only.
  4. Add -bl:artifacts/build.binlog to the release build and replay it with dotnet msbuild artifacts/build.binlog -v:n | grep CoreCompile.
  5. Turn the release steps into a ci.sh like the one in Field Notes: CI, with dotnet format --verify-no-changes as the first step.

Test yourself

In this repository, why does dotnet pack on the whole solution produce only Ada.TextKit?

  1. Tests can't be packed
  2. Directory.Build.props sets IsPackable=false and only the library overrides it to true
  3. The CLI has no PackageId
  4. Pack only takes the first project

The blog app never referenced Humanizer.Core, yet it's restored. Why?

  1. It's part of .NET
  2. It's a dependency declared in Ada.TextKit's nuspec, so it flows transitively
  3. dotnet package add added it
  4. The local feed forces it