lib/generators/pakyow/app/app_generator.rb in pakyow-0.10.2 vs lib/generators/pakyow/app/app_generator.rb in pakyow-0.11.0

- old
+ new

@@ -1,22 +1,27 @@ +require 'erb' require 'fileutils' +require 'securerandom' +require 'pakyow/version.rb' module Pakyow module Generators class AppGenerator class << self - def start - case ARGV.first - when '--help', '-h', nil - puts File.open(File.join(PAK_PATH, 'commands/USAGE-NEW')).read - else - generator = self.new(ARGV.first) - generator.build - end + def start(destination) + generator = self.new(destination) + generator.build end end + FILENAME_TRANSLATIONS = { + 'rspec' => '.rspec', + 'gitignore' => '.gitignore', + 'env' => '.env', + 'env.example' => '.env.example' + } + def initialize(dest) @src = "#{File.expand_path('../', __FILE__)}/templates/." @dest = dest end @@ -36,25 +41,53 @@ exit end end exec - puts "Done! Run `cd #{@dest}; pakyow server` to get started!" + puts "Done! Run `cd #{@dest}; bundle exec pakyow server` to get started!" end protected - # copies src files to dest def copy - FileUtils.cp_r(@src, @dest) + FileUtils.mkdir(@dest) unless File.exists?(@dest) + + Dir.glob(File.join(@src, '**', '*')).each do |path| + relative_path = path[@src.length..-1] + generated_path = File.join(@dest, File.dirname(relative_path), translated_filename(File.basename(relative_path))) + + if File.directory?(path) + FileUtils.mkdir(generated_path) + next + end + + erb = ERB.new(File.read(path)) + File.open(generated_path, 'w') { |f| f.write(erb.result(binding)) } + end end - # performs and other setup (e.g. bundle install) def exec FileUtils.cd(@dest) do puts "Running `bundle install` in #{Dir.pwd}" - system("bundle install") + system("bundle install --binstubs") end + end + + def generating_locally? + local_pakyow = Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }.detect{|k,v| k == 'pakyow'} + !local_pakyow || local_pakyow.last.last.version < Gem::Version.new(Pakyow::VERSION) + end + + def translated_filename(filename) + FILENAME_TRANSLATIONS.fetch(filename, filename) + end + + def generate_session_secret + SecureRandom.hex(64) + end + + def app_name + File.basename(@dest) end end end end