Post build event depending on configuration name in new ASP.NET 5 project
Asked Answered
M

2

7

I'm writing a unified project for 3 smart TVs. I have also 3 configurations created in Visual Studio. Now I want to execute some CLI scripts depending on selected configuration.

The problem is in new ASP.NET 5 project I don't have an editor for post build events.

I know I have to do this in project.json. What I found is:

  "scripts": {
    "postbuild": ""
  }

But using this one I can't create different CLI scripts for different configurations.

I found also:

  "configurations": {
  },

And I guess this is probably what I want, but... How to use it? Intellisense has no power here and also I wasn't lucky searching the Web...

[edit]

Maybe I should try with .xproj?

Marnamarne answered 9/2, 2016 at 8:55 Comment(2)
Can you check whether you see the configuration in the script by accessing the ASPNET_ENV environment variable?Casas
Unfortunately undefined :(Marnamarne
A
14

You'll need to build a master script which uses the available context and environment variables to switch and run the other scripts of your choice.

In addition to the list of variables Here for compile, you also get these for publish related scripts and then these are available everywhere, as are environment variables returned by Environment.GetEnvironmentVariable, which can be seen here.

The image below shows the intellisense from the VS2015 Update 3 RTM, but it's misleading, since you get others depending on the script block you're using:

enter image description here

So, your full list of context variables that you can use to control flow in your scripts is:

Every script block:

  • %project:Directory%
  • %project:Name%
  • %project:Version%

Compile specific:

  • %compile:TargetFramework%
  • %compile:FullTargetFramework%
  • %compile:Configuration%
  • %compile:OutputFile%
  • %compile:OutputDir%
  • %compile:ResponseFile%
  • %compile:RuntimeOutputDir% (only available if there is runtime output)
  • %compile:RuntimeIdentifier% (only availabe if there is runtime output)
  • %comiple:CompilerExitCode% (only available in the postcompile script block)

Publish specific:

  • %publish:ProjectPath%
  • %publish:Configuration%
  • %publish:OutputPath%
  • %publish:TargetFramework%
  • %publish:FullTargetFramework%
  • %publish:Runtime%
Alchemist answered 12/7, 2016 at 21:52 Comment(0)
C
5

I investigated on this a bit but did not really get to any good result.

There are some project variables that are exposed in scripts. Unfortunately, those are very limited:

  • %project:Name% gives you the project name
  • %project:Directory% gives you the project directory
  • %project:Version% gives you the project version

So there is no way to access the build configuration or the environment here.

The configurations option in the project.json is also limited to build configurations and only allows declaring compilation options there, so that also doesn’t work.

Unfortunately, there also doesn’t seem to be another way to solve this. At least not right now. I would consider myself sending a pull request to DNX to add some additional project variables which one could use but at the moment, it doesn’t really make any sense to invest time into DNX: After all it’s being replaced by the dotnet CLI. We’ll see if that one will come with functionality to access the environment—and if not, I might end up submitting a pull request to add this functionality. But until we get there, I’m afraid there is no solution for this.

Casas answered 10/2, 2016 at 9:37 Comment(4)
I also noticed that scripts > post/prebuild does not working. Not executing for me at all...Marnamarne
It’s working for me; how do you execute the build? If you use e.g. dnu build the scripts should be called properly.Casas
"scripts": { "postbuild": "echo test test test" }Marnamarne
Works for me when running dnu build. I get a “Executing script 'postbuild' in project.json / test test test” responseCasas

© 2022 - 2024 — McMap. All rights reserved.