Sha256: 7d8bdc62c0950196096273d0e345b3f3031d848a409fcc75434e6ea3bb1daa74

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Sprinkle
  module Installers
    # = Replace text installer
    #
    # This installer replaces a text with another one in a file.
    # 
    # == Example Usage
    #
    # Change ssh port in /etc/ssh/sshd_config
    #
    #   package :magic_beans do
    #     replace_text 'Port 22', 'Port 2500', '/etc/ssh/sshd_config'
    #   end
    #
    # If you user has access to 'sudo' and theres a file that requires
    # priveledges, you can pass :sudo => true 
    #
    #   package :magic_beans do
    #     replace_text 'Port 22', 'Port 2500', '/etc/ssh/sshd_config', :sudo => true
    #   end
    #
    # A special verify step exists for this very installer
    # its known as file_contains, it will test that a file indeed
    # contains a substring that you send it.
    #
    class ReplaceText < Installer
      attr_accessor :regex, :text, :path #:nodoc:
      
      api do
        def replace_text(regex, text, path, options={}, &block)
          install Sprinkle::Installers::ReplaceText.new(self, regex, text, path, options, &block)
        end
      end

      def initialize(parent, regex, text, path, options={}, &block) #:nodoc:
        super parent, options, &block
        @regex = regex
        @text = text
        @path = path
      end
      
      def announce
        log "--> Replace '#{@regex}' with '#{@text}' in file #{@path}"
      end

      protected
      
        def install_commands #:nodoc:
          "#{'sudo ' if option?(:sudo)}sed -i 's/#{@regex.gsub("'", "'\\\\''").gsub("/", "\\\\/").gsub("\n", '\n')}/#{@text.gsub("'", "'\\\\''").gsub("/", "\\\\/").gsub("\n", '\n')}/g' #{@path}"
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sprinkle-0.5.0.rc1 lib/sprinkle/installers/replace_text.rb