2

I have a solution composed with different projects from different path. We use foundation projects from a vanilla folder and then project specific projects from specific directory. Example:

specific project directory: c:\proj\specific 

vanilla project directory: c:\proj\vanilla

vanilla project x path: c:\proj\vanilla\repo\src\project\x\code\

In each vanilla project we have a publish profile that points to the root directory and includes a publishsettings.targets file that has the actual target where the project should be published. By using this structure we can have a lot of projects and publish them using a single target so we don't need to change that target in all projects.

We discovered now that we have a problem when using these vanilla projects as the path used in publish profile is relative to vanilla directory and actually we need it to be relative to the specific project directory (solution directory).

In our publish profile we have:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\..\..\..\..\..\publishsettings.targets" />
  <PropertyGroup>
  ...
  </PropertyGroup>
</Project>

So we need a way to specify the actual sln directory to this path so we can include the correct target so when we do the publish from visual studio it will publish to the specific project and not vanilla one.

I tried finding a "MSBuildSolutionDirectory" but it only seems to be a "MSBuildProjectDirectory" variable that can be used.

Does anyone knows a way I could get the path

Project="c:\proj\vanilla\publishsettings.targets"  

to actually be

Project="c:\proj\specific\publishsettings.targets" 

by using some msbuild or custom variable and not hardcoding it?

I need it to work both with vanilla (as I have a vanilla.sln) and also with specific project (as I have a X.sln).

Adrian C.
  • 231
  • 1
  • 7

1 Answers1

2

Here is a way to make your own version of the MSBuildSolutionDirectory you were hoping to see built in:

  <PropertyGroup>
    <SolutionDirectory>$([MSBuild]::GetDirectoryNameOfFileAbove(`$(MSBuildProjectDirectory)`, `YOUR_SOLUTION_NAME.sln`))\</SolutionDirectory>
  </PropertyGroup>

Notes on the GetDirectoryNameOfFileAbove MSBuild property function:

$([MSBuild]::GetDirectoryNameOfFileAbove(directory, filename) Looks in the designated directory, then progressively in the parent directories until it finds the file provided or hits the root. Then it returns the path to that root.

  • From my own testing:
    • The returned path does not include a trailing backslash.
    • If the filename is not found, an empty string is returned.
weir
  • 4,521
  • 2
  • 29
  • 42
  • As I can see from your comment, it searches in the directory and as I said the specific sln is not in the same directory path with the vanilla projects so it will never find the *.sln file since that file is in c:\proj\specific\specific.sln and the vanilla projects are in c:\proj\vanilla\ so not sure if the solution helps. – Adrian C. Jan 30 '17 at 11:29
  • What are the paths to vanilla.sln and X.sln? I assume you noticed the function searches above, not inside, the project directory and aims to get you the solution directory (which your question says you were hoping to use). – weir Jan 30 '17 at 11:59