From 28eaac37dcbd361064d90a235c0f8d7602610300 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 11 Jun 2025 18:17:19 -0500 Subject: [PATCH 1/5] Initial work on nuget packages. --- .../Flax.Build/Build/Builder.Projects.cs | 1 + .../Flax.Build/Build/DotNet/Builder.DotNet.cs | 21 ++++++++++ Source/Tools/Flax.Build/Build/EngineTarget.cs | 1 + .../Build/NativeCpp/BuildOptions.cs | 39 +++++++++++++++++++ .../Build/NativeCpp/Builder.NativeCpp.cs | 18 +++++++++ Source/Tools/Flax.Build/Projects/Project.cs | 6 +++ .../VisualStudio/CSProjectGenerator.cs | 13 +++++++ .../VisualStudio/CSSDKProjectGenerator.cs | 14 +++++++ 8 files changed, 113 insertions(+) diff --git a/Source/Tools/Flax.Build/Build/Builder.Projects.cs b/Source/Tools/Flax.Build/Build/Builder.Projects.cs index 008e9d982..59538364c 100644 --- a/Source/Tools/Flax.Build/Build/Builder.Projects.cs +++ b/Source/Tools/Flax.Build/Build/Builder.Projects.cs @@ -513,6 +513,7 @@ namespace Flax.Build // Combine build options from this module project.CSharp.SystemReferences.AddRange(moduleBuildOptions.ScriptingAPI.SystemReferences); project.CSharp.FileReferences.AddRange(moduleBuildOptions.ScriptingAPI.FileReferences); + project.CSharp.NugetPackageReferences.AddRange(moduleBuildOptions.NugetPackageReferences); // Find references based on the modules dependencies (external or from projects) foreach (var dependencyName in moduleBuildOptions.PublicDependencies.Concat(moduleBuildOptions.PrivateDependencies)) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 2619f1a17..75a916296 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -109,6 +109,7 @@ namespace Flax.Build // Merge module into target environment buildData.TargetOptions.LinkEnv.InputFiles.AddRange(moduleOptions.OutputFiles); buildData.TargetOptions.DependencyFiles.AddRange(moduleOptions.DependencyFiles); + buildData.TargetOptions.NugetPackageReferences.AddRange(moduleOptions.NugetPackageReferences); buildData.TargetOptions.OptionalDependencyFiles.AddRange(moduleOptions.OptionalDependencyFiles); buildData.TargetOptions.Libraries.AddRange(moduleOptions.Libraries); buildData.TargetOptions.DelayLoadLibraries.AddRange(moduleOptions.DelayLoadLibraries); @@ -141,6 +142,14 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + + 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"); + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } } } @@ -283,6 +292,18 @@ namespace Flax.Build args.Add(string.Format("/reference:\"{0}{1}.dll\"", referenceAssemblies, reference)); foreach (var reference in fileReferences) args.Add(string.Format("/reference:\"{0}\"", reference)); + + // Reference Nuget package + if (buildData.TargetOptions.NugetPackageReferences.Any()) + { + var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); + foreach (var reference in buildOptions.NugetPackageReferences) + { + var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); + args.Add(string.Format("/reference:\"{0}\"", path)); + } + } + #if USE_NETCORE foreach (var systemAnalyzer in buildOptions.ScriptingAPI.SystemAnalyzers) args.Add(string.Format("/analyzer:\"{0}{1}.dll\"", referenceAnalyzers, systemAnalyzer)); diff --git a/Source/Tools/Flax.Build/Build/EngineTarget.cs b/Source/Tools/Flax.Build/Build/EngineTarget.cs index 6a44991a2..827205e91 100644 --- a/Source/Tools/Flax.Build/Build/EngineTarget.cs +++ b/Source/Tools/Flax.Build/Build/EngineTarget.cs @@ -220,6 +220,7 @@ namespace Flax.Build exeBuildOptions.LinkEnv.InputLibraries.Add(Path.Combine(buildOptions.OutputFolder, buildOptions.Platform.GetLinkOutputFileName(LibraryName, engineLibraryType))); exeBuildOptions.LinkEnv.InputFiles.AddRange(mainModuleOptions.OutputFiles); exeBuildOptions.DependencyFiles.AddRange(mainModuleOptions.DependencyFiles); + exeBuildOptions.NugetPackageReferences.AddRange(mainModuleOptions.NugetPackageReferences); exeBuildOptions.OptionalDependencyFiles.AddRange(mainModuleOptions.OptionalDependencyFiles); exeBuildOptions.Libraries.AddRange(mainModuleOptions.Libraries); exeBuildOptions.DelayLoadLibraries.AddRange(mainModuleOptions.DelayLoadLibraries); diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs b/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs index 0aa37b84e..7bda689e1 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/BuildOptions.cs @@ -70,6 +70,40 @@ namespace Flax.Build.NativeCpp Annotations, } + /// + /// Defines a Nuget Package + /// + public struct NugetPackage + { + /// + /// The name of the nuget package. + /// + public string Name; + + /// + /// The version of the nuget package. + /// + public string Version; + + /// + /// The target framework. ex. net8.0, netstandard2.1 + /// + public string Framework; + + /// + /// Initialize the nuget package. + /// + /// The name of the package. + /// The version of the package. + /// The target framework. ex. net8.0, netstandard2.1, etc. + public NugetPackage(string name, string version, string framework) + { + Name = name; + Version = version; + Framework = framework; + } + } + /// /// The native C++ module build settings container. /// @@ -129,6 +163,11 @@ namespace Flax.Build.NativeCpp /// The collection of the modules that are required by this module (for linking). /// public List PrivateDependencies = new List(); + + /// + /// The nuget package references. + /// + public List NugetPackageReferences = new List(); /// /// The collection of defines with preprocessing symbol for a source files of this module. Inherited by the modules that include it. diff --git a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs index 1d0d3c695..664bfb63f 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs @@ -425,6 +425,7 @@ namespace Flax.Build moduleOptions.LinkEnv.InputFiles.AddRange(dependencyOptions.OutputFiles); moduleOptions.DependencyFiles.AddRange(dependencyOptions.DependencyFiles); moduleOptions.OptionalDependencyFiles.AddRange(dependencyOptions.OptionalDependencyFiles); + moduleOptions.NugetPackageReferences.AddRange(dependencyOptions.NugetPackageReferences); moduleOptions.PrivateIncludePaths.AddRange(dependencyOptions.PublicIncludePaths); moduleOptions.Libraries.AddRange(dependencyOptions.Libraries); moduleOptions.DelayLoadLibraries.AddRange(dependencyOptions.DelayLoadLibraries); @@ -440,6 +441,7 @@ namespace Flax.Build moduleOptions.LinkEnv.InputFiles.AddRange(dependencyOptions.OutputFiles); moduleOptions.DependencyFiles.AddRange(dependencyOptions.DependencyFiles); moduleOptions.OptionalDependencyFiles.AddRange(dependencyOptions.OptionalDependencyFiles); + moduleOptions.NugetPackageReferences.AddRange(dependencyOptions.NugetPackageReferences); moduleOptions.PublicIncludePaths.AddRange(dependencyOptions.PublicIncludePaths); moduleOptions.Libraries.AddRange(dependencyOptions.Libraries); moduleOptions.DelayLoadLibraries.AddRange(dependencyOptions.DelayLoadLibraries); @@ -934,6 +936,7 @@ namespace Flax.Build buildData.TargetOptions.LinkEnv.InputFiles.AddRange(moduleOptions.OutputFiles); buildData.TargetOptions.DependencyFiles.AddRange(moduleOptions.DependencyFiles); buildData.TargetOptions.OptionalDependencyFiles.AddRange(moduleOptions.OptionalDependencyFiles); + buildData.TargetOptions.NugetPackageReferences.AddRange(moduleOptions.NugetPackageReferences); buildData.TargetOptions.Libraries.AddRange(moduleOptions.Libraries); buildData.TargetOptions.DelayLoadLibraries.AddRange(moduleOptions.DelayLoadLibraries); buildData.TargetOptions.ScriptingAPI.Add(moduleOptions.ScriptingAPI); @@ -1054,6 +1057,13 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + 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"); + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } } } @@ -1192,6 +1202,7 @@ namespace Flax.Build buildData.TargetOptions.ExternalModules.AddRange(moduleOptions.ExternalModules); buildData.TargetOptions.DependencyFiles.AddRange(moduleOptions.DependencyFiles); buildData.TargetOptions.OptionalDependencyFiles.AddRange(moduleOptions.OptionalDependencyFiles); + buildData.TargetOptions.NugetPackageReferences.AddRange(moduleOptions.NugetPackageReferences); } } } @@ -1253,6 +1264,13 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + 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"); + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } } } diff --git a/Source/Tools/Flax.Build/Projects/Project.cs b/Source/Tools/Flax.Build/Projects/Project.cs index 5aa77a453..46ae6102b 100644 --- a/Source/Tools/Flax.Build/Projects/Project.cs +++ b/Source/Tools/Flax.Build/Projects/Project.cs @@ -229,6 +229,11 @@ namespace Flax.Build.Projects /// The .Net libraries references (dll or exe files paths). /// public HashSet FileReferences; + + /// + /// The nuget references. + /// + public HashSet NugetPackageReferences; /// /// The output folder path (optional). @@ -248,6 +253,7 @@ namespace Flax.Build.Projects { SystemReferences = new HashSet(), FileReferences = new HashSet(), + NugetPackageReferences = new HashSet(), }; /// diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSProjectGenerator.cs index da6907f78..35bc7b0ac 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSProjectGenerator.cs @@ -175,6 +175,19 @@ namespace Flax.Build.Projects.VisualStudio csProjectFileContent.AppendLine(" "); } + // Nuget + if (project.CSharp.NugetPackageReferences.Any()) + { + csProjectFileContent.AppendLine(" "); + + foreach (var reference in project.CSharp.NugetPackageReferences) + { + csProjectFileContent.AppendLine(string.Format(" ", reference.Name, reference.Version)); + } + + csProjectFileContent.AppendLine(" "); + } + // References csProjectFileContent.AppendLine(" "); diff --git a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs index df1424e95..f126bd3ef 100644 --- a/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs +++ b/Source/Tools/Flax.Build/Projects/VisualStudio/CSSDKProjectGenerator.cs @@ -134,6 +134,20 @@ namespace Flax.Build.Projects.VisualStudio } // References + + // Nuget + if (project.CSharp.NugetPackageReferences.Any()) + { + csProjectFileContent.AppendLine(" "); + + foreach (var reference in project.CSharp.NugetPackageReferences) + { + csProjectFileContent.AppendLine(string.Format(" ", reference.Name, reference.Version)); + } + + csProjectFileContent.AppendLine(" "); + csProjectFileContent.AppendLine(""); + } csProjectFileContent.AppendLine(" "); From ecaae2b4580deb5447e70904a6247d3db48efcbe Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 19 Jun 2025 21:34:55 -0500 Subject: [PATCH 2/5] Add downloading nuget package if needed. --- .../Flax.Build/Build/DotNet/Builder.DotNet.cs | 37 +++++++-- .../Build/NativeCpp/Builder.NativeCpp.cs | 75 ++++++++++++++++--- 2 files changed, 97 insertions(+), 15 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 75a916296..895810e9e 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -143,12 +143,39 @@ namespace Flax.Build graph.AddCopyFile(dstFile, srcFile); } - var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); - foreach (var reference in targetBuildOptions.NugetPackageReferences) + if (targetBuildOptions.NugetPackageReferences.Any()) { - var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); - var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); - graph.AddCopyFile(dstFile, path); + 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}"; + } + 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 664bfb63f..beaebbb2d 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs @@ -1057,12 +1057,39 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } - var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); - foreach (var reference in targetBuildOptions.NugetPackageReferences) + if (targetBuildOptions.NugetPackageReferences.Any()) { - var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); - var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); - graph.AddCopyFile(dstFile, path); + 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}"; + } + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } } } } @@ -1264,12 +1291,40 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } - var nugetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages"); - foreach (var reference in targetBuildOptions.NugetPackageReferences) + + if (targetBuildOptions.NugetPackageReferences.Any()) { - var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); - var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); - graph.AddCopyFile(dstFile, path); + 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}"; + } + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } } } } From 53761df85e3509bcf744d03fbecab21a3d5fb792 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 19 Jun 2025 22:07:28 -0500 Subject: [PATCH 3/5] 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. /// From fdd22c3380d3a171d2888909e0f4b3420d885029 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 19 Jun 2025 22:09:33 -0500 Subject: [PATCH 4/5] Remove extra code. --- Source/Tools/Flax.Build/Utilities/Utilities.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Tools/Flax.Build/Utilities/Utilities.cs b/Source/Tools/Flax.Build/Utilities/Utilities.cs index 39b9cedba..484aad59f 100644 --- a/Source/Tools/Flax.Build/Utilities/Utilities.cs +++ b/Source/Tools/Flax.Build/Utilities/Utilities.cs @@ -55,7 +55,6 @@ namespace Flax.Build 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"; } From c8622d180122f60ba93fcbaa7080eb469c86fbbe Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 20 Jun 2025 15:26:58 -0500 Subject: [PATCH 5/5] Change method name from add to restore. --- Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs | 2 +- .../Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs | 4 ++-- Source/Tools/Flax.Build/Utilities/Utilities.cs | 7 +++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs index 5d1e90a5a..23646a3d7 100644 --- a/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs +++ b/Source/Tools/Flax.Build/Build/DotNet/Builder.DotNet.cs @@ -150,7 +150,7 @@ namespace Flax.Build { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - Utilities.AddNugetPackage(graph, target, reference); + Utilities.RestoreNugetPackages(graph, target); 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 f88a111f6..18459ddb5 100644 --- a/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs +++ b/Source/Tools/Flax.Build/Build/NativeCpp/Builder.NativeCpp.cs @@ -1065,7 +1065,7 @@ namespace Flax.Build { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - Utilities.AddNugetPackage(graph, target, reference); + Utilities.RestoreNugetPackages(graph, target); var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); graph.AddCopyFile(dstFile, path); } @@ -1278,7 +1278,7 @@ namespace Flax.Build { var path = Path.Combine(nugetPath, reference.Name, reference.Version, "lib", reference.Framework, $"{reference.Name}.dll"); if (!File.Exists(path)) - Utilities.AddNugetPackage(graph, target, reference); + Utilities.RestoreNugetPackages(graph, target); 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 484aad59f..7552849b3 100644 --- a/Source/Tools/Flax.Build/Utilities/Utilities.cs +++ b/Source/Tools/Flax.Build/Utilities/Utilities.cs @@ -42,18 +42,17 @@ namespace Flax.Build } /// - /// Downloads a nuget package. + /// Restores a targets nuget packages. /// /// The task graph. /// The target. /// The dotnet path. - /// - public static void AddNugetPackage(Graph.TaskGraph graph, Target target, NativeCpp.NugetPackage package) + public static void RestoreNugetPackages(Graph.TaskGraph graph, Target target) { var dotNetPath = GetDotNetPath(); var task = graph.Add(); task.WorkingDirectory = target.FolderPath; - task.InfoMessage = $"Add Nuget Package: {package.Name}, Version {package.Version}"; + task.InfoMessage = $"Restoring Nuget Packages for {target.Name}"; task.CommandPath = dotNetPath; task.CommandArguments = $"restore"; }