Sha256: 893d5429c20283ddc47d7eca97722f40d298a0821561e30d76527da31fe1db82

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'fileutils'
require 'pathname'
require 'pliny/version'
require 'uri'
require 'erb'
require 'ostruct'

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!
      abort("#{name} already exists") if File.exist?(app_dir)

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

    protected

    def parse_erb_files
      Dir.glob("#{app_dir}/{*,.*}.erb").each do |file|
        static_file = file.gsub(/\.erb$/, '')

        template = ERB.new(File.read(file))
        context = OpenStruct.new(app_name: name)
        content = template.result(context.instance_eval { binding })

        File.open(static_file, "w") do |f|
          f.write content
        end
        FileUtils.rm(file)
      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
      Pathname.new(name).expand_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pliny-1.1.0 lib/pliny/commands/creator.rb
pliny-1.0.0 lib/pliny/commands/creator.rb