RuntimeVersionProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace SKMC.Api.Common.Version
  7. {
  8. public class RuntimeVersionProvider
  9. {
  10. /// <summary>
  11. /// 获取模块版本信息
  12. /// </summary>
  13. /// <param name="directory">路径</param>
  14. /// <returns></returns>
  15. public ModuleVersion GetModuleVersion(string directory)
  16. {
  17. if (!Directory.Exists(directory))
  18. return new ModuleVersion { Name = Path.GetFileName(directory) };
  19. var commit = GitCommandHelper.Run(directory, "rev-parse HEAD");
  20. var branch = GitCommandHelper.Run(directory, "rev-parse --abbrev-ref HEAD");
  21. var author = GitCommandHelper.Run(directory, "log -1 --format=%an");
  22. var time = GitCommandHelper.Run(directory, "log -1 --format=%ci");
  23. var msg = GitCommandHelper.Run(directory, "log -1 --format=%s");
  24. var dirty = !string.IsNullOrEmpty(GitCommandHelper.Run(directory, "status --porcelain", noCache: true));
  25. DateTime.TryParse(time?.Split('+')[0]?.Trim(), out var commitTime);
  26. var moduleVersion = new ModuleVersion
  27. {
  28. Name = Path.GetFileName(directory),
  29. CommitHash = commit,
  30. Branch = branch,
  31. Author = author,
  32. CommitTime = commitTime,
  33. CommitMessage = msg,
  34. HasUncommittedChanges = dirty,
  35. Path = directory,
  36. AssemblyFileVersion = GetAssemblyVersion(directory)
  37. };
  38. return moduleVersion;
  39. }
  40. /// <summary>
  41. /// 获取项目版本
  42. /// </summary>
  43. /// <param name="rootDir"></param>
  44. /// <returns></returns>
  45. public ProjectVersion GetProjectVersions(string rootDir)
  46. {
  47. var project = new ProjectVersion
  48. {
  49. ProjectName = Path.GetFileName(rootDir),
  50. BuildTime = DateTime.Now,
  51. RootCommitHash = GitCommandHelper.Run(rootDir, "rev-parse HEAD")
  52. };
  53. foreach (var dir in Directory.GetDirectories(rootDir))
  54. {
  55. var folderName = Path.GetFileName(dir);
  56. // 过滤文件
  57. if (folderName.Equals(".git") || folderName.Equals("packages") || folderName.Equals("bin") || folderName.Equals("obj"))
  58. continue;
  59. // 查找是否存在csproj格式文件
  60. if (Directory.GetFiles(dir, "*.csproj", SearchOption.TopDirectoryOnly).Length > 0)
  61. {
  62. project.Modules.Add(GetModuleVersion(dir));
  63. }
  64. }
  65. return project;
  66. }
  67. public string FindGitRoot(string startDir)
  68. {
  69. try
  70. {
  71. var dir = new DirectoryInfo(startDir);
  72. while (dir != null)
  73. {
  74. if (Directory.Exists(Path.Combine(dir.FullName, ".git")))
  75. return dir.FullName;
  76. dir = dir.Parent;
  77. }
  78. }
  79. catch { }
  80. return null;
  81. }
  82. private string GetAssemblyVersion(string modulePath)
  83. {
  84. try
  85. {
  86. var moduleName = Path.GetFileName(modulePath);
  87. var dllPath = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory), $"{moduleName}.dll");
  88. var exePath = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory), $"{moduleName}.exe");
  89. if (!System.IO.File.Exists(dllPath) && !System.IO.File.Exists(exePath))
  90. return "unknown";
  91. var path = new[] { exePath, dllPath }.FirstOrDefault(System.IO.File.Exists);
  92. var assembly = Assembly.LoadFrom(path);
  93. var informationalVersion = assembly.GetCustomAttributesData()
  94. .FirstOrDefault(a => a.AttributeType == typeof(AssemblyInformationalVersionAttribute))?
  95. .ConstructorArguments.FirstOrDefault().Value?.ToString();
  96. if (!string.IsNullOrEmpty(informationalVersion))
  97. return informationalVersion;
  98. return $"{assembly.GetName().Version}";
  99. }
  100. catch
  101. {
  102. return "unknown";
  103. }
  104. }
  105. }
  106. }