class Bundler::Thor::Actions::InjectIntoFile

:nodoc:

def content

def content
  @content ||= File.read(destination)
end

def initialize(base, destination, data, config)

def initialize(base, destination, data, config)
  super(base, destination, {verbose: true}.merge(config))
  @behavior, @flag = if @config.key?(:after)
    [:after, @config.delete(:after)]
  else
    [:before, @config.delete(:before)]
  end
  @replacement = data.is_a?(Proc) ? data.call : data
  @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp)
end

def invoke!

def invoke!
  content = if @behavior == :after
    '\0' + replacement
  else
    replacement + '\0'
  end
  if exists?
    if replace!(/#{flag}/, content, config[:force])
      say_status(:invoke)
    elsif replacement_present?
      say_status(:unchanged, color: :blue)
    else
      say_status(:unchanged, warning: WARNINGS[:unchanged_no_flag], color: :red)
    end
  else
    unless pretend?
      raise Bundler::Thor::Error, "The file #{ destination } does not appear to exist"
    end
  end
end

def replace!(regexp, string, force)


Adds the content to the file.
def replace!(regexp, string, force)
  if force || !replacement_present?
    success = content.gsub!(regexp, string)
    File.open(destination, "wb") { |file| file.write(content) } unless pretend?
    success
  end
end

def replacement_present?

def replacement_present?
  content.include?(replacement)
end

def revoke!

def revoke!
  say_status :revoke
  regexp = if @behavior == :after
    content = '\1\2'
    /(#{flag})(.*)(#{Regexp.escape(replacement)})/m
  else
    content = '\2\3'
    /(#{Regexp.escape(replacement)})(.*)(#{flag})/m
  end
  replace!(regexp, content, true)
end

def say_status(behavior, warning: nil, color: nil)

def say_status(behavior, warning: nil, color: nil)
  status = if behavior == :invoke
    if flag == /\A/
      :prepend
    elsif flag == /\z/
      :append
    else
      :insert
    end
  elsif warning
    warning
  elsif behavior == :unchanged
    :unchanged
  else
    :subtract
  end
  super(status, (color || config[:verbose]))
end