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: "Edit an existing project file in your editor",
  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: "Create or open a project file in your editor",
  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



555
556
557
558
559
560
561
562
563
564
565
# File 'lib/tmuxinator/cli.rb', line 555

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



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/tmuxinator/cli.rb', line 432

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



412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/tmuxinator/cli.rb', line 412

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



461
462
463
464
465
466
467
468
# File 'lib/tmuxinator/cli.rb', line 461

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

  delete_projects(*projects)
end

#doctorObject



536
537
538
539
540
541
542
543
544
545
# File 'lib/tmuxinator/cli.rb', line 536

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

#edit(name = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tmuxinator/cli.rb', line 127

def edit(name = nil)
  if options[:help] || (name.nil? && !options[:local])
    invoke :help, ["edit"]
    return
  end

  path = config_path(name, local: options[:local])
  if File.exist?(path)
    Kernel.system("$EDITOR #{path}") || doctor
  elsif options[:local] && name.nil?
    exit! Tmuxinator::Config::NO_LOCAL_FILE_MSG
  else
    exit! "Project #{name} doesn't exist!"
  end
end

#implodeObject



490
491
492
493
494
495
496
497
# File 'lib/tmuxinator/cli.rb', line 490

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



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

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



388
389
390
391
392
393
394
# File 'lib/tmuxinator/cli.rb', line 388

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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tmuxinator/cli.rb', line 87

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

#open(name = nil) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/tmuxinator/cli.rb', line 109

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

  new_project(name)
end

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



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/tmuxinator/cli.rb', line 313

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



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/tmuxinator/cli.rb', line 339

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/tmuxinator/cli.rb', line 368

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



530
531
532
# File 'lib/tmuxinator/cli.rb', line 530

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