Merge branch 'Tryibion-initial-nuget'

This commit is contained in:
Wojtek Figat
2025-08-19 13:18:54 +02:00
9 changed files with 171 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@@ -70,6 +70,40 @@ namespace Flax.Build.NativeCpp
Annotations,
}
/// <summary>
/// Defines a Nuget Package
/// </summary>
public struct NugetPackage
{
/// <summary>
/// The name of the nuget package.
/// </summary>
public string Name;
/// <summary>
/// The version of the nuget package.
/// </summary>
public string Version;
/// <summary>
/// The target framework. ex. net8.0, netstandard2.1
/// </summary>
public string Framework;
/// <summary>
/// Initialize the nuget package.
/// </summary>
/// <param name="name">The name of the package.</param>
/// <param name="version">The version of the package.</param>
/// <param name="framework">The target framework. ex. net8.0, netstandard2.1, etc.</param>
public NugetPackage(string name, string version, string framework)
{
Name = name;
Version = version;
Framework = framework;
}
}
/// <summary>
/// The native C++ module build settings container.
/// </summary>
@@ -129,6 +163,11 @@ namespace Flax.Build.NativeCpp
/// The collection of the modules that are required by this module (for linking).
/// </summary>
public List<string> PrivateDependencies = new List<string>();
/// <summary>
/// The nuget package references.
/// </summary>
public List<NugetPackage> NugetPackageReferences = new List<NugetPackage>();
/// <summary>
/// The collection of defines with preprocessing symbol for a source files of this module. Inherited by the modules that include it.

View File

@@ -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);
}
}
}
}

View File

@@ -229,6 +229,11 @@ namespace Flax.Build.Projects
/// The .Net libraries references (dll or exe files paths).
/// </summary>
public HashSet<string> FileReferences;
/// <summary>
/// The nuget references.
/// </summary>
public HashSet<NugetPackage> NugetPackageReferences;
/// <summary>
/// The output folder path (optional).
@@ -248,6 +253,7 @@ namespace Flax.Build.Projects
{
SystemReferences = new HashSet<string>(),
FileReferences = new HashSet<string>(),
NugetPackageReferences = new HashSet<NugetPackage>(),
};
/// <summary>

View File

@@ -175,6 +175,19 @@ namespace Flax.Build.Projects.VisualStudio
csProjectFileContent.AppendLine(" </PropertyGroup>");
}
// Nuget
if (project.CSharp.NugetPackageReferences.Any())
{
csProjectFileContent.AppendLine(" <ItemGroup>");
foreach (var reference in project.CSharp.NugetPackageReferences)
{
csProjectFileContent.AppendLine(string.Format(" <PackageReference Include=\"{0}\" Version=\"{1}\" />", reference.Name, reference.Version));
}
csProjectFileContent.AppendLine(" </ItemGroup>");
}
// References
csProjectFileContent.AppendLine(" <ItemGroup>");

View File

@@ -134,6 +134,20 @@ namespace Flax.Build.Projects.VisualStudio
}
// References
// Nuget
if (project.CSharp.NugetPackageReferences.Any())
{
csProjectFileContent.AppendLine(" <ItemGroup>");
foreach (var reference in project.CSharp.NugetPackageReferences)
{
csProjectFileContent.AppendLine(string.Format(" <PackageReference Include=\"{0}\" Version=\"{1}\" />", reference.Name, reference.Version));
}
csProjectFileContent.AppendLine(" </ItemGroup>");
csProjectFileContent.AppendLine("");
}
csProjectFileContent.AppendLine(" <ItemGroup>");

View File

@@ -16,6 +16,47 @@ namespace Flax.Build
/// </summary>
public static class Utilities
{
/// <summary>
/// Gets the .Net SDK path.
/// </summary>
/// <returns>The path.</returns>
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;
}
/// <summary>
/// Restores a targets nuget packages.
/// </summary>
/// <param name="graph">The task graph.</param>
/// <param name="target">The target.</param>
/// <param name="dotNetPath">The dotnet path.</param>
public static void RestoreNugetPackages(Graph.TaskGraph graph, Target target)
{
var dotNetPath = GetDotNetPath();
var task = graph.Add<Graph.Task>();
task.WorkingDirectory = target.FolderPath;
task.InfoMessage = $"Restoring Nuget Packages for {target.Name}";
task.CommandPath = dotNetPath;
task.CommandArguments = $"restore";
}
/// <summary>
/// Gets the hash code for the string (the same for all platforms). Matches Engine algorithm for string hashing.
/// </summary>