Module: Rip::Setup

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

Constant Summary collapse

WEBSITE =

config

"http://hellorip.com/"
STARTUP_SCRIPTS =
%w( .bash_profile .bash_login .bashrc .zshrc .profile )
HOME =
File.expand_path('~')
USER =
HOME.split('/')[-1]
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')
BINDIR =

caution: RbConfig::CONFIG does NOT work for me on OS X

File.join('/', 'usr', 'local', 'bin')

Instance Method Summary collapse

Instance Method Details

#check_installationObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rip/setup.rb', line 160

def check_installation
  script = startup_script

  if !File.read(script).include? 'RIPDIR='
    raise "no env variables in startup script"
  end

  if ENV['RIPDIR'].to_s.empty?
    if startup_script.empty?
      raise "no $RIPDIR."
    else
      raise "no $RIPDIR. you may need to run `source #{startup_script}`"
    end
  end

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

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

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

  true
end

#finish_setupObject



105
106
107
# File 'lib/rip/setup.rb', line 105

def finish_setup
  ui.puts finish_setup_banner(startup_script)
end

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rip/setup.rb', line 109

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

  Run `rip check` to be sure Rip installed successfully

  NOTE: You may need to source your #{script}
        or start a new shell session.

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

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

#installObject

setup steps



34
35
36
37
38
39
40
# File 'lib/rip/setup.rb', line 34

def install
  install_libs
  install_binary
  setup_ripenv
  setup_startup_script
  finish_setup
end

#install_binary(verbose = false) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rip/setup.rb', line 69

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
  end
end

#install_libs(verbose = false) ⇒ Object



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

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)


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

def installed?
  check_installation
  true
rescue
  false
end

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



77
78
79
80
81
82
83
84
# File 'lib/rip/setup.rb', line 77

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_scriptObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rip/setup.rb', line 86

def setup_startup_script
  script = startup_script

  if script.empty?
    ui.puts "rip: please create one of these startup scripts in $HOME:"
    ui.puts startup_scripts.map { |s| '  ' + s }
    exit
  end

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

#startup_scriptObject



145
146
147
148
149
150
151
# File 'lib/rip/setup.rb', line 145

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

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

#startup_script_templateObject



141
142
143
# File 'lib/rip/setup.rb', line 141

def startup_script_template
  STARTUP_SCRIPT_TEMPLATE % RIPDIR
end

#transaction(message, &block) ⇒ Object

helper methods



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rip/setup.rb', line 129

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



190
191
192
# File 'lib/rip/setup.rb', line 190

def ui
  Rip.ui
end

#uninstall(verbose = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rip/setup.rb', line 42

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

  # just in case...
  `gem uninstall rip 2&> /dev/null`

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