daveaglick / Scripty

Tools to let you use Roslyn-powered C# scripts for code generation
MIT License
621 stars 69 forks source link

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

Open geirsagberg opened 7 years ago

geirsagberg commented 7 years ago

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?)

TylerBrinkley commented 7 years ago

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.

geirsagberg commented 7 years ago

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

geirsagberg commented 7 years ago

Works like a charm, thanks!

TylerBrinkley commented 7 years ago

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

daveaglick commented 7 years ago

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).

geirsagberg commented 7 years ago

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 commented 7 years ago

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

akram1905 commented 7 years ago

@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!

geirsagberg commented 7 years ago

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

TylerBrinkley commented 7 years ago

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

HenryMigo commented 5 years ago

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?