Class: RecursiveReplace
- Inherits:
-
Object
- Object
- RecursiveReplace
- Defined in:
- lib/recursive_replace.rb
Constant Summary collapse
- INDENTATION =
" "
Class Method Summary collapse
-
.files_inside(path) ⇒ Object
get the working directory to start in.
-
.prepare(string) ⇒ Object
prepare string for use in replacement: escape, etc.
- .print_path(path) ⇒ Object
-
.print_path_with_indent(path) ⇒ Object
print pretty path.
-
.replace(original, replacement, options = {}) ⇒ Object
replace text in either a directory(recursively) or single file.
-
.replace_in_file(original, replacement, options = {}) ⇒ Object
do the actual replacing in a single file.
Class Method Details
.files_inside(path) ⇒ Object
get the working directory to start in
48 49 50 |
# File 'lib/recursive_replace.rb', line 48 def self.files_inside(path) # get the working directory to start in path.nil? ? Dir.glob(File.join("**", "*")) : Dir.glob(File.join(path, "**", "*")) end |
.prepare(string) ⇒ Object
prepare string for use in replacement: escape, etc.
52 53 54 55 56 57 |
# File 'lib/recursive_replace.rb', line 52 def self.prepare(string) # prepare string for use in replacement: escape, etc. prepared = Regexp.escape(string) prepared = prepared.sub("/", "\\/") # prepend escape characted forward slash with since it will be be used with sed in shell #print "Postprepare #{string} => #{prepared}" return prepared end |
.print_path(path) ⇒ Object
44 45 46 |
# File 'lib/recursive_replace.rb', line 44 def self.print_path(path) path end |
.print_path_with_indent(path) ⇒ Object
print pretty path
36 37 38 39 40 41 42 |
# File 'lib/recursive_replace.rb', line 36 def self.print_path_with_indent(path) # print pretty path indent = String.new path.split(File::SEPARATOR).each do indent << RecursiveReplace::INDENTATION unless File.dirname(path) == "." end return File.directory?(path) ? path : indent + File.basename(path) end |
.replace(original, replacement, options = {}) ⇒ Object
replace text in either a directory(recursively) or single file
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/recursive_replace.rb', line 6 def self.replace(original, replacement, = {}) # replace text in either a directory(recursively) or single file #exec("find ./ -type f | xargs sed -i 's/#{@string1}/#{@string2}/g'") #puts files.inspect if [:path].nil? || File.directory?([:path]) # recursively inside directory for file in files_inside([:path]) [:path] = file replace_in_file(original, replacement, ) unless File.directory?(file) end else # single file replace_in_file(original, replacement, ) end end |
.replace_in_file(original, replacement, options = {}) ⇒ Object
do the actual replacing in a single file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/recursive_replace.rb', line 20 def self.replace_in_file(original, replacement, = {}) # do the actual replacing in a single file raise ScriptError, "options[:path] not defined" if [:path].nil? raise LoadError, "#{[:path]} not found" unless File.exists?([:path]) raise LoadError, "#{[:path]} is a directory" if File.directory?([:path]) # Set Defaults [:verbose] = false if [:verbose].nil? original = prepare(original) # escape any special characters replacement = prepare(replacement) results = system("sed -i 's/#{original}/#{replacement}/g' #{[:path]}") puts "success".green + "\t" + INDENTATION + print_path([:path]) if !File.directory?([:path]) && [:verbose] #puts results.inspect end |