summaryrefslogtreecommitdiff
path: root/bin/leap
blob: 9e0037a0af58833e7e968a4da129cc04d66ecfbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env ruby
begin
  require 'leap_cli'
rescue LoadError
  #
  # When developing a gem with a command, you normally use `bundle exec bin/command-name`
  # to run your app. At install-time, RubyGems will make sure lib, etc. are in the load path,
  # so that you can run the command directly.
  #
  # However, I don't like using 'bundle exec'. It is slow, and limits which directory you can
  # run in. So, instead, we fall back to some path manipulation hackery.
  #
  # This allows you to run the command directly while developing the gem, and also lets you
  # run from anywhere (I like to link 'bin/leap' to /usr/local/bin/leap).
  #
  require 'rubygems'
  base_dir = File.expand_path('..', File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__))
  require File.join([base_dir, 'lib','leap_cli','version.rb'])
  LeapCli::REQUIRE_PATHS.each do |path|
    path = File.expand_path(path, base_dir)
    $LOAD_PATH.unshift path unless $LOAD_PATH.include?(path)
  end
  require 'leap_cli'
end

require 'gli'
require 'highline'
require 'forwardable'
require 'terminal-table'

#
# Typically, GLI and Highline methods are loaded into the global namespace.
# Instead, here we load these into the module LeapCli::Commands in order to
# ensure that the cli logic and code is kept isolated to leap_cli/commands/*.rb
#
# no cheating!
#
module LeapCli::Commands
  extend GLI::App
  extend Forwardable
  extend Terminal::Table::TableHelper

  #
  # delegate highline methods to make them available to sub-commands
  #
  @terminal = HighLine.new
  def_delegator :@terminal, :ask,    'self.ask'
  def_delegator :@terminal, :agree,  'self.agree'
  def_delegator :@terminal, :choose, 'self.choose'
  def_delegator :@terminal, :say,    'self.say'
  def_delegator :@terminal, :color,  'self.color'
  def_delegator :@terminal, :list,   'self.list'

  #
  # make config manager available as 'manager'
  #
  def self.manager
    @manager ||= begin
      manager = LeapCli::Config::Manager.new
      manager.load(LeapCli::Path.provider)
      manager
    end
  end

  #
  # info about leap command line suite
  #
  program_desc       LeapCli::SUMMARY
  program_long_desc  LeapCli::DESCRIPTION

  #
  # handle --version ourselves
  #
  if ARGV.grep(/--version/).any?
    puts "leap #{LeapCli::VERSION}, ruby #{RUBY_VERSION}"
    exit(0)
  end

  #
  # load commands and run
  #
  commands_from('leap_cli/commands')
  exit run(ARGV)
end