class Otis::Generator::CLI

Public Class Methods

source_root() click to toggle source
# File lib/otis/generator/cli.rb, line 7
def self.source_root
  File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
end

Public Instance Methods

generate() click to toggle source
# File lib/otis/generator/cli.rb, line 15
def generate
  wsdl = options[:wsdl]
  n = name
  name = n.chomp("/") # remove trailing slash if present
  unless wsdl
    puts "You did not provide any WSDL file. Sorry, I can't guess the structure of the webservice without it!"
    puts "Bye."
    exit
  end
  routes = routes(wsdl)

  generate_soap(name, routes)

  namespaced_path = name.tr('-', '/')
  target = File.join(Dir.pwd, name)
  create_config(target, wsdl)

  constant_name = constantize(name)
  constant_array = constant_name.split('::')
  git_user_name = `git config user.name`.chomp
  git_user_email = `git config user.email`.chomp
  opts = {
    :name            => name,
    :models          => routes.keys,
    :namespaced_path => namespaced_path,
    :constant_name   => constant_name,
    :routes          =>  Otis::Generator::RoutesPresenter.new(routes),
    :wsdl            => wsdl,
    :constant_array  => constant_array,
    :author          => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
    :email           => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
    :test            => options[:test]
  }

  gemspec_dest = File.join(target, "#{name}.gemspec")
  template(File.join("newgem/Gemfile.tt"),               File.join(target, "Gemfile"),                             opts)
  template(File.join("newgem/Rakefile.tt"),              File.join(target, "Rakefile"),                            opts)
  template(File.join("newgem/LICENSE.txt.tt"),           File.join(target, "LICENSE.txt"),                         opts)
  template(File.join("newgem/README.md.tt"),             File.join(target, "README.md"),                           opts)
  template(File.join("newgem/gitignore.tt"),             File.join(target, ".gitignore"),                          opts)
  template(File.join("newgem/newgem.gemspec.tt"),        gemspec_dest,                                             opts)
  template(File.join("newgem/lib/newgem.rb.tt"),         File.join(target, "lib/#{namespaced_path}.rb"),           opts)
  template(File.join("newgem/lib/newgem/version.rb.tt"), File.join(target, "lib/#{namespaced_path}/version.rb"),   opts)

  template(File.join("newgem/rspec.tt"),               File.join(target, ".rspec"),                              opts)
  template(File.join("newgem/spec/spec_helper.rb.tt"), File.join(target, "spec/spec_helper.rb"),                 opts)
  template(File.join("newgem/spec/newgem_spec.rb.tt"), File.join(target, "spec/#{namespaced_path}/#{namespaced_path}_spec.rb"),     opts)
  template(File.join("newgem/spec/integration/newgem_integration_spec.tt"), File.join(target, "spec/integration/#{namespaced_path}_integration_spec.rb"),     opts)

  Dir.chdir(target) { `git init`; `git add .` }
end

Private Instance Methods

constantize(name) click to toggle source
# File lib/otis/generator/cli.rb, line 90
def constantize(name)
  constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] }.join
  constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
  constant_name
end
create_config(target, wsdl) click to toggle source
# File lib/otis/generator/cli.rb, line 76
def create_config(target, wsdl)
  config = File.join(target, 'config')
  Dir.mkdir(config)
  FileUtils.cp wsdl, File.join(config, wsdl)
end
generate_soap(name, routes) click to toggle source
# File lib/otis/generator/cli.rb, line 68
def generate_soap(name, routes)
  target = File.join(Dir.pwd, name)
  # Generate the models
  routes.each_pair do |file_name, class_name|
    template(File.join("newgem/lib/newgem/model.rb.tt"), File.join(target, "lib/#{name}/#{file_name.to_s}.rb"), {klass: class_name, name: constantize(name)})
  end
end
routes(wsdl) click to toggle source
# File lib/otis/generator/cli.rb, line 82
def routes(wsdl)
  #TODO: Make it break nicely when the file is not found.
  routes = {}
  keys = Savon.client(wsdl: "./#{wsdl}").operations
  keys.each {|name| routes[name] = constantize(name.to_s)}
  routes
end