class Dotrun

Constants

DIRECTIVES_PATH

Public Instance Methods

run(*args) click to toggle source

The main operation method. This is called when running ‘dotrun` from the command line.

# File lib/dotrun.rb, line 9
def run(*args)
  print_help_and_exit if args.first == "-?"
  directive = args.first || "default"
  command = command_for(directive)
  
  if command.is_a?(Array)
    exec_multiple_directives directive, command
  else
    exec_directive directive, command
  end
end

Protected Instance Methods

check_for_ttab() click to toggle source

Checks if the ‘ttab` utility exists on the machine and errors out if not.

# File lib/dotrun.rb, line 102
def check_for_ttab
  msg = "It doesn't appear that ttab is installed. Please run "
  msg << "npm install ttab -g ".bold
  msg << "\r\nto install the utility in order to use the multi-command feature."
  abort(msg.red) unless system("which ttab")
end
command_for(directive) click to toggle source

Convenience method for fetching the command designated for the specified directive.

# File lib/dotrun.rb, line 90
def command_for(directive)
  directives[directive] || abort("Couldn't find the `#{directive}` directive in your config.\r\n".red)
end
directives() click to toggle source

Returns all of the directives as a Ruby object.

# File lib/dotrun.rb, line 85
def directives
  @directives ||= load_directives
end
exec_directive(directive, command) click to toggle source

Execute one of the directives in the list from the ‘.run` file.

# File lib/dotrun.rb, line 55
def exec_directive(directive, command)
  prep_screen
  puts "Running #{directive} directive: \"#{command}\"...".green
  puts "Use Ctrl-C to stop".green
  puts ""
  trap( :INT ) { puts "Terminating...".magenta }
  
  fork { exec command }
  Process.wait
  puts "All done.\r\n".light_blue
end
exec_multiple_directives(directive, command, auto_close = false) click to toggle source

Execute multiple commands in multiple tabs using ‘ttab`

# File lib/dotrun.rb, line 68
def exec_multiple_directives(directive, command, auto_close = false)
  check_for_ttab
  prep_screen
  puts "\n\n\n"
  puts "Running multiple directives in separate tabs:".green
  exit_cmd = "exit;" if auto_close
  for d in command
    print "  #{d}".ljust(20).magenta
    sleep 2
    system %(ttab -G eval "dotrun #{d}; #{exit_cmd}")
    puts " ✔︎".magenta
  end
  puts ""
  puts "Running!\r\n".light_blue
end
load_directives() click to toggle source

Load up the ‘.run` file from the current directory and populate the @directives instance variable.

# File lib/dotrun.rb, line 95
def load_directives
  abort("No .run file detected in current directory.\r\n".red) unless File.exist?(DIRECTIVES_PATH)
  loaded = YAML.load_file(DIRECTIVES_PATH)
  @directives = loaded.is_a?(String) ? { default: loaded } : loaded
end
prep_screen() click to toggle source

Clears the screen, prints a header, and the git branch (if available).

# File lib/dotrun.rb, line 40
def prep_screen
  system("clear")
  print "Current project is ".blue
  print "#{File.basename(File.expand_path("."))}".blue.on_light_white
  
  if File.exist?(".git")
    branch = `git rev-parse --abbrev-ref HEAD`
    print " (current branch: ".blue
    print branch.strip.blue.on_light_white
    print ")".blue
  end
  puts ""
end
print_help_and_exit() click to toggle source

Prints out the usage instructions and available command directives for the current directory.