-
Notifications
You must be signed in to change notification settings - Fork 0
/
cramkle
executable file
·93 lines (81 loc) · 2.53 KB
/
cramkle
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
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
COMMAND_DIR = File.expand_path(File.dirname(__FILE__))
BASENAME = File.basename(__FILE__)
PREFIX = "#{BASENAME}-"
class CramkleCli
attr_reader :commands
def initialize()
@commands = Dir.glob(File.join(File.dirname(__FILE__), "#{PREFIX}*"))
end
def list_commands
subcommands = []
@commands.each do |filename|
description = self.get_description(filename)
parameters = self.get_parameters(filename).join(" ")
# Remove the `BASENAME-` PREFIX from the filename to get the name of the subcommand
subcommand = File.basename(filename)[(BASENAME.size + 1)..-1]
subcommands.push({
:name => [subcommand, parameters].join(" "),
:description => description
})
end
max_subcommand_name_length = subcommands.map{ |subcommand| subcommand[:name].length }.max
subcommands
.sort_by { |subcommand| subcommand[:name] }
.each do |subcommand|
puts " %-#{max_subcommand_name_length}s %s" % [subcommand[:name], subcommand[:description]]
end
end
def get_description(filename)
# Optionally, subcommands can put a description on the second line of the file
lines = File.readlines(filename)
description = if lines[1] && lines[1].strip.start_with?('#')
lines[1].strip.gsub(/^#\s*/, '')
else
nil
end
return description
end
def get_parameters(filename)
# Optionally, retrieve parameters that are required
lines = File.readlines(filename)
header = lines.take_while{|l| l.strip.start_with?('#')}
parameters = header.map do |line|
if line.strip.start_with?('# @param')
line = line.strip.sub(/^# @param\s*/, '').sub(/-.*$/, '').strip
line = "<param>" if line.empty?
line
else
nil
end
end.compact
return parameters
end
def execute(command, args)
executable = File.join(COMMAND_DIR, PREFIX + command)
unless File.exist?(executable)
puts "I don't know how to #{command}. :("
abort
end
exec executable, *ARGV
end
end
command = ARGV.shift
cli = CramkleCli.new
case command
when nil, '--help', '-h'
puts "usage: #{BASENAME} <command> [<args>]"
puts ''
if cli.commands.empty?
puts 'Hrm, there are no commands for me to run.'
puts "I can run any executables in #{COMMAND_DIR} which have filenames that start with `#{PREFIX}`."
abort
else
puts 'Commands:'
cli.list_commands
end
when '--list', '-l'
cli.list_commands
else
cli.execute(command, ARGV)
end