clsVersion.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using TFABot.Git;
  6. namespace TFABot
  7. {
  8. static public class clsVersion
  9. {
  10. static public String GitCommitHash {get;private set;}
  11. const string versionFilePath = "version.txt";
  12. static public bool VersionChangeFlag {get; private set;}
  13. static clsVersion()
  14. {
  15. var GitCommit = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyGitCommit),false).Cast<AssemblyGitCommit>();
  16. if (GitCommit!=null && GitCommit.Any<AssemblyGitCommit>()) GitCommitHash = GitCommit.First().Hash;
  17. if (!String.IsNullOrEmpty(GitCommitHash))
  18. {
  19. VersionChangeFlag = CheckFileVersion();
  20. }
  21. else
  22. {
  23. GitCommitHash = "< Not Set >";
  24. }
  25. }
  26. static string GetFileVersion()
  27. {
  28. try
  29. {
  30. Console.Write("Get file version...");
  31. if (File.Exists(versionFilePath))
  32. {
  33. var ver = File.ReadAllText(versionFilePath);
  34. Console.Write(ver);
  35. return ver;
  36. }
  37. Console.WriteLine("not found");
  38. }
  39. catch (Exception ex)
  40. {
  41. Console.WriteLine($"GetFileVersion Error {ex.Message}");
  42. }
  43. return null;
  44. }
  45. static public string CheckForUpdate()
  46. {
  47. try
  48. {
  49. using (var git = new clsGit())
  50. {
  51. return git.Head.RemoteSha != GitCommitHash ? git.Head.ToString() : null;
  52. }
  53. } catch (Exception ex)
  54. {
  55. Console.WriteLine($"CheckForUpdate Error:{ex.Message}");
  56. return null;
  57. }
  58. }
  59. static public bool CheckFileVersion()
  60. {
  61. var fileVer = GetFileVersion();
  62. if (fileVer == null || fileVer != GitCommitHash)
  63. {
  64. VersionChangeFlag=true;
  65. SetFileVersion(GitCommitHash);
  66. return true;
  67. }
  68. return false;
  69. }
  70. static void SetFileVersion(string version)
  71. {
  72. try
  73. {
  74. File.WriteAllText(versionFilePath,version);
  75. }
  76. catch (Exception ex)
  77. {
  78. Console.WriteLine($"SetFileVersion Error {ex.Message}");
  79. }
  80. }
  81. }
  82. }