Build.cs 11 KB

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