class ServiceContract::Generator::CLI

Public Instance Methods

gem(contract_name) click to toggle source
# File lib/service_contract/generator/cli.rb, line 19
def gem(contract_name)
  path = contract_name.gsub("-", "/")
  module_name = ActiveSupport::Inflector.camelize(path)
  module_names = module_name.split("::")

  # create directory structure
  commands = [
    # create gem stub
    %{bundle gem #{contract_name}},

    # create contracts folder
    %{mkdir -p #{contract_name}/contracts/},
    %{touch #{contract_name}/contracts/},

    # append tasks to Rakefile
    %{echo "require 'service_contract/tasks'" >> #{contract_name}/Rakefile}
  ]
  commands.each do |command|
    puts command
    `#{command}`
  end

  # add service_contract to .gemspec
  gemspec = "#{contract_name}/#{contract_name}.gemspec"
  dependency_line = `grep -m 1 -n '.add_de' #{gemspec}`
  matched = false
  output = ""
  File.open(gemspec, "r") do |file|
    file.each_line do |line|
      puts line
      if !matched && match = line.match(/([^\.]+)\.add_de/)
        puts "matched"
        output += %{#{match[1]}.add_dependency "service_contract"\n}
        matched = true
      end
      output += line
    end
  end
  puts output
  File.open(gemspec, "w") do |file|
    file.write(output)
  end

  # template files
  path_to_root = (Array.new(2 + module_name.split("::").length) {".."}).join("/")
  service_name = ActiveSupport::Inflector.humanize(contract_name)
  b = binding()
  %w(documentation.rb.erb service.rb.erb).each do |template|
    output_file = File.join(contract_name, 'lib', path, File.basename(template, ".erb"))
    write_template(template, output_file, b)
  end

  write_template("module.rb.erb", File.join(contract_name, "lib", "#{path}.rb"), b)
end
protocol(namespace, protocol_name, version) click to toggle source
# File lib/service_contract/generator/cli.rb, line 10
def protocol(namespace, protocol_name, version)
  folder = File.join("contracts", version, "source")
  FileUtils.mkdir_p(folder)
  output_file = File.join(folder, "#{ActiveSupport::Inflector.underscore(protocol_name)}.avdl")
  b = binding()
  write_template("protocol.avdl.erb", output_file, b)
end

Protected Instance Methods

write_template(name, output_file, bind) click to toggle source
# File lib/service_contract/generator/cli.rb, line 76
def write_template(name, output_file, bind)
  template_folder = File.expand_path("../../../../template", __FILE__)
  input = File.join(template_folder, name)
  erb = ERB.new(File.read(input))
  File.open(output_file, 'w') do |file|
    file.write(erb.result(bind))
  end
end