Sha256: e24b4a414668d1a32e008b68b63e106e1caee38c63ab972dc518d4162c0303ef

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

require 'thor'
require 'fileutils'
require 'erb'

module Infrataster
  class CLI < Thor
    desc "init", "Initialize Infrataster specs."
    option :path, type: :string, default: '.'
    def init
      path = File.expand_path(options[:path])
      FileUtils.mkdir_p(path)
      create_file_from_template(File.expand_path("Gemfile", path))
      create_file_from_template(File.expand_path("Rakefile", path))
      FileUtils.mkdir(File.expand_path("spec", path))
      if ask_yes_or_no("Use Vagrant?")
        create_file_from_template(File.expand_path("spec/Vagrantfile", path))
      end
      create_file_from_template(File.expand_path("spec/spec_helper.rb", path))
      create_file_from_template(File.expand_path("spec/app_spec.rb", path))
    end

    private
    def ask_yes_or_no(question)
      print "#{question} (y/N): "
      answer = $stdin.gets
      if answer =~ /^y/i
        true
      else
        false
      end
    end

    def create_file_from_template(path)
      basename = File.basename(path)

      if File.exist?(path)
        puts "#{basename} exists already. Skip."
        return
      end

      open(path, 'w') do |f|
        f.write(ERB.new(File.read(File.expand_path("../fixtures/#{basename}.erb", __FILE__))).result)
      end

      puts "Created: #{basename}"
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
infrataster-0.3.2 lib/infrataster/cli.rb
infrataster-0.3.1 lib/infrataster/cli.rb
infrataster-0.3.0 lib/infrataster/cli.rb
infrataster-0.2.6 lib/infrataster/cli.rb
infrataster-0.2.5 lib/infrataster/cli.rb
infrataster-0.2.4 lib/infrataster/cli.rb
infrataster-0.2.3 lib/infrataster/cli.rb
infrataster-0.2.2 lib/infrataster/cli.rb
infrataster-0.2.1 lib/infrataster/cli.rb
infrataster-0.2.0 lib/infrataster/cli.rb
infrataster-0.2.0.beta1 lib/infrataster/cli.rb
infrataster-0.1.13 lib/infrataster/cli.rb
infrataster-0.1.12 lib/infrataster/cli.rb