Dude0001
Dude0001

Reputation: 3450

How to properly dynamically change NuGet PackageId and dependency PackageId at build time?

We have an internal convention where if the CI/CD is doing a feature branch build, the NuGet package should be publish as "PackageId-FeatureBranchName". We publish these packages to a special feed that we don't reference in production builds. In our build script, if we detect we are doing a feature branch build, we look for any .nuspec files output by the build and the build would replace in the .nuspec file "" with "-FeatureBranchName" in all .nuspec files in the solution. This works fine if there is only one package with no dependencies on other projects in the solution.

Now I have two .NET Standard projects My.Assmebly.A and My.Assmebly.B that need to have their own NuGet packages. B depends on A. I am able to use the same build script above to set the of each package correctly. However, I am not sure of a solution to also update the .nuspec for package B to also update it's dependency on A to have the renamed My.Assmebly.A-FeatureBranchName.

Is there an easy way to script this. Or, I feel like I'm not doing this in the correct way with dotnet pack but I don't see any way to set package ids through command line or other, specifically when it comes to dependencies like this. I do see the $id$ token you can setup and replace. But I'm not sure how to use this now that .NET Standard projects generate their own .nuspec during the build. Nor do I think this will work with my dependency situation.

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>My.Assmebly.B</id>
    <dependencies>
      <group targetFramework=".NETCoreApp2.1">
        <dependency id="My.Assmebly.A" version="1.0.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

Needs to look like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>My.Assmebly.B-FeatureBranchName</id>
    <dependencies>
      <group targetFramework=".NETCoreApp2.1">
        <dependency id="My.Assmebly.A-FeatureBranchName" version="1.0.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

Upvotes: 4

Views: 2019

Answers (1)

Simply Ged
Simply Ged

Reputation: 8662

Don't change the name of the assembly. The correct thing to do is to add the branch name to the version number using the version-suffix parameter. This removes the need for you to search and edit any .nuspec files to force your naming convention.

You can pack your assembly by running:

dotnet pack --version-suffix [branchname]

replacing [branchname] with the correct value.

You library would be published as:

name: My.Assembly.A
version: 1.0.0-FeatureBranchName

If you add a version-suffix to your version then this will also cause the packages to be listed as pre-release in the nuget package manager.

You can also use version-ranges in your dependency list to always get a specific version. See this link for the official documentation.

But, in essence, you can define your dependency as:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>My.Assmebly.B</id>
    <dependencies>
      <group targetFramework=".NETCoreApp2.1">
        <dependency id="My.Assmebly.A" version="1.0.*" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

Package B will now depend on the latest My.Assembly.A in your package feed. If you publish them together then they should (in theory) stay in sync.

Upvotes: 4

Related Questions