rubycut-babushka 0.10.8 → 0.15.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/Gemfile +1 -0
  2. data/Gemfile.lock +17 -15
  3. data/README.markdown +163 -41
  4. data/Rakefile +1 -1
  5. data/bin/babushka +1 -1
  6. data/deps/apt.rb +44 -0
  7. data/deps/babushka.rb +54 -42
  8. data/deps/deprecated.rb +16 -0
  9. data/deps/dev.rb +28 -3
  10. data/deps/git.rb +27 -12
  11. data/deps/homebrew.rb +2 -2
  12. data/deps/packages.rb +14 -15
  13. data/deps/pkg_managers.rb +21 -75
  14. data/deps/ruby.rb +5 -19
  15. data/deps/rubygems.rb +3 -3
  16. data/deps/system.rb +2 -2
  17. data/deps/templates/app.rb +60 -41
  18. data/deps/templates/bin.rb +16 -0
  19. data/deps/templates/installer.rb +9 -9
  20. data/deps/templates/lib.rb +17 -0
  21. data/deps/templates/managed.rb +1 -38
  22. data/deps/templates/src.rb +16 -8
  23. data/deps/templates/task.rb +11 -0
  24. data/deps/templates/tmbundle.rb +16 -2
  25. data/lib/babushka.rb +2 -3
  26. data/lib/babushka/accepts_block_for.rb +5 -3
  27. data/lib/babushka/asset.rb +172 -0
  28. data/lib/babushka/base.rb +37 -8
  29. data/lib/babushka/bug_reporter.rb +6 -6
  30. data/lib/babushka/cmdline.rb +11 -10
  31. data/lib/babushka/cmdline/handler.rb +7 -3
  32. data/lib/babushka/cmdline/helpers.rb +15 -23
  33. data/lib/babushka/cmdline/parser.rb +1 -1
  34. data/lib/babushka/core_patches/object.rb +1 -1
  35. data/lib/babushka/core_patches/string.rb +8 -3
  36. data/lib/babushka/current_ruby.rb +44 -0
  37. data/lib/babushka/dep.rb +111 -185
  38. data/lib/babushka/dep_context.rb +8 -3
  39. data/lib/babushka/dep_definer.rb +45 -15
  40. data/lib/babushka/dep_pool.rb +5 -8
  41. data/lib/babushka/{meta_dep.rb → dep_template.rb} +21 -2
  42. data/lib/babushka/dsl.rb +3 -0
  43. data/lib/babushka/git_repo.rb +143 -49
  44. data/lib/babushka/helpers/git_helpers.rb +7 -6
  45. data/lib/babushka/helpers/log_helpers.rb +51 -13
  46. data/lib/babushka/helpers/path_helpers.rb +5 -7
  47. data/lib/babushka/helpers/run_helpers.rb +15 -55
  48. data/lib/babushka/helpers/shell_helpers.rb +18 -26
  49. data/lib/babushka/helpers/uri_helpers.rb +9 -18
  50. data/lib/babushka/lambda_chooser.rb +20 -13
  51. data/lib/babushka/parameter.rb +20 -4
  52. data/lib/babushka/path_checker.rb +72 -0
  53. data/lib/babushka/pkg_helper.rb +38 -13
  54. data/lib/babushka/pkg_helpers/apt_helper.rb +15 -8
  55. data/lib/babushka/pkg_helpers/binpkgsrc_helper.rb +15 -14
  56. data/lib/babushka/pkg_helpers/binports_helper.rb +7 -7
  57. data/lib/babushka/pkg_helpers/brew_helper.rb +17 -25
  58. data/lib/babushka/pkg_helpers/gem_helper.rb +36 -27
  59. data/lib/babushka/pkg_helpers/npm_helper.rb +9 -9
  60. data/lib/babushka/pkg_helpers/pacman_helper.rb +5 -4
  61. data/lib/babushka/pkg_helpers/pip_helper.rb +14 -10
  62. data/lib/babushka/pkg_helpers/unknown_pkg_helper.rb +19 -0
  63. data/lib/babushka/pkg_helpers/yum_helper.rb +1 -1
  64. data/lib/babushka/popen.rb +13 -10
  65. data/lib/babushka/prompt.rb +14 -1
  66. data/lib/babushka/renderable.rb +11 -9
  67. data/lib/babushka/resource.rb +5 -166
  68. data/lib/babushka/run_reporter.rb +12 -3
  69. data/lib/babushka/shell.rb +54 -44
  70. data/lib/babushka/source.rb +41 -20
  71. data/lib/babushka/source_pool.rb +20 -13
  72. data/lib/babushka/system_definitions.rb +11 -3
  73. data/lib/babushka/system_detector.rb +31 -0
  74. data/lib/babushka/system_matcher.rb +53 -0
  75. data/lib/babushka/system_profile.rb +67 -89
  76. data/lib/babushka/task.rb +36 -8
  77. data/lib/babushka/{meta_dep_context.rb → templated_dep_context.rb} +1 -1
  78. data/lib/babushka/vars.rb +46 -4
  79. data/lib/babushka/version_of.rb +35 -17
  80. data/lib/babushka/version_str.rb +12 -8
  81. data/lib/components.rb +9 -8
  82. data/lib/fancypath/fancypath.rb +109 -83
  83. data/lib/inkan/inkan.rb +14 -14
  84. data/lib/{babushka → levenshtein}/levenshtein.rb +0 -0
  85. data/spec/acceptance/acceptance.rb +4 -4
  86. data/spec/acceptance_helper.rb +10 -6
  87. data/spec/babushka/accepts_for_spec.rb +137 -142
  88. data/spec/babushka/accepts_for_support.rb +13 -6
  89. data/spec/babushka/asset_spec.rb +165 -0
  90. data/spec/babushka/cmdline/help_spec.rb +11 -9
  91. data/spec/babushka/cmdline/meet_spec.rb +15 -0
  92. data/spec/babushka/cmdline/version_spec.rb +1 -1
  93. data/spec/babushka/core_patches_spec.rb +9 -0
  94. data/spec/babushka/current_ruby_spec.rb +73 -0
  95. data/spec/babushka/dep_context_spec.rb +27 -13
  96. data/spec/babushka/dep_definer_spec.rb +108 -16
  97. data/spec/babushka/dep_spec.rb +87 -104
  98. data/spec/babushka/dep_template_spec.rb +176 -0
  99. data/spec/babushka/deps_spec.rb +48 -19
  100. data/spec/babushka/gem_helper_spec.rb +46 -59
  101. data/spec/babushka/git_repo_spec.rb +242 -51
  102. data/spec/babushka/ip_spec.rb +11 -11
  103. data/spec/babushka/lambda_chooser_spec.rb +47 -9
  104. data/spec/babushka/parameter_spec.rb +21 -0
  105. data/spec/babushka/path_checker_spec.rb +35 -0
  106. data/spec/babushka/path_helpers_spec.rb +51 -50
  107. data/spec/babushka/prompt_spec.rb +4 -4
  108. data/spec/babushka/renderable_spec.rb +61 -28
  109. data/spec/babushka/shell_helpers_spec.rb +110 -85
  110. data/spec/babushka/shell_spec.rb +15 -0
  111. data/spec/babushka/source_pool_spec.rb +204 -210
  112. data/spec/babushka/source_spec.rb +125 -42
  113. data/spec/babushka/source_support.rb +1 -1
  114. data/spec/babushka/system_profile_spec.rb +86 -49
  115. data/spec/babushka/task_spec.rb +80 -13
  116. data/spec/babushka/vars_spec.rb +2 -1
  117. data/spec/babushka/version_of_spec.rb +29 -2
  118. data/spec/babushka/version_str_spec.rb +91 -65
  119. data/spec/babushka/xml_string_spec.rb +1 -1
  120. data/spec/deps/bad/broken.rb +2 -2
  121. data/spec/deps/bad/working.rb +0 -1
  122. data/spec/deps/good/{meta.rb → template.rb} +0 -0
  123. data/spec/deps/good/test.rb +0 -3
  124. data/spec/deps/outer/deps.rb +0 -2
  125. data/spec/fancypath/fancypath_spec.rb +30 -0
  126. data/spec/inkan/inkan_spec.rb +34 -32
  127. data/spec/spec_helper.rb +7 -50
  128. data/spec/system_detector_spec.rb +70 -0
  129. metadata +163 -177
  130. data/deps/os_x.rb +0 -33
  131. data/deps/templates/ppa.rb +0 -24
  132. data/lib/babushka/core_patches/io.rb +0 -8
  133. data/lib/babushka/dep_runner.rb +0 -85
  134. data/lib/babushka/helpers/suggest_helpers.rb +0 -16
  135. data/lib/babushka/pkg_helpers/base_helper.rb +0 -19
  136. data/lib/babushka/pkg_helpers/macports_helper.rb +0 -22
  137. data/spec/babushka/dep_definer_support.rb +0 -36
  138. data/spec/babushka/meta_dep_definer_spec.rb +0 -127
  139. data/spec/babushka/meta_dep_wrapper_spec.rb +0 -32
  140. data/spec/babushka/resource_spec.rb +0 -141
  141. data/spec/babushka/run_helpers_spec.rb +0 -26
  142. data/spec/babushka/source_pool_support.rb +0 -31
@@ -1,16 +1,17 @@
1
1
  module Babushka
2
2
  module GitHelpers
3
+ # Make these helpers directly callable, and private when included.
4
+ module_function
5
+
3
6
  def git uri, opts = {}, &block
4
7
  repo = GitRepo.new(opts[:to] || (BuildPrefix / File.basename(uri.to_s).chomp('.git')))
5
8
 
6
9
  if git_update(uri, repo)
7
10
  repo.root.touch # so we can tell when it was last updated
8
- block.nil? || cd(repo.path, &block)
11
+ block.nil? || PathHelpers.cd(repo.path, &block)
9
12
  end
10
13
  end
11
14
 
12
- private
13
-
14
15
  def git_update uri, repo
15
16
  if !repo.exists?
16
17
  update_and_log uri, repo, "Cloning #{uri} into #{repo.path}" do
@@ -24,14 +25,14 @@ module Babushka
24
25
  end
25
26
 
26
27
  def update_and_log uri, repo, message, &block
27
- log_block message do
28
+ LogHelpers.log_block message do
28
29
  if !block.call
29
30
  # failed
30
31
  elsif !repo.behind?
31
- log " at #{repo.current_head.colorize('yellow')},", :newline => false
32
+ LogHelpers.log " at #{repo.current_head.colorize('yellow')},", :newline => false
32
33
  true
33
34
  else
34
- log " #{repo.current_head.colorize('yellow')}..#{repo.repo_shell("git rev-parse --short origin/#{repo.current_branch}").colorize('yellow')} (#{repo.repo_shell("git log -1 --pretty=format:%s origin/#{repo.current_branch}").chomp('.')}),", :newline => false
35
+ LogHelpers.log " #{repo.current_head.colorize('yellow')}..#{repo.repo_shell("git rev-parse --short origin/#{repo.current_branch}").colorize('yellow')} (#{repo.repo_shell("git log -1 --pretty=format:%s origin/#{repo.current_branch}").chomp('.')}),", :newline => false
35
36
  repo.reset_hard! "origin/#{repo.current_branch}"
36
37
  end
37
38
  end
@@ -2,6 +2,15 @@
2
2
 
3
3
  module Babushka
4
4
  module LogHelpers
5
+ # Make these helpers directly callable, and private when included.
6
+ module_function
7
+
8
+ # Log +message+ to STDERR. This is a shortcut for
9
+ # log(message, :as => :error)
10
+ def log_stderr message, opts = {}, &block
11
+ log message, opts.merge(:as => :stderr), &block
12
+ end
13
+
5
14
  # Log +message+ as an error. This is a shortcut for
6
15
  # log(message, :as => :error)
7
16
  def log_error message, opts = {}, &block
@@ -14,11 +23,6 @@ module Babushka
14
23
  log message, opts.merge(:as => :warning), &block
15
24
  end
16
25
 
17
- def log_verbose message, opts = {}, &block
18
- log_error "#{caller.first}: #log_verbose has been deprecated. Instead, just use #log." # deprecated
19
- log message, opts, &block
20
- end
21
-
22
26
  # Yield the block, writing a note to the log about it beforehand and
23
27
  # afterwards.
24
28
  #
@@ -33,7 +37,7 @@ module Babushka
33
37
  def log_block message, opts = {}, &block
34
38
  log "#{message}...", :newline => false
35
39
  block.call.tap {|result|
36
- log result ? ' done.' : ' failed', :as => (result ? nil : :error), :indentation => false
40
+ log result ? " #{opts[:success] || 'done'}." : " #{opts[:failure] || 'failed'}", :as => (result ? nil : :error), :indentation => false
37
41
  }
38
42
  end
39
43
 
@@ -56,6 +60,24 @@ module Babushka
56
60
  log message, opts.merge(:debug => !opts[:log]), &block
57
61
  end
58
62
 
63
+ def deprecated! date, opts = {}
64
+ callpoint = "#{caller[opts[:skip] || 1].sub(/\:in `.*$/, '')}: " unless opts[:callpoint] == false
65
+ opts[:method_name] ||= "##{caller[0].scan(/`(\w+)'$/).flatten.first}"
66
+ warning = "#{callpoint}#{opts[:method_name]} has been deprecated and will be removed on #{date}."
67
+ instead = " Use #{opts[:instead]} instead#{opts[:example] ? ", e.g.:" : '.'}" unless opts[:instead].nil?
68
+ log_warn "#{warning}#{instead}"
69
+ log opts[:example].strip unless opts[:example].nil?
70
+ log ''
71
+ end
72
+
73
+ def removed! opts = {}
74
+ opts[:method_name] ||= "##{caller[0].scan(/`(\w+)'$/).flatten.first}"
75
+ warning = "#{opts[:method_name]} has been removed after being deprecated."
76
+ instead = " Use #{opts[:instead]} instead#{opts[:example] ? ", e.g.:" : '.'}" unless opts[:instead].nil?
77
+ message = ["#{warning}#{instead}", opts[:example]].compact.join("\n")
78
+ raise NoMethodError.new(message)
79
+ end
80
+
59
81
  # Write +message+ to the log.
60
82
  #
61
83
  # By default, the log is written to STDOUT, and to ~/.babushka/logs/<dep_name>.
@@ -81,12 +103,14 @@ module Babushka
81
103
  # runs - so please consider other logging styles before using this one, so as
82
104
  # not to visually confuse dep runs with other operations.)
83
105
  def log message, opts = {}, &block
84
- # now = Time.now
85
- # print "#{now.to_i}.#{now.usec}: ".ljust(20) unless opts[:debug]
86
106
  printable = !opts[:debug] || Base.task.opt(:debug)
87
- Logging.print_log Logging.indentation, printable unless opts[:indentation] == false
107
+ if Base.task.opt(:profile)
108
+ delta = Time.now - Base.start_time
109
+ Logging.print_log("%.4f ".colorize('grey') % delta, printable, opts[:as])
110
+ end
111
+ Logging.print_log(Logging.indentation, printable, opts[:as]) unless opts[:indentation] == false
88
112
  if block_given?
89
- Logging.print_log "#{message} {\n".colorize('grey'), printable
113
+ Logging.print_log("#{message} {".colorize('grey') + "\n", printable, opts[:as])
90
114
  Logging.indent! if printable
91
115
  yield.tap {|result|
92
116
  Logging.undent! if printable
@@ -99,7 +123,7 @@ module Babushka
99
123
  message = message.colorize 'yellow' if opts[:as] == :warning
100
124
  message = message.colorize 'bold' if opts[:as] == :stderr
101
125
  message = message.end_with "\n" unless opts[:newline] == false
102
- Logging.print_log message, printable
126
+ Logging.print_log(message, printable, opts[:as])
103
127
  $stdout.flush
104
128
  nil
105
129
  end
@@ -126,6 +150,11 @@ module Babushka
126
150
  end
127
151
  end
128
152
 
153
+ def self.log_exception exception
154
+ log_error "#{exception.backtrace.first}: #{exception.message}"
155
+ debug exception.backtrace * "\n"
156
+ end
157
+
129
158
  def self.log_table headings, rows
130
159
  all_rows = rows.map {|row|
131
160
  row.map(&:to_s)
@@ -149,8 +178,14 @@ module Babushka
149
178
 
150
179
  private
151
180
 
152
- def self.print_log message, printable
153
- print message if printable
181
+ def self.print_log message, printable, as
182
+ if !printable
183
+ # Only written to the log file.
184
+ elsif [:error, :stderr].include?(as)
185
+ $stderr.print message
186
+ elsif !Base.task.opt(:silent)
187
+ $stdout.print message
188
+ end
154
189
  write_to_persistent_log message
155
190
  end
156
191
 
@@ -161,13 +196,16 @@ module Babushka
161
196
  def self.indentation
162
197
  ' ' * indentation_level * 2
163
198
  end
199
+
164
200
  def self.indentation_level
165
201
  @indentation_level ||= 0
166
202
  end
203
+
167
204
  def self.indent!
168
205
  @indentation_level ||= 0
169
206
  @indentation_level += 1
170
207
  end
208
+
171
209
  def self.undent!
172
210
  @indentation_level ||= 0
173
211
  @indentation_level -= 1
@@ -1,16 +1,19 @@
1
1
  module Babushka
2
2
  module PathHelpers
3
+ # Make these helpers directly callable, and private when included.
4
+ module_function
5
+
3
6
  def cd dir, opts = {}, &block
4
7
  if dir.nil?
5
8
  yield Dir.pwd.p
6
9
  else
7
10
  path = dir.p
8
- shell("mkdir -p '#{path}'", :sudo => opts[:sudo]) if opts[:create] unless path.exists?
11
+ ShellHelpers.shell("mkdir -p '#{path}'", :sudo => opts[:sudo]) if opts[:create] unless path.exists?
9
12
  if Dir.pwd == path
10
13
  yield path
11
14
  else
12
15
  Dir.chdir path do
13
- debug "in dir #{dir} (#{path})" do
16
+ LogHelpers.debug "in dir #{dir} (#{path})" do
14
17
  yield path
15
18
  end
16
19
  end
@@ -18,11 +21,6 @@ module Babushka
18
21
  end
19
22
  end
20
23
 
21
- def in_dir dir, opts = {}, &block
22
- log_error "#{caller.first}: #in_dir has been renamed to #cd." # deprecated
23
- cd dir, opts, &block
24
- end
25
-
26
24
  def in_build_dir path = '', &block
27
25
  cd Babushka::BuildPrefix / path, :create => true, &block
28
26
  end
@@ -5,85 +5,49 @@ module Babushka
5
5
  include PathHelpers
6
6
 
7
7
  def hostname
8
- shell 'hostname -f'
8
+ removed! :instead => "#shell directly", :example => "shell('hostname -f')"
9
9
  end
10
10
 
11
11
  def rake cmd, &block
12
- sudo "rake #{cmd} RAILS_ENV=#{var :app_env}", :as => var(:username), &block
12
+ removed! :instead => "#shell directly", :example => "shell({'RAILS_ENV' => env}, 'rake #{cmd}')"
13
13
  end
14
14
 
15
15
  def bundle_rake cmd, &block
16
- cd var(:rails_root) do
17
- shell "bundle exec rake #{cmd} --trace RAILS_ENV=#{var :app_env}", :as => var(:username), :log => true, &block
18
- end
16
+ removed! :instead => "#shell directly", :example => "shell({'RAILS_ENV' => 'env'}, 'bundle exec #{cmd}')"
19
17
  end
20
18
 
21
19
  def check_file file_name, method_name
22
- File.send(method_name, file_name).tap {|result|
23
- log_error "#{file_name} failed #{method_name.to_s.sub(/[?!]$/, '')} check." unless result
24
- }
20
+ removed! :instead => "Fancypath##{method_name}", :example => "path.p.#{method_name}"
25
21
  end
26
22
 
27
23
  def grep pattern, file
28
- if (path = file.p).exists?
29
- output = if pattern.is_a? String
30
- path.readlines.select {|l| l[pattern] }
31
- elsif pattern.is_a? Regexp
32
- path.readlines.grep pattern
33
- end
34
- output unless output.blank?
35
- end
24
+ removed! :instead => 'Fancypath#grep', :example => "#{file.inspect}.p.grep(#{pattern.inspect})"
36
25
  end
37
26
 
38
27
  def change_line line, replacement, filename
39
- path = filename.p
40
-
41
- log "Patching #{path}"
42
- shell "cat > #{path}", :as => path.owner, :input => path.readlines.map {|l|
43
- l.gsub(/^(\s*)(#{Regexp.escape(line)})/, "\\1# #{edited_by_babushka}\n\\1# was: \\2\n\\1#{replacement}")
44
- }.join("")
28
+ removed! :instead => "sed via #shell", :example => "shell(\"sed -i'' -e 's/^#{Regexp.escape(line)}$/#{replacement}/' '#{filename}'\")"
45
29
  end
46
30
 
47
31
  def insert_into_file insert_before, path, lines, opts = {}
48
- opts.defaults! :comment_char => '#', :insert_after => nil
49
- nlines = lines.split("\n").length
50
- before, after = path.p.readlines.cut {|l| l.strip == insert_before.strip }
51
-
52
- log "Patching #{path}"
53
- if after.empty? || (opts[:insert_after] && before.last.strip != opts[:insert_after].strip)
54
- log_error "Couldn't find the spot to write to in #{path}."
55
- else
56
- shell "cat > #{path}", :as => path.owner, :sudo => !File.writable?(path), :input => [
57
- before,
58
- added_by_babushka(nlines).start_with(opts[:comment_char] + ' ').end_with("\n"),
59
- lines.end_with("\n"),
60
- after
61
- ].join
62
- end
32
+ removed! :instead => "a template with #render_erb"
63
33
  end
64
34
 
65
35
  def change_with_sed keyword, from, to, file
66
- # Remove the incorrect setting if it's there
67
- shell("#{sed} -ri 's/^#{keyword}\s+#{from}//' #{file}", :sudo => !File.writable?(file))
68
- # Add the correct setting unless it's already there
69
- grep(/^#{keyword}\s+#{to}/, file) or shell("echo '#{keyword} #{to}' >> #{file}", :sudo => !File.writable?(file))
70
- end
71
-
72
- def sed
73
- Base.host.linux? ? 'sed' : 'gsed'
36
+ removed! :instead => "sed via #shell", :example => "shell(\"sed -i'' -e 's/^#{Regexp.escape(keyword)}\\s+#{Regexp.escape(from)}\\b/#{keyword} #{to}/' '#{file}'\")"
74
37
  end
75
38
 
76
39
  def append_to_file text, file, opts = {}
77
- text = text.to_s
78
- shell %Q{echo "\n# #{added_by_babushka(text.split("\n").length)}\n#{text.gsub('"', '\"')}" >> #{file}}, opts
40
+ removed! :instead => 'Fancypath#append', :example => "'#{file}'.p.append(#{text.inspect})"
79
41
  end
80
42
 
81
43
  def _by_babushka
82
44
  "by babushka-#{VERSION} at #{Time.now}"
83
45
  end
46
+
84
47
  def edited_by_babushka
85
48
  "This line edited #{_by_babushka}"
86
49
  end
50
+
87
51
  def added_by_babushka nlines
88
52
  if nlines == 1
89
53
  "This line added #{_by_babushka}"
@@ -103,14 +67,13 @@ module Babushka
103
67
  end
104
68
 
105
69
  def yaml path
106
- require 'yaml'
107
- YAML.load_file path.p
70
+ removed! :instead => 'Fancypath#yaml', :example => "'#{path}'.p.yaml"
108
71
  end
109
72
 
110
73
  def render_erb erb, opts = {}
111
74
  if (path = erb_path_for(erb)).nil?
112
75
  log_error "If you use #render_erb within a dynamically defined dep, you have to give the full path to the erb template."
113
- elsif !File.exists?(path) && !opts[:optional]
76
+ elsif !File.exists?(path)
114
77
  log_error "Couldn't find erb to render at #{path}."
115
78
  elsif File.exists?(path)
116
79
  Renderable.new(opts[:to]).render(path, opts.merge(:context => self)).tap {|result|
@@ -132,14 +95,11 @@ module Babushka
132
95
  end
133
96
 
134
97
  def log_and_open message, url
135
- log "#{message} Hit Enter to open the download page.", :newline => false
136
- read_from_prompt ' '
137
- shell "open #{url}"
98
+ removed! :instead => "a plain #log with the URL", :example => "log('Download here: http://website.org')"
138
99
  end
139
100
 
140
101
  def mysql cmd, username = 'root', include_password = true
141
- password_segment = "--password='#{var :db_password}'" if include_password
142
- shell "echo \"#{cmd.gsub('"', '\"').end_with(';')}\" | mysql -u #{username} #{password_segment}"
102
+ removed! :instead => "#shell directly", :example => "shell('mysql', '-u', username, :input => cmd.end_with(';'))"
143
103
  end
144
104
  end
145
105
  end
@@ -2,6 +2,9 @@ module Babushka
2
2
  module ShellHelpers
3
3
  include LogHelpers
4
4
 
5
+ # Make these helpers directly callable, and private when included.
6
+ module_function
7
+
5
8
  # Run +cmd+.
6
9
  #
7
10
  # If the command succeeds (i.e. returns 0), its output will be returned
@@ -43,10 +46,10 @@ module Babushka
43
46
  if cmd.extract_options[:log]
44
47
  # Don't log the error if the command already logged
45
48
  elsif e.stdout.empty? && e.stderr.empty?
46
- log "$ #{e.cmd.join(' ')}".colorize('grey') + ' ' + "#{Logging::CrossChar} shell command failed".colorize('red')
49
+ LogHelpers.log "$ #{e.cmd.join(' ')}".colorize('grey') + ' ' + "#{Logging::CrossChar} shell command failed".colorize('red')
47
50
  else
48
- log "$ #{e.cmd.join(' ')}", :closing_status => 'shell command failed' do
49
- log_error(e.stderr.empty? ? e.stdout : e.stderr)
51
+ LogHelpers.log "$ #{e.cmd.join(' ')}", :closing_status => 'shell command failed' do
52
+ LogHelpers.log_error(e.stderr.empty? ? e.stdout : e.stderr)
50
53
  end
51
54
  end
52
55
  end
@@ -69,10 +72,6 @@ module Babushka
69
72
  opts = cmd.extract_options!
70
73
  cmd = cmd.first if cmd.map(&:class) == [Array]
71
74
 
72
- if opts[:dir] # deprecated
73
- log_error "#{caller.first}: #shell's :dir option has been renamed to :cd."
74
- opts[:cd] = opts[:dir]
75
- end
76
75
  if opts[:cd]
77
76
  if !opts[:cd].p.exists?
78
77
  if opts[:create]
@@ -80,6 +79,8 @@ module Babushka
80
79
  else
81
80
  raise Errno::ENOENT, opts[:cd]
82
81
  end
82
+ elsif !opts[:cd].p.dir?
83
+ raise Errno::ENOTDIR, opts[:cd]
83
84
  end
84
85
  end
85
86
  shell_method = (opts[:as] || opts[:sudo]) ? :sudo : :shell_cmd
@@ -97,22 +98,16 @@ module Babushka
97
98
  shell(*cmd) {|s| s }
98
99
  end
99
100
 
100
- def failable_shell *cmd
101
- log_error "#failable_shell has been renamed to #raw_shell." # deprecated
102
- raw_shell(*cmd)
103
- end
104
-
105
101
  # Run +cmd+ in a separate interactive shell. This is useful for running
106
102
  # commands that depend on something shell-related that was changed during
107
103
  # this run, like changing the user's shell. It's also useful for running
108
104
  # commands that are only valid on an interactive shell, like rvm-related
109
105
  # commands.
110
- # TODO: specs.
111
106
  def login_shell cmd, opts = {}, &block
112
107
  if shell('echo $SHELL').p.basename == 'zsh'
113
- shell %Q{zsh -i -c "#{cmd.gsub('"', '\"')}"}, opts, &block
108
+ shell "zsh -i -c '#{cmd}'", opts, &block
114
109
  else
115
- shell %Q{bash -l -c "#{cmd.gsub('"', '\"')}"}, opts, &block
110
+ shell "bash -l -c '#{cmd}'", opts, &block
116
111
  end
117
112
  end
118
113
 
@@ -144,17 +139,16 @@ module Babushka
144
139
  raise ArgumentError, "#sudo commands have to be passed as a single string, not splatted strings or an array, since the `sudo` is composed from strings."
145
140
  end
146
141
 
147
- as = opts[:as] || (opts[:sudo].is_a?(String) ? opts[:sudo] : 'root')
142
+ raw_as = opts[:as] || opts[:sudo] || 'root'
143
+ as = raw_as == true ? 'root' : raw_as
148
144
  cmd = cmd.last
149
145
 
150
146
  sudo_cmd = if current_username == as
151
147
  cmd # Don't sudo if we're already running as the specified user.
148
+ elsif opts[:su] || cmd[' |'] || cmd[' >']
149
+ "sudo su - #{as} -c \"#{cmd.gsub('"', '\"')}\""
152
150
  else
153
- if opts[:su] || cmd[' |'] || cmd[' >']
154
- "sudo su - #{as} -c \"#{cmd.gsub('"', '\"')}\""
155
- else
156
- "sudo -u #{as} #{cmd}"
157
- end
151
+ "sudo -u #{as} #{cmd}"
158
152
  end
159
153
 
160
154
  shell [env, sudo_cmd], opts.discard(:as, :sudo, :su), &block
@@ -169,7 +163,7 @@ module Babushka
169
163
  # which('babushka') #=> nil
170
164
  #
171
165
  # This is roughly equivalent to using `which` or `type` on the shell.
172
- # However, because those commands' behaviour and ouptut vary across
166
+ # However, because those commands' behaviour and output vary across
173
167
  # platforms and shells, we instead use the logic in #cmd_dir.
174
168
  def which cmd_name
175
169
  matching_dir = cmd_dir(cmd_name)
@@ -210,13 +204,11 @@ module Babushka
210
204
  # Sleeping for a bit... done.
211
205
  def log_shell message, *cmd, &block
212
206
  opts = cmd.extract_options!
213
- log_block message do
214
- shell *cmd.dup.push(opts.merge(:spinner => true)), &block
207
+ LogHelpers.log_block message do
208
+ shell(*cmd.dup.push(opts.merge(:spinner => true)), &block)
215
209
  end
216
210
  end
217
211
 
218
- private
219
-
220
212
  def shell_cmd *cmd, &block
221
213
  Shell.new(*cmd).run(&block)
222
214
  end