using MsbuildAnalyzer.Models; namespace MsbuildAnalyzer.Utilities; /// /// Pack target creation methods for TargetBuilder. /// public static partial class TargetBuilder { private static void AddPackTarget( Dictionary targets, string projectName, string fileName, Dictionary properties, string projectDirectory, string workspaceRoot, PluginOptions options, string productionInput, List directoryBuildInputs) { // Create a copy of properties with Configuration=Release var releaseProperties = new Dictionary(properties) { ["Configuration"] = "Release" }; var packageOutputPath = GetPackageOutputPath(releaseProperties, projectName, projectDirectory, workspaceRoot); // `dotnet pack` writes intermediate state into the intermediate (obj) // directory, so it must be declared as an output alongside the package // output, mirroring the build target. var intermediatePath = GetIntermediateOutputPath(releaseProperties, projectName, projectDirectory, workspaceRoot); var buildReleaseTarget = $"{options.BuildTargetName}:release"; targets[options.PackTargetName] = new Target { Command = "dotnet pack", Options = new TargetOptions { Cwd = "{projectRoot}", Args = ["--no-dependencies", "--no-build", "--configuration", "Release"] }, Configurations = new Dictionary { ["debug"] = new TargetConfiguration { Args = ["--no-dependencies", "--no-build", "--configuration", "Debug"] }, ["release"] = new TargetConfiguration { Args = ["--no-dependencies", "--no-build", "--configuration", "Release"] } }, DependsOn = [buildReleaseTarget], Cache = true, Inputs = [ "default", $"^{productionInput}", "{workspaceRoot}/.editorconfig", new { workingDirectory = "absolute" }, new { dependentTasksOutputFiles = "**/*" }, .. directoryBuildInputs ], Outputs = new[] { packageOutputPath is null ? null : $"{packageOutputPath.TrimEnd('/')}/*.nupkg", intermediatePath } .Where(p => p is not null) .ToArray()!, Metadata = new TargetMetadata { Description = "Create NuGet package", Technologies = ProjectUtilities.GetTechnologies(fileName) } }; } }