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"
  ]
}

2 Answers 2

19

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

1
  • 2
    Where can we find the complete list of variables available for these script events?
    – Dealdiane
    Aug 26, 2016 at 3:11
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.