我正在从RubiGen转换一个生成器,并希望如果条件不满足,Thor :: Group中的任务组不会完成。
RubiGen生成器看起来像这样:
def initialize(runtime_args, runtime_options = {})
super
usage if args.size != 2
@name = args.shift
@site_name=args.shift
check_if_site_exists
extract_options
end
def check_if_site_exists
unless File.directory?(File.join(destination_root,'lib','sites',site_name.underscore))
$stderr.puts "******No such site #{site_name} exists.******"
usage
end
end
因此,如果该网站尚未生成,则显示使用横幅并退出。
使用thor重新创建它的最佳方法是什么?
这是我的任务。
class Page < Thor::Group
include Thor::Actions
source_root File.expand_path('../templates', __FILE__)
argument :name
argument :site_name
argument :subtype, :optional => true
def create_page
check_if_site_exists
page_path = File.join('lib', 'sites', "#{site_name}")
template('page.tt', "#{page_path}/pages/#{name.underscore}_page.rb")
end
def create_spec
base_spec_path = File.join('spec', 'isolation', "#{site_name}")
if subtype.nil?
spec_path = base_spec_path
else
spec_path = File.join("#{base_spec_path}", 'isolation')
end
template('functional_page_spec.tt', "#{spec_path}/#{name.underscore}_page_spec.rb")
end
protected
def check_if_site_exists # :nodoc:
$stderr.puts "#{site_name} does not exist." unless File.directory?(File.join(destination_root,'lib','sites', site_name.underscore))
end
end
答案 0 :(得分:0)
在查看spree gem的生成器之后,我首先添加了一个方法来检查站点,然后在向控制台吐出错误消息后找不到站点时使用代码1退出。代码看起来像这样:
def check_if_site_exists
unless File.directory?(path/to/site)
say "site does not exist."
exit 1
end
end