build.ps1 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # This script builds memos for all listed platforms.
  2. # It's only for local builds.
  3. # Before using, setup a proper development environment as described here:
  4. # * https://usememos.com/docs/contribution/development
  5. # * https://github.com/usememos/memos/blob/main/docs/development.md
  6. # Requirements:
  7. # * go
  8. # * node.js
  9. # * npm
  10. # Usage:
  11. # ./scripts/build.ps1
  12. #
  13. # Output: ./build/memos-<os>-<arch>[.exe]
  14. $goBuilds = @(
  15. # "darwin/amd64"
  16. # "darwin/arm64"
  17. # "linux/amd64"
  18. # "linux/arm64"
  19. "windows/amd64"
  20. )
  21. $ldFlags = @(
  22. "-s" # Omit symbol table and debug information
  23. "-w" # Omit DWARF symbol table
  24. )
  25. ##
  26. foreach ($dir in @(".", "../")) {
  27. if (Test-Path (Join-Path $dir ".gitignore")) {
  28. $repoRoot = (Resolve-Path $dir).Path
  29. break
  30. }
  31. }
  32. if ([string]::IsNullOrWhiteSpace($repoRoot)) {
  33. Write-Host -BackgroundColor red -ForegroundColor white "Could not find repository root."
  34. Exit 1
  35. }
  36. Write-Host "Repository root: " -NoNewline
  37. Write-Host $repoRoot -f Blue
  38. Set-Location "$repoRoot/web"
  39. if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
  40. Write-Host "Installing pnpm..." -f DarkYellow
  41. npm install -g pnpm
  42. if (!$?) {
  43. Write-Host -BackgroundColor red -ForegroundColor white "Could not install pnpm. See above."
  44. Exit 1
  45. }
  46. }
  47. Write-Host "`nInstalling frontend dependencies..." -f DarkYellow
  48. pnpm i --frozen-lockfile
  49. if (!$?) {
  50. Write-Host -BackgroundColor red -ForegroundColor white "Could not install frontend dependencies. See above."
  51. Exit 1
  52. }
  53. Write-Host "Frontend dependencies installed!" -f green
  54. Write-Host "`nBuilding frontend..." -f DarkYellow
  55. $frontendTime = Measure-Command {
  56. &pnpm build | Out-Host
  57. }
  58. if (!$?) {
  59. Write-Host -BackgroundColor red -ForegroundColor white "Could not build frontend. See above."
  60. Exit 1
  61. } else {
  62. Write-Host "Frontend built!" -f green
  63. }
  64. f (!$?) {
  65. Write-Host -BackgroundColor red -ForegroundColor white "Could not generate buf types. See above."
  66. Exit 1
  67. } else {
  68. Write-Host "buf types generated!" -f green
  69. }
  70. Write-Host "`nBacking up frontend placeholder..." -f Magenta
  71. Move-Item "$repoRoot/server/frontend/dist" "$repoRoot/server/frontend/dist.bak" -Force -ErrorAction Stop
  72. if (!$?) {
  73. Write-Host -BackgroundColor red -ForegroundColor white "Could not backup frontend placeholder. See above."
  74. Exit 1
  75. }
  76. Write-Host "Moving frontend build to ./server/frontend/dist..." -f Magenta
  77. Move-Item "$repoRoot/web/dist" "$repoRoot/server/" -Force -ErrorAction Stop
  78. if (!$?) {
  79. Write-Host -BackgroundColor red -ForegroundColor white "Could not move frontend build to /server/frontend/dist. See above."
  80. Exit 1
  81. }
  82. Set-Location $repoRoot
  83. Write-Host "`nBuilding backend..." -f DarkYellow
  84. $backendTime = Measure-Command {
  85. foreach ($build in $goBuilds) {
  86. $os, $arch = $build.Split("/")
  87. $Env:CGO_ENABLED = 0
  88. $Env:GOOS = $os
  89. $Env:GOARCH = $arch
  90. $output = [IO.Path]::Combine($repoRoot, "build", "memos-$os-$arch")
  91. if ($os -eq "windows") {
  92. $output += ".exe"
  93. }
  94. Write-Host "Building $os/$arch to $output..." -f Blue
  95. &go build -trimpath -o $output -ldflags="$($ldFlags -join " ")" ./bin/memos/main.go | Out-Host
  96. if (!$?) {
  97. Write-Host -BackgroundColor red -ForegroundColor white "'go build' failed for $build ($outputBinary)!. See above."
  98. continue
  99. }
  100. }
  101. }
  102. Write-Host "Backend built!" -f green
  103. Write-Host "`nFrontend build took $($frontendTime.TotalSeconds) seconds." -f Cyan
  104. Write-Host "Backend builds took $($backendTime.TotalSeconds) seconds." -f Cyan
  105. Write-Host "`nRemoving frontend from ./server/frontend/dist ..." -f Magenta
  106. Remove-Item "$repoRoot/server/frontend/dist" -Recurse -Force -ErrorAction SilentlyContinue
  107. if (!$?) {
  108. Write-Host -BackgroundColor red -ForegroundColor white "Could not remove frontend from /server/frontend/dist. See above."
  109. Exit 1
  110. }
  111. Write-Host "Restoring frontend placeholder..." -f Magenta
  112. Move-Item "$repoRoot/server/frontend/dist.bak" "$repoRoot/server/frontend/dist" -Force -ErrorAction Stop
  113. if (!$?) {
  114. Write-Host -BackgroundColor red -ForegroundColor white "Could not restore frontend placeholder. See above."
  115. Exit 1
  116. }
  117. Write-Host "`nBuilds:" -f White
  118. foreach ($build in $goBuilds) {
  119. $output = [IO.Path]::Combine($repoRoot, "build", "memos-$os-$arch")
  120. if ($os -eq "windows") {
  121. $output = "$output.exe"
  122. }
  123. Write-Host $output -f White
  124. }
  125. Write-Host -f Green "`nYou can test the build with" -NoNewline
  126. Write-Host -f White "` ./build/memos-<os>-<arch>" -NoNewline
  127. Write-Host -f DarkGray "`.exe" -NoNewline
  128. Write-Host -f White " --mode demo"
  129. Set-Location -Path $repoRoot