1
0

Build.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Xml.Linq;
  10. using Nuke.Common;
  11. using Nuke.Common.Git;
  12. using Nuke.Common.ProjectModel;
  13. using Nuke.Common.Tooling;
  14. using Nuke.Common.Tools.DotNet;
  15. using Nuke.Common.Tools.MSBuild;
  16. using Nuke.Common.Tools.Npm;
  17. using Nuke.Common.Utilities;
  18. using Nuke.Common.Utilities.Collections;
  19. using static Nuke.Common.EnvironmentInfo;
  20. using static Nuke.Common.IO.FileSystemTasks;
  21. using static Nuke.Common.IO.PathConstruction;
  22. using static Nuke.Common.Tools.MSBuild.MSBuildTasks;
  23. using static Nuke.Common.Tools.DotNet.DotNetTasks;
  24. using static Nuke.Common.Tools.Xunit.XunitTasks;
  25. using static Nuke.Common.Tools.VSWhere.VSWhereTasks;
  26. /*
  27. Before editing this file, install support plugin for your IDE,
  28. running and debugging a particular target (optionally without deps) would be way easier
  29. ReSharper/Rider - https://plugins.jetbrains.com/plugin/10803-nuke-support
  30. VSCode - https://marketplace.visualstudio.com/items?itemName=nuke.support
  31. */
  32. partial class Build : NukeBuild
  33. {
  34. [Solution("Avalonia.sln")] readonly Solution Solution;
  35. BuildParameters Parameters { get; set; }
  36. protected override void OnBuildInitialized()
  37. {
  38. Parameters = new BuildParameters(this);
  39. Information("Building version {0} of Avalonia ({1}) using version {2} of Nuke.",
  40. Parameters.Version,
  41. Parameters.Configuration,
  42. typeof(NukeBuild).Assembly.GetName().Version.ToString());
  43. if (Parameters.IsLocalBuild)
  44. {
  45. Information("Repository Name: " + Parameters.RepositoryName);
  46. Information("Repository Branch: " + Parameters.RepositoryBranch);
  47. }
  48. Information("Configuration: " + Parameters.Configuration);
  49. Information("IsLocalBuild: " + Parameters.IsLocalBuild);
  50. Information("IsRunningOnUnix: " + Parameters.IsRunningOnUnix);
  51. Information("IsRunningOnWindows: " + Parameters.IsRunningOnWindows);
  52. Information("IsRunningOnAzure:" + Parameters.IsRunningOnAzure);
  53. Information("IsPullRequest: " + Parameters.IsPullRequest);
  54. Information("IsMainRepo: " + Parameters.IsMainRepo);
  55. Information("IsMasterBranch: " + Parameters.IsMasterBranch);
  56. Information("IsReleaseBranch: " + Parameters.IsReleaseBranch);
  57. Information("IsReleasable: " + Parameters.IsReleasable);
  58. Information("IsMyGetRelease: " + Parameters.IsMyGetRelease);
  59. Information("IsNuGetRelease: " + Parameters.IsNuGetRelease);
  60. void ExecWait(string preamble, string command, string args)
  61. {
  62. Console.WriteLine(preamble);
  63. Process.Start(new ProcessStartInfo(command, args) {UseShellExecute = false}).WaitForExit();
  64. }
  65. ExecWait("dotnet version:", "dotnet", "--info");
  66. ExecWait("dotnet workloads:", "dotnet", "workload list");
  67. Information("Processor count: " + Environment.ProcessorCount);
  68. Information("Available RAM: " + GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / 0x100000 + "MB");
  69. }
  70. DotNetConfigHelper ApplySettingCore(DotNetConfigHelper c)
  71. {
  72. if (Parameters.IsRunningOnAzure)
  73. c.AddProperty("JavaSdkDirectory", GetVariable<string>("JAVA_HOME_11_X64"));
  74. c.AddProperty("PackageVersion", Parameters.Version)
  75. .AddProperty("iOSRoslynPathHackRequired", true)
  76. .SetConfiguration(Parameters.Configuration)
  77. .SetVerbosity(DotNetVerbosity.Minimal);
  78. return c;
  79. }
  80. DotNetBuildSettings ApplySetting(DotNetBuildSettings c, Configure<DotNetBuildSettings> configurator = null) =>
  81. ApplySettingCore(c).Build.Apply(configurator);
  82. DotNetPackSettings ApplySetting(DotNetPackSettings c, Configure<DotNetPackSettings> configurator = null) =>
  83. ApplySettingCore(c).Pack.Apply(configurator);
  84. DotNetTestSettings ApplySetting(DotNetTestSettings c, Configure<DotNetTestSettings> configurator = null) =>
  85. ApplySettingCore(c).Test.Apply(configurator);
  86. Target Clean => _ => _.Executes(() =>
  87. {
  88. Parameters.BuildDirs.ForEach(DeleteDirectory);
  89. Parameters.BuildDirs.ForEach(EnsureCleanDirectory);
  90. EnsureCleanDirectory(Parameters.ArtifactsDir);
  91. EnsureCleanDirectory(Parameters.NugetIntermediateRoot);
  92. EnsureCleanDirectory(Parameters.NugetRoot);
  93. EnsureCleanDirectory(Parameters.ZipRoot);
  94. EnsureCleanDirectory(Parameters.TestResultsRoot);
  95. });
  96. Target CompileHtmlPreviewer => _ => _
  97. .DependsOn(Clean)
  98. .OnlyWhenStatic(() => !Parameters.SkipPreviewer)
  99. .Executes(() =>
  100. {
  101. var webappDir = RootDirectory / "src" / "Avalonia.DesignerSupport" / "Remote" / "HtmlTransport" / "webapp";
  102. NpmTasks.NpmInstall(c => c
  103. .SetProcessWorkingDirectory(webappDir)
  104. .SetProcessArgumentConfigurator(a => a.Add("--silent")));
  105. NpmTasks.NpmRun(c => c
  106. .SetProcessWorkingDirectory(webappDir)
  107. .SetCommand("dist"));
  108. });
  109. Target CompileNative => _ => _
  110. .DependsOn(Clean)
  111. .DependsOn(GenerateCppHeaders)
  112. .OnlyWhenStatic(() => EnvironmentInfo.IsOsx)
  113. .Executes(() =>
  114. {
  115. var project = $"{RootDirectory}/native/Avalonia.Native/src/OSX/Avalonia.Native.OSX.xcodeproj/";
  116. var args = $"-project {project} -configuration {Parameters.Configuration} CONFIGURATION_BUILD_DIR={RootDirectory}/Build/Products/Release";
  117. ProcessTasks.StartProcess("xcodebuild", args).AssertZeroExitCode();
  118. });
  119. Target Compile => _ => _
  120. .DependsOn(Clean, CompileNative)
  121. .DependsOn(CompileHtmlPreviewer)
  122. .Executes(() =>
  123. {
  124. DotNetBuild(c => ApplySetting(c)
  125. .SetProjectFile(Parameters.MSBuildSolution)
  126. );
  127. });
  128. void RunCoreTest(string projectName)
  129. {
  130. Information($"Running tests from {projectName}");
  131. var project = Solution.GetProject(projectName).NotNull("project != null");
  132. foreach (var fw in project.GetTargetFrameworks())
  133. {
  134. if (fw.StartsWith("net4")
  135. && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
  136. && Environment.GetEnvironmentVariable("FORCE_LINUX_TESTS") != "1")
  137. {
  138. Information($"Skipping {projectName} ({fw}) tests on Linux - https://github.com/mono/mono/issues/13969");
  139. continue;
  140. }
  141. Information($"Running for {projectName} ({fw}) ...");
  142. DotNetTest(c => ApplySetting(c)
  143. .SetProjectFile(project)
  144. .SetFramework(fw)
  145. .EnableNoBuild()
  146. .EnableNoRestore()
  147. .When(Parameters.PublishTestResults, _ => _
  148. .SetLoggers("trx")
  149. .SetResultsDirectory(Parameters.TestResultsRoot)));
  150. }
  151. }
  152. Target RunHtmlPreviewerTests => _ => _
  153. .DependsOn(CompileHtmlPreviewer)
  154. .OnlyWhenStatic(() => !(Parameters.SkipPreviewer || Parameters.SkipTests))
  155. .Executes(() =>
  156. {
  157. var webappTestDir = RootDirectory / "tests" / "Avalonia.DesignerSupport.Tests" / "Remote" / "HtmlTransport" / "webapp";
  158. NpmTasks.NpmInstall(c => c
  159. .SetProcessWorkingDirectory(webappTestDir)
  160. .SetProcessArgumentConfigurator(a => a.Add("--silent")));
  161. NpmTasks.NpmRun(c => c
  162. .SetProcessWorkingDirectory(webappTestDir)
  163. .SetCommand("test"));
  164. });
  165. Target RunCoreLibsTests => _ => _
  166. .OnlyWhenStatic(() => !Parameters.SkipTests)
  167. .DependsOn(Compile)
  168. .Executes(() =>
  169. {
  170. RunCoreTest("Avalonia.Base.UnitTests");
  171. RunCoreTest("Avalonia.Controls.UnitTests");
  172. RunCoreTest("Avalonia.Controls.DataGrid.UnitTests");
  173. RunCoreTest("Avalonia.Markup.UnitTests");
  174. RunCoreTest("Avalonia.Markup.Xaml.UnitTests");
  175. RunCoreTest("Avalonia.Skia.UnitTests");
  176. RunCoreTest("Avalonia.ReactiveUI.UnitTests");
  177. });
  178. Target RunRenderTests => _ => _
  179. .OnlyWhenStatic(() => !Parameters.SkipTests)
  180. .DependsOn(Compile)
  181. .Executes(() =>
  182. {
  183. RunCoreTest("Avalonia.Skia.RenderTests");
  184. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  185. RunCoreTest("Avalonia.Direct2D1.RenderTests");
  186. });
  187. Target RunDesignerTests => _ => _
  188. .OnlyWhenStatic(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  189. .DependsOn(Compile)
  190. .Executes(() =>
  191. {
  192. RunCoreTest("Avalonia.DesignerSupport.Tests");
  193. });
  194. [PackageExecutable("JetBrains.dotMemoryUnit", "dotMemoryUnit.exe")] readonly Tool DotMemoryUnit;
  195. Target RunLeakTests => _ => _
  196. .OnlyWhenStatic(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  197. .DependsOn(Compile)
  198. .Executes(() =>
  199. {
  200. void DoMemoryTest()
  201. {
  202. var testAssembly = "tests\\Avalonia.LeakTests\\bin\\Release\\net461\\Avalonia.LeakTests.dll";
  203. DotMemoryUnit(
  204. $"{XunitPath.DoubleQuoteIfNeeded()} --propagate-exit-code -- {testAssembly}",
  205. timeout: 120_000);
  206. }
  207. ControlFlow.ExecuteWithRetry(DoMemoryTest, delay: TimeSpan.FromMilliseconds(3));
  208. });
  209. Target ZipFiles => _ => _
  210. .After(CreateNugetPackages, Compile, RunCoreLibsTests, Package)
  211. .Executes(() =>
  212. {
  213. var data = Parameters;
  214. Zip(data.ZipNuGetArtifacts, data.NugetRoot);
  215. });
  216. Target CreateIntermediateNugetPackages => _ => _
  217. .DependsOn(Compile)
  218. .After(RunTests)
  219. .Executes(() =>
  220. {
  221. DotNetPack(c => ApplySetting(c).SetProject(Parameters.MSBuildSolution));
  222. });
  223. Target CreateNugetPackages => _ => _
  224. .DependsOn(CreateIntermediateNugetPackages)
  225. .Executes(() =>
  226. {
  227. BuildTasksPatcher.PatchBuildTasksInPackage(Parameters.NugetIntermediateRoot / "Avalonia.Build.Tasks." +
  228. Parameters.Version + ".nupkg");
  229. var config = Numerge.MergeConfiguration.LoadFile(RootDirectory / "nukebuild" / "numerge.config");
  230. EnsureCleanDirectory(Parameters.NugetRoot);
  231. if(!Numerge.NugetPackageMerger.Merge(Parameters.NugetIntermediateRoot, Parameters.NugetRoot, config,
  232. new NumergeNukeLogger()))
  233. throw new Exception("Package merge failed");
  234. });
  235. Target RunTests => _ => _
  236. .DependsOn(RunCoreLibsTests)
  237. .DependsOn(RunRenderTests)
  238. .DependsOn(RunDesignerTests)
  239. .DependsOn(RunHtmlPreviewerTests)
  240. .DependsOn(RunLeakTests);
  241. Target Package => _ => _
  242. .DependsOn(RunTests)
  243. .DependsOn(CreateNugetPackages);
  244. Target CiAzureLinux => _ => _
  245. .DependsOn(RunTests);
  246. Target CiAzureOSX => _ => _
  247. .DependsOn(Package)
  248. .DependsOn(ZipFiles);
  249. Target CiAzureWindows => _ => _
  250. .DependsOn(Package)
  251. .DependsOn(ZipFiles);
  252. public static int Main() =>
  253. RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
  254. ? Execute<Build>(x => x.Package)
  255. : Execute<Build>(x => x.RunTests);
  256. }
  257. public static class ToolSettingsExtensions
  258. {
  259. public static T Apply<T>(this T settings, Configure<T> configurator)
  260. {
  261. return configurator != null ? configurator(settings) : settings;
  262. }
  263. }