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
-
.edit(name) ⇒ Object
For a gem recipe, offer to copy to user dir first so edits survive
hammer h:update. - .fail_unknown(name) ⇒ Object
-
.install(name, target = nil) ⇒ Object
With no NAME: arrow-key picker.
-
.list ⇒ Object
Group by source dir; each row shows desc and installed-or-not.
- .path(name) ⇒ Object
- .require_name!(name, action) ⇒ Object
- .show(name) ⇒ Object
Class Method Details
.edit(name) ⇒ Object
For a gem recipe, offer to copy to user dir first so edits
survive hammer h:update. Then exec $EDITOR on the file.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/hammer/builtins.rb', line 321 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.('~/.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
348 349 350 351 |
# File 'lib/hammer/builtins.rb', line 348 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.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/hammer/builtins.rb', line 281 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.(target) File.write(path, stub) File.chmod(0o755, path) Shell.say "installed #{name} -> #{path}", :green else puts stub end end |
.list ⇒ Object
Group by source dir; each row shows desc and installed-or-not.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/hammer/builtins.rb', line 253 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 h:recipes --install #{name}]" line = " #{name.ljust(width)} # #{desc}" Shell.say line Shell.say " #{' ' * width} #{status}", :gray end end end |
.path(name) ⇒ Object
314 315 316 317 |
# File 'lib/hammer/builtins.rb', line 314 def path(name) path = Hammer::Recipe.path(name) or fail_unknown(name) puts path end |
.require_name!(name, action) ⇒ Object
246 247 248 249 250 |
# File 'lib/hammer/builtins.rb', line 246 def require_name!(name, action) return name if name Shell.print_error "missing recipe name (usage: h:recipes --#{action} NAME)" exit 1 end |