gemset 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #!/usr/bin/env bash
  2. __rvm_current_gemset()
  3. {
  4. # Fetch the current gemset via GEM_HOME
  5. \typeset current_gemset
  6. current_gemset="${GEM_HOME:-}"
  7. # We only care about the stuff to the right of the separator.
  8. current_gemset="${current_gemset##*${rvm_gemset_separator:-@}}"
  9. if [[ "${current_gemset}" == "${GEM_HOME:-}" ]] ; then
  10. echo ''
  11. else
  12. echo "${current_gemset}"
  13. fi
  14. }
  15. __rvm_using_gemset_globalcache()
  16. {
  17. __rvm_db_ "$rvm_user_path/db" "use_gemset_globalcache" |
  18. __rvm_grep '^true$' >/dev/null 2>&1
  19. return $?
  20. }
  21. __rvm_remove_broken_symlinks()
  22. {
  23. if [[ ! -e "$1" && -L "$1" ]]
  24. then __rvm_rm_rf "$1"
  25. fi
  26. }
  27. gemset_create()
  28. {
  29. \typeset gem_home gemset gemsets prefix
  30. [[ -n "$rvm_ruby_string" ]] || __rvm_select
  31. prefix="${rvm_ruby_gem_home%%${rvm_gemset_separator:-"@"}*}"
  32. for gemset in "$@"
  33. do
  34. if
  35. [[ -z "$rvm_ruby_string" || "$rvm_ruby_string" == "system" ]]
  36. then
  37. rvm_error "Can not create gemset when using system ruby. Try 'rvm use <some ruby>' first."
  38. return 1
  39. elif
  40. [[ "$gemset" == *"${rvm_gemset_separator:-"@"}"* ]]
  41. then
  42. rvm_error "Can not create gemset '$gemset', it contains a \"${rvm_gemset_separator:-"@"}\"."
  43. return 2
  44. elif
  45. [[ "$gemset" == *"${rvm_gemset_separator:-"@"}" ]]
  46. then
  47. rvm_error "Can not create gemset '$gemset', Missing name. "
  48. return 3
  49. fi
  50. gem_home="${prefix}${gemset:+${rvm_gemset_separator:-"@"}}${gemset}"
  51. __rvm_remove_broken_symlinks "$gem_home"
  52. [[ -d "$gem_home/bin" ]] || mkdir -p "$gem_home/bin"
  53. if
  54. [[ ! -d "$gem_home/bin" ]]
  55. then
  56. rvm_error "Can not create gemset '$gemset', permissions problem? "
  57. return 4
  58. fi
  59. : rvm_gems_cache_path:${rvm_gems_cache_path:=${rvm_gems_path:-"$rvm_path/gems"}/cache}
  60. # When the globalcache is enabled, we need to ensure we setup the cache directory correctly.
  61. if
  62. __rvm_using_gemset_globalcache
  63. then
  64. if [[ -d "$gem_home/cache" && ! -L "$gem_home/cache" ]]
  65. then \command \mv -n "$gem_home/cache"/*.gem "$rvm_gems_cache_path/" 2>/dev/null
  66. fi
  67. __rvm_rm_rf "$gem_home/cache"
  68. ln -fs "$rvm_gems_cache_path" "$gem_home/cache"
  69. else
  70. __rvm_remove_broken_symlinks "$gem_home/cache"
  71. mkdir -p "$gem_home/cache"
  72. fi
  73. rvm_log "$rvm_ruby_string - #gemset created $gem_home"
  74. if
  75. (( ${rvm_skip_gemsets_flag:-0} == 0 ))
  76. then
  77. __rvm_with "${rvm_ruby_string}${gemset:+@}${gemset}" gemset_initial ${gemset:-default}
  78. fi
  79. done
  80. if
  81. (( ${rvm_skip_gemsets_flag:-0} != 0 ))
  82. then
  83. rvm_log "Skipped importing default gemsets"
  84. fi
  85. }
  86. __rvm_parse_gems_args()
  87. {
  88. \typeset gem="${*%%;*}"
  89. if
  90. __rvm_string_match "$gem" "*.gem$"
  91. then
  92. gem_name="$(basename "${gem/.gem/}" | __rvm_awk -F'-' '{$NF=NULL;print}')"
  93. gem_version="$(basename "${gem/.gem/}" | __rvm_awk -F'-' '{print $NF}' )"
  94. else
  95. gem_name="${gem/ */}"
  96. case "$gem" in
  97. *--version*)
  98. gem_version=$(
  99. echo "$gem" | __rvm_sed -e 's#.*--version[=]*[ ]*##' | __rvm_awk '{print $1}'
  100. )
  101. ;;
  102. *-v*)
  103. gem_version=$(
  104. echo "$gem" | __rvm_sed -e 's#.*-v[=]*[ ]*##' | __rvm_awk '{print $1}'
  105. )
  106. ;;
  107. esac
  108. fi
  109. }
  110. is_gem_installed()
  111. {
  112. \typeset gem_spec
  113. gem_spec="gem '$gem_name'"
  114. if
  115. [[ -n "${gem_version}" ]]
  116. then
  117. gem_spec+=", '$gem_version'"
  118. version_check="${gem_version#*=}"
  119. else
  120. version_check="*([[:digit:]\.])"
  121. fi
  122. __rvm_ls -ld "${rvm_ruby_gem_home:-$GEM_HOME}/gems"/${gem_name}-${version_check} >/dev/null 2>&1 ||
  123. "${rvm_ruby_binary}" -rrubygems -e "$gem_spec" 2>/dev/null ||
  124. return $?
  125. }
  126. # Install a gem
  127. gem_install()
  128. {
  129. \typeset gem_name gem_version version_check
  130. gem_version="" # no version
  131. __rvm_parse_gems_args "$@"
  132. if [[ -z "${gem_version}" ]]
  133. then __rvm_db "gem_${gem_name}_version" "gem_version"
  134. fi
  135. if
  136. (( ${rvm_force_flag:-0} == 0 )) && is_gem_installed
  137. then
  138. rvm_log "gem ${gem_name} ${gem_version:-} is already installed"
  139. return 0
  140. else
  141. gem_install_force || return $?
  142. fi
  143. true # for OSX
  144. }
  145. gem_install_force()
  146. {
  147. \typeset __available_gem
  148. \typeset -a install_params
  149. install_params=()
  150. __available_gem="$( __rvm_ls -v1 "${rvm_path}/gem-cache"/${gem_name}-${version_check}.gem 2>/dev/null | tail -n 1 )"
  151. if [[ -n "${__available_gem}" ]]
  152. then install_params+=( --local )
  153. elif [[ -n "${gem_version}" ]]
  154. then install_params+=( -v "${gem_version}" )
  155. fi
  156. if __rvm_version_compare "$(\command \gem --version)" -ge 2.2
  157. then install_params+=( --no-document )
  158. else install_params+=( --no-ri --no-rdoc )
  159. fi
  160. for __gem_option in ${rvm_gem_options}
  161. do
  162. case "${__gem_option}" in
  163. (--no-ri|--no-rdoc|--no-document)
  164. # skip
  165. ;;
  166. (*)
  167. install_params+=( "${__gem_option}" )
  168. ;;
  169. esac
  170. done
  171. __rvm_log_command \
  172. "gem.install.${gem_name}${gem_version:+-}${gem_version:-}" \
  173. "installing gem ${__available_gem:-${gem_name}} ${install_params[*]}" \
  174. \command \gem install "${__available_gem:-${gem_name}}" "${install_params[@]}" ||
  175. return $?
  176. }
  177. __rvm_lines_without_comments()
  178. {
  179. __rvm_remove_from_array lines "#*|+( )" "${lines[@]}"
  180. }
  181. __rvm_lines_without_gems()
  182. {
  183. [[ -n "${rvm_without_gems}" ]] || return 0
  184. \typeset -a __gems_to_remove
  185. __gems_to_remove=()
  186. __rvm_custom_separated_array __gems_to_remove " " "${rvm_without_gems}"
  187. (( ${#__gems_to_remove[@]} )) || return 0
  188. \typeset __gem
  189. for __gem in "${__gems_to_remove[@]}"
  190. do __rvm_remove_from_array lines "${__gem}|${__gem% *} *" "${lines[@]}"
  191. done
  192. }
  193. __rvm_lines_with_gems()
  194. {
  195. \typeset -a __gems_to_add
  196. __gems_to_add=()
  197. case "${1}" in
  198. (global) __rvm_custom_separated_array __gems_to_add " " "${rvm_with_gems:-}" ;;
  199. (default) __rvm_custom_separated_array __gems_to_add " " "${rvm_with_default_gems:-}" ;;
  200. (*) return 0 ;;
  201. esac
  202. (( ${#__gems_to_add[@]} )) || return 0
  203. \typeset __gem __version
  204. for __gem in "${__gems_to_add[@]}"
  205. do
  206. __version="${__gem#*=}"
  207. __gem="${__gem%%=*}"
  208. if [[ "${__gem}" == "${__version}" ]] # no version given
  209. then lines+=( "${__gem}" )
  210. else lines+=( "${__gem} -v ${__version}" )
  211. fi
  212. done
  213. }
  214. gemset_import_list()
  215. {
  216. case "${rvm_ruby_string}" in
  217. (*jruby*)
  218. # jruby is very slow to install gems one by one, install everything in one call
  219. \command \gem install "$@"
  220. ;;
  221. (*)
  222. \typeset line
  223. for line
  224. do gem_install $line || rvm_error "there was an error installing gem $line"
  225. done
  226. ;;
  227. esac
  228. }
  229. gemset_import()
  230. {
  231. \typeset __prefix rvm_file_name
  232. unset -f gem
  233. __rvm_select
  234. __prefix="$1"
  235. if
  236. [[ -n "${2:-}" ]]
  237. then
  238. rvm_file_name="${2%.gems*}.gems"
  239. else
  240. \typeset -a gem_file_names
  241. gem_file_names=( "${rvm_gemset_name}.gems" "default.gems" "system.gems" ".gems" )
  242. __rvm_find_first_file rvm_file_name "${gem_file_names[@]}" ||
  243. {
  244. rvm_error "No *.gems file found."
  245. return 1
  246. }
  247. fi
  248. [[ -d "$rvm_ruby_gem_home/specifications/" ]] || mkdir -p "$rvm_ruby_gem_home/specifications/"
  249. [[ -d "$rvm_gems_cache_path" ]] || mkdir -p "$rvm_gems_cache_path" # Ensure the base cache dir is initialized.
  250. \typeset -a lines
  251. lines=()
  252. if
  253. [[ -s "$rvm_file_name" ]]
  254. then
  255. __rvm_read_lines lines "${rvm_file_name}"
  256. __rvm_lines_without_comments
  257. fi
  258. rvm_debug "lines from ${rvm_file_name}: ${lines[*]}"
  259. if
  260. [[ -n "${3:-}" ]]
  261. then
  262. __rvm_lines_without_gems
  263. __rvm_lines_with_gems "${3}"
  264. rvm_debug "recalculated lines($3): ${lines[*]}"
  265. fi
  266. if
  267. (( ${#lines[@]} ))
  268. then
  269. __rvm_log_command "gemsets.import${3:+.}${3:-}" \
  270. "${__prefix} $rvm_file_name" \
  271. gemset_import_list "${lines[@]}"
  272. else
  273. rvm_log "${__prefix}file $rvm_file_name evaluated to empty gem list"
  274. fi
  275. }
  276. # Loads the default gemsets for the current interpreter and gemset.
  277. gemset_initial()
  278. {
  279. \typeset gemsets gemset _iterator paths _jruby_opts
  280. _jruby_opts=$JRUBY_OPTS
  281. export JRUBY_OPTS="${JRUBY_OPTS} --dev"
  282. true ${rvm_gemsets_path:="$rvm_path/gemsets"}
  283. [[ -d "$rvm_gems_path/${rvm_ruby_string}/cache" ]] ||
  284. mkdir -p "$rvm_gems_path/${rvm_ruby_string}/cache" 2>/dev/null
  285. __rvm_ensure_has_environment_files
  286. paths=( $( __rvm_ruby_string_paths_under "$rvm_gemsets_path" | sort -r ) )
  287. for _iterator in "${paths[@]}"
  288. do
  289. if
  290. [[ -f "${_iterator}/$1.gems" ]]
  291. then
  292. gemset_import "$rvm_ruby_string - #importing gemset" "${_iterator}/$1.gems" "$1"
  293. break # stop right here
  294. else
  295. rvm_debug "$rvm_ruby_string - #gemset definition does not exist ${_iterator}/$1.gems"
  296. fi
  297. done
  298. __rvm_log_command "gemset.wrappers.$1" \
  299. "$rvm_ruby_string - #generating ${1} wrappers" \
  300. run_gem_wrappers regenerate 2>/dev/null || true
  301. export JRUBY_OPTS=${_jruby_opts}
  302. }
  303. run_gem_wrappers()
  304. {
  305. gem_install gem-wrappers >/dev/null &&
  306. gem wrappers "$@" ||
  307. return $?
  308. }
  309. __rvm_rubygems_create_link()
  310. {
  311. \typeset ruby_lib_gem_path
  312. \command \mkdir -p "$rvm_ruby_gem_home/bin"
  313. rubygems_detect_ruby_lib_gem_path "${1:-ruby}" || return 0
  314. if [[ -L "$ruby_lib_gem_path" && -w "$ruby_lib_gem_path" ]]
  315. then rm -rf "$ruby_lib_gem_path"
  316. fi
  317. if [[ -e "$rvm_ruby_global_gems_path" && ! -L "$rvm_ruby_global_gems_path" ]]
  318. then rm -rf "$rvm_ruby_global_gems_path"
  319. fi
  320. [[ -d "$ruby_lib_gem_path" ]] ||
  321. \command \mkdir -p "$ruby_lib_gem_path"
  322. if
  323. [[ -w "$ruby_lib_gem_path" ]]
  324. then
  325. [[ -L "$rvm_ruby_global_gems_path" ]] ||
  326. ln -fs "$ruby_lib_gem_path" "$rvm_ruby_global_gems_path"
  327. else
  328. [[ -d "$rvm_ruby_global_gems_path" ]] ||
  329. \command \mkdir -p "$rvm_ruby_global_gems_path"
  330. fi
  331. \command \mkdir -p "$rvm_ruby_global_gems_path/bin"
  332. }
  333. __rvm_set_executable()
  334. {
  335. for __file
  336. do [[ -x "${__file}" ]] || chmod +x "${__file}"
  337. done
  338. }
  339. __rvm_initial_gemsets_create()
  340. {
  341. __rvm_initial_gemsets_setup "$1" &&
  342. __rvm_initial_gemsets_create_gemsets
  343. }
  344. __rvm_initial_gemsets_setup()
  345. {
  346. __rvm_log_command "chmod.bin" \
  347. "$rvm_ruby_string - #making binaries executable" \
  348. __rvm_set_executable "$rvm_ruby_home/bin"/* &&
  349. __rvm_rubygems_create_link "$1" &&
  350. (
  351. rvm_ruby_binary="${1:-$rvm_ruby_home/bin/ruby}" \
  352. rubygems_setup ${rvm_rubygems_version:-latest}
  353. ) # () for exit in rubygems_fatal_error
  354. }
  355. __rvm_initial_gemsets_create_without_rubygems()
  356. {
  357. __rvm_rubygems_create_link "$1" &&
  358. __rvm_initial_gemsets_create_gemsets
  359. }
  360. __rvm_initial_gemsets_create_gemsets()
  361. {
  362. gemset_create "global" &&
  363. __rvm_with "${rvm_ruby_string}@global" __rvm_remove_without_gems &&
  364. gemset_create ""
  365. }
  366. __rvm_remove_without_gems()
  367. {
  368. [[ -n "${rvm_without_gems}" ]] || return 0
  369. \typeset -a __gems_to_remove __extra_flags
  370. __rvm_read_lines __gems_to_remove <(
  371. GEM_PATH="$GEM_HOME" __rvm_list_gems "" "${rvm_without_gems}"
  372. )
  373. (( ${#__gems_to_remove[@]} )) || return 0
  374. __extra_flags=()
  375. if __rvm_version_compare "$(\command \gem --version)" -ge 2.1.0
  376. then __extra_flags+=( --abort-on-dependent )
  377. fi
  378. \typeset __gem __name __version
  379. for __gem in "${__gems_to_remove[@]}"
  380. do
  381. __name="${__gem% *}"
  382. __version="${__gem##* }"
  383. __rvm_log_command "gem.uninstall.${__name}-${__version}" \
  384. "$rvm_ruby_string - #uninstalling gem ${__name}-${__version}" \
  385. \command \gem uninstall "${__name}" -v "${__version}" -x "${__extra_flags[@]}" || true
  386. done
  387. }
  388. gemset_reset_env()
  389. (
  390. export rvm_internal_use_flag=1
  391. export rvm_use_flag=0
  392. __rvm_use "${1:-}"
  393. __rvm_ensure_has_environment_files &&
  394. run_gem_wrappers regenerate ||
  395. return $?
  396. )
  397. # Transform the list of gems one version per line
  398. __rvm_list_gems()
  399. {
  400. \typeset __checks __names
  401. __checks="${1:-}"
  402. shift || true
  403. __names="$*"
  404. if [[ -n "${__names}" ]]
  405. then __checks="%w{${__names}}.include?(gem.name)${__checks:+" && ( ${__checks} )"}"
  406. fi
  407. if [[ -n "${__checks}" ]]
  408. then __checks="if ${__checks}"
  409. fi
  410. rvm_debug "gem list check: ${__checks}"
  411. ruby -rrubygems -e "
  412. Gem::Specification.each{|gem|
  413. puts \"#{gem.name} #{gem.version}\" ${__checks}
  414. }
  415. " 2>/dev/null ||
  416. gem list $@ |
  417. __rvm_sed '/\*\*\*/ d ; /^$/ d; s/ (/,/; s/, /,/g; s/)//;' |
  418. __rvm_awk -F ',' '{for(i=2;i<=NF;i++) print $1" "$i }'
  419. }
  420. gemset_pristine()
  421. {
  422. if
  423. (
  424. unset -f gem
  425. builtin command -v gem > /dev/null
  426. )
  427. then
  428. \typeset _gem _version _platforms
  429. \typeset -a _failed _pristine_command
  430. _failed=()
  431. _pristine_command=( \command \gem pristine )
  432. if __rvm_version_compare "$(\command \gem --version)" -ge 2.2.0
  433. then _pristine_command+=( --extensions )
  434. fi
  435. rvm_log "Restoring gems to pristine condition..."
  436. while
  437. read _gem _version _platforms
  438. do
  439. printf "%b" "${_gem}-${_version} "
  440. "${_pristine_command[@]}" "${_gem}" --version "${_version}" >/dev/null ||
  441. _failed+=( "${_gem} --version ${_version}" )
  442. done <<<"$(
  443. GEM_PATH="$GEM_HOME" __rvm_list_gems \
  444. "${pristine_gems_filter:-"! gem.executables.empty? || ! gem.extensions.empty?"}"
  445. )"
  446. if
  447. (( ${#_failed[@]} > 0 ))
  448. then
  449. rvm_error "\n'${_pristine_command[*]} ${_failed[*]}' failed, you need to fix these gems manually."
  450. return 1
  451. else
  452. rvm_log "\nfinished."
  453. fi
  454. else
  455. rvm_error "'gem' command not found in PATH."
  456. return 1
  457. fi
  458. }
  459. __rvm_gemset_pristine()
  460. {
  461. __rvm_log_command "gemset.pristine-$1" "Making gemset $1 pristine" \
  462. __rvm_with "$1" gemset_pristine
  463. }