rubygems 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #!/usr/bin/env bash
  2. rubygems_remove()
  3. {
  4. \typeset rubygems_path __ruby entry
  5. rvm_log "Removing old Rubygems files..."
  6. case "$rvm_ruby_string" in
  7. (rbx-*)
  8. __ruby="['prefix']"
  9. ;;
  10. (*)
  11. __ruby=".values_at('sitelibdir','vendorlibdir').detect{ |path| File.directory?(File.join(path.to_s, 'rubygems')) }.to_s"
  12. ;;
  13. esac
  14. rubygems_path="$(${1:-ruby} -rrbconfig -e "puts ::Kernel.const_get('RbConfig')::CONFIG${ruby__}")"
  15. # Remove common files installed by ruby gems.
  16. entries=(
  17. "${rubygems_path}/ubygems.rb"
  18. "${rubygems_path}/gauntlet_rubygems.rb"
  19. "${rubygems_path}/rbconfig/"
  20. )
  21. for entry in "${entries[@]}" "${rubygems_path}/rubygems"*
  22. do
  23. __rvm_rm_rf "$entry"
  24. done
  25. }
  26. can_switch_rubygems()
  27. {
  28. case "$rvm_ruby_string" in
  29. maglev*)
  30. return 1
  31. ;;
  32. *)
  33. return 0
  34. ;;
  35. esac
  36. }
  37. make_sure_jruby_can_work_with_rubygems()
  38. {
  39. case "$rvm_ruby_string" in
  40. jruby-head*)
  41. true
  42. ;;
  43. jruby*)
  44. __rvm_version_compare "$rvm_ruby_version" -ge 1.7.1 || return 1
  45. ;;
  46. *)
  47. return 0 # do not care about other rubies
  48. ;;
  49. esac
  50. case "${rvm_rubygems_version}" in
  51. head|master)
  52. true
  53. ;;
  54. *)
  55. __rvm_version_compare "$rvm_rubygems_version" -ge 2.0.0 || return 1
  56. ;;
  57. esac
  58. }
  59. rubygems_fatal_error()
  60. {
  61. rvm_error "$1"
  62. exit ${2:-1}
  63. }
  64. rubygems_version_list()
  65. {
  66. __rvm_curl -sS https://rubygems.org/api/v1/versions/rubygems-update.json |
  67. __rvm_awk -v RS=',' -v FS='"' '$2=="number"{print $4}' |
  68. __rvm_version_sort |
  69. __rvm_grep '^[[:digit:]\.]*$'
  70. }
  71. rubygems_current_rubygems_version()
  72. {
  73. "${rvm_ruby_binary}" -rrubygems -e 'puts(Gem::VERSION)' 2>/dev/null
  74. }
  75. rubygems_master_sha()
  76. {
  77. __rvm_curl -s "https://api.github.com/repos/rubygems/rubygems/commits?page=last&per_page=1" |
  78. __rvm_sed -n '/^ "sha":/ {s/^.*".*": "\(.*\)".*$/\1/;p;}'
  79. }
  80. bundler_rubygems_master_sha()
  81. {
  82. __rvm_curl -s "https://api.github.com/repos/rubygems/rubygems/contents/bundler" |
  83. __rvm_sed -n '/^ "sha":/ {s/^.*".*": "\(.*\)".*$/\1/;p;}'
  84. }
  85. rubygems_select_current()
  86. {
  87. case "$version" in
  88. latest|current)
  89. version="$(__rvm_db "${rvm_ruby_string//-/_}_rubygems_version")"
  90. version="${version:-"$(__rvm_db "${rvm_ruby_interpreter}_${rvm_ruby_version}_rubygems_version")"}"
  91. if (( ${rvm_head_flag:-0} == 1 )) && [[ -z "${version:-}" ]]
  92. then version="$(__rvm_db "${rvm_ruby_interpreter}_head_rubygems_version")"
  93. fi
  94. version="${version:-"$(__rvm_db "${rvm_ruby_interpreter}_rubygems_version")"}"
  95. version="${version:-"$(__rvm_db "rubygems_version")"}"
  96. ;;
  97. esac
  98. }
  99. rubygems_select_latest()
  100. {
  101. case "$version" in
  102. latest-*)
  103. version="${version#latest-}"
  104. version="$(rubygems_version_list | __rvm_grep "^${version}\." | __rvm_tail -n 1 )"
  105. version="${version}"
  106. ;;
  107. esac
  108. }
  109. rubygems_select_version_url()
  110. {
  111. case "${version:-missing}" in
  112. ignore|none)
  113. rvm_rubygems_version="$version"
  114. ;;
  115. head|master)
  116. \typeset sha
  117. sha="$(rubygems_master_sha)"
  118. rvm_rubygems_version="$version"
  119. rvm_gem_package_name="rubygems-${sha}"
  120. rvm_gem_url="https://github.com/rubygems/rubygems/archive/${sha}.tar.gz"
  121. __bundler_sha="$(bundler_rubygems_master_sha)"
  122. __bundler_package_name="bundler-${__bundler_sha}"
  123. __bundler_url="https://github.com/bundler/bundler/archive/${__bundler_sha}.tar.gz"
  124. export rvm_verify_downloads_flag=1
  125. ;;
  126. v*)
  127. rvm_rubygems_version="$version"
  128. rvm_gem_package_name="rubygems-${version#v}"
  129. rvm_gem_url="https://github.com/rubygems/rubygems/archive/$version.tar.gz"
  130. export rvm_verify_downloads_flag=1
  131. ;;
  132. missing)
  133. rvm_error "There was an error while trying to resolve rubygems version for '$1'. \nHalting the installation."
  134. return 4
  135. ;;
  136. *)
  137. rvm_rubygems_version="$version"
  138. rvm_gem_package_name="rubygems-${rvm_rubygems_version}"
  139. rvm_rubygems_url=$(__rvm_db "rubygems_url")
  140. rvm_gem_url="${rvm_rubygems_url}/${rvm_gem_package_name}.tgz"
  141. ;;
  142. esac
  143. }
  144. rubygems_select_version()
  145. {
  146. rubygems_select_current "$1" &&
  147. rubygems_select_latest "$1" &&
  148. rubygems_select_version_url "$1" ||
  149. return $?
  150. }
  151. __rvm_warn_on_rubyopt()
  152. {
  153. if [[ -n "${RUBYOPT:-""}" ]]; then
  154. rvm_warn \
  155. "Please note: You have the RUBYOPT environment variable set and this \
  156. may interfere with normal rvm operations. We sugges unsetting it."
  157. return 1
  158. else
  159. return 0
  160. fi
  161. }
  162. rubygems_setup()
  163. {
  164. export version="$1"
  165. \typeset __bundler_sha __bundler_package_name __bundler_url
  166. rvm_debug "rubygems_setup version:${version} rvm_ruby_string:${rvm_ruby_string} rvm_ruby_home:${rvm_ruby_home} rvm_ruby_binary:${rvm_ruby_binary}"
  167. case "${version:-missing}" in
  168. ignore|none)
  169. rvm_debug "Skipping installing rubygems."
  170. return 0
  171. ;;
  172. esac
  173. can_switch_rubygems ||
  174. rubygems_fatal_error "RVM can not install rubygems for maglev and older jruby, select other ruby and try again." $?
  175. __rvm_warn_on_rubyopt
  176. true ${rvm_ruby_selected_flag:=0}
  177. unset RUBYOPT
  178. (( rvm_ruby_selected_flag == 0 )) || __rvm_select
  179. rubygems_select_version "$version" || return $?
  180. rvm_debug "rubygems_setup_rubygems version:${rvm_rubygems_version} rvm_gem_package_name:${rvm_gem_package_name} rvm_gem_url:${rvm_gem_url}"
  181. rvm_debug "rubygems_setup_bundler sha:${__bundler_sha} __bundler_package_name:${__bundler_package_name} __bundler_url:${__bundler_url}"
  182. if
  183. [[ "${rvm_force_flag:-0}" -eq 1 ]]
  184. then
  185. true # just let us continue with installation
  186. else
  187. \typeset __current_rubygems_version="$( rubygems_current_rubygems_version )"
  188. if
  189. __rvm_version_compare "${__current_rubygems_version}" -eq "$version"
  190. then
  191. rvm_log "Rubygems $version already available in installed ruby, skipping installation, use --force to reinstall."
  192. return 0
  193. elif
  194. __rvm_version_compare "${__current_rubygems_version}" -gt "$version"
  195. then
  196. rvm_log "Installed rubygems ${__current_rubygems_version} is newer than $version provided with installed ruby, skipping installation, use --force to force installation."
  197. return 0
  198. fi
  199. fi
  200. make_sure_jruby_can_work_with_rubygems ||
  201. rubygems_fatal_error "RVM can not install rubygems for older jruby, select other ruby and try again." $?
  202. # always cleanup!
  203. [[ -d "${rvm_src_path}/$rvm_gem_package_name" ]] ||
  204. __rvm_rm_rf "${rvm_src_path}/$rvm_gem_package_name"
  205. rvm_log "$rvm_ruby_string - #downloading $rvm_gem_package_name"
  206. "$rvm_scripts_path/fetch" "$rvm_gem_url" "${rvm_gem_package_name}.tgz" ||
  207. rubygems_fatal_error "There has been an error while trying to fetch rubygems. \nHalting the installation." $?
  208. if
  209. [[ -n "${__bundler_sha:-}" ]]
  210. then
  211. rvm_log "$rvm_ruby_string - #downloading $__bundler_package_name"
  212. "$rvm_scripts_path/fetch" "$__bundler_url" "${__bundler_package_name}.tgz" ||
  213. rubygems_fatal_error "There has been an error while trying to fetch bundler. \nHalting the installation." $?
  214. fi
  215. __rvm_log_command "rubygems.extract" "$rvm_ruby_string - #extracting $rvm_gem_package_name" \
  216. __rvm_package_extract "${rvm_archives_path}/$rvm_gem_package_name.tgz" "${rvm_src_path}" ||
  217. rubygems_fatal_error "There has been an error while trying to extract rubygems. \nHalting the installation." $?
  218. if
  219. [[ -n "${__bundler_sha:-}" ]]
  220. then
  221. __rvm_log_command "rubygems.bundler.extract" "$rvm_ruby_string - #extracting $__bundler_package_name" \
  222. __rvm_package_extract "${rvm_archives_path}/$__bundler_package_name.tgz" "${rvm_src_path}" "*/spec/*" ||
  223. rubygems_fatal_error "There has been an error while trying to extract bundler. \nHalting the installation." $?
  224. \rm -rf "${rvm_src_path}/$rvm_gem_package_name/bundler" &&
  225. ln -nfs "${rvm_src_path}/$__bundler_package_name/" "${rvm_src_path}/$rvm_gem_package_name/bundler" ||
  226. rubygems_fatal_error "There has been an error while linking bundler. \nHalting the installation." $?
  227. fi
  228. __rvm_log_command "rubygems.remove" "$rvm_ruby_string - #removing old rubygems" rubygems_remove "${rvm_ruby_binary}"
  229. __rvm_cd "${rvm_src_path}/$rvm_gem_package_name"
  230. \typeset -a __command
  231. __command=(
  232. GEM_HOME="$rvm_gems_path/${rvm_ruby_string}@global" GEM_PATH="" "${rvm_ruby_binary}" -d "${rvm_src_path}/$rvm_gem_package_name/setup.rb"
  233. )
  234. if [[ ${rvm_debug_flag:-0} == 1 || ${rvm_trace_flag:-0} == 1 ]]
  235. then __command+=( --verbose )
  236. fi
  237. if
  238. (( ${rvm_docs_flag:-0} == 0 ))
  239. then
  240. if __rvm_version_compare "$version" -ge 2.2
  241. then __command+=( --no-document )
  242. else __command+=( --no-ri --no-rdoc )
  243. fi
  244. fi
  245. __setup_lang_fallback
  246. __rvm_log_command "rubygems.install" \
  247. "$rvm_ruby_string - #installing $rvm_gem_package_name" \
  248. "${__command[@]}" ||
  249. return $?
  250. [[ -e "$rvm_ruby_home/bin/gem" ]] ||
  251. {
  252. \typeset program_suffix
  253. program_suffix="$( __rvm_ruby_config_get configure_args "${rvm_ruby_binary}" )"
  254. case "${program_suffix:-}" in
  255. (*--program-suffix=*)
  256. program_suffix="${program_suffix#*--program-suffix=}"
  257. program_suffix="${program_suffix%%[\' ]*}"
  258. __rvm_log_command "link.gem" "$rvm_ruby_string - #linking gem${program_suffix} -> gem" \
  259. ln -s "$rvm_ruby_home/bin/gem${program_suffix}" "$rvm_ruby_home/bin/gem"
  260. ;;
  261. esac
  262. }
  263. true # for osx
  264. }
  265. rubygems_validate_gemfile_extract()
  266. (
  267. \command \mkdir -p "$2/gem" &&
  268. \command \tar -C "$2" -xf "$1" --touch &&
  269. \command \cd "$2" &&
  270. \command \gzip -d "metadata.gz" &&
  271. \command \tar -C gem -xzf "data.tar.gz" --touch &&
  272. \command \rm "data.tar.gz" ||
  273. {
  274. \typeset ret=$?
  275. rvm_error "Error extracting '$1' to '$2'."
  276. \command \rm -rf "${rvm_tmp_path}/rg$$"
  277. return $ret
  278. }
  279. )
  280. # Adapted from: https://gist.github.com/4678778
  281. rubygems_validate_gemfile()
  282. (
  283. gem_file="$1"
  284. downloaded_file="${gem_file##*/}"
  285. gem__url="${2:-https://d2chzxaqi4y7f8.cloudfront.net/gems/$downloaded_file}"
  286. dir_local_copy="${downloaded_file%.gem}--local"
  287. dir_remote_copy="${downloaded_file%.gem}-remote"
  288. mkdir -p "${rvm_tmp_path}/rg$$"
  289. cd "${rvm_tmp_path}/rg$$"
  290. __rvm_curl "$gem__url" -o "$downloaded_file" &&
  291. [[ -f "$downloaded_file" ]] ||
  292. {
  293. \typeset ret=$?
  294. rvm_error "Could not download '$gem__url'."
  295. \command \rm -rf "${rvm_tmp_path}/rg$$"
  296. return $ret
  297. }
  298. rubygems_validate_gemfile_extract "$gem_file" "$dir_local_copy" || return $?
  299. rubygems_validate_gemfile_extract "$downloaded_file" "$dir_remote_copy" || return $?
  300. # compare
  301. diff -ru "$dir_local_copy" "$dir_remote_copy"
  302. # cleanup
  303. \command \rm -rf "${rvm_tmp_path}/rg$$"
  304. )
  305. rubygems_validate_list_gemdirs()
  306. {
  307. for gemdir in "$@"
  308. do
  309. __rvm_readlink_deep gemdir
  310. echo "${gemdir}"
  311. done | sort -u
  312. }
  313. # Adapted from: https://gist.github.com/4678189
  314. rubygems_validate()
  315. {
  316. __rvm_which openssl > /dev/null ||
  317. rubygems_fatal_error "'openssl' was not found, please install it."
  318. \typeset gemdir gemfile url gem loc rem log
  319. \typeset -a gemdirs
  320. rvm_log "Validating RubyGems... This will take a while..."
  321. __rvm_read_lines gemdirs <(
  322. rubygems_validate_list_gemdirs "$@"
  323. )
  324. (( ${#gemdirs[@]} )) ||
  325. __rvm_read_lines gemdirs <(
  326. rubygems_validate_list_gemdirs "$rvm_gems_path"/*/cache
  327. )
  328. for gemdir in "${gemdirs[@]}"
  329. do
  330. for gem in "$gemdir"/*.gem
  331. do
  332. gemfile="${gem##*/}"
  333. url="https://d2chzxaqi4y7f8.cloudfront.net/gems/$gemfile"
  334. printf "%b" "${rvm_notify_clr}${gem}${rvm_reset_clr}"
  335. loc=$(openssl md5 $gem |cut -d" " -f2)
  336. rem=$(__rvm_curl -3 -s -D - -X HEAD -H 'Connection:close' "$url" | __rvm_grep 'ETag' | cut -d'"' -f2)
  337. [[ $loc == $rem ]] ||
  338. {
  339. printf "%b" " "
  340. log="${rvm_log_path}/$gemfile.diff"
  341. rubygems_validate_gemfile "$gem" "$url" > "$log"
  342. printf "%b" "${rvm_error_clr}mismatch, local: $loc, remote: $rem${rvm_reset_clr}, differences recorded in '$log'"
  343. }
  344. printf "%b" ", "
  345. done
  346. done
  347. rvm_log "\nDone verifying gems."
  348. }