Class: Git::Gsub::Commands::Gsub

Inherits:
Command
  • Object
show all
Defined in:
lib/git/gsub.rb

Instance Attribute Summary

Attributes inherited from Command

#from, #options, #paths, #to

Instance Method Summary collapse

Methods inherited from Command

#args, #initialize, #run_commands

Constructor Details

This class inherits a constructor from Git::Gsub::Commands::Command

Instance Method Details

#build_command(from, to, paths = [], _options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/git/gsub.rb', line 105

def build_command(from, to, paths = [], _options = {})
  from, to, *paths = [from, to, *paths].map { |s| Shellwords.escape s }
  [from, to].each { |s| s.gsub! '@', '\@' }

  target_files = `git grep -l #{from} #{paths.join ' '}`.each_line.map(&:chomp)
  return if target_files.empty?


  ['perl', '-pi', '-e', "s{#{from}}{#{to}}g", *target_files]
end

#runObject



100
101
102
103
# File 'lib/git/gsub.rb', line 100

def run
  commands = args.map { |from, to| build_command(from, to, paths) }.compact
  run_commands commands
end