Class: Rover

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
Defined in:
lib/rover.rb

Constant Summary collapse

CONFIG_FILE_NAMES =
{
  "npm" => 'package.json',
  'bundle' => 'Gemfile',
  'pip' => 'requirements.txt'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#which

Methods included from Logging

#logger, logger

Constructor Details

#initializeRover

Returns a new instance of Rover.



53
54
55
56
57
# File 'lib/rover.rb', line 53

def initialize
  @start_directory = Dir.pwd

  puts "Rover is starting in #{@start_directory}"
end

Instance Attribute Details

#start_directoryObject

Returns the value of attribute start_directory.



45
46
47
# File 'lib/rover.rb', line 45

def start_directory
  @start_directory
end

Instance Method Details

#config_env(config_type) ⇒ Object



119
120
121
122
# File 'lib/rover.rb', line 119

def config_env config_type
  return nil unless config_type
  self.send("config_env_#{config_type}")
end

#config_env_bundleObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rover.rb', line 133

def config_env_bundle
  # nothing to do
  unless which('bundle')
    exec_cmd "gem install bundler"
    unless which('bundle')
      puts "Please install Bundler (gem install bundler) to continue installing dependencies"
      return false
    end
  end

  true
end

#config_env_npmObject



124
125
126
127
128
129
130
131
# File 'lib/rover.rb', line 124

def config_env_npm
  unless which('npm')
    puts "Please install npm to continue installing dependencies"
    return false
  end

  true
end

#config_env_pipObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rover.rb', line 146

def config_env_pip
  ['virtualenv'].each do |exe|
    unless which(exe)
      raise "you're fucked; missing #{exe}. please install first"
    end
  end

  python_dir = "#{@start_directory}/.python"
  exec_cmd "mkdir -p #{python_dir}"
  exec_cmd "virtualenv #{python_dir}"
  ENV['PATH'] = "#{python_dir}/bin:#{ENV['PATH']}"
  ENV['PYTHONPATH'] = ""

  true
end

#get_gem_info(gem_name) ⇒ Object



63
64
65
# File 'lib/rover.rb', line 63

def get_gem_info gem_name
  JSON.parse open("http://rubygems.org/api/v1/gems/#{gem_name}.json").read
end

#install_configsObject



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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rover.rb', line 162

def install_configs
  discovered_config_files = discover_config_files

  if discovered_config_files.empty?
    puts "Rover did not find any configuration files in this project"
  else
    puts "Rover found #{discovered_config_files.size} configuration files".underline
    puts "\n"

    discovered_config_files.each do |config_file_name,config_parts|
      puts "Installing Config: #{config_file_name}".colorize( :color => :white, :background => :blue )

      next unless config_env(config_parts['config_type'])
      
      cmd = "#{config_parts['config_type']} "
      case config_parts['config_type']
      when 'pip'
        cmd += "install -r #{config_parts['config_file']}"
      when 'bundle'
        cmd += "install"
      when 'npm'
        cmd += "install"
      else
        logger.info "Unknown Config Type: #{config_parts['config_type']}"
        next
      end
        
      change_dir(config_parts['config_path'])
      exec_cmd(cmd)

      puts "\n\n"
    end

    puts "If you are using a Python project please add the following lines to a .env file in the project's root directory".colorize( :color => :white, :background => :blue )
    puts "PATH=#{@start_directory}/.python/bin:$PATH".colorize( :color => :red).underline
    puts "PYTHONPATH=''".colorize( :color => :red).underline
    puts "Finished attempting to install config files. Moving back to the starting directory".colorize( :color => :white, :background => :blue )

    change_dir(@start_directory)
  end
end

#list_configsObject



59
60
61
# File 'lib/rover.rb', line 59

def list_configs
  discover_config_files
end

#pretty_print_configsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rover.rb', line 101

def pretty_print_configs
  configs = discover_config_files
  
  out = "Rover found #{configs.size} dependency configurations\n"

  index_count = 1
  discover_config_files.each do |config,config_parts|
    out += "--\n"
    out +=  "#{index_count}: #{config}\n"
    out += "#{index_count}: Type: #{config_parts['config_type']}\n"
    out += "#{index_count}: File: #{config_parts['config_file']}\n"
    out += "#{index_count}: Path: #{config_parts['config_path']}\n"
    index_count+=1
  end

  puts out.colorize(:color => :blue)
end


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
# File 'lib/rover.rb', line 67

def print_gem_infos
  begin
    gemfile = File.read('Gemfile')
  rescue Exception => e
    puts e
    puts "Is there a Gemfile in the directory you're running me in?"

    return
  end

  gemfile.each_line do |l|
    next unless l.lstrip.start_with? "gem"
    parts = l.lstrip.split(/\s|,/)

    gem_name = if parts.size == 2
      # no version specified
      parts.last
    elsif parts.size > 2
      parts[1]
    else
      nil
      #bad line
    end
    

    if gem_name
      gem_friendly = gem_name.gsub(/[^0-9a-z_-]/i, '')
      puts gem_friendly
      puts get_gem_info(gem_friendly)['info']
      puts ""
    end
  end
end