Sha256: be8fcca325c9e097c0ebde8513b22f690899f51edcdcb582435b6328b349479f

Contents?: true

Size: 1.66 KB

Versions: 41

Compression:

Stored size: 1.66 KB

Contents

require "rspec/support"
require "rspec/core"
RSpec::Support.require_rspec_core "project_initializer"
require "rspec/rails/feature_check"

module Rspec
  module Generators
    # @private
    class InstallGenerator < ::Rails::Generators::Base
      desc <<DESC
Description:
    Copy rspec files to your application.
DESC

      def self.source_root
        @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
      end

      def copy_spec_files
        Dir.mktmpdir do |dir|
          generate_rspec_init dir
          template File.join(dir, '.rspec'), '.rspec'
          directory File.join(dir, 'spec'), 'spec'
        end
      end

      def copy_rails_files
        template 'spec/rails_helper.rb'
      end

    private

      def generate_rspec_init(tmpdir)
        initializer = ::RSpec::Core::ProjectInitializer.new(
          :destination => tmpdir,
          :report_stream => StringIO.new
        )
        initializer.run

        spec_helper_path = File.join(tmpdir, 'spec', 'spec_helper.rb')

        replace_generator_command(spec_helper_path)
        remove_warnings_configuration(spec_helper_path)
      end

      def replace_generator_command(spec_helper_path)
        gsub_file spec_helper_path,
                  'rspec --init',
                  'rails generate rspec:install',
                  :verbose => false
      end

      def remove_warnings_configuration(spec_helper_path)
        empty_line = '^\n'
        comment_line = '^\s*#.+\n'
        gsub_file spec_helper_path,
                  /#{empty_line}(#{comment_line})+\s+config\.warnings = true\n/,
                  '',
                  :verbose => false
      end
    end
  end
end

Version data entries

41 entries across 40 versions & 5 rubygems

Version Path
rspec-rails-3.9.1 lib/generators/rspec/install/install_generator.rb
rspec-rails-4.0.0.beta3 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.8.3 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.9.0 lib/generators/rspec/install/install_generator.rb
rspec-rails-4.0.0.beta2 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.8.2 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.8.1 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.8.0 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.7.2 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.7.1 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.7.0 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.6.1 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.6.0 lib/generators/rspec/install/install_generator.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/rspec-rails-3.5.2/lib/generators/rspec/install/install_generator.rb
rspec-rails-3.6.0.beta2 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.6.0.beta1 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.5.2 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.5.1 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.5.0 lib/generators/rspec/install/install_generator.rb
rspec-rails-3.5.0.beta4 lib/generators/rspec/install/install_generator.rb