Module: Hobo::Helper

Defined in:
lib/hobo/helper/shell.rb,
lib/hobo/helper/vm_command.rb,
lib/hobo/helper/file_locator.rb,
lib/hobo/helper/http_download.rb

Instance Method Summary collapse

Instance Method Details

#bundle_shell(*args, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hobo/helper/shell.rb', line 4

def bundle_shell *args, &block
  has_bundle = begin
    shell "bundle", "exec", "ruby -v"
    true
  rescue ::Hobo::ExternalCommandError
    false
  end

  if has_bundle
    args = [ 'bundle', 'exec' ] + args
  end

  shell *args, &block
end

#chunk_line_iterator(stream) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hobo/helper/shell.rb', line 23

def chunk_line_iterator stream
  begin
    until (chunk = stream.readpartial(1024)).nil? do
      chunk.each_line do |outer_line|
        outer_line.each_line("\r") do |line|
          yield line
        end
      end
    end
  rescue EOFError
    # NOP
  end
end

#http_download(url, target_file, opts = {}) ⇒ Object



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
# File 'lib/hobo/helper/http_download.rb', line 3

def http_download url, target_file, opts = {}
  require 'net/http'
  require 'uri'
  opts = { :progress => Hobo.method(:progress) }.merge(opts)
  uri = URI.parse(url)
  size = 0
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  # TODO: May want to verify SSL validity...
  # http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/#comment-22252
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  http.start do
    begin
      file = open(target_file, 'wb+')
      http.request_get(uri.path) do |response|
        size = response.content_length
        response.read_body do |chunk|
          file.write(chunk)
          opts[:progress].call(
            target_file,
            chunk.length,
            size,
            :update
          ) if opts[:progress]
        end
      end
    ensure
      opts[:progress].call(target_file, 0, size, :finsh) if opts[:progress]
      file.close
    end
  end
end

#locate(pattern, opts = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hobo/helper/file_locator.rb', line 3

def locate(pattern, opts = {}, &block)
  match = nil

  Dir.chdir Hobo.project_path do
    match = locate_git(pattern, &block)
  end

  return match unless block_given?
  return true if match

  Hobo.ui.warning opts[:missing] if opts[:missing]
  return false
end

#shell(*args, &block) ⇒ Object



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
# File 'lib/hobo/helper/shell.rb', line 19

def shell *args, &block
  require 'open3'
  require 'tempfile'

  def chunk_line_iterator stream
    begin
      until (chunk = stream.readpartial(1024)).nil? do
        chunk.each_line do |outer_line|
          outer_line.each_line("\r") do |line|
            yield line
          end
        end
      end
    rescue EOFError
      # NOP
    end
  end

  opts = (args.size > 1 && args.last.is_a?(Hash)) ? args.pop : {}
  opts = {
    :capture => false,
    :strip => true,
    :indent => 0,
    :realtime => false,
    :env => {},
    :ignore_errors => false,
    :exit_status => false
  }.merge! opts

  Hobo::Logging.logger.debug("helper.shell: Invoking '#{args.join(" ")}' with #{opts.to_s}")

  ::Bundler.with_clean_env do
    indent = " " * opts[:indent]
    ::Open3.popen3 opts[:env], *args do |stdin, out, err, external|
      buffer = ::Tempfile.new 'hobo_run_buf'
      buffer.sync = true
      threads = [external]
      last_buf = ""

      ## Create a thread to read from each stream
      { :out => out, :err => err }.each do |key, stream|
        threads.push(::Thread.new do
          chunk_line_iterator stream do |line|
            line = ::Hobo.ui.color(line, :error) if key == :err
            line_formatted = if opts[:strip]
              line.strip
            else
              line
            end
            buffer.write("#{line_formatted}\n")
            Hobo::Logging.logger.debug("helper.shell: #{line_formatted}")
            line = yield line if block
            print indent + line if opts[:realtime] && !line.nil?
          end
        end)
      end

      threads.each do |t|
        t.join
      end

      buffer.fsync
      buffer.rewind

      return external.value.exitstatus if opts[:exit_status]

      raise ::Hobo::ExternalCommandError.new(args.join(" "), external.value.exitstatus, buffer) if external.value.exitstatus != 0 && !opts[:ignore_errors]

      if opts[:capture]
        return buffer.read unless opts[:strip]
        return buffer.read.strip
      else
        return nil
      end
    end
  end
end

#vm_command(command = nil, opts = {}) ⇒ Object



24
25
26
# File 'lib/hobo/helper/vm_command.rb', line 24

def vm_command command = nil, opts = {}
  ::Hobo::Lib::Vm::Command.new command, opts
end

#vm_mysql(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hobo/helper/vm_command.rb', line 8

def vm_mysql opts = {}
  opts = {
    :auto_echo => true,
    :db => "",
    :user => maybe(Hobo.project_config.mysql.username) || "",
    :pass => maybe(Hobo.project_config.mysql.password) || "",
    :mysql => 'mysql'
  }.merge(opts)

  opts[:user] = "-u#{opts[:user].shellescape}" unless opts[:user].empty?
  opts[:pass] = "-p#{opts[:pass].shellescape}" unless opts[:pass].empty?
  opts[:db] = opts[:db].shellescape unless opts[:db].empty?

  ::Hobo::Lib::Vm::Command.new "#{opts[:mysql]} #{opts[:user]} #{opts[:pass]} #{opts[:db]}".strip, opts
end

#vm_shell(command, opts = {}) ⇒ Object



4
5
6
# File 'lib/hobo/helper/vm_command.rb', line 4

def vm_shell command, opts = {}
  shell ::Hobo::Lib::Vm::Command.new(command, opts).to_s, opts
end