Scripty icon indicating copy to clipboard operation
Scripty copied to clipboard

Add a flag to run Scripty.MsBuild after DLL is built

Open geirsagberg opened this issue 8 years ago • 11 comments

I am using Scripty.MsBuild to generate TypeScript typings in a ASP.NET Core 2.0 project quite successfully, by referencing the output DLL in the bin folder. However, this doesn't work if the project is not built; also, I have to build twice in order to pick up changes.

Is there any way to run the MsBuild task after the project itself is built (and the DLL will be available in the bin folder?)

geirsagberg avatar Jul 06 '17 14:07 geirsagberg

I use the same technique for my json configuration files generation. You could move your types into a separate class library and reference that instead.

TylerBrinkley avatar Jul 06 '17 14:07 TylerBrinkley

Good idea, I'll give that a shot :)

geirsagberg avatar Jul 06 '17 14:07 geirsagberg

Works like a charm, thanks!

geirsagberg avatar Jul 06 '17 14:07 geirsagberg

It still might be a good idea for Scripty to provide an option to run the task post project build.

TylerBrinkley avatar Jul 06 '17 14:07 TylerBrinkley

I agree - it's not a bad idea at all. I just don't know enough about MSBuild tasks to toggle where in the process a task occurs based on a flag. Will need to research before I can implement (or take a PR).

daveaglick avatar Jul 06 '17 14:07 daveaglick

There is a target named AfterBuild that might be used conditionally, based on a property perhaps? Reference: https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets

geirsagberg avatar Jul 07 '17 06:07 geirsagberg

AfterBuild seems to only allow one task though; I think maybe DependsOnTargets or AfterTargets might be appropriate: https://docs.microsoft.com/en-us/visualstudio/msbuild/target-build-order

geirsagberg avatar Jul 07 '17 06:07 geirsagberg

@geirsagberg , @TylerBrinkley , Can you share your working solution? For me:

  1. I had to add in .csx file #r "bin\\Debug\\MY_DLL.dll"! (I don't like this)
  2. Whenever I do Rebuild/Clean scripty task throws an Exception that the dll was not found. I have to change/touch file and do Build (not rebuild). (I hate this)

Tell me you have a solution to this!

akram1905 avatar Nov 01 '17 07:11 akram1905

The following worked for me:

  1. Put the code that your .csx-script depends on in a separate class library, e.g. MyApp.Data
  2. Make sure the .csx-script is in your main project, and add a reference to the class library
  3. In your script, add the relative path to the .dll in the bin folder of the class library, e.g.:

#r "../MyApp.Data/bin/debug/MyApp.Data.dll"

Now obviously you will run into trouble if you are building in release mode. So maybe add a post build task to the class library that copies the DLL to a better location.

Haven't tried this myself, but this might get you started: https://docs.microsoft.com/en-us/cpp/build/how-to-use-build-events-in-msbuild-projects

geirsagberg avatar Nov 01 '17 11:11 geirsagberg

When using this method I always set my build directory to just /bin for both debug and release to prevent any issues.

TylerBrinkley avatar Nov 01 '17 12:11 TylerBrinkley

I am unsure if you are trying to reference a .dll of the same project that the script is in, I am only able to get this error when doing so.

Which IMHO you shouldn't be doing anyway, as you will always need to rebuild the project to contain the models you've just generated, this will cause a recursive dependency.

Try separating the script into another library and reference that .dll file.

Or am I wrong?

HenryMigo avatar Dec 06 '18 16:12 HenryMigo