ExtendedVersion.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Jellyfin.Versioning/ExtendedVersion.cs
  2. // Part of the Jellyfin project (https://jellyfin.media)
  3. //
  4. // All copyright belongs to the Jellyfin contributors; a full list can
  5. // be found in the file CONTRIBUTORS.md
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Runtime.Serialization;
  23. using System.Text;
  24. namespace Jellyfin.Versioning
  25. {
  26. public class ExtendedVersion
  27. {
  28. [IgnoreDataMember]
  29. public Version ApiVersion { get; }
  30. public string CommitHash { get; } = String.Empty;
  31. public long Revision { get; } = 0;
  32. public string Branch { get; } = String.Empty;
  33. public string TagDescription { get; } = String.Empty;
  34. [IgnoreDataMember]
  35. public Uri Remote { get; } = null;
  36. public ExtendedVersion(Version apiVersion, Stream extendedVersionFileStream)
  37. {
  38. ApiVersion = apiVersion;
  39. int line = 1;
  40. using (var reader = new StreamReader(extendedVersionFileStream))
  41. {
  42. while (!reader.EndOfStream)
  43. {
  44. string item = reader.ReadLine();
  45. if (string.IsNullOrWhiteSpace(item.Trim()))
  46. {
  47. //empty line, skip
  48. continue;
  49. }
  50. var kvpair = item.Split('=');
  51. if (kvpair.Length != 2)
  52. {
  53. throw new ArgumentException(nameof(extendedVersionFileStream),
  54. $"ExtendedVersionFile contains bad key-value pair '{item}' at line {line}.");
  55. }
  56. var key = kvpair[0].Trim().ToLower();
  57. var value = kvpair[1].Trim();
  58. switch (key)
  59. {
  60. case "commit":
  61. if (value.Length < 7 || value.Length > 40)
  62. {
  63. throw new ArgumentException(nameof(extendedVersionFileStream),
  64. $"ExtendedVersionFile has a bad commit hash '{value}' on line {line}, it should be a string between 7 and 40 characters.");
  65. }
  66. CommitHash = value;
  67. break;
  68. case "branch":
  69. if (string.IsNullOrWhiteSpace(value))
  70. {
  71. throw new ArgumentException(nameof(extendedVersionFileStream),
  72. $"ExtendedVersionFile has a bad branch '{value}' on line {line}, it can not be empty.");
  73. }
  74. Branch = value;
  75. break;
  76. case "revision":
  77. if (!long.TryParse(value, out long rev))
  78. {
  79. throw new ArgumentException(nameof(extendedVersionFileStream),
  80. $"ExtendedVersionFile has a bad revision '{value}' on line {line}, it should be an integer.");
  81. }
  82. Revision = rev;
  83. break;
  84. case "tagdesc":
  85. if (string.IsNullOrWhiteSpace(value))
  86. {
  87. throw new ArgumentException(nameof(extendedVersionFileStream),
  88. $"ExtendedVersionFile has a bad tag description '{value}' on line {line}, it can not be empty.");
  89. }
  90. TagDescription = value;
  91. break;
  92. case "remote":
  93. var remoteRepo = value.Replace(".git", string.Empty).Replace("git@github.com:", "https://github.com/");
  94. if (Uri.IsWellFormedUriString(remoteRepo, UriKind.Absolute))
  95. {
  96. Remote = new Uri(remoteRepo);
  97. }
  98. else if (Uri.IsWellFormedUriString(value, UriKind.Absolute))
  99. {
  100. //fallback if the replace about broke the Uri
  101. Remote = new Uri(value);
  102. }
  103. else
  104. {
  105. throw new ArgumentException(nameof(extendedVersionFileStream),
  106. $"ExtendedVersionFile has a bad remote URI '{value}' on line {line}, it should be a valid remote URI (ssh or https).");
  107. }
  108. break;
  109. default:
  110. throw new ArgumentException(nameof(extendedVersionFileStream),
  111. $"ExtendedVersionFile contains an unrecognized key-value pair '{item}' at line {line}.");
  112. }
  113. line++;
  114. }
  115. }
  116. }
  117. public override string ToString()
  118. {
  119. return $"{ApiVersion};{CommitHash};{Revision};{Branch};{TagDescription};{Remote}";
  120. }
  121. }
  122. }