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.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/hammer/builtins.rb', line 277 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
304 305 306 307 |
# File 'lib/hammer/builtins.rb', line 304 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.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/hammer/builtins.rb', line 237 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.
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 209 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
270 271 272 273 |
# File 'lib/hammer/builtins.rb', line 270 def path(name) path = Hammer::Recipe.path(name) or fail_unknown(name) puts path end |
.require_name!(name, action) ⇒ Object
202 203 204 205 206 |
# File 'lib/hammer/builtins.rb', line 202 def require_name!(name, action) return name if name Shell.print_error "missing recipe name (usage: h:recipes --#{action} NAME)" exit 1 end |