Module: Hammer::Builtins::RecipesActions

Defined in:
lib/hammer/builtins.rb

Overview

Implementations of the ‘recipes` task’s action flags, plus the no-flag listing view. Separate module so the task definition above stays small.

Class Method Summary collapse

Class Method Details

.edit(name) ⇒ Object

For a gem recipe, offer to copy to user dir first so edits survive ‘hammer update`. Then exec $EDITOR on the file.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/hammer/builtins.rb', line 246

def edit(name)
  path = Hammer::Recipe.path(name) or fail_unknown(name)
  editor = ENV['EDITOR'] || ENV['VISUAL']
  unless editor
    Shell.print_error '$EDITOR not set'
    exit 1
  end

  if path.start_with?(Hammer::Recipe::GEM_DIR)
    user_dir = ENV['HAMMER_RECIPES_DIR'] || File.expand_path('~/.config/hammer/recipes')
    target = File.join(user_dir, "#{name}.rb")
    unless File.exist?(target)
      if Shell.yes?("copy gem recipe to #{target} before editing? (recommended)")
        require 'fileutils'
        FileUtils.mkdir_p(user_dir)
        FileUtils.cp(path, target)
        Shell.say "copied to #{target}", :green
        path = target
      end
    else
      path = target
    end
  end

  system(editor, path)
end

.fail_unknown(name) ⇒ Object



273
274
275
276
# File 'lib/hammer/builtins.rb', line 273

def fail_unknown(name)
  Shell.print_error "unknown recipe: #{name}"
  exit 1
end

.install(name, target = nil) ⇒ Object

With no NAME: arrow-key picker. With NAME only: print the stub to stdout (user pipes it themselves). With NAME + TARGET: write the stub to TARGET and chmod +x in one shot.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/hammer/builtins.rb', line 206

def install(name, target = nil)
  if name.nil?
    names = Hammer::Recipe.all.keys.sort
    if names.empty?
      Shell.print_error 'no recipes available'
      exit 1
    end
    idx = Shell.choose('pick a recipe to install', names)
    exit 1 if idx.nil?
    name = names[idx]
  end

  unless Hammer::Recipe.path(name)
    Shell.print_error "unknown recipe: #{name}"
    exit 1
  end

  stub = Hammer::Recipe.stub(name)
  if target
    path = File.expand_path(target)
    File.write(path, stub)
    File.chmod(0o755, path)
    Shell.say "installed #{name} -> #{path}", :green
  else
    puts stub
  end
end

.listObject

Group by source dir; each row shows desc and installed-or-not.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hammer/builtins.rb', line 178

def list
  groups = Hammer::Recipe.grouped
  if groups.empty?
    Shell.say 'no recipes found', :gray
    return
  end

  rows = Hammer::Recipe.all.map do |name, path|
    [name, Hammer::Recipe.desc(path), Hammer::Recipe.installed_path(name)]
  end
  width = rows.map { |n, _, _| n.length }.max

  groups.each_with_index do |(source, items), i|
    Shell.say '' if i > 0
    Shell.say "#{source}:", :yellow
    items.each_key do |name|
      _n, desc, installed = rows.find { |r| r.first == name }
      status = installed ? "(installed: #{installed})" : "[install: hammer recipes --install #{name}]"
      line = "  #{name.ljust(width)}  # #{desc}"
      Shell.say line
      Shell.say "  #{' ' * width}    #{status}", :gray
    end
  end
end

.path(name) ⇒ Object



239
240
241
242
# File 'lib/hammer/builtins.rb', line 239

def path(name)
  path = Hammer::Recipe.path(name) or fail_unknown(name)
  puts path
end

.require_name!(name, action) ⇒ Object



171
172
173
174
175
# File 'lib/hammer/builtins.rb', line 171

def require_name!(name, action)
  return name if name
  Shell.print_error "missing recipe name (usage: recipes --#{action} NAME)"
  exit 1
end

.show(name) ⇒ Object



234
235
236
237
# File 'lib/hammer/builtins.rb', line 234

def show(name)
  path = Hammer::Recipe.path(name) or fail_unknown(name)
  puts File.read(path)
end