BuildParameters.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text.RegularExpressions;
  7. using System.Xml.Linq;
  8. using Nuke.Common;
  9. using Nuke.Common.CI.AzurePipelines;
  10. using Nuke.Common.IO;
  11. public partial class Build
  12. {
  13. [Parameter(Name = "configuration")]
  14. public string? Configuration { get; set; }
  15. [Parameter(Name = "skip-tests")]
  16. public bool SkipTests { get; set; }
  17. [Parameter(Name = "force-nuget-version")]
  18. public string? ForceNugetVersion { get; set; }
  19. [Parameter(Name = "skip-previewer")]
  20. public bool SkipPreviewer { get; set; }
  21. [Parameter(Name = "force-api-baseline")]
  22. public string? ForceApiValidationBaseline { get; set; }
  23. [Parameter(Name = "update-api-suppression")]
  24. public bool? UpdateApiValidationSuppression { get; set; }
  25. [Parameter(Name = "version-output-dir")]
  26. public AbsolutePath? VersionOutputDir { get; set; }
  27. public class BuildParameters
  28. {
  29. public string Configuration { get; }
  30. public bool SkipTests { get; }
  31. public bool SkipPreviewer {get;}
  32. public string MainRepo { get; }
  33. public string MasterBranch { get; }
  34. public string? RepositoryName { get; }
  35. public string? RepositoryBranch { get; }
  36. public string ReleaseConfiguration { get; }
  37. public Regex ReleaseBranchRegex { get; }
  38. public string MSBuildSolution { get; }
  39. public bool IsLocalBuild { get; }
  40. public bool IsRunningOnUnix { get; }
  41. public bool IsRunningOnWindows { get; }
  42. public bool IsRunningOnAzure { get; }
  43. public bool IsPullRequest { get; }
  44. public bool IsMainRepo { get; }
  45. public bool IsMasterBranch { get; }
  46. public bool IsReleaseBranch { get; }
  47. public bool IsReleasable { get; }
  48. public bool IsMyGetRelease { get; }
  49. public bool IsNuGetRelease { get; }
  50. public bool PublishTestResults { get; }
  51. public string Version { get; set; }
  52. public const string LocalBuildVersion = "9999.0.0-localbuild";
  53. public bool IsPackingToLocalCache { get; private set; }
  54. public AbsolutePath ArtifactsDir { get; }
  55. public AbsolutePath NugetIntermediateRoot { get; }
  56. public AbsolutePath NugetRoot { get; }
  57. public AbsolutePath ZipRoot { get; }
  58. public AbsolutePath TestResultsRoot { get; }
  59. public string DirSuffix { get; }
  60. public List<AbsolutePath> BuildDirs { get; }
  61. public string FileZipSuffix { get; }
  62. public AbsolutePath ZipCoreArtifacts { get; }
  63. public AbsolutePath ZipNuGetArtifacts { get; }
  64. public string? ForceApiValidationBaseline { get; }
  65. public bool UpdateApiValidationSuppression { get; }
  66. public AbsolutePath ApiValidationSuppressionFiles { get; }
  67. public AbsolutePath? VersionOutputDir { get; }
  68. public BuildParameters(Build b, bool isPackingToLocalCache)
  69. {
  70. // ARGUMENTS
  71. Configuration = b.Configuration ?? "Release";
  72. SkipTests = b.SkipTests;
  73. SkipPreviewer = b.SkipPreviewer;
  74. // CONFIGURATION
  75. MainRepo = "https://github.com/AvaloniaUI/Avalonia";
  76. MasterBranch = "refs/heads/master";
  77. ReleaseBranchRegex = new("^refs/heads/release/(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$");
  78. ReleaseConfiguration = "Release";
  79. MSBuildSolution = RootDirectory / "dirs.proj";
  80. // PARAMETERS
  81. IsLocalBuild = NukeBuild.IsLocalBuild;
  82. IsRunningOnUnix = Environment.OSVersion.Platform == PlatformID.Unix ||
  83. Environment.OSVersion.Platform == PlatformID.MacOSX;
  84. IsRunningOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  85. IsRunningOnAzure = Host is AzurePipelines;
  86. if (IsRunningOnAzure)
  87. {
  88. RepositoryName = AzurePipelines.Instance.RepositoryUri;
  89. RepositoryBranch = AzurePipelines.Instance.SourceBranch;
  90. IsPullRequest = AzurePipelines.Instance.PullRequestId.HasValue;
  91. IsMainRepo = StringComparer.OrdinalIgnoreCase.Equals(MainRepo, AzurePipelines.Instance.RepositoryUri);
  92. }
  93. IsMainRepo =
  94. StringComparer.OrdinalIgnoreCase.Equals(MainRepo,
  95. RepositoryName);
  96. IsMasterBranch = StringComparer.OrdinalIgnoreCase.Equals(MasterBranch,
  97. RepositoryBranch);
  98. IsReleaseBranch = RepositoryBranch is not null && ReleaseBranchRegex.IsMatch(RepositoryBranch);
  99. IsReleasable = StringComparer.OrdinalIgnoreCase.Equals(ReleaseConfiguration, Configuration);
  100. IsMyGetRelease = IsReleasable;
  101. IsNuGetRelease = IsMainRepo && IsReleasable && IsReleaseBranch;
  102. // VERSION
  103. Version = b.ForceNugetVersion ?? GetVersion();
  104. ForceApiValidationBaseline = b.ForceApiValidationBaseline;
  105. UpdateApiValidationSuppression = b.UpdateApiValidationSuppression ?? IsLocalBuild;
  106. if (IsRunningOnAzure)
  107. {
  108. if (!IsNuGetRelease)
  109. {
  110. // Use AssemblyVersion with Build as version
  111. var buildId = Environment.GetEnvironmentVariable("BUILD_BUILDID") ??
  112. throw new InvalidOperationException("Missing environment variable BUILD_BUILDID");
  113. Version += "-cibuild" + int.Parse(buildId).ToString("0000000") + "-alpha";
  114. }
  115. PublishTestResults = true;
  116. }
  117. if (isPackingToLocalCache)
  118. {
  119. IsPackingToLocalCache = true;
  120. Version = LocalBuildVersion;
  121. }
  122. // DIRECTORIES
  123. ArtifactsDir = RootDirectory / "artifacts";
  124. NugetRoot = ArtifactsDir / "nuget";
  125. NugetIntermediateRoot = RootDirectory / "build-intermediate" / "nuget";
  126. ZipRoot = ArtifactsDir / "zip";
  127. TestResultsRoot = ArtifactsDir / "test-results";
  128. BuildDirs = RootDirectory.GlobDirectories("**/bin")
  129. .Concat(RootDirectory.GlobDirectories("**/obj"))
  130. .Where(dir => !((string)dir).Contains("nukebuild"))
  131. .Concat(RootDirectory.GlobDirectories("**/node_modules"))
  132. .ToList();
  133. DirSuffix = Configuration;
  134. FileZipSuffix = Version + ".zip";
  135. ZipCoreArtifacts = ZipRoot / ("Avalonia-" + FileZipSuffix);
  136. ZipNuGetArtifacts = ZipRoot / ("Avalonia-NuGet-" + FileZipSuffix);
  137. ApiValidationSuppressionFiles = RootDirectory / "api";
  138. VersionOutputDir = b.VersionOutputDir;
  139. }
  140. string GetVersion()
  141. {
  142. var xdoc = XDocument.Load(RootDirectory / "build/SharedVersion.props");
  143. return xdoc.Descendants().First(x => x.Name.LocalName == "Version").Value;
  144. }
  145. }
  146. }