build.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. # This script builds memos for all listed platforms.
  3. # It's only for local builds.
  4. # Before using, setup a proper development environment as described here:
  5. # * https://usememos.com/docs/contribution/development
  6. # * https://github.com/usememos/memos/blob/main/docs/development.md
  7. # Requirements:
  8. # * go
  9. # * node.js
  10. # * npm
  11. # Usage:
  12. # chmod +x ./scripts/build.sh
  13. # ./scripts/build.sh
  14. #
  15. # Output: ./build/memos-<os>-<arch>[.exe]
  16. goBuilds=(
  17. # "darwin/amd64"
  18. # "darwin/arm64"
  19. "linux/amd64"
  20. # "linux/arm64"
  21. # "windows/amd64"
  22. )
  23. ldFlags=(
  24. "-s" # Omit symbol table and debug information
  25. "-w" # Omit DWARF symbol table
  26. )
  27. ##
  28. find_repo_root() {
  29. # Usage: find_repo_root <file_at_root> <dir1> <dir2> ...
  30. local looking_for="${1:-".gitignore"}"
  31. shift
  32. local default_dirs=("." "../")
  33. local dirs=("${@:-${default_dirs[@]}}")
  34. for dir in "${dirs[@]}"; do
  35. if [ -f "$dir/$looking_for" ]; then
  36. echo $(realpath "$dir")
  37. return
  38. fi
  39. done
  40. }
  41. repo_root=$(find_repo_root)
  42. if [ -z "$repo_root" ]; then
  43. echo -e "\033[0;31mRepository root not found! Exiting.\033[0m"
  44. exit 1
  45. else
  46. echo -e "Repository root: \033[0;34m$repo_root\033[0m"
  47. fi
  48. cd "$repo_root/web"
  49. if ! command -v pnpm &> /dev/null
  50. then
  51. echo -e "\n\033[35mInstalling pnpm...\033[0m"
  52. npm install -g pnpm
  53. if [ $? -ne 0 ]; then
  54. echo -e "\033[0;31mFailed to install pnpm! Exiting.\033[0m"
  55. exit 1
  56. fi
  57. fi
  58. echo -e "\n\033[33mInstalling frontend dependencies...\033[0m"
  59. pnpm i --frozen-lockfile
  60. if [ $? -ne 0 ]; then
  61. echo -e "\033[0;31mFrontend dependencies failed to install! Exiting.\033[0m"
  62. exit 1
  63. fi
  64. echo -e "\033[32mFrontend dependencies installed!\033[0m"
  65. echo -e "\n\033[33mBuilding frontend...\033[0m"
  66. pnpm build
  67. if [ $? -ne 0 ]; then
  68. echo -e "\033[0;31mFrontend build failed! Exiting.\033[0m"
  69. exit 1
  70. fi
  71. echo -e "\033[32mFrontend built!\033[0m"
  72. cd $repo_root
  73. echo -e "\n\033[35mBacking up frontend placeholder...\033[0m"
  74. mv -f "$repo_root/server/dist" "$repo_root/server/dist.bak"
  75. if [ $? -ne 0 ]; then
  76. echo -e "\033[0;31mFailed to backup frontend placeholder! Exiting.\033[0m"
  77. exit 1
  78. fi
  79. echo -e "\033[35mMoving frontend build to ./server/dist...\033[0m"
  80. mv -f "$repo_root/web/dist" "$repo_root/server/"
  81. if [ $? -ne 0 ]; then
  82. echo -e "\033[0;31mFailed to move frontend build! Exiting.\033[0m"
  83. exit 1
  84. fi
  85. cd "$repo_root"
  86. echo -e "\n\033[33mBuilding backend...\033[0m"
  87. for build in "${goBuilds[@]}"; do
  88. os=$(echo $build | cut -d'/' -f1)
  89. arch=$(echo $build | cut -d'/' -f2)
  90. output="$repo_root/build/memos-$os-$arch"
  91. if [ "$os" = "windows" ]; then
  92. output="$output.exe"
  93. fi
  94. CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="${ldFlags[*]}" -o "$output" ./main.go
  95. echo -e "\033[34mBuilding $os/$arch to $output...\033[0m"
  96. GOOS=$os GOARCH=$arch go build -ldflags="${ldFlags[*]}" -o "./build/memos-$os-$arch" ./main.go
  97. if [ $? -ne 0 ]; then
  98. echo -e "\033[0;31mgo build failed for $os/$arch($output)! See above.\033[0m"
  99. fi
  100. done
  101. echo -e "\033[32mBackend built!\033[0m"
  102. echo -e "\n\033[35mRemoving frontend from ./server/dist...\033[0m"
  103. rm -rf $repo_root/server/dist
  104. if [ $? -ne 0 ]
  105. then
  106. echo -e "\033[93mCould not remove frontend from /server/dist.\033[0m"
  107. exit 1
  108. fi
  109. echo -e "\033[35mRestoring frontend placeholder...\033[0m"
  110. mv $repo_root/server/dist.bak $repo_root/server/dist
  111. if [ $? -ne 0 ]
  112. then
  113. echo -e "\033[93mCould not restore frontend placeholder.\033e[0m"
  114. exit 1
  115. fi
  116. echo -e "\n\033[37mBuilds:\033[0m"
  117. for build in "${goBuilds[@]}"; do
  118. os=$(echo $build | cut -d'/' -f1)
  119. arch=$(echo $build | cut -d'/' -f2)
  120. output="$repo_root/build/memos-$os-$arch"
  121. if [ "$os" = "windows" ]; then
  122. output="$output.exe"
  123. fi
  124. echo -e "\033[37m$output\033[0m"
  125. done
  126. echo -e "\n\033[32mYou can test the build with \033[37m./build/memos-<os>-<arch>\033[0m\033[90m.exe\033[0m \033[37m--mode demo\033[0m"
  127. cd $repo_root