replacer.rb 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/replacer.rb +106 -0
- metadata +46 -0
data/bin/replacer.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
if ARGV.length.zero?
|
4
|
+
puts <<-usage_information
|
5
|
+
Usage:
|
6
|
+
|
7
|
+
$ replacer.rb 'where to find' 'what to find'
|
8
|
+
|
9
|
+
$ replacer.rb 'where to find' 'what to find' 'replace with'
|
10
|
+
$ replacer.rb 'where to find -- what to find -- replace with'
|
11
|
+
|
12
|
+
To cleanup old backups:
|
13
|
+
|
14
|
+
$ replacer.rb --cleanup 'where to cleanup'
|
15
|
+
usage_information
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
# check for cleanup routine
|
20
|
+
if ARGV.first =~ /--cleanup/
|
21
|
+
puts 'Cleaning up...'
|
22
|
+
cleanup, where = *ARGV
|
23
|
+
Dir.glob(where || './**/*~*').each do |file_name|
|
24
|
+
if file_name =~ /~\d+$/
|
25
|
+
File.unlink(file_name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
exit
|
29
|
+
end
|
30
|
+
|
31
|
+
# parse arguments
|
32
|
+
arguments = if ARGV.count == 1
|
33
|
+
ARGV.first.split('--').map{ |argument| argument.strip }
|
34
|
+
else
|
35
|
+
ARGV
|
36
|
+
end
|
37
|
+
|
38
|
+
where, what, with = *arguments
|
39
|
+
|
40
|
+
# use defaults for optional parameters
|
41
|
+
where ||= '*'
|
42
|
+
with ||= ''
|
43
|
+
|
44
|
+
# translate to regexps
|
45
|
+
what_regexp = eval(what) rescue nil
|
46
|
+
raise '%s is not a valid regular expression' % what unless what_regexp.is_a?(Regexp)
|
47
|
+
|
48
|
+
STDOUT.write 'Working'
|
49
|
+
|
50
|
+
diff = ""
|
51
|
+
files = []
|
52
|
+
# actual find/replace
|
53
|
+
Dir.glob(where).each do |file_name|
|
54
|
+
# read
|
55
|
+
file_name = File.realpath(file_name)
|
56
|
+
|
57
|
+
# skip if is not a file
|
58
|
+
next if File.ftype(file_name) != 'file'
|
59
|
+
|
60
|
+
content = ""
|
61
|
+
File.open(file_name) do |file|
|
62
|
+
content = file.read
|
63
|
+
end
|
64
|
+
|
65
|
+
# replace and write
|
66
|
+
counter = 0
|
67
|
+
begin
|
68
|
+
new_file_name = file_name + '~' + counter.to_s
|
69
|
+
counter += 1
|
70
|
+
end while File.exists?(new_file_name)
|
71
|
+
|
72
|
+
files << new_file_name
|
73
|
+
File.open(new_file_name, 'w') do |file|
|
74
|
+
file.write content.gsub(what_regexp, with)
|
75
|
+
end
|
76
|
+
|
77
|
+
# compute difference
|
78
|
+
diff << `git diff --no-index --color #{file_name} #{new_file_name}`
|
79
|
+
|
80
|
+
STDOUT.write '.'
|
81
|
+
end
|
82
|
+
|
83
|
+
puts # Working......\n
|
84
|
+
|
85
|
+
# output
|
86
|
+
Kernel.open('|less -R', 'w') do |file|
|
87
|
+
file.write diff
|
88
|
+
end
|
89
|
+
|
90
|
+
# replacement part
|
91
|
+
STDOUT.write 'Type "yes" to commit: '
|
92
|
+
if STDIN.gets =~ /yes/
|
93
|
+
puts 'Replacing...'
|
94
|
+
|
95
|
+
# replace
|
96
|
+
files.each do |file_name|
|
97
|
+
File.rename(file_name, file_name.gsub(/~\d+$/, ''))
|
98
|
+
end
|
99
|
+
|
100
|
+
puts 'Done.'
|
101
|
+
else
|
102
|
+
puts 'Cancelled'
|
103
|
+
|
104
|
+
# cleanup
|
105
|
+
files.each{ |file_name| File.unlink file_name }
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: replacer.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- arthrrr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-18 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
executables:
|
17
|
+
- replacer.rb
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/replacer.rb
|
22
|
+
homepage: https://github.com/arthrrr/replacer.rb
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.15
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Like sed, but with oniguruma and git-diff
|
46
|
+
test_files: []
|