2

I'm setting up the dev environment for a new project. It's a Web API project that serves an @angular project from static files. In order to facilitate this, on post build, I run the angular cli's build function, then copy the files into wwwroot. That's all working fine.

However, I want to be able to have two different npm scripts run depending on whether the project is in Debug or Release mode. In my post build scripts, I tried this:

if "$(ConfigurationName)" == "Debug"
(
   npm run build
)
else if "$(ConfigurationName)"=="Release"
(
   npm run prod
)

but I'm getting

The command "if "" == "Debug"
(
   npm run build
)
else if ""=="Release"
(
   npm run prod
)" exited with code 255.    

It looks like there's no value being provided for ConfigurationName. I've tried both debug and release mode and get the same error. Did the ConfigurationName variable change in VS 2017 or something?

EDIT: However, if I click Edit Post-build and then click Macros, I can see here that ConfigurationName has a value. It's just not seen by my script

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
Alex Kibler
  • 4,674
  • 9
  • 44
  • 74

1 Answers1

5
  1. $(Configuration) likely works in your case since it is given a value early enough in the build process.

  2. Pre & Post build events don't really work with "SDK-based" projects (without modification) since most of the properties aren't defined when the build event properties are evaluated. See https://github.com/dotnet/project-system/issues/1569 for more details.

I recommend doing the following in your project:

<Project Sdk="...">
  <!-- other content -->
  <Target Name="RunNpmBuild" BeforeTargets="BeforeBuild" Condition="'$(Configuration)' != 'Release'">
    <Exec Command="npm run build" />
  </Target>
  <Target Name="RunNpmProd" BeforeTargets="BeforeBuild" Condition="'$(Configuration)' == 'Release'">
    <Exec Command="npm run prod" />
  </Target>
</Project>

Note that in 1.0.* CLIs, files generated during build which did not exist before may not be published - see https://github.com/aspnet/websdk/issues/114 for workarounds.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Aha, thanks! I went to that issue and saw the recommendation to import `` before the build events, and that worked for me. I wasn't able to get the conditional part working, but I ended up just replacing it with `npm run build-$(ConfigurationName) and made npm's build and prod scripts build-Debug and build-Release. Thanks for the advice Martin! – Alex Kibler May 18 '17 at 11:59
  • Added a pure MSBuild version of what you are trying to do if that is of any interest. (the publish-advice still applies to all forms of content generation). – Martin Ullrich May 18 '17 at 12:10