BuildParameters.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Xml.Linq;
  6. using Nuke.Common;
  7. using Nuke.Common.CI.AzurePipelines;
  8. using Nuke.Common.IO;
  9. using static Nuke.Common.IO.PathConstruction;
  10. public partial class Build
  11. {
  12. [Parameter("configuration")]
  13. public string Configuration { get; set; }
  14. [Parameter("skip-tests")]
  15. public bool SkipTests { get; set; }
  16. [Parameter("force-nuget-version")]
  17. public string ForceNugetVersion { get; set; }
  18. [Parameter("skip-previewer")]
  19. public bool SkipPreviewer { get; set; }
  20. public class BuildParameters
  21. {
  22. public string Configuration { get; }
  23. public bool SkipTests { get; }
  24. public bool SkipPreviewer {get;}
  25. public string MainRepo { get; }
  26. public string MasterBranch { get; }
  27. public string RepositoryName { get; }
  28. public string RepositoryBranch { get; }
  29. public string ReleaseConfiguration { get; }
  30. public string ReleaseBranchPrefix { get; }
  31. public string MSBuildSolution { get; }
  32. public bool IsLocalBuild { get; }
  33. public bool IsRunningOnUnix { get; }
  34. public bool IsRunningOnWindows { get; }
  35. public bool IsRunningOnAzure { get; }
  36. public bool IsPullRequest { get; }
  37. public bool IsMainRepo { get; }
  38. public bool IsMasterBranch { get; }
  39. public bool IsReleaseBranch { get; }
  40. public bool IsReleasable { get; }
  41. public bool IsMyGetRelease { get; }
  42. public bool IsNuGetRelease { get; }
  43. public bool PublishTestResults { get; }
  44. public string Version { get; }
  45. public AbsolutePath ArtifactsDir { get; }
  46. public AbsolutePath NugetIntermediateRoot { get; }
  47. public AbsolutePath NugetRoot { get; }
  48. public AbsolutePath ZipRoot { get; }
  49. public AbsolutePath BinRoot { get; }
  50. public AbsolutePath TestResultsRoot { get; }
  51. public string DirSuffix { get; }
  52. public List<string> BuildDirs { get; }
  53. public string FileZipSuffix { get; }
  54. public AbsolutePath ZipCoreArtifacts { get; }
  55. public AbsolutePath ZipNuGetArtifacts { get; }
  56. public AbsolutePath ZipTargetControlCatalogNetCoreDir { get; }
  57. public BuildParameters(Build b)
  58. {
  59. // ARGUMENTS
  60. Configuration = b.Configuration ?? "Release";
  61. SkipTests = b.SkipTests;
  62. SkipPreviewer = b.SkipPreviewer;
  63. // CONFIGURATION
  64. MainRepo = "https://github.com/AvaloniaUI/Avalonia";
  65. MasterBranch = "refs/heads/master";
  66. ReleaseBranchPrefix = "refs/heads/release/";
  67. ReleaseConfiguration = "Release";
  68. MSBuildSolution = RootDirectory / "dirs.proj";
  69. // PARAMETERS
  70. IsLocalBuild = Host == HostType.Console;
  71. IsRunningOnUnix = Environment.OSVersion.Platform == PlatformID.Unix ||
  72. Environment.OSVersion.Platform == PlatformID.MacOSX;
  73. IsRunningOnWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  74. IsRunningOnAzure = Host == HostType.AzurePipelines ||
  75. Environment.GetEnvironmentVariable("LOGNAME") == "vsts";
  76. if (IsRunningOnAzure)
  77. {
  78. RepositoryName = AzurePipelines.Instance.RepositoryUri;
  79. RepositoryBranch = AzurePipelines.Instance.SourceBranch;
  80. IsPullRequest = AzurePipelines.Instance.PullRequestId.HasValue;
  81. IsMainRepo = StringComparer.OrdinalIgnoreCase.Equals(MainRepo, AzurePipelines.Instance.RepositoryUri);
  82. }
  83. IsMainRepo =
  84. StringComparer.OrdinalIgnoreCase.Equals(MainRepo,
  85. RepositoryName);
  86. IsMasterBranch = StringComparer.OrdinalIgnoreCase.Equals(MasterBranch,
  87. RepositoryBranch);
  88. IsReleaseBranch = RepositoryBranch?.StartsWith(ReleaseBranchPrefix, StringComparison.OrdinalIgnoreCase) ==
  89. true;
  90. IsReleasable = StringComparer.OrdinalIgnoreCase.Equals(ReleaseConfiguration, Configuration);
  91. IsMyGetRelease = IsReleasable;
  92. IsNuGetRelease = IsMainRepo && IsReleasable && IsReleaseBranch;
  93. // VERSION
  94. Version = b.ForceNugetVersion ?? GetVersion();
  95. if (IsRunningOnAzure)
  96. {
  97. if (!IsNuGetRelease)
  98. {
  99. // Use AssemblyVersion with Build as version
  100. Version += "-cibuild" + int.Parse(Environment.GetEnvironmentVariable("BUILD_BUILDID")).ToString("0000000") + "-beta";
  101. }
  102. PublishTestResults = true;
  103. }
  104. // DIRECTORIES
  105. ArtifactsDir = RootDirectory / "artifacts";
  106. NugetRoot = ArtifactsDir / "nuget";
  107. NugetIntermediateRoot = RootDirectory / "build-intermediate" / "nuget";
  108. ZipRoot = ArtifactsDir / "zip";
  109. BinRoot = ArtifactsDir / "bin";
  110. TestResultsRoot = ArtifactsDir / "test-results";
  111. BuildDirs = GlobDirectories(RootDirectory, "**bin").Concat(GlobDirectories(RootDirectory, "**obj")).ToList();
  112. DirSuffix = Configuration;
  113. FileZipSuffix = Version + ".zip";
  114. ZipCoreArtifacts = ZipRoot / ("Avalonia-" + FileZipSuffix);
  115. ZipNuGetArtifacts = ZipRoot / ("Avalonia-NuGet-" + FileZipSuffix);
  116. ZipTargetControlCatalogNetCoreDir = ZipRoot / ("ControlCatalog.NetCore-" + FileZipSuffix);
  117. }
  118. string GetVersion()
  119. {
  120. var xdoc = XDocument.Load(RootDirectory / "build/SharedVersion.props");
  121. return xdoc.Descendants().First(x => x.Name.LocalName == "Version").Value;
  122. }
  123. }
  124. }