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..23646a3d7 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,19 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + + if (targetBuildOptions.NugetPackageReferences.Any()) + { + 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)) + Utilities.RestoreNugetPackages(graph, target); + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } + } } } @@ -283,6 +297,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..18459ddb5 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,19 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + + if (targetBuildOptions.NugetPackageReferences.Any()) + { + 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)) + Utilities.RestoreNugetPackages(graph, target); + var dstFile = Path.Combine(outputPath, Path.GetFileName(path)); + graph.AddCopyFile(dstFile, path); + } + } } } @@ -1192,6 +1208,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 +1270,19 @@ namespace Flax.Build var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); graph.AddCopyFile(dstFile, srcFile); } + + if (targetBuildOptions.NugetPackageReferences.Any()) + { + 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)) + Utilities.RestoreNugetPackages(graph, target); + 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(" "); diff --git a/Source/Tools/Flax.Build/Utilities/Utilities.cs b/Source/Tools/Flax.Build/Utilities/Utilities.cs index 2b971f153..7552849b3 100644 --- a/Source/Tools/Flax.Build/Utilities/Utilities.cs +++ b/Source/Tools/Flax.Build/Utilities/Utilities.cs @@ -16,6 +16,47 @@ 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; + } + + /// + /// Restores a targets nuget packages. + /// + /// The task graph. + /// The target. + /// The dotnet path. + public static void RestoreNugetPackages(Graph.TaskGraph graph, Target target) + { + var dotNetPath = GetDotNetPath(); + var task = graph.Add(); + task.WorkingDirectory = target.FolderPath; + task.InfoMessage = $"Restoring Nuget Packages for {target.Name}"; + task.CommandPath = dotNetPath; + task.CommandArguments = $"restore"; + } + /// /// Gets the hash code for the string (the same for all platforms). Matches Engine algorithm for string hashing. ///