Search code examples
msbuildvisual-studio-2017projects-and-solutionscsprojsolution

Post Build Script


I using VS2017 for my solution.

I have a main repository in bitbucket (say directory name is A) and few dependencies in the form of Subtree (this is inside A folder and other subfolder say B).

I want to add a post build in all Subtree (project inside B) such that if the subtree project is present under a folder A (in this case) then copy the dll from B's bin folder to a folder in A. But this script should not run if subtree project in master directory.

So resolve I wanted to find out the parent directory of folder B. If this parent direct is A then only copy dll from B/din/.dll to A/Assembles/bin/.dll

How can I find out of B's parent folder is A in post build script in VS2017


Solution

  • Run post-build event in condition

    So, you want to run a PostBuild event only in a specific case. For achieve that, you may use Condition.

    In your Condition, you want to check the parent folder of your Solution or Project dir (honestly, I'm not sure what you meant).

    MSBuild get the parent of dir

    How you can get the parent dir?

    <PropertyGroup>
        <ProjectParentDir>$([System.IO.Path]::GetDirectoryName($(ProjectDir))))</ProjectParentDir>
        <SolutionParentDir>$([System.IO.Path]::GetDirectoryName($(SolutionDir)))</SolutionParentDir>
    </PropertyGroup>
    

    Answer

    So, now you can combine the above knowledge:

    <Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="$(ProjectParentDir.EndsWith('A'))">
        // Do your post build
    </Target>
    

    Comment

    Because I think that maybe your question is wrong, and maybe you can achieve your solution without PostBuild, I think you may use the tool I described to control the OutputPath itself.

    <PropertyGroup>
        <ProjectParentDir>$([System.IO.Path]::GetDirectoryName($(ProjectDir))))</ProjectParentDir>
    </PropertyGroup>
    
    <PropertyGroup Condition="$(ProjectParentDir.EndsWith('A'))">
        <OutputPath>Path/to/somewhere</OutputPath>
    </PropertyGroup>
    <PropertyGroup Condition="!$(ProjectParentDir.EndsWith('A'))">
        <OutputPath>Path/to/somewhere</OutputPath>
    </PropertyGroup>
    

    Read More

    I'm not sure my syntax is correct, please read more: