CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.05k stars 172 forks source link

Publish the `CarterTemplate` package automatically #302

Closed mderriey closed 1 year ago

mderriey commented 1 year ago

Related to #298. Discussed in https://github.com/CarterCommunity/Carter/pull/299#issuecomment-1257956828

Goal

We want to publish the CarterTemplate NuGet package at the same time as the Carter package. In other words, if we publish a new Carter package, let's say 7.1.2, we want to release a new CarterTemplate package with the same version.

The result can be seen in this run where we published the following packages to feedz:

Considerations

Today, the template project references the Carter NuGet package through a <PackageReference> element. If we want to publish a new template package, we have to:

  1. Publish a new Carter package
  2. Update the package reference in the template project
  3. Create the NuGet package and publish it

There are several downsides with this approach:

  1. When we make changes to Carter, we don't know if they affect the template project since it's pinned to a specific version. It's only when we want to publish a new version of the template package that we discover potential issues due to API changes.
  2. Updating the package reference, creating the package and publishing it is done manually.

This PR brings some changes to this:

Detailed walkthrough

There's plenty of changes in this PR, here's my attempt at documenting them.

MinVer version computed outside of MSBuild

We now compute the MinVer version outside of MSBuild with the MinVer CLI, see https://github.com/CarterCommunity/Carter/pull/302/files#diff-ac55abaaeaf67fb06efd5a2233f5a5f9ea20e835e7c502ff05c1bf2b952f1825R27-R31

We do this because of a chicken-and-egg problem: we need to know which version MinVer computes to inject the package reference in the template project before packing it, but as of now the version is only computed in MSBuild.

Special MinVer version for PRs

I faced an issue where several different commits in my PR resulted in the same computed MinVer versions. It's an expected behaviour as per https://github.com/adamralph/minver/issues/225.

To avoid this, we override the MinVer version with a unique prerelease tag that contains both the PR number and the GitHub Actions run id, see https://github.com/CarterCommunity/Carter/pull/302/files#diff-ac55abaaeaf67fb06efd5a2233f5a5f9ea20e835e7c502ff05c1bf2b952f1825R32-R46.

An example of this is 6.1.2-pr.302.build.562312354.

Explicitly packing the Template project

See https://github.com/CarterCommunity/Carter/pull/302/files#diff-ac55abaaeaf67fb06efd5a2233f5a5f9ea20e835e7c502ff05c1bf2b952f1825R83-R91

The Template project is not included in the VS solution, so a top-level dotnet build doesn't do anything with it. I'm not sure whether it's intentional or not, happy to hear your thoughts.

Using PowerShell

I'm more comfortable with PowerShell than with Bash, which is why I implemented the "swap project reference with package reference" and "override MinVer version" with PS. I have no strong feeling about this, happy for it to be in Bash, but it might be quicker for someone who knows Bash to do it.

Did you make it all the way down here? If so, thanks 🙏

mderriey commented 1 year ago

Superseded by #303