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

ぐ巨炮叔叔 提交于 2019-11-27 23:21:14

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/37596115/how-to-run-scripts-based-on-solution-configuration-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!