Build.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Xml.Linq;
  9. using Nuke.Common;
  10. using Nuke.Common.Git;
  11. using Nuke.Common.ProjectModel;
  12. using Nuke.Common.Tooling;
  13. using Nuke.Common.Tools.DotNet;
  14. using Nuke.Common.Tools.MSBuild;
  15. using Nuke.Common.Utilities;
  16. using static Nuke.Common.EnvironmentInfo;
  17. using static Nuke.Common.IO.FileSystemTasks;
  18. using static Nuke.Common.IO.PathConstruction;
  19. using static Nuke.Common.Tools.MSBuild.MSBuildTasks;
  20. using static Nuke.Common.Tools.DotNet.DotNetTasks;
  21. using static Nuke.Common.Tools.Xunit.XunitTasks;
  22. /*
  23. Before editing this file, install support plugin for your IDE,
  24. running and debugging a particular target (optionally without deps) would be way easier
  25. ReSharper/Rider - https://plugins.jetbrains.com/plugin/10803-nuke-support
  26. VSCode - https://marketplace.visualstudio.com/items?itemName=nuke.support
  27. */
  28. partial class Build : NukeBuild
  29. {
  30. BuildParameters Parameters { get; set; }
  31. protected override void OnBuildInitialized()
  32. {
  33. Parameters = new BuildParameters(this);
  34. Information("Building version {0} of Avalonia ({1}) using version {2} of Nuke.",
  35. Parameters.Version,
  36. Parameters.Configuration,
  37. typeof(NukeBuild).Assembly.GetName().Version.ToString());
  38. if (Parameters.IsLocalBuild)
  39. {
  40. Information("Repository Name: " + Parameters.RepositoryName);
  41. Information("Repository Branch: " + Parameters.RepositoryBranch);
  42. }
  43. Information("Configuration: " + Parameters.Configuration);
  44. Information("IsLocalBuild: " + Parameters.IsLocalBuild);
  45. Information("IsRunningOnUnix: " + Parameters.IsRunningOnUnix);
  46. Information("IsRunningOnWindows: " + Parameters.IsRunningOnWindows);
  47. Information("IsRunningOnAzure:" + Parameters.IsRunningOnAzure);
  48. Information("IsPullRequest: " + Parameters.IsPullRequest);
  49. Information("IsMainRepo: " + Parameters.IsMainRepo);
  50. Information("IsMasterBranch: " + Parameters.IsMasterBranch);
  51. Information("IsReleaseBranch: " + Parameters.IsReleaseBranch);
  52. Information("IsReleasable: " + Parameters.IsReleasable);
  53. Information("IsMyGetRelease: " + Parameters.IsMyGetRelease);
  54. Information("IsNuGetRelease: " + Parameters.IsNuGetRelease);
  55. void ExecWait(string preamble, string command, string args)
  56. {
  57. Console.WriteLine(preamble);
  58. Process.Start(new ProcessStartInfo(command, args) {UseShellExecute = false}).WaitForExit();
  59. }
  60. ExecWait("dotnet version:", "dotnet", "--version");
  61. if (Parameters.IsRunningOnUnix)
  62. ExecWait("Mono version:", "mono", "--version");
  63. }
  64. Target Clean => _ => _.Executes(() =>
  65. {
  66. DeleteDirectories(Parameters.BuildDirs);
  67. EnsureCleanDirectories(Parameters.BuildDirs);
  68. EnsureCleanDirectory(Parameters.ArtifactsDir);
  69. EnsureCleanDirectory(Parameters.NugetIntermediateRoot);
  70. EnsureCleanDirectory(Parameters.NugetRoot);
  71. EnsureCleanDirectory(Parameters.ZipRoot);
  72. EnsureCleanDirectory(Parameters.TestResultsRoot);
  73. });
  74. Target Compile => _ => _
  75. .DependsOn(Clean)
  76. .Executes(() =>
  77. {
  78. if (Parameters.IsRunningOnWindows)
  79. MSBuild(Parameters.MSBuildSolution, c => c
  80. .SetArgumentConfigurator(a => a.Add("/r"))
  81. .SetConfiguration(Parameters.Configuration)
  82. .SetVerbosity(MSBuildVerbosity.Minimal)
  83. .AddProperty("PackageVersion", Parameters.Version)
  84. .AddProperty("iOSRoslynPathHackRequired", "true")
  85. .SetToolsVersion(MSBuildToolsVersion._15_0)
  86. .AddTargets("Build")
  87. );
  88. else
  89. DotNetBuild(Parameters.MSBuildSolution, c => c
  90. .AddProperty("PackageVersion", Parameters.Version)
  91. .SetConfiguration(Parameters.Configuration)
  92. );
  93. });
  94. void RunCoreTest(string project)
  95. {
  96. if(!project.EndsWith(".csproj"))
  97. project = System.IO.Path.Combine(project, System.IO.Path.GetFileName(project)+".csproj");
  98. Information("Running tests from " + project);
  99. XDocument xdoc;
  100. using (var s = File.OpenRead(project))
  101. xdoc = XDocument.Load(s);
  102. List<string> frameworks = null;
  103. var targets = xdoc.Root.Descendants("TargetFrameworks").FirstOrDefault();
  104. if (targets != null)
  105. frameworks = targets.Value.Split(';').Where(f => !string.IsNullOrWhiteSpace(f)).ToList();
  106. else
  107. frameworks = new List<string> {xdoc.Root.Descendants("TargetFramework").First().Value};
  108. foreach(var fw in frameworks)
  109. {
  110. if (fw.StartsWith("net4")
  111. && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
  112. && Environment.GetEnvironmentVariable("FORCE_LINUX_TESTS") != "1")
  113. {
  114. Information($"Skipping {fw} tests on Linux - https://github.com/mono/mono/issues/13969");
  115. continue;
  116. }
  117. Information("Running for " + fw);
  118. DotNetTest(c =>
  119. {
  120. c = c
  121. .SetProjectFile(project)
  122. .SetConfiguration(Parameters.Configuration)
  123. .SetFramework(fw)
  124. .EnableNoBuild()
  125. .EnableNoRestore();
  126. // NOTE: I can see that we could maybe add another extension method "Switch" or "If" to make this more convenient
  127. if (Parameters.PublishTestResults)
  128. c = c.SetLogger("trx").SetResultsDirectory(Parameters.TestResultsRoot);
  129. return c;
  130. });
  131. }
  132. }
  133. Target RunCoreLibsTests => _ => _
  134. .OnlyWhen(() => !Parameters.SkipTests)
  135. .DependsOn(Compile)
  136. .Executes(() =>
  137. {
  138. RunCoreTest("./tests/Avalonia.Animation.UnitTests");
  139. RunCoreTest("./tests/Avalonia.Base.UnitTests");
  140. RunCoreTest("./tests/Avalonia.Controls.UnitTests");
  141. RunCoreTest("./tests/Avalonia.Input.UnitTests");
  142. RunCoreTest("./tests/Avalonia.Interactivity.UnitTests");
  143. RunCoreTest("./tests/Avalonia.Layout.UnitTests");
  144. RunCoreTest("./tests/Avalonia.Markup.UnitTests");
  145. RunCoreTest("./tests/Avalonia.Markup.Xaml.UnitTests");
  146. RunCoreTest("./tests/Avalonia.Styling.UnitTests");
  147. RunCoreTest("./tests/Avalonia.Visuals.UnitTests");
  148. RunCoreTest("./tests/Avalonia.Skia.UnitTests");
  149. RunCoreTest("./tests/Avalonia.ReactiveUI.UnitTests");
  150. });
  151. Target RunRenderTests => _ => _
  152. .OnlyWhen(() => !Parameters.SkipTests)
  153. .DependsOn(Compile)
  154. .Executes(() =>
  155. {
  156. RunCoreTest("./tests/Avalonia.Skia.RenderTests/Avalonia.Skia.RenderTests.csproj");
  157. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  158. RunCoreTest("./tests/Avalonia.Direct2D1.RenderTests/Avalonia.Direct2D1.RenderTests.csproj");
  159. });
  160. Target RunDesignerTests => _ => _
  161. .OnlyWhen(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  162. .DependsOn(Compile)
  163. .Executes(() =>
  164. {
  165. RunCoreTest("./tests/Avalonia.DesignerSupport.Tests");
  166. });
  167. [PackageExecutable("JetBrains.dotMemoryUnit", "dotMemoryUnit.exe")] readonly Tool DotMemoryUnit;
  168. Target RunLeakTests => _ => _
  169. .OnlyWhen(() => !Parameters.SkipTests && Parameters.IsRunningOnWindows)
  170. .DependsOn(Compile)
  171. .Executes(() =>
  172. {
  173. var testAssembly = "tests\\Avalonia.LeakTests\\bin\\Release\\net461\\Avalonia.LeakTests.dll";
  174. DotMemoryUnit(
  175. $"{XunitPath.DoubleQuoteIfNeeded()} --propagate-exit-code -- {testAssembly}",
  176. timeout: 120_000);
  177. });
  178. Target ZipFiles => _ => _
  179. .After(CreateNugetPackages, Compile, RunCoreLibsTests, Package)
  180. .Executes(() =>
  181. {
  182. var data = Parameters;
  183. Zip(data.ZipCoreArtifacts, data.BinRoot);
  184. Zip(data.ZipNuGetArtifacts, data.NugetRoot);
  185. Zip(data.ZipTargetControlCatalogDesktopDir,
  186. GlobFiles(data.ZipSourceControlCatalogDesktopDir, "*.dll").Concat(
  187. GlobFiles(data.ZipSourceControlCatalogDesktopDir, "*.config")).Concat(
  188. GlobFiles(data.ZipSourceControlCatalogDesktopDir, "*.so")).Concat(
  189. GlobFiles(data.ZipSourceControlCatalogDesktopDir, "*.dylib")).Concat(
  190. GlobFiles(data.ZipSourceControlCatalogDesktopDir, "*.exe")));
  191. });
  192. Target CreateIntermediateNugetPackages => _ => _
  193. .DependsOn(Compile)
  194. .After(RunTests)
  195. .Executes(() =>
  196. {
  197. if (Parameters.IsRunningOnWindows)
  198. MSBuild(Parameters.MSBuildSolution, c => c
  199. .SetConfiguration(Parameters.Configuration)
  200. .SetVerbosity(MSBuildVerbosity.Minimal)
  201. .AddProperty("PackageVersion", Parameters.Version)
  202. .AddProperty("iOSRoslynPathHackRequired", "true")
  203. .SetToolsVersion(MSBuildToolsVersion._15_0)
  204. .AddTargets("Pack"));
  205. else
  206. DotNetPack(Parameters.MSBuildSolution, c =>
  207. c.SetConfiguration(Parameters.Configuration)
  208. .AddProperty("PackageVersion", Parameters.Version));
  209. });
  210. Target CreateNugetPackages => _ => _
  211. .DependsOn(CreateIntermediateNugetPackages)
  212. .Executes(() =>
  213. {
  214. var config = Numerge.MergeConfiguration.LoadFile(RootDirectory / "nukebuild" / "numerge.config");
  215. EnsureCleanDirectory(Parameters.NugetRoot);
  216. if(!Numerge.NugetPackageMerger.Merge(Parameters.NugetIntermediateRoot, Parameters.NugetRoot, config,
  217. new NumergeNukeLogger()))
  218. throw new Exception("Package merge failed");
  219. });
  220. Target RunTests => _ => _
  221. .DependsOn(RunCoreLibsTests)
  222. .DependsOn(RunRenderTests)
  223. .DependsOn(RunDesignerTests)
  224. .DependsOn(RunLeakTests);
  225. Target Package => _ => _
  226. .DependsOn(RunTests)
  227. .DependsOn(CreateNugetPackages);
  228. Target CiAzureLinux => _ => _
  229. .DependsOn(RunTests);
  230. Target CiAzureOSX => _ => _
  231. .DependsOn(Package)
  232. .DependsOn(ZipFiles);
  233. Target CiAzureWindows => _ => _
  234. .DependsOn(Package)
  235. .DependsOn(ZipFiles);
  236. public static int Main() =>
  237. RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
  238. ? Execute<Build>(x => x.Package)
  239. : Execute<Build>(x => x.RunTests);
  240. }