Sha256: 9e4d71eb0a4aa07700d61d2a1370f9f167d17994dd6fb789bc13b75629c0527e

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require 'fileutils'
require 'colorize'
require 'active_support/core_ext/string'

module ThorTemplate
  class Generator
    attr_reader :options
    def initialize(options={})
      @options = options
      Dir.chdir(options[:cwd]) if options[:cwd]
      @name = options[:name]
    end

    def run
      copy
      rename
      git
      puts "Created #{@name} project!"
    end

    def copy
      project_root = @name
      source_root = File.expand_path("../../starter_project", __FILE__)
      paths = Dir.glob("#{source_root}/**/{*,.*}").
                select {|p| File.file?(p) }
      paths.each do |src|
        dest = src.gsub(%r{.*starter_project/},'')
        dest = "#{project_root}/#{dest}"

        if File.exist?(dest) and !options[:force]
          puts "already exists: #{dest}".colorize(:yellow) unless options[:quiet]
        else
          puts "creating: #{dest}".colorize(:green) unless options[:quiet]
          dirname = File.dirname(dest)
          FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
          FileUtils.cp(src, dest)
        end
      end
    end

    def rename
      Renamer.new(@name).rename!
    end

    def git
      system("cd #{@name} && git init")
      system("cd #{@name} && git add .")
      system("cd #{@name} && git commit -m 'first commit'")
    end

private
    def template(src,dest)
      templates = File.expand_path("../../templates", __FILE__)
      FileUtils.cp("#{templates}/#{src}", "#{@name}/#{dest}")
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
thor_template-2.2.1 lib/thor_template/generator.rb
thor_template-2.2.0 lib/thor_template/generator.rb
thor_template-2.1.1 lib/thor_template/generator.rb
thor_template-2.1.0 lib/thor_template/generator.rb
thor_template-2.0.2 lib/thor_template/generator.rb