From 889ac90a67ed762cb07a3d627f6586f67e8c2864 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 1 Feb 2024 18:37:07 +0100 Subject: [PATCH] Ready To run feature feature toggle --- starsky/.nuke/build.schema.json | 4 ++ starsky/build/Build.cs | 46 ++++++++++--- .../helpers/DotnetRuntimeSpecificHelper.cs | 26 ++++++-- starsky/build/helpers/RuntimeIdentifier.cs | 64 ++++++++++++++++++- 4 files changed, 125 insertions(+), 15 deletions(-) diff --git a/starsky/.nuke/build.schema.json b/starsky/.nuke/build.schema.json index 1b005440c8..26ccbe899d 100644 --- a/starsky/.nuke/build.schema.json +++ b/starsky/.nuke/build.schema.json @@ -98,6 +98,10 @@ "type": "string" } }, + "ReadyToRun": { + "type": "boolean", + "description": "Enable Ready to run builds" + }, "Root": { "type": "string", "description": "Root directory during build execution" diff --git a/starsky/build/Build.cs b/starsky/build/Build.cs index c40f25fe94..a201a96f04 100644 --- a/starsky/build/Build.cs +++ b/starsky/build/Build.cs @@ -107,6 +107,25 @@ string GetBranchName() } return branchName; } + + /// + /// Enable for Ready to run build + /// Only is combination is supported + /// @see: https://learn.microsoft.com/en-us/dotnet/core/deploying/ready-to-run + /// for build combinations, if not supported, it will auto skip + /// + /// true if explicit enabled + [Parameter("Enable Ready to run builds")] + readonly bool ReadyToRun; + + /// + /// --ready-to-run + /// + /// + public bool IsReadyToRunEnabled() + { + return ReadyToRun; + } /// /// Only the OS specific runtimes, so skip generic-netcore @@ -140,6 +159,11 @@ List GetRuntimesWithoutGeneric() "starskythumbnailmetacli", "starsky" }; + + /// + /// Link to GeoCli.csproj + /// + const string GeoCliCsproj = "starskygeocli/starskygeocli.csproj"; /// /// Npm and node are required for preflight checks and building frontend code @@ -216,7 +240,10 @@ void ShowSettingsInfo() Log.Information(NoDependencies ? "External dependencies: disabled" : "External dependencies: enabled"); - + + Log.Information(NoDependencies ? "External dependencies: disabled" : + "External dependencies: enabled"); + if ( !string.IsNullOrEmpty(GetBranchName()) ) { Log.Information("(Overwrite) Branch:"); @@ -262,9 +289,8 @@ void ShowSettingsInfo() "starskytest/coverage-merge-sonarqube.xml"); DotnetGenericHelper.BuildNetCoreGenericCommand(Solution,Configuration); DotnetTestHelper.TestNetCoreGenericCommand(Configuration,IsUnitTestDisabled()); - DotnetGenericHelper.DownloadDependencies(Configuration, - "starskygeocli/starskygeocli.csproj",NoDependencies, - "generic-netcore"); + DotnetGenericHelper.DownloadDependencies(Configuration, GeoCliCsproj + ,NoDependencies, GenericRuntimeName); MergeCoverageFiles.Merge(IsUnitTestDisabled()); SonarQube.SonarEnd(IsUnitTestDisabled(),NoSonar); DotnetGenericHelper.PublishNetCoreGenericCommand(Configuration, IsPublishDisabled()); @@ -278,9 +304,9 @@ void ShowSettingsInfo() { DotnetGenericHelper.DownloadDependencies(Configuration, "starskygeocli/starskygeocli.csproj",NoDependencies, - "generic-netcore"); + GenericRuntimeName); DotnetRuntimeSpecificHelper.CopyDependenciesFiles(NoDependencies, - "generic-netcore",GetRuntimesWithoutGeneric()); + GenericRuntimeName,GetRuntimesWithoutGeneric()); }); @@ -307,12 +333,14 @@ void ShowSettingsInfo() foreach ( var runtime in GetRuntimesWithoutGeneric() ) { - DotnetRuntimeSpecificHelper.BuildNetCoreCommand(Solution, Configuration, runtime); - DotnetRuntimeSpecificHelper.PublishNetCoreGenericCommand(Configuration, runtime); + DotnetRuntimeSpecificHelper.BuildNetCoreCommand(Solution, Configuration, + runtime, IsReadyToRunEnabled()); + DotnetRuntimeSpecificHelper.PublishNetCoreGenericCommand(Configuration, + runtime, IsReadyToRunEnabled()); } DotnetRuntimeSpecificHelper.CopyDependenciesFiles(NoDependencies, - "generic-netcore",GetRuntimesWithoutGeneric()); + GenericRuntimeName,GetRuntimesWithoutGeneric()); }); // ReSharper disable once UnusedMember.Local diff --git a/starsky/build/helpers/DotnetRuntimeSpecificHelper.cs b/starsky/build/helpers/DotnetRuntimeSpecificHelper.cs index cd0e1045e7..d504a2ea12 100644 --- a/starsky/build/helpers/DotnetRuntimeSpecificHelper.cs +++ b/starsky/build/helpers/DotnetRuntimeSpecificHelper.cs @@ -95,7 +95,7 @@ public static void CopyDependenciesFiles(bool noDependencies, } } - + /// /// Specific build for runtime /// Runs the command: dotnet build @@ -103,11 +103,17 @@ public static void CopyDependenciesFiles(bool noDependencies, /// the solution file (sln) /// Config file /// which runtime e.g. linux-arm or osx-x64 + /// Is Ready To Run Enabled public static void BuildNetCoreCommand(Solution solution, Configuration - configuration, string runtime) + configuration, string runtime, bool isReadyToRunEnabled) { Log.Information("> dotnet build next for: solution: " + solution + " runtime: " + runtime); + var readyToRunArgument = + RuntimeIdentifier.IsReadyToRunSupported(runtime) && isReadyToRunEnabled + ? "-p:PublishReadyToRun=true" + : ""; + DotNetBuild(p => p .SetProjectFile(solution) // Implicit restore here, since .NET 8 self contained is disabled @@ -127,6 +133,7 @@ public static void BuildNetCoreCommand(Solution solution, Configuration .Add("/p:WarningLevel=0") // SonarQube analysis is done in the generic build .Add("/p:noSonar=true") + .Add(readyToRunArgument) )); } @@ -136,11 +143,14 @@ public static void BuildNetCoreCommand(Solution solution, Configuration /// /// Release /// runtime identifier - public static void PublishNetCoreGenericCommand(Configuration configuration, string runtime) + /// Is Ready To Run Enabled + public static void PublishNetCoreGenericCommand(Configuration configuration, + string runtime, bool isReadyToRunEnabled) { foreach ( var publishProject in Build.PublishProjectsList ) { - Log.Information(">> next publishProject: " + publishProject + " runtime: " + runtime); + Log.Information(">> next publishProject: " + + publishProject + " runtime: " + runtime); var publishProjectFullPath = Path.Combine( WorkingDirectory.GetSolutionParentFolder(), @@ -149,7 +159,12 @@ public static void PublishNetCoreGenericCommand(Configuration configuration, str var outputFullPath = Path.Combine( WorkingDirectory.GetSolutionParentFolder(), runtime); - + + var readyToRunArgument = + RuntimeIdentifier.IsReadyToRunSupported(runtime) && isReadyToRunEnabled + ? "-p:PublishReadyToRun=true" + : ""; + DotNetPublish(p => p .SetConfiguration(configuration) .EnableNoRestore() @@ -163,6 +178,7 @@ public static void PublishNetCoreGenericCommand(Configuration configuration, str .SetProcessArgumentConfigurator(args => args.Add("/p:noSonar=true") .Add($"/p:OverwriteRuntimeIdentifier={runtime}") + .Add(readyToRunArgument) ) ); } diff --git a/starsky/build/helpers/RuntimeIdentifier.cs b/starsky/build/helpers/RuntimeIdentifier.cs index bccce63661..985182872a 100644 --- a/starsky/build/helpers/RuntimeIdentifier.cs +++ b/starsky/build/helpers/RuntimeIdentifier.cs @@ -1,5 +1,6 @@ -using System; +using System.Collections.Generic; using System.Runtime.InteropServices; +using Serilog; namespace helpers; @@ -26,4 +27,65 @@ public static string GetCurrentRuntimeIdentifier() return $"{os}-{architecture}"; } + + /// + /// SDK-platform Supported target platforms + /// Windows X64 Windows (X86, X64, Arm64), Linux (X64, Arm32, Arm64), macOS (X64, Arm64) + /// win-x64 - supports: win-x86, win-x64, win-arm64, linux-x64, linux-arm, linux-arm64, osx-x64, osx-arm64 + /// Windows X86 Windows (X86), Linux (Arm32) + /// win-x86 - supports: win-x86, linux-arm + /// Linux X64 Linux (X64, Arm32, Arm64), macOS (X64, Arm64) + /// linux-x64 - supports linux-x64, linux-arm, linux-arm64, osx-x64, osx-arm64 + /// Linux Arm32 Linux Arm32 + /// linux-arm, supports linux-arm + /// Linux Arm64 Linux (X64, Arm32, Arm64), macOS (X64, Arm64) + /// linux-arm64 supports linux-x64, linux-arm, linux-arm64, osx-x64, osx-arm64 + /// macOS X64 Linux (X64, Arm32, Arm64), macOS (X64, Arm64) + /// osx-x64 supports linux-x64, linux-arm, linux-arm64, osx-x64, osx-arm64 + /// macOS Arm64 Linux (X64, Arm32, Arm64), macOS (X64, Arm64) + /// osx-arm64 supports linux-x64, linux-arm, linux-arm64, osx-x64, osx-arm64 + /// + /// @see: https://learn.microsoft.com/en-us/dotnet/core/deploying/ready-to-run + /// + static readonly Dictionary> SupportedPlatforms = new() + { + { "win-x64", + [ + "win-x86", "win-x64", "win-arm64", "linux-x64", "linux-arm", + "linux-arm64", "osx-x64", "osx-arm64" + ] + }, + { "win-x86", ["win-x86", "linux-arm"] }, + { "linux-x64", + ["linux-x64", "linux-arm", "linux-arm64", "osx-x64", "osx-arm64"] + }, + { "linux-arm", ["linux-arm"] }, + { "linux-arm64", + ["linux-x64", "linux-arm", "linux-arm64", "osx-x64", "osx-arm64"] + }, + { "osx-x64", + ["linux-x64", "linux-arm", "linux-arm64", "osx-x64", "osx-arm64"] + }, + { "osx-arm64", + ["linux-x64", "linux-arm", "linux-arm64", "osx-x64", "osx-arm64"] + } + }; + + public static bool IsReadyToRunSupported(string toRuntimeIdentifier) + { + var currentIdentifier = GetCurrentRuntimeIdentifier(); + return IsReadyToRunSupported(currentIdentifier, toRuntimeIdentifier); + } + + static bool IsReadyToRunSupported(string currentIdentifier, string toRuntimeIdentifier) + { + if (SupportedPlatforms.TryGetValue(currentIdentifier, out var supportedTargets)) + { + return supportedTargets.Contains(toRuntimeIdentifier); + } + + // Handle unsupported currentIdentifier + Log.Error($"Unsupported currentIdentifier: {currentIdentifier}"); + return false; + } }