Build.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. });
  195. Target RunRenderTests => _ => _
  196. .OnlyWhenStatic(() => !Parameters.SkipTests)
  197. .DependsOn(Compile)
  198. .Executes(() =>
  199. {
  200. RunCoreTest("Avalonia.Skia.RenderTests");
  201. if (Parameters.IsRunningOnWindows)
  202. RunCoreTest("Avalonia.Direct2D1.RenderTests");
  203. });
  204. Target RunToolsTests => _ => _
  205. .OnlyWhenStatic(() => !Parameters.SkipTests)
  206. .DependsOn(Compile)
  207. .Executes(() =>
  208. {
  209. RunCoreTest("Avalonia.Generators.Tests");
  210. if (Parameters.IsRunningOnWindows)
  211. RunCoreTest("Avalonia.DesignerSupport.Tests");
  212. });
  213. Target RunLeakTests => _ => _
  214. .OnlyWhenStatic(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  215. .DependsOn(Compile)
  216. .Executes(() =>
  217. {
  218. void DoMemoryTest()
  219. {
  220. RunCoreTest("Avalonia.LeakTests");
  221. }
  222. ControlFlow.ExecuteWithRetry(DoMemoryTest, delay: TimeSpan.FromMilliseconds(3));
  223. });
  224. Target ZipFiles => _ => _
  225. .After(CreateNugetPackages, Compile, RunCoreLibsTests, Package)
  226. .Executes(() =>
  227. {
  228. var data = Parameters;
  229. Zip(data.ZipNuGetArtifacts, data.NugetRoot);
  230. });
  231. Target CreateIntermediateNugetPackages => _ => _
  232. .DependsOn(Compile)
  233. .After(RunTests)
  234. .Executes(() =>
  235. {
  236. DotNetPack(c => ApplySetting(c).SetProject(Parameters.MSBuildSolution));
  237. });
  238. Target CreateNugetPackages => _ => _
  239. .DependsOn(CreateIntermediateNugetPackages)
  240. .Executes(() =>
  241. {
  242. BuildTasksPatcher.PatchBuildTasksInPackage(Parameters.NugetIntermediateRoot / "Avalonia.Build.Tasks." +
  243. Parameters.Version + ".nupkg");
  244. var config = Numerge.MergeConfiguration.LoadFile(RootDirectory / "nukebuild" / "numerge.config");
  245. EnsureCleanDirectory(Parameters.NugetRoot);
  246. if(!Numerge.NugetPackageMerger.Merge(Parameters.NugetIntermediateRoot, Parameters.NugetRoot, config,
  247. new NumergeNukeLogger()))
  248. throw new Exception("Package merge failed");
  249. });
  250. Target RunTests => _ => _
  251. .DependsOn(RunCoreLibsTests)
  252. .DependsOn(RunRenderTests)
  253. .DependsOn(RunToolsTests)
  254. .DependsOn(RunHtmlPreviewerTests)
  255. .DependsOn(RunLeakTests);
  256. Target Package => _ => _
  257. .DependsOn(RunTests)
  258. .DependsOn(CreateNugetPackages);
  259. Target CiAzureLinux => _ => _
  260. .DependsOn(RunTests);
  261. Target CiAzureOSX => _ => _
  262. .DependsOn(Package)
  263. .DependsOn(ZipFiles);
  264. Target CiAzureWindows => _ => _
  265. .DependsOn(Package)
  266. .DependsOn(ZipFiles);
  267. Target GenerateCppHeaders => _ => _.Executes(() =>
  268. {
  269. var file = MicroComCodeGenerator.Parse(
  270. File.ReadAllText(RootDirectory / "src" / "Avalonia.Native" / "avn.idl"));
  271. File.WriteAllText(RootDirectory / "native" / "Avalonia.Native" / "inc" / "avalonia-native.h",
  272. file.GenerateCppHeader());
  273. });
  274. public static int Main() =>
  275. RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
  276. ? Execute<Build>(x => x.Package)
  277. : Execute<Build>(x => x.RunTests);
  278. }
  279. public static class ToolSettingsExtensions
  280. {
  281. public static T Apply<T>(this T settings, Configure<T> configurator)
  282. {
  283. return configurator != null ? configurator(settings) : settings;
  284. }
  285. }