You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 line
3.3 KiB

  1. using System.Xml;
  2. public class BuildParameters
  3. {
  4. public BuildParameters(ICakeContext context)
  5. {
  6. Context = context;
  7. }
  8. public ICakeContext Context { get; }
  9. public BuildVersion Version { get; private set; }
  10. public string Configuration { get; private set; }
  11. public bool IsTagged { get; private set; }
  12. public bool IsCI { get; private set; }
  13. public DirectoryPathCollection Projects { get; set; }
  14. public DirectoryPathCollection TestProjects { get; set; }
  15. public FilePathCollection ProjectFiles { get; set; }
  16. public FilePathCollection TestProjectFiles { get; set; }
  17. public static BuildParameters Create(ICakeContext context)
  18. {
  19. var buildParameters = new BuildParameters(context);
  20. buildParameters.Initialize();
  21. return buildParameters;
  22. }
  23. public string FullVersion()
  24. {
  25. return Version.VersionWithSuffix();
  26. }
  27. private void Initialize()
  28. {
  29. InitializeCore();
  30. InitializeVersion();
  31. }
  32. private void InitializeCore()
  33. {
  34. Projects = Context.GetDirectories("./src/*");
  35. TestProjects = Context.GetDirectories("./test/*");
  36. ProjectFiles = Context.GetFiles("./src/*/*.csproj");
  37. TestProjectFiles = Context.GetFiles("./test/*/*.csproj");
  38. var buildSystem = Context.BuildSystem();
  39. if (!buildSystem.IsLocalBuild)
  40. {
  41. IsCI = true;
  42. if ((buildSystem.IsRunningOnAppVeyor && buildSystem.AppVeyor.Environment.Repository.Tag.IsTag) ||
  43. (buildSystem.IsRunningOnTravisCI && string.IsNullOrWhiteSpace(buildSystem.TravisCI.Environment.Build.Tag)))
  44. {
  45. IsTagged = true;
  46. }
  47. }
  48. Configuration = Context.Argument("Configuration", "Debug");
  49. if (IsCI)
  50. {
  51. Configuration = "Release";
  52. }
  53. }
  54. private void InitializeVersion()
  55. {
  56. var versionFile = Context.File("./build/version.props");
  57. var content = System.IO.File.ReadAllText(versionFile.Path.FullPath);
  58. XmlDocument doc = new XmlDocument();
  59. doc.LoadXml(content);
  60. var versionMajor = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionMajor").InnerText;
  61. var versionMinor = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionMinor").InnerText;
  62. var versionPatch = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionPatch").InnerText;
  63. var versionQuality = doc.DocumentElement.SelectSingleNode("/Project/PropertyGroup/VersionQuality").InnerText;
  64. versionQuality = string.IsNullOrWhiteSpace(versionQuality) ? null : versionQuality;
  65. var suffix = versionQuality;
  66. if (!IsTagged)
  67. {
  68. suffix += (IsCI ? "preview-" : "dv-") + Util.CreateStamp();
  69. }
  70. suffix = string.IsNullOrWhiteSpace(suffix) ? null : suffix;
  71. Version =
  72. new BuildVersion(int.Parse(versionMajor), int.Parse(versionMinor), int.Parse(versionPatch), versionQuality);
  73. Version.Suffix = suffix;
  74. }
  75. }
  76. public class BuildVersion
  77. {
  78. public BuildVersion(int major, int minor, int patch, string quality)
  79. {
  80. Major = major;
  81. Minor = minor;
  82. Patch = patch;
  83. Quality = quality;
  84. }
  85. public int Major { get; set; }
  86. public int Minor { get; set; }
  87. public int Patch { get; set; }
  88. public string Quality { get; set; }
  89. public string Suffix { get; set; }
  90. public string VersionWithoutQuality()
  91. {
  92. return $"{Major}.{Minor}.{Patch}";
  93. }
  94. public string Version()
  95. {
  96. return VersionWithoutQuality() + (Quality == null ? string.Empty : $"-{Quality}");
  97. }
  98. public string VersionWithSuffix()
  99. {
  100. return Version() + (Suffix == null ? string.Empty : $"-{Suffix}");
  101. }
  102. }