Class: Tmuxinator::Cli

Inherits:
Thor
  • Object
show all
Includes:
Util
Defined in:
lib/tmuxinator/cli.rb

Constant Summary collapse

COMMANDS =
{
  commands: "Lists commands available in tmuxinator",
  completions: "Used for shell completion",
  copy: %w{
    Copy an existing project to a new project and
    open it in your editor
  }.join(" "),
  debug: "Output the shell commands that are generated by tmuxinator",
  delete: "Deletes given project",
  doctor: "Look for problems in your configuration",
  edit: "Alias of new",
  help: "Shows help for a specific command",
  implode: "Deletes all tmuxinator projects",
  local: "Start a tmux session using ./.tmuxinator.y[a]ml",
  list: "Lists all tmuxinator projects",
  new: "Create a new project file and open it in your editor",
  open: "Alias of new",
  start: %w{
    Start a tmux session using a project's name (with an optional [ALIAS]
    for project reuse) or a path to a project config file (via the -p flag)
  }.join(" "),
  stop: "Stop a tmux session using a project's tmuxinator config",
  stop_all: "Stop all tmux sessions which are using tmuxinator projects",
  version: "Display installed tmuxinator version",
}.freeze
THOR_COMMANDS =

For future reference: due to how tmuxinator currently consumes command-line arguments (see ::bootstrap, below), invocations of Thor’s base commands (i.e. ‘help’, etc) can be instead routed to #start (rather than to ::start). In order to prevent this, the THOR_COMMANDS and RESERVED_COMMANDS constants have been introduced. The former enumerates any/all Thor commands we want to insure get passed through to Thor.start. The latter is the superset of the Thor commands and any tmuxinator commands, defined in COMMANDS, above.

%w[-v help].freeze
RESERVED_COMMANDS =
(COMMANDS.keys + THOR_COMMANDS).map(&:to_s).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#current_session_name, #exit!, #yes_no

Class Method Details

.bootstrap(args = []) ⇒ Object

This method was defined as something of a workaround… Previously the conditional contained within was in the executable (i.e. bin/tmuxinator). It has been moved here so as to be testable. A couple of notes:

  • ::start (defined in Thor::Base) expects the first argument to be an

array or ARGV, not a varargs. Perhaps ::bootstrap should as well?

  • ::start has a different purpose from #start and hence a different

signature



516
517
518
519
520
521
522
523
524
525
526
# File 'lib/tmuxinator/cli.rb', line 516

def self.bootstrap(args = [])
  name = args[0] || nil
  if args.empty? && Tmuxinator::Config.local?
    Tmuxinator::Cli.new.local
  elsif name && !Tmuxinator::Cli::RESERVED_COMMANDS.include?(name) &&
        Tmuxinator::Config.exist?(name: name)
    Tmuxinator::Cli.start([:start, *args])
  else
    Tmuxinator::Cli.start(args)
  end
end

.exit_on_failure?Boolean

By default, Thor returns exit(0) when an error occurs. Please see: github.com/tmuxinator/tmuxinator/issues/192

Returns:

  • (Boolean)


9
10
11
# File 'lib/tmuxinator/cli.rb', line 9

def self.exit_on_failure?
  true
end

Instance Method Details

#commands(shell = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tmuxinator/cli.rb', line 57

def commands(shell = nil)
  out = if shell == "zsh"
          COMMANDS.map do |command, desc|
            "#{command}:#{desc}"
          end.join("\n")
        else
          COMMANDS.keys.join("\n")
        end

  say out
end

#completions(arg) ⇒ Object



71
72
73
74
75
76
# File 'lib/tmuxinator/cli.rb', line 71

def completions(arg)
  if %w(start stop edit open copy delete).include?(arg)
    configs = Tmuxinator::Config.configs
    say configs.join("\n")
  end
end

#copy(existing = nil, new = nil) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/tmuxinator/cli.rb', line 393

def copy(existing = nil, new = nil)
  if options[:help] || existing.nil? || new.nil?
    invoke :help, ["copy"]
    return
  end

  existing_config_path = Tmuxinator::Config.project(existing)
  new_config_path = Tmuxinator::Config.project(new)

  exit!("Project #{existing} doesn't exist!") \
    unless Tmuxinator::Config.exist?(name: existing)

  new_exists = Tmuxinator::Config.exist?(name: new)
  question = "#{new} already exists, would you like to overwrite it?"
  if !new_exists || yes?(question, :red)
    say "Overwriting #{new}" if Tmuxinator::Config.exist?(name: new)
    FileUtils.copy_file(existing_config_path, new_config_path)
  end

  Kernel.system("$EDITOR #{new_config_path}")
end

#debug(name = nil, *args) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/tmuxinator/cli.rb', line 373

def debug(name = nil, *args)
  if options[:help]
    invoke :help, ["debug"]
    return
  end

  params = start_params(name, *args)

  project = create_project(params)

  say project.render
end

#delete(*projects) ⇒ Object



422
423
424
425
426
427
428
429
# File 'lib/tmuxinator/cli.rb', line 422

def delete(*projects)
  if options[:help] || projects.empty?
    invoke :help, ["delete"]
    return
  end

  delete_projects(*projects)
end

#doctorObject



497
498
499
500
501
502
503
504
505
506
# File 'lib/tmuxinator/cli.rb', line 497

def doctor
  say "Checking if tmux is installed ==> "
  yes_no Tmuxinator::Doctor.installed?

  say "Checking if $EDITOR is set ==> "
  yes_no Tmuxinator::Doctor.editor?

  say "Checking if $SHELL is set ==> "
  yes_no Tmuxinator::Doctor.shell?
end

#implodeObject



451
452
453
454
455
456
457
458
# File 'lib/tmuxinator/cli.rb', line 451

def implode
  if yes?("Are you sure you want to delete all tmuxinator configs?", :red)
    Tmuxinator::Config.directories.each do |directory|
      FileUtils.remove_dir(directory)
    end
    say "Deleted all tmuxinator projects."
  end
end

#listObject



473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/tmuxinator/cli.rb', line 473

def list
  if options[:help]
    invoke :help, ["list"]
    return
  end

  say "tmuxinator projects:"
  configs = Tmuxinator::Config.configs(active: options[:active])
  if options[:newline]
    say configs.join("\n")
  else
    print_in_columns configs
  end
end

#localObject



349
350
351
352
353
354
355
# File 'lib/tmuxinator/cli.rb', line 349

def local
  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  render_project(create_project(attach: options[:attach]))
end

#new(name = nil, session = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tmuxinator/cli.rb', line 91

def new(name = nil, session = nil)
  if options[:help] || name.nil?
    invoke :help, ["new"]
    return
  end

  if session
    new_project_with_session(name, session)
  else
    new_project(name)
  end
end

#start(name = nil, *args) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/tmuxinator/cli.rb', line 274

def start(name = nil, *args)
  if options[:help]
    invoke :help, ["start"]
    return
  end

  params = start_params(name, *args)

  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  project = create_project(params)
  render_project(project)
end

#stop(name = nil) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/tmuxinator/cli.rb', line 300

def stop(name = nil)
  if options[:help]
    invoke :help, ["stop"]
    return
  end

  # project-config takes precedence over a named project in the case that
  # both are provided.
  if options["project-config"]
    name = nil
  end

  params = {
    name: name,
    project_config: options["project-config"]
  }
  show_version_warning if version_warning?(
    options["suppress-tmux-version-warning"]
  )

  project = create_project(params)
  kill_project(project)
end

#stop_allObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/tmuxinator/cli.rb', line 329

def stop_all
  # We only need to stop active projects
  configs = Tmuxinator::Config.configs(active: true)

  unless options[:noconfirm]
    say "Stop all active projects:\n\n", :yellow
    say configs.join("\n")
    say "\n"

    return unless yes?("Are you sure? (n/y)")
  end

  Project.stop_all
end

#versionObject



491
492
493
# File 'lib/tmuxinator/cli.rb', line 491

def version
  say "tmuxinator #{Tmuxinator::VERSION}"
end