Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use extended regex for replace_text. #215

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/sprinkle/installers/npm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Npm < Installer
attr_accessor :package_name

api do
def npm(package, &block)
install Npm.new(self, package, &block)
def npm(package, options={}, &block)
install Npm.new(self, package, options, &block)
end
end

Expand All @@ -27,15 +27,15 @@ def has_npm(package)
end
end

def initialize(parent, package_name, &block) #:nodoc:
super parent, &block
def initialize(parent, package_name, options={}, &block) #:nodoc:
super parent, options, &block
@package_name = package_name
end

protected

def install_commands #:nodoc:
"npm install --global #{@package_name}"
"#{sudo_cmd}npm install --global #{@package_name}"
end

end
Expand Down
20 changes: 11 additions & 9 deletions lib/sprinkle/installers/replace_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ module Sprinkle
module Installers
# = Replace text installer
#
# This installer replaces a text with another one in a file.
#
# This installer replaces a text matching the regex 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'
# replace_text 'Port [0-9]+', 'Port 2500', '/etc/ssh/sshd_config'
# end
#
# Because we use sed under the hood, the regex is extended regex.
#
# If you user has access to 'sudo' and theres a file that requires
# privileges, you can pass :sudo => true
# privileges, you can pass :sudo => true
#
# package :magic_beans do
# replace_text 'Port 22', 'Port 2500', '/etc/ssh/sshd_config', :sudo => true
Expand All @@ -25,7 +27,7 @@ module Installers
#
class ReplaceText < Installer
attr_accessor :regex, :text, :path #:nodoc:

api do
def replace_text(regex, text, path, options={}, &block)
install ReplaceText.new(self, regex, text, path, options, &block)
Expand All @@ -38,19 +40,19 @@ def initialize(parent, regex, text, path, options={}, &block) #:nodoc:
@text = text
@path = path
end

def announce
log "--> Replace '#{@regex}' with '#{@text}' in file #{@path}"
end

protected

def escape_sed_arg(s)
escape_shell_arg(s).gsub("/", "\\\\/").gsub('&', '\\\&')
end

def install_commands #:nodoc:
"#{sudo_cmd}sed -i 's/#{escape_sed_arg(@regex)}/#{escape_sed_arg(@text)}/g' #{@path}"
"#{sudo_cmd}sed -r -i 's/#{escape_sed_arg(@regex)}/#{escape_sed_arg(@text)}/g' #{@path}"
end

end
Expand Down