require 'erubis' require 'mixlib/cli' module ZooMQ class CLI include Mixlib::CLI option :help, :short => '-h', :long => '--help', :description => "Show this message", :on => :tail, :boolean => true, :show_options => true, :exit => 0 option :author, :short => '-a', :long => '--author', :description => 'Gem author', :default => `git config user.name`.chomp option :email, :short => '-e', :long => '--email', :description => 'Author E-Mail', :default => `git config user.email`.chomp def initialize super parse_options puts config.inspect @root = File.expand_path('../../..', __FILE__) end def run self.__send__(ARGV.shift, *ARGV) end def generate(name) if File.exist?(name) puts "!!! #{name} already exists" exit(1) end puts ">>> Generating ZooMQ skeleton for #{name}" constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] }.join constant_name = constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/ constant_array = constant_name.split('::') config.merge!({ name: name, constant_name: constant_name, constant_array: constant_array, }) { "Gemfile" => "Gemfile", "Rakefile" => "Rakefile", "LICENSE.txt" => "LICENSE.txt", "README.md" => "README.md", ".gitignore" => "gitignore", "#{name}.gemspec" => "gemspec", "bin/#{name}" => "binwrapper", "bin/#{name}-console" => "console", "config.yml" => "config.yml", "config/mixins/production.yml" => "production.yml", "config/mixins/staging.yml" => "staging.yml", "config/mixins/testing.yml" => "testing.yml", "lib/#{name}/protocol.proto" => "protocol.proto", "lib/#{name}/protocol.rb" => "protocol.rb", "lib/#{name}/client.rb" => "client.rb", "#{name}/console.rb" => "console.rb", "#{name}/server.rb" => "server.rb", "#{name}/server/ping_request.rb" => "ping_request.rb", }.each do |dest, source| puts " * #{dest}" source = File.join(@root, 'templates', "#{source}.tt") dest = File.join(name, dest) FileUtils.mkdir_p(File.dirname(dest)) input = File.read(source) eruby = Erubis::Eruby.new(input) output = File.open(dest, "w") output.write(eruby.result(binding())) output.close end Dir.chdir(name) do puts ">>> Installing dependencies" system("bundle install") puts ">>> Generating protocol" system("rake protoc") system("chmod +x bin/*") end end end end