This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
forked from thoughtbot/hoptoad_notifier
/
terminal.rb
103 lines (83 loc) · 2.21 KB
/
terminal.rb
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
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
100
101
102
103
require 'fileutils'
Before do
@terminal = Terminal.new
end
After do |story|
if story.failed?
# puts @terminal.output
end
end
class Terminal
attr_reader :output, :status
attr_accessor :environment_variables, :invoke_heroku_rake_tasks_locally
def initialize
@cwd = FileUtils.pwd
@output = ""
@status = 0
@logger = Logger.new(File.join(TEMP_DIR, 'terminal.log'))
@invoke_heroku_rake_tasks_locally = false
@environment_variables = {
"GEM_HOME" => LOCAL_GEM_ROOT,
"GEM_PATH" => "#{LOCAL_GEM_ROOT}:#{BUILT_GEM_ROOT}",
"PATH" => "#{gem_bin_path}:#{ENV['PATH']}"
}
end
def cd(directory)
@cwd = directory
end
def run(command)
command = optionally_invoke_heroku_rake_tasks_locally(command)
output << "#{command}\n"
FileUtils.cd(@cwd) do
# The ; forces ruby to shell out so the env settings work right
cmdline = "#{environment_settings} #{command} 2>&1 ; "
logger.debug(cmdline)
result = `#{cmdline}`
logger.debug(result)
output << result
end
@status = $?
end
def optionally_invoke_heroku_rake_tasks_locally(command)
if invoke_heroku_rake_tasks_locally
command.sub(/^heroku /, '')
else
command
end
end
def echo(string)
logger.debug(string)
end
def build_and_install_gem(gemspec)
pkg_dir = File.join(TEMP_DIR, 'pkg')
FileUtils.mkdir_p(pkg_dir)
output = `gem build #{gemspec} 2>&1`
gem_file = Dir.glob("*.gem").first
unless gem_file
raise "Gem didn't build:\n#{output}"
end
target = File.join(pkg_dir, gem_file)
FileUtils.mv(gem_file, target)
install_gem_to(BUILT_GEM_ROOT, target)
end
def install_gem(gem)
install_gem_to(LOCAL_GEM_ROOT, gem)
end
def uninstall_gem(gem)
`gem uninstall -i #{BUILT_GEM_ROOT} #{gem}`
end
def prepend_path(path)
@environment_variables['PATH'] = path + ":" + @environment_variables['PATH']
end
private
def install_gem_to(root, gem)
`gem install -i #{root} --no-ri --no-rdoc #{gem}`
end
def environment_settings
@environment_variables.map { |key, value| "#{key}=#{value}" }.join(' ')
end
def gem_bin_path
File.join(LOCAL_GEM_ROOT, "bin")
end
attr_reader :logger
end