From 53761df85e3509bcf744d03fbecab21a3d5fb792 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 19 Jun 2025 22:07:28 -0500 Subject: [PATCH] Add utility methods for redundant code. --- .../Flax.Build/Build/DotNet/Builder.DotNet.cs | 24 +-------- .../Build/NativeCpp/Builder.NativeCpp.cs | 51 ++----------------- .../Tools/Flax.Build/Utilities/Utilities.cs | 43 ++++++++++++++++ 3 files changed, 48 insertions(+), 70 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 895810e9e..5d1e90a5a 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -145,34 +145,12 @@ namespace Flax.Build if (targetBuildOptions.NugetPackageReferences.Any()) { - var buildPlatform = Platform.BuildTargetPlatform; - var dotnetSdk = DotNetSdk.Instance; - if (!dotnetSdk.IsValid) - throw new DotNetSdk.MissingException(); - var dotnetPath = "dotnet"; - switch (buildPlatform) - { - case TargetPlatform.Windows: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet.exe"); - break; - case TargetPlatform.Linux: break; - case TargetPlatform.Mac: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet"); - break; - default: throw new InvalidPlatformException(buildPlatform); - } var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); foreach (var reference in targetBuildOptions.NugetPackageReferences) { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - { - var task = graph.Add(); - task.WorkingDirectory = target.FolderPath; - task.InfoMessage = $"Adding Nuget Package: {reference.Name}, Version {reference.Version}"; - task.CommandPath = dotnetPath; - task.CommandArguments = $"add package {reference.Name} --version {reference.Version}"; - } + Utilities.AddNugetPackage(graph, target, reference); var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); graph.AddCopyFile(dstFile, path); } diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs index beaebbb2d..f88a111f6 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs @@ -1057,36 +1057,15 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + if (targetBuildOptions.NugetPackageReferences.Any()) { - var buildPlatform = Platform.BuildTargetPlatform; - var dotnetSdk = DotNetSdk.Instance; - if (!dotnetSdk.IsValid) - throw new DotNetSdk.MissingException(); - var dotnetPath = "dotnet"; - switch (buildPlatform) - { - case TargetPlatform.Windows: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet.exe"); - break; - case TargetPlatform.Linux: break; - case TargetPlatform.Mac: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet"); - break; - default: throw new InvalidPlatformException(buildPlatform); - } var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); foreach (var reference in targetBuildOptions.NugetPackageReferences) { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - { - var task = graph.Add(); - task.WorkingDirectory = target.FolderPath; - task.InfoMessage = $"Adding Nuget Package: {reference.Name}, Version {reference.Version}"; - task.CommandPath = dotnetPath; - task.CommandArguments = $"add package {reference.Name} --version {reference.Version}"; - } + Utilities.AddNugetPackage(graph, target, reference); var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); graph.AddCopyFile(dstFile, path); } @@ -1291,37 +1270,15 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } - + if (targetBuildOptions.NugetPackageReferences.Any()) { - var buildPlatform = Platform.BuildTargetPlatform; - var dotnetSdk = DotNetSdk.Instance; - if (!dotnetSdk.IsValid) - throw new DotNetSdk.MissingException(); - var dotnetPath = "dotnet"; - switch (buildPlatform) - { - case TargetPlatform.Windows: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet.exe"); - break; - case TargetPlatform.Linux: break; - case TargetPlatform.Mac: - dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet"); - break; - default: throw new InvalidPlatformException(buildPlatform); - } var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); foreach (var reference in targetBuildOptions.NugetPackageReferences) { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - { - var task = graph.Add(); - task.WorkingDirectory = target.FolderPath; - task.InfoMessage = $"Adding Nuget Package: {reference.Name}, Version {reference.Version}"; - task.CommandPath = dotnetPath; - task.CommandArguments = $"add package {reference.Name} --version {reference.Version}"; - } + Utilities.AddNugetPackage(graph, target, reference); var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); graph.AddCopyFile(dstFile, path); } diff --git a/Source/Tools/Flax.Build/Utilities/Utilities.cs b/Source/Tools/Flax.Build/Utilities/Utilities.cs index 2b971f153..39b9cedba 100644 --- a/Source/Tools/Flax.Build/Utilities/Utilities.cs +++ b/Source/Tools/Flax.Build/Utilities/Utilities.cs @@ -16,6 +16,49 @@ namespace Flax.Build /// public static class Utilities { + /// + /// Gets the .Net SDK path. + /// + /// The path. + public static string GetDotNetPath() + { + var buildPlatform = Platform.BuildTargetPlatform; + var dotnetSdk = DotNetSdk.Instance; + if (!dotnetSdk.IsValid) + throw new DotNetSdk.MissingException(); + var dotnetPath = "dotnet"; + switch (buildPlatform) + { + case TargetPlatform.Windows: + dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet.exe"); + break; + case TargetPlatform.Linux: break; + case TargetPlatform.Mac: + dotnetPath = Path.Combine(dotnetSdk.RootPath, "dotnet"); + break; + default: throw new InvalidPlatformException(buildPlatform); + } + return dotnetPath; + } + + /// + /// Downloads a nuget package. + /// + /// The task graph. + /// The target. + /// The dotnet path. + /// + public static void AddNugetPackage(Graph.TaskGraph graph, Target target, NativeCpp.NugetPackage package) + { + var dotNetPath = GetDotNetPath(); + var task = graph.Add(); + task.WorkingDirectory = target.FolderPath; + task.InfoMessage = $"Add Nuget Package: {package.Name}, Version {package.Version}"; + task.CommandPath = dotNetPath; + //task.CommandArguments = $"add package {package.Name} --version {package.Version}"; + task.CommandArguments = $"restore"; + } + /// /// Gets the hash code for the string (the same for all platforms). Matches Engine algorithm for string hashing. ///