15

I have the following script in my project.json file which I am using to build my class library project into a NuGet package. I don't want to build any Debug NuGet packages. How can I limit this script to only run when the solution configuration is set to Release mode to speed up the build time in debug mode?

Alternatively, as a matter of curiosity, how can I pass the solution configuration into the command below so that 'Release' is not hard coded.

"scripts": {
  "postcompile": [
    "dotnet pack --no-build --configuration Release"
  ]
}
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

2 Answers2

19

You can use %compile:Configuration% to get the current Configuration. Here is the list of variables available to precompile and postcompile scripts.

Pawel
  • 31,342
  • 4
  • 73
  • 104
5

To actually limit the creation of the nuget package based on the configuration, you'll need to pass it into a script, like this makeNuget.cmd file which I store at the root of my project:

@Echo off
IF "%1" == "%2" dotnet pack --no-build --configuration %1 -o ../%3

Then, in my project.json, I have:

  "scripts": {        
    "postcompile": [
      "makeNuget.cmd %compile:Configuration% Release \\packages\\%project:Name%"
    ]
  }

This creates the nuget package and places it in my solution level packages/[project name] folder (though you may need to adjust the relative folder reference in the -o parameter depending on your solution layout). Also, you don't need the .cmd, by default .cmd is inferred for windows and .sh for other environments.

I also made the target configuration a parameter so that the script is generic and can be used regardless of the configuration name I've chosen as my 'release' config.

Erikest
  • 4,997
  • 2
  • 25
  • 37