version.ps1 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Define the function to get version variables
  2. function Get-GPUStackVersionVar {
  3. # Get the build date
  4. $BUILD_DATE = Get-Date -Format 'yyyy-MM-ddTHH:mm:ssZ'
  5. # Initialize other variables
  6. $GIT_TREE_STATE = "unknown"
  7. $GIT_COMMIT = "unknown"
  8. $GIT_VERSION = "unknown"
  9. # Check if the source was exported through git archive
  10. if ('%$Format:%' -eq '%') {
  11. $GIT_TREE_STATE = "archive"
  12. $GIT_COMMIT = '$Format:%H$'
  13. # Parse the version from '$Format:%D$'
  14. if ('%$Format:%D$' -match 'tag:\s+(v[^ ,]+)') {
  15. $GIT_VERSION = $matches[1]
  16. }
  17. else {
  18. $GIT_VERSION = $GIT_COMMIT.Substring(0, 7)
  19. }
  20. # Respect specified version
  21. $GIT_VERSION = $env:VERSION -or $GIT_VERSION
  22. return
  23. }
  24. # Return if git client is not found
  25. if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
  26. $GIT_VERSION = $env:VERSION -or $GIT_VERSION
  27. return
  28. }
  29. # Find git info via git client
  30. $GIT_COMMIT = git rev-parse "HEAD^{commit}" 2>$null
  31. if ($LASTEXITCODE -eq 0) {
  32. # Check if the tree is clean or dirty
  33. $gitStatus = (git status --porcelain 2>$null)
  34. if ($gitStatus) {
  35. $GIT_TREE_STATE = "dirty"
  36. }
  37. else {
  38. $GIT_TREE_STATE = "clean"
  39. }
  40. # Get the version from HEAD
  41. $GIT_VERSION = git rev-parse --abbrev-ref HEAD 2>$null
  42. if ($LASTEXITCODE -eq 0) {
  43. # Check if HEAD is tagged
  44. $gitTag = git tag -l --contains HEAD 2>$null | Select-Object -First 1
  45. if (-not [string]::IsNullOrEmpty($gitTag)) {
  46. $GIT_VERSION = $gitTag
  47. }
  48. }
  49. # Set version to 'v0.0.0' if the tree is dirty or version format does not match
  50. if ($GIT_TREE_STATE -eq "dirty" -or -not ($GIT_VERSION -match '^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-?[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$')) {
  51. $GIT_VERSION = "v0.0.0"
  52. }
  53. # Respect specified version
  54. if ($env:VERSION) {
  55. $GIT_VERSION = $env:VERSION
  56. }
  57. }
  58. $global:BUILD_DATE = $BUILD_DATE
  59. $global:GIT_TREE_STATE = $GIT_TREE_STATE
  60. $global:GIT_COMMIT = $GIT_COMMIT
  61. $global:GIT_VERSION = $GIT_VERSION
  62. }