Sha256: 24b378d761423dfe6bb31d999c37eb11bc8e3b80de36205a8d009a5e8c11c629

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'erb'
require 'pathname'

module Shoe
  class Generator
    def initialize(root)
      @root          = Pathname.new(root).expand_path
      @template_path = Pathname.new(__FILE__).dirname.join('templates')
    end

    def run
      path('README.rdoc').install template('readme.erb')
      path('Rakefile').install    template('rakefile.erb')
      path(version_path).install  template('version.erb')
      path(gemspec_path).install  template('gemspec.erb')
    end

    private

    def project_name
      @root.basename.to_s
    end

    def project_module
      project_name.capitalize.gsub(/_(\w)/) { $1.upcase }
    end

    def project_version
      '0.0.0'
    end

    def version_path
      "lib/#{project_name}/version.rb"
    end

    def gemspec_path
      "#{project_name}.gemspec"
    end

    def template(name)
      ERB.new(template_contents(name)).result(binding)
    end

    def template_contents(name)
      @template_path.join(name).read
    end

    def path(name)
      path = @root.join(name)
      path.dirname.mkpath
      path.extend(PathExtensions)
    end

    module PathExtensions #:nodoc:
      def install(contents)
        if exist?
          $stderr.puts "#{to_s} exists. Not clobbering."
        else
          open('w') { |file| file.write(contents) }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shoe-0.5.1 lib/shoe/generator.rb
shoe-0.5.0 lib/shoe/generator.rb