class Thor::Actions::InjectIntoFile
:nodoc:
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! say_status :invoke content = if @behavior == :after '\0' + replacement else replacement + '\0' end replace!(/#{flag}/, content) end
def replace!(regexp, string)
Adds the content to the file.
def replace!(regexp, string) unless base.options[:pretend] content = File.binread(destination) content.gsub!(regexp, string) File.open(destination, 'wb') { |file| file.write(content) } end 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) end
def say_status(behavior)
def say_status(behavior) status = if flag == /\A/ behavior == :invoke ? :prepend : :unprepend elsif flag == /\z/ behavior == :invoke ? :append : :unappend else behavior == :invoke ? :inject : :deinject end super(status, config[:verbose]) end