public
Description: The Ruby Implementation of SWX RPC
Homepage: http://swxruby.org
Clone URL: git://github.com/meekish/swxruby.git
swxruby / install.rb
100644 69 lines (58 sloc) 2.706 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
# Taken from Railties
def gsub_file(relative_destination, regexp, *args, &block)
  path = relative_destination
  content = File.read(path).gsub(regexp, *args, &block)
  File.open(path, 'wb') { |file| file.write(content) }
end
 
SWX_RUBY_ROOT ||= File.expand_path(File.dirname(__FILE__))
readme = IO.readlines(File.join(SWX_RUBY_ROOT, 'README'))
@rails_usage = readme.slice(readme.index("=== Rails Plugin Usage\n")..-1)
 
begin
  require 'fileutils'
include FileUtils
  
# Copy config file
  unless File.exist?("#{RAILS_ROOT}/config/swx.yml")
puts '*** Copying config file to config/swx.yml ***'
    cp(File.join(SWX_RUBY_ROOT, 'lib', 'swxruby', 'rails_integration', 'swx.yml'), "#{RAILS_ROOT}/config/swx.yml")
  end
 
# Copy SWX controller
unless File.exist?("#{RAILS_ROOT}/app/controllers/swx_controller.rb")
puts '*** Copying SWX controller to app/controllers/swx_controller.rb ***'
cp(File.join(SWX_RUBY_ROOT, 'lib', 'swxruby', 'rails_integration', 'swx_controller.rb'), "#{RAILS_ROOT}/app/controllers/swx_controller.rb")
end
 
# Create services directory
unless File.exist?("#{RAILS_ROOT}/app/services")
puts '*** Creating services directory at app/services ***'
mkdir("#{RAILS_ROOT}/app/services")
end
 
unless ESSENTIALS
   # Copy TestDataTypes class to app/services
   unless File.exist?("#{RAILS_ROOT}/app/services/test_data_types.rb")
   puts '*** Copying TestDataTypes service class to app/services ***'
   cp(File.join(SWX_RUBY_ROOT, 'lib', 'swxruby', 'services', 'test_data_types.rb'), "#{RAILS_ROOT}/app/services/test_data_types.rb")
   end
 
   # Copy HelloWorld class to app/services
   unless File.exist?("#{RAILS_ROOT}/app/services/hello_world.rb")
   puts '*** Copying HelloWorld service class to app/services ***'
   cp(File.join(SWX_RUBY_ROOT, 'lib', 'swxruby', 'services', 'hello_world.rb'), "#{RAILS_ROOT}/app/services/hello_world.rb")
   end
end
 
# Add route for SWX gateway to routes.rb
puts '*** Adding route for SWX gateway to routes.rb ***'
sentinel = 'ActionController::Routing::Routes.draw do |map|'
gsub_file "#{RAILS_ROOT}/config/routes.rb", /(#{Regexp.escape(sentinel)})/mi do |match|
"#{match}\n map.swx '/swx', :controller => 'swx', :action => 'gateway'\n"
end
 
# Check for installation of JSON gem
print '*** Checking if JSON gem is installed'
require 'rubygems'
require 'json'
puts ': JSON gem detected ***'
 
puts @rails_usage
rescue LoadError
puts @rails_usage
 
puts '!!!!! You do not have the JSON gem installed. SWX Ruby will not function without it.'
puts '!!!!! Please "gem install json" to get the JSON gem, then SWX Ruby should be ready to roll.'
rescue Exception => e
  puts 'ERROR INSTALLING SWX Ruby: ' + e.message
end