Paolo Fulgoni
Paolo Fulgoni

Reputation: 5498

How to get the OutputPath of a project in a Nuke build

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");

Upvotes: 3

Views: 1572

Answers (4)

chrkon
chrkon

Reputation: 21

I have just startet to use nuke.build tool and I ran into the same question.

How to get the output folder with the compiled files?

This is difficult with .net 8 Apps, because the processor type (x86/x64), the configuration (debug/release) and the target .net version are part of the path down to the compiled files. So this is my solution:

Project _app => Solution.GetProject("<Projectname>");    
var OutputPath = _app.Path.Parent.GlobDirectories($"bin/**/Release/**")
    .First(d => d.ContainsFile(_app.Path.NameWithoutExtension + ".dll"));

In this code I start in the bin folder of the project and then I search the folders below until I found the compiled .dll file. This works fine if I clean the build folders before the next build.

I hope this information will help you.

Upvotes: 0

Manuel Amstutz
Manuel Amstutz

Reputation: 1388

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

Upvotes: 1

Paolo Fulgoni
Paolo Fulgoni

Reputation: 5498

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.

Upvotes: 2

Sean Kearon
Sean Kearon

Reputation: 11427

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";

Upvotes: 1

Related Questions