build.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. pushd $repo_root
  49. cd "$repo_root/web"
  50. if ! command -v pnpm &>/dev/null; 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. popd
  56. exit 1
  57. fi
  58. fi
  59. echo -e "\n\033[33mInstalling frontend dependencies...\033[0m"
  60. pnpm i --frozen-lockfile
  61. if [ $? -ne 0 ]; then
  62. echo -e "\033[0;31mFrontend dependencies failed to install! Exiting.\033[0m"
  63. popd
  64. exit 1
  65. fi
  66. echo -e "\033[32mFrontend dependencies installed!\033[0m"
  67. echo -e "\n\033[35mRemoving previous frontend build from ./build/dist...\033[0m"
  68. rm -rf $repo_root/build/dist
  69. if [ $? -ne 0 ]; then
  70. echo -e "\033[93mCould not remove frontend from ./build/dist.\033[0m"
  71. popd
  72. exit 1
  73. fi
  74. echo -e "\n\033[33mBuilding frontend...\033[0m"
  75. pnpm build
  76. if [ $? -ne 0 ]; then
  77. echo -e "\033[0;31mFrontend build failed! Exiting.\033[0m"
  78. popd
  79. exit 1
  80. fi
  81. echo -e "\033[32mFrontend built!\033[0m"
  82. cd $repo_root
  83. echo -e "\033[35mMoving frontend build to ./build/dist...\033[0m"
  84. mv -f "$repo_root/web/dist" "$repo_root/build/"
  85. if [ $? -ne 0 ]; then
  86. echo -e "\033[0;31mFailed to move frontend build! Exiting.\033[0m"
  87. popd
  88. exit 1
  89. fi
  90. cd "$repo_root"
  91. echo -e "\n\033[33mBuilding backend...\033[0m"
  92. for build in "${goBuilds[@]}"; do
  93. os=$(echo $build | cut -d'/' -f1)
  94. arch=$(echo $build | cut -d'/' -f2)
  95. output="$repo_root/build/memos-$os-$arch"
  96. if [ "$os" = "windows" ]; then
  97. output="$output.exe"
  98. fi
  99. CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="${ldFlags[*]}" -o "$output" ./bin/memos/main.go
  100. echo -e "\033[34mBuilding $os/$arch to $output...\033[0m"
  101. GOOS=$os GOARCH=$arch go build -ldflags="${ldFlags[*]}" -o "./build/memos-$os-$arch" ./bin/memos/main.go
  102. if [ $? -ne 0 ]; then
  103. echo -e "\033[0;31mgo build failed for $os/$arch($output)! See above.\033[0m"
  104. fi
  105. done
  106. echo -e "\033[32mBackend built!\033[0m"
  107. echo -e "\n\033[37mBuilds:\033[0m"
  108. for build in "${goBuilds[@]}"; do
  109. os=$(echo $build | cut -d'/' -f1)
  110. arch=$(echo $build | cut -d'/' -f2)
  111. output="$repo_root/build/memos-$os-$arch"
  112. if [ "$os" = "windows" ]; then
  113. output="$output.exe"
  114. fi
  115. echo -e "\033[37m$output\033[0m"
  116. done
  117. 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"
  118. cd $repo_root