build.ps1 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Set error handling
  2. $ErrorActionPreference = "Stop"
  3. $ROOT_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -Parent | Split-Path -Parent -Resolve
  4. # Include the common functions
  5. . "$ROOT_DIR/hack/lib/windows/init.ps1"
  6. function Build {
  7. $distDir = Join-Path -Path $ROOT_DIR -ChildPath "dist"
  8. Remove-Item -Path $distDir -Recurse -Force -ErrorAction SilentlyContinue
  9. uv build
  10. if ($LASTEXITCODE -ne 0) {
  11. GPUStack.Log.Fatal "failed to run uv build."
  12. }
  13. $whlFiles = Get-ChildItem -Path $distDir -Filter "*.whl" -File
  14. if ($whlFiles.Count -eq 0) {
  15. GPUStack.Log.Fatal "no wheel files found in $distDir"
  16. }
  17. foreach ($whlFile in $whlFiles) {
  18. $orginalName = $whlFile.Name
  19. $newName = $orginalName -replace "any", "win_amd64"
  20. $newFilePath = Join-Path -Path $distDir -ChildPath $newName
  21. Remove-Item -Path $newFilePath -Force -ErrorAction SilentlyContinue
  22. Rename-Item -Path $whlFile.FullName -NewName $newFilePath -Force
  23. GPUStack.Log.Info "renamed $orginalName to $newName"
  24. }
  25. }
  26. function Install-Dependency {
  27. & "$ROOT_DIR\hack\windows\install.ps1"
  28. }
  29. function Set-Version {
  30. $versionFile = Join-Path -Path $ROOT_DIR -ChildPath "gpustack\__init__.py"
  31. $pyprojectFile = Join-Path -Path $ROOT_DIR -ChildPath "pyproject.toml"
  32. $version = if ($null -ne $global:GIT_VERSION) { $global:GIT_VERSION } else { "v0.0.0" }
  33. $gitCommit = if ($null -ne $global:GIT_COMMIT) { $global:GIT_COMMIT } else { "HEAD" }
  34. $gitCommitShort = $gitCommit.Substring(0, [Math]::Min(7, $gitCommit.Length))
  35. GPUStack.Log.Info "setting version to $version"
  36. GPUStack.Log.Info "setting git commit to $gitCommitShort"
  37. # Replace the __version__ variable in the __init__.py file
  38. $fileContent = Get-Content -Path $versionFile
  39. $fileContent = $fileContent -replace "__version__ = .*", "__version__ = '$version'"
  40. $fileContent = $fileContent -replace "__git_commit__ = .*", "__git_commit__ = '$gitCommitShort'"
  41. Set-Content -Path $versionFile -Value $fileContent
  42. # Update the version in pyproject.toml
  43. $pyprojectContent = Get-Content -Path $pyprojectFile
  44. $pyprojectContent = $pyprojectContent -replace "^version = .*", "version = `"$version`""
  45. Set-Content -Path $pyprojectFile -Value $pyprojectContent
  46. }
  47. function Restore-Version-File {
  48. $versionFile = Join-Path -Path $ROOT_DIR -ChildPath "gpustack\__init__.py"
  49. $pyprojectFile = Join-Path -Path $ROOT_DIR -ChildPath "pyproject.toml"
  50. git checkout -- $versionFile $pyprojectFile
  51. if ($LASTEXITCODE -ne 0) {
  52. GPUStack.Log.Fatal "failed restore version files."
  53. }
  54. }
  55. #
  56. # main
  57. #
  58. GPUStack.Log.Info "+++ BUILD +++"
  59. try {
  60. Install-Dependency
  61. Set-Version
  62. Build
  63. Restore-Version-File
  64. }
  65. catch {
  66. GPUStack.Log.Fatal "failed to build: $($_.Exception.Message)"
  67. }
  68. GPUStack.Log.Info "--- BUILD ---"