build.ps1 4.8 KB

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