Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Visual Studio to use FAKE for building my solution?

I'm looking for a solution that invokes my FAKE build script when I do a "Build Solution" (Ctrl + F6). Bonus points for a way to specify a target other than the default.

like image 252
GeorgS Avatar asked May 13 '15 14:05

GeorgS


People also ask

What happens when we build a solution in Visual Studio?

Build Solution - compiles code files (dll and exe) that have changed. Rebuild Solution - Deletes all compiled files and Compiles them again regardless of whether or not the code has changed.

How do I stop Visual Studio from rebuilding?

You can hit Ctrl + Break on the keyboard to cancel/stop a build that is currently in progress.


2 Answers

I solved this problem in another way. It requires manual edit of csproj files and the trick is in conditional override of builtin Build, Clean and Rebuild targets.

First I created custom fake.targets file and saved it in Targets folder at the solution level:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Build">
    <Exec Command="packages\FAKE\tools\FAKE.exe build.fsx Build proj=$(ProjectPath) conf=$(Configuration) plat=$(Platform)" WorkingDirectory="..\" />
  </Target>

  <Target Name="Rebuild">
    <Exec Command="packages\FAKE\tools\FAKE.exe build.fsx Rebuild proj=$(ProjectPath) conf=$(Configuration) plat=$(Platform)" WorkingDirectory="..\" />
  </Target>

  <Target Name="Clean">
    <Exec Command="packages\FAKE\tools\FAKE.exe build.fsx Clean proj=$(ProjectPath) conf=$(Configuration) plat=$(Platform)" WorkingDirectory="..\" />
  </Target>
</Project>

Next, at the bottom of <Project /> section in each *.csproj project file I added:

<PropertyGroup>
  <FakeTargetsPath>..\Targets\fake.targets</FakeTargetsPath>
</PropertyGroup>
<Import Project="$(FakeTargetsPath)" Condition="Exists($(FakeTargetsPath)) And '$(RealBuild)'!='true'" />

Note: FakeTargetsPath is relative to the csproj file.

Last step was to create build.fsx that invokes MSBuild with RealBuild = true:

#r @"packages/FAKE/tools/FakeLib.dll"
open Fake

let solution = "solution.sln"

let commonBuild target =
    let project = getBuildParamOrDefault "proj" solution
    let configuration = getBuildParamOrDefault "conf" "Release"
    let platform = getBuildParamOrDefault "plat" "AnyCPU"
    let setParams defaults =
        { defaults with
            Verbosity = Some(Quiet)
            Targets = [ target ]
            Properties =
                [
                    "Configuration", configuration
                    "Platform", platform
                    "RealBuild", "true"
                ]
        }

    build setParams project

Target "Build" (fun _ ->
    commonBuild "Build"
)

Target "Clean" (fun _ ->
    commonBuild "Clean"
)

Target "Rebuild" (fun _ ->
    commonBuild "Rebuild"
)

RunTargetOrDefault "Build"
like image 95
kfazi Avatar answered Sep 21 '22 17:09

kfazi


The closest I found so far is to define FAKE as an external tool via

 Tools -> External tools...

ExternalTool

Set it to use the output window and to prompt for arguments. Then, define a keyboard shortcut via

 Tools -> Options -> Environment -> Keyboard -> Tools.ExternalCommand6

When you invoke it you can provide a target or just press enter to build the default.

like image 34
GeorgS Avatar answered Sep 20 '22 17:09

GeorgS