0

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

したがって、サイトがまだ生成されていない場合は、使用状況のバナーが表示され、終了します。

トールを使用してこれを再現する最良の方法は何ですか?

これが私の仕事です。

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
4

1 に答える 1

0

spree gemのジェネレーターを調べた後、最初にサイトをチェックするメソッドを追加し、コンソールにエラーメッセージを出力した後、サイトが見つからない場合はコード1で終了します。コードは次のようになります。

def check_if_site_exists 
  unless File.directory?(path/to/site) 
     say "site does not exist." 
     exit 1
  end
end
于 2013-01-19T05:47:19.980 に答える