Class: Samovar::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/command.rb,
lib/samovar/command/system.rb,
lib/samovar/command/track_time.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ Command

Returns a new instance of Command.



46
47
48
49
50
# File 'lib/samovar/command.rb', line 46

def initialize(input = nil)
	self.class.table.parse(input) do |key, value|
		self.send("#{key}=", value)
	end if input
end

Class Attribute Details

.descriptionObject

Returns the value of attribute description.



57
58
59
# File 'lib/samovar/command.rb', line 57

def description
  @description
end

Class Method Details

.append(row) ⇒ Object



64
65
66
67
68
# File 'lib/samovar/command.rb', line 64

def self.append(row)
	attr_accessor(row.key) if row.respond_to?(:key)
	
	self.table << row
end

.command_line(name) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/samovar/command.rb', line 104

def self.command_line(name)
	if @table
		"#{name} #{@table.usage}"
	else
		name
	end
end

.many(*args, **options) ⇒ Object



82
83
84
# File 'lib/samovar/command.rb', line 82

def self.many(*args, **options)
	append Many.new(*args, **options)
end

.nested(*args, **options) ⇒ Object



74
75
76
# File 'lib/samovar/command.rb', line 74

def self.nested(*args, **options)
	append Nested.new(*args, **options)
end

.one(*args, **options) ⇒ Object



78
79
80
# File 'lib/samovar/command.rb', line 78

def self.one(*args, **options)
	append One.new(*args, **options)
end

.options(*args, **options, &block) ⇒ Object



70
71
72
# File 'lib/samovar/command.rb', line 70

def self.options(*args, **options, &block)
	append Options.parse(*args, **options, &block)
end

.parse(input) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/samovar/command.rb', line 38

def self.parse(input)
	command = self.new(input)
	
	raise IncompleteParse.new("Could not parse #{input}") unless input.empty?
	
	return command
end

.split(*args, **options) ⇒ Object



86
87
88
# File 'lib/samovar/command.rb', line 86

def self.split(*args, **options)
	append Split.new(*args, **options)
end

.tableObject



60
61
62
# File 'lib/samovar/command.rb', line 60

def self.table
	@table ||= Table.new
end

.usage(rows, name) ⇒ Object



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

def self.usage(rows, name)
	rows.nested(name, self) do |rows|
		return unless @table
		
		@table.rows.each do |row|
			if row.respond_to?(:usage)
				row.usage(rows)
			else
				rows << row
			end
		end
	end
end

Instance Method Details

#[](key) ⇒ Object



52
53
54
# File 'lib/samovar/command.rb', line 52

def [] key
	@attributes[key]
end


112
113
114
115
116
117
118
# File 'lib/samovar/command.rb', line 112

def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter)
	rows = Output::Rows.new
	
	self.class.usage(rows, *args)
	
	formatter.print(rows, output)
end

#system(*args, **options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/samovar/command/system.rb', line 28

def system(*args, **options)
	command_line = args.join(' ')
	
	pid = Process.spawn(*args, **options)
	
	puts Rainbow(command_line).color(:blue)
	
	status = Process.waitpid2(pid).last
	
	return status.success?
rescue Errno::ENOENT
	return false
end

#system!(*args, **options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/samovar/command/system.rb', line 42

def system!(*args, **options)
	command_line = args.join(' ')
	
	pid = Process.spawn(*args, **options)
	
	puts Rainbow(command_line).color(:blue)
	
	status = Process.waitpid2(pid).last
	
	if status.success?
		return true
	else
		raise SystemError.new("Command #{command_line.dump} failed: #{status.to_s}")
	end
rescue Errno::ENOENT
	raise SystemError.new("Command #{command_line.dump} failed: #{$!}")
end

#track_timeObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/samovar/command/track_time.rb', line 25

def track_time
	start_time = Time.now
	
	yield
ensure
	end_time = Time.now
	elapsed_time = end_time - start_time
	
	$stdout.flush
	$stderr.puts Rainbow("Elapsed Time: %0.3fs" % elapsed_time).magenta
end