build.ps1 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Write-Host "`nBacking up frontend placeholder..." -f Magenta
  65. Move-Item "$repoRoot/server/dist" "$repoRoot/server/dist.bak" -Force -ErrorAction Stop
  66. if (!$?) {
  67. Write-Host -BackgroundColor red -ForegroundColor white "Could not backup frontend placeholder. See above."
  68. Exit 1
  69. }
  70. Write-Host "Moving frontend build to ./server/dist..." -f Magenta
  71. Move-Item "$repoRoot/web/dist" "$repoRoot/server/" -Force -ErrorAction Stop
  72. if (!$?) {
  73. Write-Host -BackgroundColor red -ForegroundColor white "Could not move frontend build to /server/dist. See above."
  74. Exit 1
  75. }
  76. Set-Location $repoRoot
  77. Write-Host "`nBuilding backend..." -f DarkYellow
  78. $backendTime = Measure-Command {
  79. foreach ($build in $goBuilds) {
  80. $os, $arch = $build.Split("/")
  81. $Env:CGO_ENABLED = 0
  82. $Env:GOOS = $os
  83. $Env:GOARCH = $arch
  84. $output = [IO.Path]::Combine($repoRoot, "build", "memos-$os-$arch")
  85. if ($os -eq "windows") {
  86. $output += ".exe"
  87. }
  88. Write-Host "Building $os/$arch to $output..." -f Blue
  89. &go build -trimpath -o $output -ldflags="$($ldFlags -join " ")" ./main.go | Out-Host
  90. if (!$?) {
  91. Write-Host -BackgroundColor red -ForegroundColor white "'go build' failed for $build ($outputBinary)!. See above."
  92. continue
  93. }
  94. }
  95. }
  96. Write-Host "Backend built!" -f green
  97. Write-Host "`nFrontend build took $($frontendTime.TotalSeconds) seconds." -f Cyan
  98. Write-Host "Backend builds took $($backendTime.TotalSeconds) seconds." -f Cyan
  99. Write-Host "`nRemoving frontend from ./server/dist ..." -f Magenta
  100. Remove-Item "$repoRoot/server/dist" -Recurse -Force -ErrorAction SilentlyContinue
  101. if (!$?) {
  102. Write-Host -BackgroundColor red -ForegroundColor white "Could not remove frontend from /server/dist. See above."
  103. Exit 1
  104. }
  105. Write-Host "Restoring frontend placeholder..." -f Magenta
  106. Move-Item "$repoRoot/server/dist.bak" "$repoRoot/server/dist" -Force -ErrorAction Stop
  107. if (!$?) {
  108. Write-Host -BackgroundColor red -ForegroundColor white "Could not restore frontend placeholder. See above."
  109. Exit 1
  110. }
  111. Write-Host "`nBuilds:" -f White
  112. foreach ($build in $goBuilds) {
  113. $output = [IO.Path]::Combine($repoRoot, "build", "memos-$os-$arch")
  114. if ($os -eq "windows") {
  115. $output = "$output.exe"
  116. }
  117. Write-Host $output -f White
  118. }
  119. Write-Host -f Green "`nYou can test the build with" -NoNewline
  120. Write-Host -f White "` ./build/memos-<os>-<arch>" -NoNewline
  121. Write-Host -f DarkGray "`.exe" -NoNewline
  122. Write-Host -f White " --mode demo"
  123. Set-Location -Path $repoRoot