Build.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. using MicroCom.CodeGenerator;
  27. /*
  28. Before editing this file, install support plugin for your IDE,
  29. running and debugging a particular target (optionally without deps) would be way easier
  30. ReSharper/Rider - https://plugins.jetbrains.com/plugin/10803-nuke-support
  31. VSCode - https://marketplace.visualstudio.com/items?itemName=nuke.support
  32. */
  33. partial class Build : NukeBuild
  34. {
  35. [Solution("Avalonia.sln")] readonly Solution Solution;
  36. BuildParameters Parameters { get; set; }
  37. protected override void OnBuildInitialized()
  38. {
  39. Parameters = new BuildParameters(this);
  40. Information("Building version {0} of Avalonia ({1}) using version {2} of Nuke.",
  41. Parameters.Version,
  42. Parameters.Configuration,
  43. typeof(NukeBuild).Assembly.GetName().Version.ToString());
  44. if (Parameters.IsLocalBuild)
  45. {
  46. Information("Repository Name: " + Parameters.RepositoryName);
  47. Information("Repository Branch: " + Parameters.RepositoryBranch);
  48. }
  49. Information("Configuration: " + Parameters.Configuration);
  50. Information("IsLocalBuild: " + Parameters.IsLocalBuild);
  51. Information("IsRunningOnUnix: " + Parameters.IsRunningOnUnix);
  52. Information("IsRunningOnWindows: " + Parameters.IsRunningOnWindows);
  53. Information("IsRunningOnAzure:" + Parameters.IsRunningOnAzure);
  54. Information("IsPullRequest: " + Parameters.IsPullRequest);
  55. Information("IsMainRepo: " + Parameters.IsMainRepo);
  56. Information("IsMasterBranch: " + Parameters.IsMasterBranch);
  57. Information("IsReleaseBranch: " + Parameters.IsReleaseBranch);
  58. Information("IsReleasable: " + Parameters.IsReleasable);
  59. Information("IsMyGetRelease: " + Parameters.IsMyGetRelease);
  60. Information("IsNuGetRelease: " + Parameters.IsNuGetRelease);
  61. void ExecWait(string preamble, string command, string args)
  62. {
  63. Console.WriteLine(preamble);
  64. Process.Start(new ProcessStartInfo(command, args) {UseShellExecute = false}).WaitForExit();
  65. }
  66. ExecWait("dotnet version:", "dotnet", "--info");
  67. ExecWait("dotnet workloads:", "dotnet", "workload list");
  68. Information("Processor count: " + Environment.ProcessorCount);
  69. Information("Available RAM: " + GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / 0x100000 + "MB");
  70. }
  71. DotNetConfigHelper ApplySettingCore(DotNetConfigHelper c)
  72. {
  73. if (Parameters.IsRunningOnAzure)
  74. c.AddProperty("JavaSdkDirectory", GetVariable<string>("JAVA_HOME_11_X64"));
  75. c.AddProperty("PackageVersion", Parameters.Version)
  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. // Nuke and MSBuild tools have build-in helpers to get target frameworks from the project.
  133. // Unfortunately, it gets broken with every second SDK update, so we had to do it manually.
  134. var fileXml = XDocument.Parse(File.ReadAllText(project.Path));
  135. var targetFrameworks = fileXml.Descendants("TargetFrameworks")
  136. .FirstOrDefault()?.Value.Split(';').Select(f => f.Trim());
  137. if (targetFrameworks is null)
  138. {
  139. var targetFramework = fileXml.Descendants("TargetFramework").FirstOrDefault()?.Value;
  140. if (targetFramework is not null)
  141. {
  142. targetFrameworks = new[] { targetFramework };
  143. }
  144. }
  145. if (targetFrameworks is null)
  146. {
  147. throw new InvalidOperationException("No target frameworks were found in the test project");
  148. }
  149. foreach (var fw in targetFrameworks)
  150. {
  151. if (fw.StartsWith("net4")
  152. && (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  153. && Environment.GetEnvironmentVariable("FORCE_LINUX_TESTS") != "1")
  154. {
  155. Information($"Skipping {projectName} ({fw}) tests on *nix - https://github.com/mono/mono/issues/13969");
  156. continue;
  157. }
  158. Information($"Running for {projectName} ({fw}) ...");
  159. DotNetTest(c => ApplySetting(c)
  160. .SetProjectFile(project)
  161. .SetFramework(fw)
  162. .EnableNoBuild()
  163. .EnableNoRestore()
  164. .When(Parameters.PublishTestResults, _ => _
  165. .SetLoggers("trx")
  166. .SetResultsDirectory(Parameters.TestResultsRoot)));
  167. }
  168. }
  169. Target RunHtmlPreviewerTests => _ => _
  170. .DependsOn(CompileHtmlPreviewer)
  171. .OnlyWhenStatic(() => !(Parameters.SkipPreviewer || Parameters.SkipTests))
  172. .Executes(() =>
  173. {
  174. var webappTestDir = RootDirectory / "tests" / "Avalonia.DesignerSupport.Tests" / "Remote" / "HtmlTransport" / "webapp";
  175. NpmTasks.NpmInstall(c => c
  176. .SetProcessWorkingDirectory(webappTestDir)
  177. .SetProcessArgumentConfigurator(a => a.Add("--silent")));
  178. NpmTasks.NpmRun(c => c
  179. .SetProcessWorkingDirectory(webappTestDir)
  180. .SetCommand("test"));
  181. });
  182. Target RunCoreLibsTests => _ => _
  183. .OnlyWhenStatic(() => !Parameters.SkipTests)
  184. .DependsOn(Compile)
  185. .Executes(() =>
  186. {
  187. RunCoreTest("Avalonia.Base.UnitTests");
  188. RunCoreTest("Avalonia.Controls.UnitTests");
  189. RunCoreTest("Avalonia.Controls.DataGrid.UnitTests");
  190. RunCoreTest("Avalonia.Markup.UnitTests");
  191. RunCoreTest("Avalonia.Markup.Xaml.UnitTests");
  192. RunCoreTest("Avalonia.Skia.UnitTests");
  193. RunCoreTest("Avalonia.ReactiveUI.UnitTests");
  194. RunCoreTest("Avalonia.Headless.UnitTests");
  195. });
  196. Target RunRenderTests => _ => _
  197. .OnlyWhenStatic(() => !Parameters.SkipTests)
  198. .DependsOn(Compile)
  199. .Executes(() =>
  200. {
  201. RunCoreTest("Avalonia.Skia.RenderTests");
  202. if (Parameters.IsRunningOnWindows)
  203. RunCoreTest("Avalonia.Direct2D1.RenderTests");
  204. });
  205. Target RunToolsTests => _ => _
  206. .OnlyWhenStatic(() => !Parameters.SkipTests)
  207. .DependsOn(Compile)
  208. .Executes(() =>
  209. {
  210. RunCoreTest("Avalonia.Generators.Tests");
  211. if (Parameters.IsRunningOnWindows)
  212. RunCoreTest("Avalonia.DesignerSupport.Tests");
  213. });
  214. Target RunLeakTests => _ => _
  215. .OnlyWhenStatic(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  216. .DependsOn(Compile)
  217. .Executes(() =>
  218. {
  219. void DoMemoryTest()
  220. {
  221. RunCoreTest("Avalonia.LeakTests");
  222. }
  223. ControlFlow.ExecuteWithRetry(DoMemoryTest, delay: TimeSpan.FromMilliseconds(3));
  224. });
  225. Target ZipFiles => _ => _
  226. .After(CreateNugetPackages, Compile, RunCoreLibsTests, Package)
  227. .Executes(() =>
  228. {
  229. var data = Parameters;
  230. Zip(data.ZipNuGetArtifacts, data.NugetRoot);
  231. });
  232. Target CreateIntermediateNugetPackages => _ => _
  233. .DependsOn(Compile)
  234. .After(RunTests)
  235. .Executes(() =>
  236. {
  237. DotNetPack(c => ApplySetting(c).SetProject(Parameters.MSBuildSolution));
  238. });
  239. Target CreateNugetPackages => _ => _
  240. .DependsOn(CreateIntermediateNugetPackages)
  241. .Executes(() =>
  242. {
  243. BuildTasksPatcher.PatchBuildTasksInPackage(Parameters.NugetIntermediateRoot / "Avalonia.Build.Tasks." +
  244. Parameters.Version + ".nupkg");
  245. var config = Numerge.MergeConfiguration.LoadFile(RootDirectory / "nukebuild" / "numerge.config");
  246. EnsureCleanDirectory(Parameters.NugetRoot);
  247. if(!Numerge.NugetPackageMerger.Merge(Parameters.NugetIntermediateRoot, Parameters.NugetRoot, config,
  248. new NumergeNukeLogger()))
  249. throw new Exception("Package merge failed");
  250. RefAssemblyGenerator.GenerateRefAsmsInPackage(Parameters.NugetRoot / "Avalonia." +
  251. Parameters.Version + ".nupkg");
  252. });
  253. Target RunTests => _ => _
  254. .DependsOn(RunCoreLibsTests)
  255. .DependsOn(RunRenderTests)
  256. .DependsOn(RunToolsTests)
  257. .DependsOn(RunHtmlPreviewerTests)
  258. .DependsOn(RunLeakTests);
  259. Target Package => _ => _
  260. .DependsOn(RunTests)
  261. .DependsOn(CreateNugetPackages);
  262. Target CiAzureLinux => _ => _
  263. .DependsOn(RunTests);
  264. Target CiAzureOSX => _ => _
  265. .DependsOn(Package)
  266. .DependsOn(ZipFiles);
  267. Target CiAzureWindows => _ => _
  268. .DependsOn(Package)
  269. .DependsOn(ZipFiles);
  270. Target GenerateCppHeaders => _ => _.Executes(() =>
  271. {
  272. var file = MicroComCodeGenerator.Parse(
  273. File.ReadAllText(RootDirectory / "src" / "Avalonia.Native" / "avn.idl"));
  274. File.WriteAllText(RootDirectory / "native" / "Avalonia.Native" / "inc" / "avalonia-native.h",
  275. file.GenerateCppHeader());
  276. });
  277. public static int Main() =>
  278. RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
  279. ? Execute<Build>(x => x.Package)
  280. : Execute<Build>(x => x.RunTests);
  281. }
  282. public static class ToolSettingsExtensions
  283. {
  284. public static T Apply<T>(this T settings, Configure<T> configurator)
  285. {
  286. return configurator != null ? configurator(settings) : settings;
  287. }
  288. }