Class: Gritano::Console::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gritano/console/base.rb

Direct Known Subclasses

Executor, Gritano, Installer, Remote

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/gritano/console/base.rb', line 7

def initialize(stdin = STDIN, home_dir = Etc.getpwuid.dir)
  @home_dir = home_dir
  @stdin = stdin
end

Class Method Details

.add_command(command, parameters = "", &block) ⇒ Object



12
13
14
15
# File 'lib/gritano/console/base.rb', line 12

def self.add_command(command, parameters = "", &block)
  define_method(command.gsub(':', '_'), &block)
  commands[command] = parameters
end

.before_each_command(&block) ⇒ Object



17
18
19
# File 'lib/gritano/console/base.rb', line 17

def self.before_each_command(&block)
  define_method(:before_each_command_filter, &block);
end

.bin_nameObject



29
30
31
# File 'lib/gritano/console/base.rb', line 29

def self.bin_name
  @bin_name || "gritano "
end

.bin_name=(name) ⇒ Object



33
34
35
# File 'lib/gritano/console/base.rb', line 33

def self.bin_name=(name)
  @bin_name = name
end

.commandsObject



21
22
23
# File 'lib/gritano/console/base.rb', line 21

def self.commands
  @commands || @commands = Hash.new
end

.commands=(cmds) ⇒ Object



25
26
27
# File 'lib/gritano/console/base.rb', line 25

def self.commands=(cmds)
  @commands = cmds
end

.helpObject



37
38
39
40
41
42
43
44
45
# File 'lib/gritano/console/base.rb', line 37

def self.help
  msg = "  #{self.bin_name}[command]\n\n"
  msg += "  Examples:\n"
  commands.each do |command, parameters|
    msg += "  #{self.bin_name}#{command} #{parameters}\n"
  end
  msg += "\n  --\n  v#{File.open(File.join(File.dirname(__FILE__), '..', '..', '..', 'VERSION')).readlines.join}"
  msg
end

Instance Method Details

#before_each_command_filterObject



74
75
# File 'lib/gritano/console/base.rb', line 74

def before_each_command_filter
end

#check_gitObject



63
64
65
66
67
68
# File 'lib/gritano/console/base.rb', line 63

def check_git
  if unknown_command('git')
    puts "Error: git must be installed on the local system"
    exit
  end
end

#check_gritanoObject



56
57
58
59
60
61
# File 'lib/gritano/console/base.rb', line 56

def check_gritano
  unless File.exist?(File.join(@home_dir, '.gritano'))
    puts "Error: First run 'gritano setup:prepare && gritano setup:install'"
    exit
  end
end

#execute(argv) ⇒ Object



51
52
53
54
# File 'lib/gritano/console/base.rb', line 51

def execute(argv)
  send(:before_each_command_filter)
  execute_without_filters(argv)
end

#execute_without_filters(argv) ⇒ Object



47
48
49
# File 'lib/gritano/console/base.rb', line 47

def execute_without_filters(argv)
  send(argv[0].gsub(':', '_'), argv[1..-1])
end

#unknown_command(command) ⇒ Object



70
71
72
# File 'lib/gritano/console/base.rb', line 70

def unknown_command(command)
  `which #{command}` == ""
end