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 39

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 37

attr_reader :replacement, :flag, :behavior

#flag (readonly)

[ GitHub ]

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

attr_reader :replacement, :flag, :behavior

#replacement (readonly)

[ GitHub ]

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

attr_reader :replacement, :flag, :behavior

#replacement_present?Boolean (readonly, protected)

[ GitHub ]

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

def replacement_present?
  content.include?(replacement)
end

Instance Method Details

#content (protected)

[ GitHub ]

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

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

#invoke!

[ GitHub ]

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

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

#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 120

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

#revoke!

[ GitHub ]

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

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, warning: nil, color: nil) (protected)

[ GitHub ]

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

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