helper.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # $1 = package name
  3. # $2 = target directory path
  4. # $3 = branch/commit/revision string, if empty then maste ist used
  5. function go_get {
  6. if [[ $1 == github.com* ]]
  7. then
  8. get_git $1 $2 $3
  9. elif [[ $1 == code.google.com* ]]
  10. then
  11. get_hg $1 $2 $3
  12. elif [[ $1 == golang.org* ]]
  13. then
  14. get_golang $1 $2 $3
  15. else
  16. go get $1
  17. fi
  18. }
  19. # $1 = git package
  20. # $2 = target directory path
  21. # $3 = branch/commit/revision string, if empty then maste ist used
  22. function get_git {
  23. git clone https://$1 $2
  24. if [[ $3 != "master" ]] && [[ ${3:0} == commit* ]]
  25. then
  26. cd $2
  27. git checkout -q ${3:7}
  28. elif [[ $3 != "master" ]] && [[ ${3:0} == tag* ]]
  29. then
  30. cd $2
  31. git checkout -q tags/${3:4}
  32. else
  33. cd $2
  34. git checkout -q ${3:7}
  35. fi
  36. }
  37. # $1 = mercury package name
  38. # $2 = target directory path
  39. # $3 = branch/commit/revision string, if empty then maste ist used
  40. function get_hg {
  41. if [[ $3 == "master" ]] || [[ $3 == "" ]]
  42. then
  43. hg clone https://$1 $2
  44. else
  45. hg clone https://$1 -r $3 $2
  46. fi
  47. }
  48. function get_golang {
  49. local package=$1
  50. local match="golang.org/x"
  51. local replace="github.com/golang"
  52. local result=""
  53. result=${package/$match/$replace}
  54. get_git $result $2 $3
  55. }
  56. # Read the .gopmfile file and clone the branch/commits of the depends
  57. # $1 = .gopmfile file path
  58. # $2 = target directory path
  59. function get_gopm {
  60. local startStr=""
  61. local revStr=""
  62. while read line
  63. do
  64. if [[ $startStr == 'X' ]] && [[ $line == '' ]]
  65. then
  66. break
  67. elif [[ $startStr == 'X' ]]
  68. then
  69. IFS="=" read -a array <<< "$line"
  70. if [[ ${array[1]} != "" ]]
  71. then
  72. local revStr=${array[1]//\`}
  73. go_get ${array[0]} "$2/${array[0]}" $revStr
  74. else
  75. go_get ${array[0]} "$2/${array[0]}" master
  76. fi
  77. elif [[ $line == '[deps]' ]]
  78. then
  79. local startStr="X"
  80. fi
  81. done <$1
  82. }