3

I'm adding a Nuke build project to my solution.

I need to create a target which copies the compiled files to a custom folder. You can think of it as a sort of deploy.

How do I get the output folder of a specific project?

For example, if the project is called "MyProject" and it's in the C:\git\test\MyProject folder, I need to get the output path based on the current configuration and platform, such as C:\git\test\MyProject\bin\x64\Release.

I tried this one, but it gives me the first value of the OutputPath property, not the one for the current configuration and platform:

readonly Configuration Configuration = Configuration.Release;
readonly MSBuildTargetPlatform Platform = MSBuildTargetPlatform.x64;

// ...

Target LocalDeploy => _ => _
    .DependsOn(Compile)
    .Executes(() =>
    {
        var myProject = Solution.GetProject("MyProject");
        var outputPath = myProject.GetProperty("OutputPath"); // this returns bin\Debug
        var fullOutputPath = myProject.Directory / outputPath;
        CopyDirectoryRecursively(fullOutputPath, @"C:\DeployPath");
    });

I also tried this way, which honor the Configuration but not the Platform:

    var myProject = Solution.GetProject("MyProject");
    var myMSBuildProject = visionTools3Project.GetMSBuildProject(Configuration);
    var outputPath = myProject.GetProperty("OutputPath"); // this returns bin\Release
    var fullOutputPath = myProject.Directory / outputPath;
    CopyDirectoryRecursively(fullOutputPath, @"C:\DeployPath");
Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55

3 Answers3

2

This is the workaround I finally used.

readonly Configuration Configuration;
readonly MSBuildTargetPlatform Platform;

//...

Target Example => _ => _
    .DependsOn(Compile)
    .Executes(() =>
    {
        var project = Solution.GetProject("MyProject");
        var outputPath = GetOutputPath(project);
        // ...
    });

private AbsolutePath GetOutputPath(Project project)
{
    var relativeOutputPath = (RelativePath)"bin";

    if (Platform == MSBuildTargetPlatform.x64)
    {
        relativeOutputPath = relativeOutputPath / "x64";
    }

    relativeOutputPath = relativeOutputPath / Configuration;

    return project.Directory / relativeOutputPath;
}

It's quite hardcodeded and it doesn't take the the default output path of netstandard projects into account. Hopefully it can be useful as starting point for those who are trying to solve the same issue.

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
1

A simple way to do this is to construct the path to your output folder like this:

AbsolutePath OutputDirectory = RootDirectory / "MyProjectFolder" / "bin" / Configuration / "netstandard2.0";
Sean Kearon
  • 10,987
  • 13
  • 77
  • 93
0

Here a not hard coded version Because the ouput path can depend on Configuration etc.. the Solution have to be configured with Configuration and Target Framework if required

Solution.GetProject("MyProject")
.GetMSBuildProject(Configuration) // optional parameter with target Framework exists
.GetPropertyValue("OutputPath")
Manuel Amstutz
  • 1,311
  • 13
  • 33