rs-wnfs.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/usr/bin/env bash
  2. set -e
  3. # PATHS
  4. # Get current working directory
  5. current_dir=`pwd`
  6. # Get the absolute path where current script is running from.
  7. script_path=$(readlink -f $(which $0))
  8. # Get the canonical directory of script.
  9. if [[ -L $script_path ]]; then
  10. script_dir=$(dirname $(readlink -f $script_path))
  11. else
  12. script_dir=$(dirname $script_path)
  13. fi
  14. # RETURN VARIABLE
  15. ret=""
  16. # ARGUMENTS
  17. args="${@:2}" # All arguments except the first
  18. # COLORS
  19. red='\033[0;31m'
  20. green='\033[0;32m'
  21. purple='\033[0;35m'
  22. none='\033[0m'
  23. yellow="\033[0;33m"
  24. # DESCRIPTION:
  25. # Where execution starts
  26. #
  27. main() {
  28. case $1 in
  29. build )
  30. build
  31. ;;
  32. test )
  33. test
  34. ;;
  35. bench )
  36. bench
  37. ;;
  38. coverage )
  39. coverage
  40. ;;
  41. publish )
  42. publish
  43. ;;
  44. setup )
  45. setup
  46. ;;
  47. *)
  48. help
  49. ;;
  50. esac
  51. exit 0
  52. }
  53. # DESCRIPTION:
  54. # Prints the help info.
  55. #
  56. # USAGE:
  57. # rs-wnfs build
  58. #
  59. help() {
  60. echo ""
  61. echo "Rust WNFS Utility Script"
  62. echo ""
  63. echo "USAGE:"
  64. echo " rs-wnfs [COMMAND] [...args]"
  65. echo ""
  66. echo "COMMAND:"
  67. echo " * build [--fs|--wasm|--common|--hamt|--accumulator] - build projects"
  68. echo " * test [--fs|--wasm|--common|--hamt|--accumulator] - run tests"
  69. echo " * bench - run wnfs benchmarks"
  70. echo " * setup - install rs-wnfs script"
  71. echo " * help - print this help message"
  72. echo ""
  73. echo ""
  74. }
  75. #-------------------------------------------------------------------------------
  76. # Commands
  77. #-------------------------------------------------------------------------------
  78. # DESCRIPTION:
  79. # Builds the project.
  80. #
  81. # USAGE:
  82. # rs-wnfs build [--fs|--wasm|--common|--hamt|--accumulator]
  83. #
  84. build() {
  85. if check_flag --fs; then
  86. build_fs
  87. elif check_flag --wasm; then
  88. build_wasm
  89. elif check_flag --common; then
  90. build_common
  91. elif check_flag --hamt; then
  92. build_hamt
  93. elif check_flag --accumulator; then
  94. build_accumulator
  95. else
  96. build_common
  97. build_hamt
  98. build_accumulator
  99. build_fs
  100. build_wasm
  101. fi
  102. }
  103. build_fs() {
  104. display_header "💿 | BUILDING WNFS PROJECT | 💿"
  105. cargo build -p wnfs --release
  106. }
  107. build_common() {
  108. display_header "💿 | BUILDING WNFS-COMMON PROJECT | 💿"
  109. cargo build -p wnfs-common --release
  110. }
  111. build_hamt() {
  112. display_header "💿 | BUILDING WNFS-HAMT PROJECT | 💿"
  113. cargo build -p wnfs-hamt --release
  114. }
  115. build_accumulator() {
  116. display_header "💿 | BUILDING WNFS-NAMEACCUMULATOR PROJECT | 💿"
  117. cargo build -p wnfs-nameaccumulator --release
  118. }
  119. build_wasm() {
  120. display_header "💿 | BUILDING WNFS-WASM PROJECT | 💿"
  121. cd $script_dir/../wnfs-wasm
  122. npm run release
  123. sed -i.bak \
  124. -e 's/"name": "wnfs-wasm"/"name": "wnfs"/g' \
  125. package.json
  126. rm package.json.bak
  127. }
  128. # DESCRIPTION:
  129. # Runs tests.
  130. #
  131. # USAGE:
  132. # rs-wnfs test [--fs|--wasm|--common|--hamt|--accumulator]
  133. #
  134. test() {
  135. if check_flag --fs; then
  136. test_fs
  137. elif check_flag --wasm; then
  138. test_wasm
  139. elif check_flag --common; then
  140. test_common
  141. elif check_flag --hamt; then
  142. test_hamt
  143. elif check_flag --accumulator; then
  144. test_accumulator
  145. else
  146. test_common
  147. test_hamt
  148. test_accumulator
  149. test_fs
  150. test_wasm
  151. fi
  152. }
  153. test_fs() {
  154. display_header "🧪 | RUNNING WNFS TESTS | 🧪"
  155. cargo test -p wnfs
  156. }
  157. test_common() {
  158. display_header "🧪 | RUNNING WNFS-COMMON TESTS | 🧪"
  159. cargo test -p wnfs-common
  160. }
  161. test_hamt() {
  162. display_header "🧪 | RUNNING WNFS-HAMT TESTS | 🧪"
  163. cargo test -p wnfs-hamt
  164. }
  165. test_accumulator() {
  166. display_header "🧪 | RUNNING WNFS-NAMEACCUMULATOR TESTS | 🧪"
  167. cargo test -p wnfs-nameaccumulator
  168. }
  169. test_wasm() {
  170. display_header "🧪 | RUNNING WNFS-WASM TESTS | 🧪"
  171. cd $script_dir/../wnfs-wasm
  172. yarn
  173. yarn playwright test
  174. }
  175. # DESCRIPTION:
  176. # Runs benchmarks.
  177. #
  178. # USAGE:
  179. # rs-wnfs bench
  180. #
  181. bench() {
  182. display_header "📈 | RUNNING WNFS BENCHMARKS | 📈"
  183. cargo bench -p wnfs-bench
  184. }
  185. #------------------------------------------------------------------------------
  186. # Helper functions
  187. #------------------------------------------------------------------------------
  188. # DESCRIPTION:
  189. # Gets the value following a flag
  190. #
  191. get_flag_value() {
  192. local found=false
  193. local key=$1
  194. local count=0
  195. # For every argument in the list of arguments
  196. for arg in $args; do
  197. count=$((count + 1))
  198. # Check if any of the argument matches the key provided
  199. if [[ $arg = $key ]]; then
  200. found=true
  201. break
  202. fi
  203. done
  204. local args=($args)
  205. local value=${args[count]}
  206. # Check if argument specified was found
  207. if [[ $found = true ]]; then
  208. # Check if there exists a word after the key
  209. # And that such word doesn't start with hyphen
  210. if [[ ! -z $value ]] && [[ $value != "-"* ]]; then
  211. ret=$value
  212. else
  213. ret=""
  214. fi
  215. else
  216. ret=""
  217. fi
  218. }
  219. # DESCRIPTION:
  220. # Checks if the flag is present in the list of arguments
  221. #
  222. check_flag() {
  223. local found=1
  224. local key=$1
  225. # For every argument in the list of arguments
  226. for arg in $args; do
  227. # Check if any of the argument matches the key provided
  228. if [[ $arg = $key ]]; then
  229. found=0
  230. break
  231. fi
  232. done
  233. return $found
  234. }
  235. upgrade_privilege() {
  236. if ! has sudo; then
  237. errorln '"sudo" command not found.'
  238. displayln "If you are on Windows, please run your shell as an administrator, then"
  239. displayln "rerun this script. Otherwise, please run this script as root, or install"
  240. displayln "sudo first."
  241. exit 1
  242. fi
  243. if ! sudo -v; then
  244. errorln "Superuser not granted, aborting installation"
  245. exit 1
  246. fi
  247. }
  248. # DESCRIPTION:
  249. # check if the current user has write perm to specific dir by trying to write to it
  250. #
  251. is_writeable() {
  252. path="${1:-}/test.txt"
  253. if touch "${path}" 2>/dev/null; then
  254. rm "${path}"
  255. return 0
  256. else
  257. return 1
  258. fi
  259. }
  260. # DESCRIPTION:
  261. # Sets up the script by making it excutable and available system wide
  262. #
  263. setup() {
  264. displayln "Make script executable"
  265. chmod u+x $script_path
  266. displayln "Drop a link to it in /usr/local/bin"
  267. sudo=""
  268. if is_writeable "/usr/local/bin"; then
  269. msg="Installing rs-wnfs, please wait…"
  270. else
  271. warnln "Higher permissions are required to install to /usr/local/bin"
  272. upgrade_privilege
  273. sudo="sudo"
  274. msg="Installing rs-wnfs as ROOT, please wait…"
  275. fi
  276. displayln "$msg"
  277. # try to make a symlink, using sudo if required
  278. if ${sudo} ln -s $script_path /usr/local/bin/rs-wnfs; then
  279. successln "Successfully installed"
  280. else
  281. local result=$?
  282. errorln "Failed to install"
  283. exit $result
  284. fi
  285. }
  286. # DESCRIPTION:
  287. # Prints a message.
  288. #
  289. displayln() {
  290. printf "\n::: $1 :::\n"
  291. }
  292. # DESCRIPTION:
  293. # Prints an error message.
  294. #
  295. errorln() {
  296. printf "\n${red}::: $1 :::${none}\n\n"
  297. }
  298. # DESCRIPTION:
  299. # Prints an success message.
  300. #
  301. successln() {
  302. printf "\n${green}::: $1 :::${none}\n\n"
  303. }
  304. # DESCRIPTION:
  305. # Prints a warning message.
  306. #
  307. warnln() {
  308. printf "\n${yellow}!!! $1 !!!${none}\n\n"
  309. }
  310. # DESCRIPTION:
  311. # Prints a header message.
  312. #
  313. display_header() {
  314. printf "\n${purple}$1${none}\n\n"
  315. }
  316. # DESCRIPTION:
  317. # test command availability
  318. #
  319. has() {
  320. command -v "$1" 1>/dev/null 2>&1
  321. }
  322. main $@