Module: Rip::Setup

Extended by:
Setup
Included in:
Setup
Defined in:
lib/rip/setup.rb

Defined Under Namespace

Classes: InstallationError, StaleEnvironmentError

Constant Summary collapse

WEBSITE =

config

"http://hellorip.com/"
STARTUP_SCRIPTS =
%w( .bash_profile .bash_login .bashrc .zshenv .profile .zshrc )
FISH_STARTUP_SCRIPT =
".config/fish/config.fish"
HOME =
File.expand_path('~')
USER =

TODO: cough

HOME.split('/')[-1]
BINDIR =

Work around Apple’s Ruby.

if defined? RUBY_FRAMEWORK_VERSION
  File.join("/", "usr", "bin")
else
  RbConfig::CONFIG["bindir"]
end
LIBDIR =
RbConfig::CONFIG['sitelibdir']
RIPDIR =
File.expand_path(ENV['RIPDIR'] || File.join(HOME, '.rip'))
RIPROOT =
File.expand_path(File.join(__DIR__, '..', '..'))
RIPINSTALLDIR =
File.join(LIBDIR, 'rip')

Instance Method Summary collapse

Instance Method Details

#check_installationObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rip/setup.rb', line 225

def check_installation
  if ENV['RIPDIR'].to_s.empty?
    if startup_script_contains_rip_configuration?
      raise StaleEnvironmentError, <<-end_error
No $RIPDIR. Rip has been integrated into your shell startup scripts but your
shell hasn't yet picked up the changes.

To complete the installation process please restart your shell or run:
  source #{startup_script}
end_error
    else
      raise InstallationError, <<-end_error
No $RIPDIR. Rip hasn't been integrated into your shell startup scripts yet.
Please run `rip setup` to do so.
end_error
    end
  end

  if !File.exists? File.join(BINDIR, 'rip')
    raise InstallationError, "no rip in #{BINDIR}"
  end

  if !File.exists? File.join(LIBDIR, 'rip')
    raise InstallationError, "no rip in #{LIBDIR}"
  end

  if !File.exists? File.join(LIBDIR, 'rip')
    raise InstallationError, "no rip.rb in #{LIBDIR}"
  end

  true
end

#finish_setupObject



156
157
158
# File 'lib/rip/setup.rb', line 156

def finish_setup
  ui.puts finish_setup_banner(startup_script)
end

#finish_setup_banner(script = "~/.bash_profile") ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rip/setup.rb', line 160

def finish_setup_banner(script = "~/.bash_profile")
  <<-EOI.gsub(/^ +/, "")
  ****************************************************
  So far so good...

  Rip needs certain env variables to run. We've tried
  to install them automatically but may have failed.

  Run `rip check` to check the status of your
  installation.

  Get started: `rip -h` or #{WEBSITE}

  ****************************************************
  EOI
end

#fish?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/rip/setup.rb', line 214

def fish?
  File.exists?(File.join(HOME, FISH_STARTUP_SCRIPT))
end

#fish_startup_scriptObject



210
211
212
# File 'lib/rip/setup.rb', line 210

def fish_startup_script
  FISH_STARTUP_SCRIPT if fish?
end

#installObject

setup steps



52
53
54
55
56
57
58
# File 'lib/rip/setup.rb', line 52

def install
  install_libs
  install_binary
  setup_ripenv
  setup_startup_script
  finish_setup
end

#install_binary(verbose = false) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rip/setup.rb', line 98

def install_binary(verbose = false)
  transaction "installing rip binary" do
    src = File.join(RIPROOT, 'bin', 'rip')
    dst = File.join(BINDIR, 'rip')
    FileUtils.cp src, dst, :verbose => verbose, :preserve => true
    FileUtils.chmod(0755, dst)

    ruby_bin = File.expand_path(File.join(BINDIR, RbConfig::CONFIG['ruby_install_name']))
    if File.exist? ruby_bin
      ui.puts "rip: using Ruby bin: #{ruby_bin}"
      rewrite_bang_line(dst, "#!#{ruby_bin}")
    end
  end
end

#install_libs(verbose = false) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/rip/setup.rb', line 89

def install_libs(verbose = false)
  transaction "installing library files" do
    riprb = File.join(RIPROOT, 'lib', 'rip.rb')
    ripdr = File.join(RIPROOT, 'lib', 'rip')
    FileUtils.cp_r riprb, LIBDIR, :verbose => verbose
    FileUtils.cp_r ripdr, LIBDIR, :verbose => verbose
  end
end

#installed?Boolean

Returns:

  • (Boolean)


218
219
220
221
222
223
# File 'lib/rip/setup.rb', line 218

def installed?
  check_installation
  true
rescue
  false
end

#remove_libs(verbose = false) ⇒ Object



84
85
86
87
# File 'lib/rip/setup.rb', line 84

def remove_libs(verbose = false)
  FileUtils.rm_rf RIPINSTALLDIR, :verbose => verbose
  FileUtils.rm_rf File.join(LIBDIR, 'rip.rb'), :verbose => verbose
end

#rewrite_bang_line(file, first_line) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/rip/setup.rb', line 258

def rewrite_bang_line(file, first_line)
  lines = File.readlines(file)[1..-1]
  File.open(file, 'w') do |f|
    f.puts first_line
    f.puts lines.join
    f.flush
  end
end

#setup_ripenv(ripdir = RIPDIR, verbose = false) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/rip/setup.rb', line 113

def setup_ripenv(ripdir=RIPDIR, verbose = false)
  transaction "setting up ripenv" do
    FileUtils.mkdir_p File.join(ripdir, 'rip-packages')
    Rip.dir = ripdir
    Rip::Env.create 'base'
    FileUtils.chown_R USER, nil, ripdir, :verbose => verbose
  end
end

#setup_startup_script(script = nil) ⇒ Object

Modifies the shell startup script(s) and inserts the Rip configuration statements.

Returns whether a startup script has been modified. If one of the startup scripts already contain the Rip configuration statements, then nothing will be modified and false will be returned.

TODO: Requires the startup script, but probably acceptable for most? –rue



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rip/setup.rb', line 132

def setup_startup_script(script = nil)
  if script
    script = File.expand_path(script)
  else
    script = startup_script
  end

  if script.empty? || !File.exists?(script)
    ui.puts "rip: please create one of these startup scripts in $HOME and re-run:"
    ui.abort STARTUP_SCRIPTS.map { |s| '  ' + s }
  end

  if File.read(script).include? 'RIPDIR'
    ui.puts "rip: env variables already present in startup script"
    false
  else
    ui.puts "rip: adding env variables to #{script}"
    File.open(script, 'a+') do |f|
      f.puts startup_script_template
    end
    true
  end
end

#startup_scriptObject



197
198
199
200
201
202
203
# File 'lib/rip/setup.rb', line 197

def startup_script
  script = fish_startup_script || STARTUP_SCRIPTS.detect do |script|
    File.exists? file = File.join(HOME, script)
  end

  script ? File.join(HOME, script) : ''
end

#startup_script_contains_rip_configuration?Boolean

Returns:

  • (Boolean)


205
206
207
208
# File 'lib/rip/setup.rb', line 205

def startup_script_contains_rip_configuration?
  filename = startup_script
  !filename.empty? && File.read(filename).include?(startup_script_template)
end

#startup_script_templateObject



193
194
195
# File 'lib/rip/setup.rb', line 193

def startup_script_template
  (fish? ? FISH_CONFIG_TEMPLATE : STARTUP_SCRIPT_TEMPLATE) % RIPDIR
end

#transaction(message, &block) ⇒ Object

helper methods



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rip/setup.rb', line 181

def transaction(message, &block)
  ui.puts 'rip: ' + message
  block.call
rescue Errno::EACCES
  uninstall
  ui.abort "access denied. please try running again with `sudo`"
rescue => e
  ui.puts "rip: something failed, rolling back..."
  uninstall
  raise e
end

#uiObject



267
268
269
# File 'lib/rip/setup.rb', line 267

def ui
  Rip.ui
end

#uninstall(verbose = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rip/setup.rb', line 66

def uninstall(verbose = false)
  FileUtils.rm File.join(BINDIR, 'rip'), :verbose => verbose
  remove_libs verbose
  FileUtils.rm_rf RIPDIR, :verbose => verbose

  # just in case...
  gembin = ENV['GEMBIN'] || 'gem'
  `#{gembin} uninstall rip 2&> /dev/null`

  ui.abort "rip uninstalled" if verbose
rescue Errno::EACCES
  ui.abort "uninstall failed. please try again with `sudo`" if verbose
rescue Errno::ENOENT
  nil
rescue => e
  raise e if verbose
end

#upgradeObject



60
61
62
63
64
# File 'lib/rip/setup.rb', line 60

def upgrade
  remove_libs
  install_libs
  ui.puts "rip upgraded"
end