hardmake.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env bash
  2. set -e
  3. set -u
  4. set -o pipefail
  5. rname=origin
  6. rpath=
  7. build=stable
  8. mode=test
  9. UserAt=
  10. ingredients=
  11. hardmake=./
  12. configfile=
  13. # TODO: Finish the help.
  14. print_help() {
  15. echo "script usage: $(basename $0)" &&\
  16. echo " [-a add remotes mode]" &&\
  17. echo " [-b set the build for checkout]" &&\
  18. echo " [-c checkout a build; default stable]" &&\
  19. echo " [-d clone dependencies from the remote]" &&\
  20. echo " [-f set the config file]" &&\
  21. echo " [-h help]" &&\
  22. echo " [-i set the projects to use from the config file]" &&\
  23. echo " [-p set the relative path to hardmake.sh]" &&\
  24. echo " [-r set name of the remote to use]" &&\
  25. echo " [-R set generic path for the set of remotes to use]" &&\
  26. echo " [-s use ssh instead of https - WARNING: planned removal]" &&\
  27. echo " [-t run the test]" &&\
  28. echo " [-u set the username for https]"
  29. }
  30. while getopts 'ab:cdf:hi:p:r:R:stu:' OPTION; do
  31. case "$OPTION" in
  32. a)
  33. mode=remotes
  34. #echo "mode: $mode"
  35. ;;
  36. b)
  37. build="$OPTARG"
  38. #echo "build: $build"
  39. ;;
  40. c)
  41. mode=checkout
  42. #echo "mode: $mode"
  43. ;;
  44. d)
  45. mode=clone
  46. #echo "mode: $mode"
  47. ;;
  48. f)
  49. configfile="$OPTARG"
  50. #echo "configfile: $configfile"
  51. ;;
  52. h)
  53. print_help >&2
  54. exit 0
  55. ;;
  56. i)
  57. ingredients="$OPTARG"
  58. #echo "ingredients: $ingredients"
  59. ;;
  60. p)
  61. hardmake="$OPTARG"
  62. ;;
  63. r)
  64. rname="$OPTARG"
  65. #echo "rname: $rname"
  66. ;;
  67. R)
  68. rpath="$OPTARG"
  69. #echo "rpath: $rpath"
  70. ;;
  71. s)
  72. rname=ssh
  73. #echo "using ssh instead of https"
  74. ;;
  75. t)
  76. mode=test
  77. #echo "mode: $mode"
  78. ;;
  79. u)
  80. UserAt="$OPTARG"
  81. #echo "userAt: $UserAt"
  82. ;;
  83. ?) print_help >&2; exit 1;;
  84. esac
  85. done
  86. shift "$(($OPTIND -1))"
  87. add_user_to_remote() {
  88. local remote=$1
  89. local UserAt=${2:-""}
  90. if [ ! "$UserAt" = "" ] ; then
  91. local remoteMod="${remote/https\:\/\//https\:\/\/$UserAt@}"
  92. echo $remoteMod
  93. else
  94. echo $remote
  95. fi
  96. }
  97. add_remotes() {
  98. create_variables ${configfile} ''
  99. local ingredients=( "$@" )
  100. for current in "${ingredients[@]}"
  101. do
  102. # echo cloning $current
  103. local path=$(eval echo "\$${current}_path")
  104. #NOTE: Clever, but too noisy on misses.
  105. # local remotePath=${rpath:-$remoteInit}${rpath+$remoteIndex}
  106. if [ "$path" = "" ]; then
  107. echo ";; no path found for $current"
  108. elif [ "$rname" = "" ] ; then
  109. echo ";; no rname found"
  110. elif [ ! -d ./$path ]; then
  111. echo ";; no repo found for $path"
  112. elif [ ! "$rpath" = "" ] ; then
  113. # Add a remote from the UI, given the rpath and rname
  114. local remoteOrigin=$(eval echo "\$${current}_remote_origin")
  115. local remoteIndex=${remoteOrigin##*/}
  116. local remoteFoo=${rpath}${remoteIndex}
  117. local remoteMod=$(add_user_to_remote $remoteFoo $UserAt)
  118. git --git-dir ${path}.git remote add ${rname} ${remoteMod}
  119. else
  120. # Add a remote from the config file, given the rname
  121. local remotePath=$(eval echo "\$${current}_remote_${rname}")
  122. local remoteMod=$(add_user_to_remote $remotePath $UserAt)
  123. git --git-dir ${path}.git remote add ${rname} ${remoteMod}
  124. fi
  125. done
  126. #echo done
  127. }
  128. clone_dependencies() {
  129. create_variables ${configfile} ''
  130. local ingredients=( "$@" )
  131. for current in "${ingredients[@]}"
  132. do
  133. # echo cloning $current
  134. local path=$(eval echo "\$${current}_path")
  135. local remotePath=$(eval echo "\$${current}_remote_${rname}")
  136. if [ "$path" = "" ]; then
  137. echo ";; no path found for $current"
  138. elif [ "$remotePath" = "" ] ; then
  139. echo ";; no remote found for $current"
  140. elif [ ! -d ./$path ]; then
  141. local remoteMod=$(add_user_to_remote $remotePath $UserAt)
  142. # echo $path
  143. # echo $remotePath
  144. # echo $UserAt
  145. # echo $remoteMod
  146. git clone ${remoteMod} ${path}
  147. else
  148. git --git-dir ${path}.git fetch
  149. fi
  150. done
  151. #echo done
  152. }
  153. checkout_version() {
  154. create_variables ${configfile} ''
  155. local ingredients=( "$@" )
  156. for current in "${ingredients[@]}"
  157. do
  158. # echo checkout $current
  159. local path=$(eval echo "\$${current}_path")
  160. local hash=$(eval echo "\$${current}_hash_${build}")
  161. if [ "$path" = "" ]; then
  162. echo ";; no path found for $current"
  163. elif [ "$hash" = "" ]; then
  164. echo ";; no hash found for $current"
  165. elif [ -d ./$path ]; then
  166. echo "${hash} - ${path}"
  167. local temp=`pwd`
  168. cd ${path} && git checkout ${hash}
  169. cd $temp
  170. #NOTE: This doesn't seem to work and I haven't dug into it much for why.
  171. # git --git-dir ./${path}.git checkout ${hash} -q
  172. else
  173. echo ";; path does not exist: $path"
  174. fi
  175. done
  176. #echo done
  177. }
  178. test_config() {
  179. create_variables ${configfile} ''
  180. local ingredients=( "$@" )
  181. for current in "${ingredients[@]}"
  182. do
  183. # TODO: Kind of a weak test; add more than just echoing a few of these
  184. echo testing $current
  185. if [ ! -z $(eval echo "\$${current}_path") ] ; then
  186. local path=$(eval echo "\$${current}_path")
  187. echo $path
  188. fi
  189. if [ ! -z $(eval echo "\$${current}_hash_${build}") ] ; then
  190. local hash=$(eval echo "\$${current}_hash_${build}")
  191. echo $hash
  192. fi
  193. if [ ! -z $(eval echo "\$${current}_remote_${rname}") ] ; then
  194. local remote=$(eval echo "\$${current}_remote_${rname}")
  195. if [ ! "$UserAt" = "" ] ; then
  196. local remoteMod=$(add_user_to_remote $remote $UserAt)
  197. echo $remoteMod
  198. else
  199. echo $remote
  200. fi
  201. local foo=https://foo/
  202. local bar=$(eval echo "\$${current}_remote_${rname}")
  203. local glux=${bar##*/}
  204. local remotePath=${foo:-$bar}${foo+$glux}
  205. echo $remotePath
  206. fi
  207. done
  208. #echo done
  209. }
  210. source ${hardmake}lib/yaml.sh
  211. if [ "$configfile" = "" ]; then
  212. echo "No configfile."
  213. exit 1
  214. elif [ "$ingredients" = "" ]; then
  215. echo "No dependencies."
  216. exit 1
  217. elif [ "$mode" = "clone" ]; then
  218. clone_dependencies $ingredients
  219. elif [ "$mode" = "checkout" ]; then
  220. checkout_version $ingredients
  221. elif [ "$mode" = "remotes" ]; then
  222. add_remotes $ingredients
  223. else
  224. test_config $ingredients
  225. fi
  226. exit