-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_prvm
159 lines (144 loc) · 3.89 KB
/
bash_prvm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- mode: shell-script -*-
[[ -n "${debug}" ]] && echo "${BASH_SOURCE}"
#
# File of functions specifically for prvm
#
# prvm is my replacement for rvm. I found rvm way too heavy and
# invasive. The directory structure is simple. I will use
# /usr/local/prvm as the base:
#
# /usr/local/prvm:
# download-images
# ruby-1.9.1-p376.tar.gz
# ruby-1.9.3-p194.tar.gz
# ... (the original compressed tarballs)
# ruby-1.9.1-p376
# bin
# include
# lib
# share
# src
# This is your build directory and make
# --prefix=/usr/local/prvm/ruby-1.9.1-p376
# ruby-1.9.3-p194
# bin
# include
# lib
# share
# src
# This is your build directory and make
# --prefix=/usr/local/prvm/ruby-1.9.3-p194
# ...
#
# The scheme revolves around bundler and putting the gems for a
# particular project into .bundle/gems and their associated executable
# into .bundle/bin. This idea comes from:
# http://tomafro.net/2012/06/tip-bundler-with-binstubs
#
# GEM_HOME would thus be .bundle/gems/ruby/1.9.1 (except made into an
# absolute path)
#
# There will be a path for the base of the prvm versions of ruby --
# typically set to /usr/local/prvm.
#
# Thus a single line such as:
#
# prvm_use ruby-1.9.3-p194
#
# would set PATH to start with the absolute path to the project with
# .bundle/bin appeneded followed by
# /usr/local/prvm/ruby-1.9.3-p194/bin
#
# GEM_HOME would be set as mentioned above with GEM_PATH set to
# /usr/local/prvm/ruby-1.9.3-p194/lib/ruby/gems/1.9.1
#
# Some type of options are probably needed to add or alter to these
# defaults but until I need them, I won't define them.
PRVM_BASE=/usr/local/prvm
# $1 is a regular expression of the elements of PATH to delete
function delete-path-elements
{
typeset OIFS=$IFS
typeset path
typeset NPATH
IFS=:
for path in $PATH ; do
if [[ "$path" =~ $1 ]] ; then
true
elif [[ -z "$NPATH" ]] ; then
NPATH="$path"
else
NPATH="$NPATH:$path"
fi
done
PATH="$NPATH"
IFS=$OIFS
}
# $1 is an absolute path to prepend to PATH keeping '.' as the first
# element if it started out that way
function prepend-path
{
local flag
if [[ "${PATH%%:*}" == "." ]] ; then
flag=t
PATH="${PATH#*:}"
fi
delete-path-elements "^${1%/}$"
PATH="${1%/}:${PATH}"
if [[ "$flag" ]] ; then
PATH=".:$PATH"
fi
}
# $1 specifies which ruby to use and $2 specifies the directory
function prvm_setup
{
typeset ruby="$1"
typeset prvmrc_path="$2"
typeset cwd="${PWD}"
typeset prvmrc_dir
typeset temp
if [[ "$prvmrc_path" =~ ^/.* ]] ; then
true
else
prvmrc_path="${PWD}/${prvmrc_path}"
fi
# This is silly but I can't resist...
# strip out /./ in path.
prvmrc_path="$( echo "$prvmrc_path" | sed -e 's%/\./%/%g' )"
# strip out /foo/../ in path
while : ; do
typeset temp="$( echo "$prvmrc_path" | sed -e 's%/[^/]*/\.\./%/%' )"
if [[ "$temp" == "$prvmrc_path" ]] ; then
break
fi
prvmrc_path="$temp"
done
prvmrc_dir=${prvmrc_path%/.*}
# We want to delete what a previous prvm_use added. This would be
# some path to /**/.bundle/bin as well as /usr/local/prvm/**
delete-path-elements '.*/\.bundle/bin$'
delete-path-elements "${PRVM_BASE}/.*"
cd "${prvmrc_dir}"
# Now add in the new path pieces
prepend-path "${PRVM_BASE}/${ruby}/bin"
if type -t bundle > /dev/null && bundle exec true > /dev/null 2>&1 ; then
temp=$( bundle exec env |
egrep 'GEM_HOME' |
sed -e 's%^%export %' -e "s%=%='%" -e "s%$%';%" )
eval $temp
fi
prepend-path "${GEM_HOME}/bin"
prepend-path "${prvmrc_dir}/.bundle/bin"
if [[ -d "${prvmrc_dir}/bin" ]] ; then
prepend-path "${prvmrc_dir}/bin"
fi
}
# $1 specifies which ruby to use. e.g. prvm_use ruby-1.9.3-p194
function prvm_use
{
prvm_setup "$1" "${BASH_SOURCE[1]}"
}
function bundle_install
{
bundle install --path .bundle/gems --binstubs .bundle/bin
}