Skip to content

Commit 382414a

Browse files
committed
Add support for automatically running a task when no task is specified.
1 parent edca6cb commit 382414a

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

runx.rb

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
require 'pathname'
22

33
class Task
4-
def initialize(name, doc, block, dir)
4+
def initialize(name, doc, block, dir, auto)
55
@name = name
66
@doc = doc
77
@block = block
88
@dir = dir
9+
@auto = auto
910
end
1011

1112
def run(manager, *args)
@@ -14,6 +15,10 @@ def run(manager, *args)
1415
end
1516
end
1617

18+
def auto?
19+
@auto
20+
end
21+
1722
attr_accessor :name, :doc
1823
end
1924

@@ -33,6 +38,15 @@ def initialize(name)
3338
attr_reader :name
3439
end
3540

41+
class MultipleAutoError < StandardError
42+
def initialize(auto, current)
43+
@auto = auto
44+
@current = current
45+
end
46+
47+
attr_reader :auto, :current
48+
end
49+
3650
class TaskManager
3751
def initialize
3852
@tasks = {}
@@ -47,13 +61,24 @@ def load(file)
4761

4862
def show_help
4963
$stderr.puts 'Tasks:'
50-
width = @tasks.map { |name, task| name.length }.max
51-
@tasks.each do |name, task|
64+
65+
# Format to show which task is the auto task, if any.
66+
tasks = Hash[@tasks.map { |name, task|
67+
[name + (task.auto? ? '*' : ''), task]
68+
}]
69+
70+
width = tasks.keys.map(&:length).max
71+
tasks.each do |name, task|
5272
space = ' ' * (width - name.length + 6)
53-
$stderr.puts " #{task.name}#{space}#{task.doc}"
73+
$stderr.puts " #{name}#{space}#{task.doc}"
5474
end
5575
end
5676

77+
def auto_task
78+
task = @tasks.values.find(&:auto?)
79+
task.name if task
80+
end
81+
5782
def task_defined?(name)
5883
!@tasks[name.to_s.downcase].nil?
5984
end
@@ -73,9 +98,15 @@ def initialize(dir, manager)
7398
@tasks = {}
7499
@doc = nil
75100
@dir = dir
101+
@auto = false
102+
@auto_task = nil
76103
@manager = manager
77104
end
78105

106+
def auto
107+
@auto = true
108+
end
109+
79110
def doc(doc)
80111
@doc = doc
81112
end
@@ -90,8 +121,19 @@ def run(*args, &block)
90121
raise DuplicateTaskError.new(name)
91122
end
92123

93-
@tasks[key] = Task.new(name.to_s, @doc, block, @dir)
124+
task = Task.new(name.to_s, @doc, block, @dir, @auto)
125+
@tasks[key] = task
126+
127+
if @auto
128+
if @auto_task
129+
raise MultipleAutoError.new(@auto_task, task)
130+
else
131+
@auto_task = task
132+
end
133+
end
134+
94135
@doc = nil
136+
@auto = false
95137
else
96138
# Invoke task.
97139
name = args.first
@@ -158,9 +200,9 @@ def find_runfile
158200
dir = File.dirname(runfile)
159201
$stderr.puts "[runx] In #{dir}."
160202

161-
task_name = ARGV[0]
203+
task_name = ARGV[0] || manager.auto_task
162204

163-
is_help = ['--help', 'help'].include?(task_name)
205+
is_help = ['-h', '--help', 'help'].include?(task_name)
164206
show_help = !task_name || (is_help && !manager.task_defined?(task_name))
165207

166208
if show_help
@@ -180,6 +222,9 @@ def find_runfile
180222
rescue DuplicateTaskError => e
181223
$stderr.puts "[runx] Task '#{e.name}' is already defined."
182224
exit 1
225+
rescue MultipleAutoError => e
226+
$stderr.puts "[runx] Task '#{e.current.name}' cannot be auto, '#{e.auto.name}' is already auto."
227+
exit 1
183228
rescue Interrupt => e
184229
# Ignore interrupt and exit.
185230
end

0 commit comments

Comments
 (0)