Module: Reup::Helpers

Defined in:
lib/reup/helpers.rb

Constant Summary collapse

DEFAULT_ENV_FILE =
File.expand_path "../.reup.yaml", __FILE__
USER_ENV_FILE =
File.expand_path ".reup.yaml", Dir.home

Instance Method Summary collapse

Instance Method Details

#cocaine_run(command, quiet:) ⇒ Object



25
26
27
28
29
30
# File 'lib/reup/helpers.rb', line 25

def cocaine_run(command, quiet:)
  cmd = Cocaine::CommandLine.new(command)
  result = cmd.run
  puts result unless quiet
  result
end

#detect_change_to(files, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/reup/helpers.rb', line 36

def detect_change_to(files, &block)
  before = files.map { |f| last_modified(f) }
  yield
  after  = files.map { |f| last_modified(f) }
  before != after # Not equal means there was a change
end

#envObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/reup/helpers.rb', line 84

def env
  @env ||= (
    possible_envs = env_file.keys
    env = nil
    possible_envs.each do |possible|
      indicator_files = Array(env_file[possible]["indicator_files"])
      if indicator_files.any? { |f| File.exist? f }
        env = possible
        break
      end
    end
    raise "You don't appear to be in a supported environment" unless env
    env
  )
end

#env_fileObject



51
52
53
54
55
56
57
58
59
# File 'lib/reup/helpers.rb', line 51

def env_file
  @env_file ||= (
    if File.exist? USER_ENV_FILE
      YAML.load_file USER_ENV_FILE
    else
      YAML.load_file DEFAULT_ENV_FILE
    end
  )
end

#install(args = {}) ⇒ Object

API methods



104
105
106
# File 'lib/reup/helpers.rb', line 104

def install(args = {})
  run install_command, args
end

#install_commandObject



65
66
67
# File 'lib/reup/helpers.rb', line 65

def install_command
  @install_command ||= env_file[env]["install"]["command"]
end

#kernel_run(command) ⇒ Object



32
33
34
# File 'lib/reup/helpers.rb', line 32

def kernel_run(command)
  Kernel.exec command
end

#last_modified(file) ⇒ Object

Environment helpers



47
48
49
# File 'lib/reup/helpers.rb', line 47

def last_modified(file)
  File.mtime file
end

#reset_db(args = {}) ⇒ Object



108
109
110
# File 'lib/reup/helpers.rb', line 108

def reset_db(args = {})
  run reset_db_command, args
end

#reset_db_commandObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/reup/helpers.rb', line 73

def reset_db_command
  @reset_db_command ||= (
    db_section = env_file[env]["db"]
    if db_section
      db_section["reset_command"]
    else
      nil
    end
  )
end

#run(command, quiet: false, print_command: true, replace_process: false) ⇒ Object

Executable helpers



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reup/helpers.rb', line 13

def run(command, quiet: false, print_command: true, replace_process: false)
  return unless command
  puts command if print_command
  command += " &>/dev/null" if quiet

  if replace_process
    kernel_run command
  else
    cocaine_run command, quiet: quiet
  end
end

#serveObject



112
113
114
# File 'lib/reup/helpers.rb', line 112

def serve
  run(serve_command, replace_process: true)
end

#serve_commandObject



69
70
71
# File 'lib/reup/helpers.rb', line 69

def serve_command
  @serve_command ||= env_file[env]["serve"]["command"]
end

#watch_filesObject



61
62
63
# File 'lib/reup/helpers.rb', line 61

def watch_files
  @watch_files ||= Array(env_file[env]["install"]["only_if_changed"])
end