Sha256: 7f77586a9afaa7e5b93b8fd4f010bbd3248b21ab81986a933d17db153338a848

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require "fileutils"

module Pliny::Commands
  class Creator
    attr_accessor :args, :opts, :stream

    def self.run(args, opts={}, stream=$stdout)
      new(args, opts, stream).run!
    end

    def initialize(args={}, opts={}, stream=$stdout)
      @args = args
      @opts = opts
      @stream = stream
    end

    def run!
      if File.exists?(app_dir)
        abort("#{name} already exists")
      end

      FileUtils.copy_entry template_dir, app_dir
      FileUtils.rm_rf("#{app_dir}/.git")
      setup_database_urls
      display "Pliny app created. To start, run:"
      display "cd #{app_dir} && bin/setup"
    end

    protected

    def setup_database_urls
      db = URI.parse("postgres:///#{name}")
      {
        ".env.sample" => "development",
        ".env.test"   => "test"
      }.each do |env_file, db_env_suffix|
        env_path = "#{app_dir}/#{env_file}"
        db.path  = "/#{name}-#{db_env_suffix}"
        env      = File.read(env_path)
        File.open(env_path, "w") do |f|
          # ruby's URI#to_s renders foo:/bar when there's no host
          # we want foo:///bar instead!
          db_url = db.to_s.sub(":/", ":///")
          f.puts env.sub(/DATABASE_URL=.*/, "DATABASE_URL=#{db_url}")
        end
      end
    end

    def display(msg)
      stream.puts msg
    end

    def name
      args.first
    end

    def template_dir
      File.expand_path('../../../template', File.dirname(__FILE__))
    end

    def app_dir
      "./#{name}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pliny-0.2.1 lib/pliny/commands/creator.rb
pliny-0.2.0 lib/pliny/commands/creator.rb
pliny-0.1.0 lib/pliny/commands/creator.rb
pliny-0.0.4 lib/pliny/commands/creator.rb