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 (with MSBuild) #303

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

Context

Please see the "Goal" and "Considerations" sections of #302. This PR achieves the same result but with a different method, which is explained below.

Detailed walkthrough

I kept thinking at the solution used in #302, which, while it works, brings a lot of changes and cognitive load:

Overall, I found it a bit too complicated.

I thought about how we could keep the process closer to what it is currently, and came to the conclusion that executing our logic (overriding the MinVer version for PRs, and updating the csproj file) within MSBuild would be the easiest way to do this.

Updating the csproj file

MSBuild allows declaring tasks with C# code that is then compiled by Roslyn. C# code in .targets file is yuck, but what we're doing here is simple.

The steps are as follows:

  1. We import a targets file from the Template project: https://github.com/CarterCommunity/Carter/pull/303/files#diff-800f45f46df5585e5ef019f729b3fd356ae505f2e3cf7870866074ba39244967R36
  2. We declare our custom task which does the swap between <ProjectReference> and PackageReference: https://github.com/CarterCommunity/Carter/pull/303/files#diff-b9d07d74605b6299754f85f8bb50c521c8c6fe2ce642ede2df1dca75faa4a019R19-R43
  3. We declare a target that runs our task: https://github.com/CarterCommunity/Carter/pull/303/files#diff-b9d07d74605b6299754f85f8bb50c521c8c6fe2ce642ede2df1dca75faa4a019R45-R49
  4. We mark our target as a dependency to the NuGet pack task: https://github.com/CarterCommunity/Carter/pull/303/files#diff-b9d07d74605b6299754f85f8bb50c521c8c6fe2ce642ede2df1dca75faa4a019R16

Result

When PRs get merged or the repo is tagged, we'll now publish both the Carter and the CarterTemplate packages at the same time.

For example, if the repo is tagged with 7.0.0:

  1. We'll publish the Carter package version 7.0.0 to NuGet, and
  2. We'll publish the CarterTemplate package version 7.0.0; when we scaffold the project using this template, it will reference Carter version 7.0.0.
mderriey commented 1 year ago

@jchannon when you have a moment, could you please take a look at this, as well as #302 which does the same thing but a different way? Cheers.

jchannon commented 1 year ago

Away at the moment. Will take a look next week:)

On Fri, 21 Oct 2022 at 10:22, Mickaël Derriey @.***> wrote:

@jchannon https://github.com/jchannon when you have a moment, could you please take a look at this, as well as #302 https://github.com/CarterCommunity/Carter/pull/302 which does the same thing but a different way? Cheers.

— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/pull/303#issuecomment-1286693575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJXX7MBV4ZKRR4F45NTWEJODRANCNFSM6AAAAAARK6PILM . You are receiving this because you were mentioned.Message ID: @.***>

mderriey commented 1 year ago

Friendly bump @jchannon. As always, this is just a reminder in case you forgot about this, but please have a look whenever you want.

jchannon commented 1 year ago

Sorry just realised I didn't start a review and submitted comments individually

jchannon commented 1 year ago

Looks good though! Do you want to close #302 ?