123456789_123456789_123456789_123456789_123456789_

Class: Bundler::Thor::Actions::InjectIntoFile

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Bundler::Thor::Actions::EmptyDirectory
Defined in: lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb

Class Method Summary

EmptyDirectory - Inherited

.new

Initializes given the source and destination.

Instance Attribute Summary

EmptyDirectory - Inherited

#base, #config, #destination,
#exists?

Checks if the destination file already exists.

#given_destination, #relative_destination,
#destination=

Sets the absolute destination value from a relative destination value.

#pretend?

Shortcut for pretend.

Instance Method Summary

EmptyDirectory - Inherited

#invoke!, #revoke!,
#convert_encoded_instructions

Filenames in the encoded form are converted.

#invoke_with_conflict_check

Receives a hash of options and just execute the block if some conditions are met.

#on_conflict_behavior

What to do when the destination file already exists.

#on_file_clash_behavior,
#say_status

Shortcut to say_status shell method.

Constructor Details

.new(base, destination, data, config) ⇒ InjectIntoFile

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 34

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

Instance Attribute Details

#behavior (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 32

attr_reader :replacement, :flag, :behavior

#flag (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 32

attr_reader :replacement, :flag, :behavior

#replacement (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 32

attr_reader :replacement, :flag, :behavior

Instance Method Details

#invoke!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 47

def invoke!
  say_status :invoke

  content = if @behavior == :after
    '\0' + replacement
  else
    replacement + '\0'
  end

  if exists?
    replace!(/#{flag}/, content, config[:force])
  else
    unless pretend?
      raise Bundler::Thor::Error, "The file #{ destination } does not appear to exist"
    end
  end
end

#replace!(regexp, string, force) (protected)

Adds the content to the file.

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 99

def replace!(regexp, string, force)
  return if pretend?
  content = File.read(destination)
  if force || !content.include?(replacement)
    content.gsub!(regexp, string)
    File.open(destination, "wb") { |file| file.write(content) }
  end
end

#revoke!

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 65

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

#say_status(behavior) (protected)

[ GitHub ]

  
# File 'lib/bundler/vendor/thor/lib/thor/actions/inject_into_file.rb', line 81

def say_status(behavior)
  status = if behavior == :invoke
    if flag == /\A/
      :prepend
    elsif flag == /\z/
      :append
    else
      :insert
    end
  else
    :subtract
  end

  super(status, config[:verbose])
end