-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall-gems.sh
More file actions
executable file
·142 lines (108 loc) · 2.88 KB
/
install-gems.sh
File metadata and controls
executable file
·142 lines (108 loc) · 2.88 KB
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
#!/usr/bin/env bash
set -eEuo pipefail
function boolean-var {
variable_name=$1
default=${2:-no}
val=${!variable_name:=$default}
if [[ "n|no|f|false|off|0" =~ $val ]]; then
echo 'false'
elif [[ "y|yes|t|true|on|1" =~ $val ]]; then
echo 'true'
else
echo "Variable \$$variable_name is set to \`$val' which is not a boolean value" >&2
echo >&2
exit 1
fi
}
echo
echo "Installing gems locally"
echo "= = ="
if [ -z ${REMOVE_GEMS+x} ]; then
echo
echo "(REMOVE_GEMS is not set. Using \"on\" by default.)"
remove_gems="on"
else
remove_gems=$REMOVE_GEMS
fi
if [ -z ${POSTURE+x} ]; then
echo "(POSTURE is not set. Using \"operational\" by default.)"
posture="operational"
else
posture=$POSTURE
fi
gemspec=$(ls *.gemspec | head -n 1 || true)
if [ -z "$gemspec" ]; then
echo "No gemspec found"
exit 1
fi
gem_dir="./gems"
install_dir="$gem_dir/ruby/$(ruby -rrbconfig -e "puts RbConfig::CONFIG['ruby_version']")"
export GEM_HOME="$install_dir"
echo "Posture: $posture"
echo "Gemspec: $gemspec"
remove_gems=$(boolean-var remove_gems)
if $remove_gems; then
echo
echo "Removing installed gems"
echo "- - -"
cmd="rm -rf $gem_dir"
echo $cmd
($cmd)
fi
echo
echo "Installing gems"
echo "- - -"
cmd="gem build $gemspec --output install-gems.gem --quiet"
echo $cmd
gem_name=$($cmd | sed -E -n 's/^[[:blank:]]*Name:[[:blank:]]*(.*)$/\1/p')
cmd="gem install --no-user-install install-gems.gem"
if [ operational != "$posture" ]; then
cmd="$cmd --development"
fi
echo $cmd
($cmd)
echo
cmd="gem uninstall --executables --no-user-install $gem_name"
echo $cmd
($cmd)
echo
cmd="gem cleanup --no-user-install"
echo $cmd
($cmd)
echo
rm -vf install-gems.gem
echo
mkdir -p $gem_dir/exec
ruby -rrubygems -rpathname <<RUBY
relative_gem_dir = '$gem_dir'
gem_dir = Pathname(relative_gem_dir)
File.open(gem_dir.join('gems_init.rb'), 'w') do |gems_init_rb|
gems_init_rb.puts "# Generated by $0\n\n"
Dir['$install_dir/specifications/*.gemspec'].each do |gemspec|
spec = Gem::Specification.load(gemspec)
spec.full_require_paths.each do |full_require_path|
require_path = Pathname(full_require_path).relative_path_from(gem_dir.expand_path)
gems_init_rb.puts "\$LOAD_PATH.push(File.expand_path('#{require_path}', __dir__))"
end
spec.executables.each do |executable|
internal_executable_path = Pathname(spec.bin_file(executable)).relative_path_from(gem_dir.expand_path('exec'))
File.open(gem_dir.join('exec', executable), 'w') do |executable|
executable.write(<<-RUBY)
#!/usr/bin/env ruby
#
# Generated by install-gems.sh
require_relative '../gems_init'
load File.expand_path('#{internal_executable_path}', __dir__)
RUBY
File.chmod(0755, executable.path)
puts "Wrote executable: #{executable.path} (#{spec.name})"
end
end
end
puts "Wrote #{gems_init_rb.path}"
end
RUBY
echo
echo "(done)"
echo "- - -"
echo