aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/vendor/thor-0.11.3/lib/thor/actions/inject_into_file.rb
blob: 089bd894e4aaf691e8bbee2f4d05d7ff546e526e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'thor/actions/empty_directory'

class Thor
  module Actions

    # Injects the given content into a file. Different from append_file,
    # prepend_file and gsub_file, this method is reversible. By this reason,
    # the flag can only be strings. gsub_file is your friend if you need to
    # deal with more complex cases.
    #
    # ==== Parameters
    # destination<String>:: Relative path to the destination root
    # data<String>:: Data to add to the file. Can be given as a block.
    # config<Hash>:: give :verbose => false to not log the status and the flag
    #                for injection (:after or :before).
    # 
    # ==== Examples
    #
    #   inject_into_file "config/environment.rb", "config.gem thor", :after => "Rails::Initializer.run do |config|\n"
    #
    #   inject_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
    #     gems = ask "Which gems would you like to add?"
    #     gems.split(" ").map{ |gem| "  config.gem #{gem}" }.join("\n")
    #   end
    #
    def inject_into_file(destination, *args, &block)
      if block_given?
        data, config = block, args.shift
      else
        data, config = args.shift, args.shift
      end

      log_status = args.empty? || args.pop
      action InjectIntoFile.new(self, destination, data, config)
    end

    class InjectIntoFile < EmptyDirectory
      attr_reader :flag, :replacement

      def initialize(base, destination, data, config)
        super(base, destination, { :verbose => true }.merge(config))

        data = data.call if data.is_a?(Proc)

        @replacement = if @config.key?(:after)
          @flag = @config.delete(:after)
          @flag + data
        else
          @flag = @config.delete(:before)
          data + @flag
        end
      end

      def invoke!
        say_status :inject, config[:verbose]
        replace!(flag, replacement)
      end

      def revoke!
        say_status :deinject, config[:verbose]
        replace!(replacement, flag)
      end

      protected

        # Adds the content to the file.
        #
        def replace!(regexp, string)
          unless base.options[:pretend]
            content = File.read(destination)
            content.gsub!(regexp, string)
            File.open(destination, 'wb') { |file| file.write(content) }
          end
        end

    end
  end
end