How to run scripts based on solution configuration in ASP.NET Core
Asked Answered
C

2

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"
  ]
}
Chaise answered 2/6, 2016 at 15:27 Comment(0)
C
19

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

Climactic answered 2/6, 2016 at 17:17 Comment(1)
Where can we find the complete list of variables available for these script events?Derisive
S
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.

Soissons answered 12/7, 2016 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.