Add utility methods for redundant code.

This commit is contained in:
Chandler Cox
2025-06-19 22:07:28 -05:00
parent ecaae2b458
commit 53761df85e
3 changed files with 48 additions and 70 deletions

View File

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

View File

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

View File

@@ -16,6 +16,49 @@ 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>
/// Downloads a nuget package.
/// </summary>
/// <param name="graph">The task graph.</param>
/// <param name="target">The target.</param>
/// <param name="dotNetPath">The dotnet path.</param>
/// <param name="package"></param>
public static void AddNugetPackage(Graph.TaskGraph graph, Target target, NativeCpp.NugetPackage package)
{
var dotNetPath = GetDotNetPath();
var task = graph.Add<Graph.Task>();
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";
}
/// <summary>
/// Gets the hash code for the string (the same for all platforms). Matches Engine algorithm for string hashing.
/// </summary>