Skip to content

Commit 0f73c96

Browse files
committed
Allow Runfile to be named 'Runfile' or 'Runfile.rb'. Throw error if both exist.
1 parent a8a9c42 commit 0f73c96

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

runx.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ def initialize(auto, current)
4747
attr_reader :auto, :current
4848
end
4949

50+
class MultipleRunfileError < StandardError
51+
def initialize(path, files)
52+
@path = path
53+
@files = files.map { |file| File.basename(file) }
54+
end
55+
56+
attr_reader :path, :files
57+
end
58+
5059
class TaskManager
5160
def initialize
5261
@tasks = {}
@@ -175,9 +184,15 @@ def restore_env
175184

176185
def find_runfile
177186
Pathname.getwd.ascend do |path|
178-
runfile = File.join(path.to_s, 'Runfile')
179-
if File.exist?(runfile)
180-
return runfile.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
187+
files = ['Runfile', 'Runfile.rb'].map { |file|
188+
File.join(path.to_s, file)
189+
}.select { |file|
190+
File.exist?(file)
191+
}
192+
if files.length == 1
193+
return files.first.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
194+
elsif files.length == 2
195+
raise MultipleRunfileError.new(path, files)
181196
end
182197
end
183198

@@ -187,13 +202,13 @@ def find_runfile
187202
# Restore environment to match original.
188203
restore_env
189204

190-
runfile = find_runfile
191-
if runfile.nil?
192-
$stderr.puts '[runx] No Runfile found.'
193-
exit 1
194-
end
195-
196205
begin
206+
runfile = find_runfile
207+
if runfile.nil?
208+
$stderr.puts '[runx] Error: No Runfile or Runfile.rb found.'
209+
exit 1
210+
end
211+
197212
manager = TaskManager.new
198213
manager.load(runfile)
199214

@@ -216,14 +231,17 @@ def find_runfile
216231

217232
manager.run_task(task_name, *args)
218233
end
234+
rescue MultipleRunfileError => e
235+
$stderr.puts "[runx] Error: Multiple Runfiles found in #{e.path}: #{e.files.join(', ')}."
236+
exit 1
219237
rescue TaskNotFoundError => e
220-
$stderr.puts "[runx] Task '#{e.name}' not found."
238+
$stderr.puts "[runx] Error: Task '#{e.name}' not found."
221239
exit 1
222240
rescue DuplicateTaskError => e
223-
$stderr.puts "[runx] Task '#{e.name}' is already defined."
241+
$stderr.puts "[runx] Error: Task '#{e.name}' is already defined."
224242
exit 1
225243
rescue MultipleAutoError => e
226-
$stderr.puts "[runx] Task '#{e.current.name}' cannot be auto, '#{e.auto.name}' is already auto."
244+
$stderr.puts "[runx] Error: Task '#{e.current.name}' cannot be auto, '#{e.auto.name}' is already auto."
227245
exit 1
228246
rescue Interrupt => e
229247
# Ignore interrupt and exit.

0 commit comments

Comments
 (0)