string_replacer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +92 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/bin/string_replacer +8 -0
- data/lib/string_replacer.rb +34 -0
- data/string_replacer.gemspec +54 -0
- data/test/helper.rb +10 -0
- data/test/test_string_replacer.rb +7 -0
- metadata +79 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Seamus Abshere
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= string_replacer
|
2
|
+
|
3
|
+
Repeatedly replace text in a file without disturbing the rest of the file.
|
4
|
+
|
5
|
+
== Use
|
6
|
+
|
7
|
+
$ string_replacer PATH STRING [REPLACEMENT_ID] [AFTER_LINE]
|
8
|
+
|
9
|
+
For example:
|
10
|
+
|
11
|
+
$ echo "hello" > test.txt
|
12
|
+
$ cat test.txt
|
13
|
+
hello
|
14
|
+
$ string_replacer test.txt "Don't forget to say \"hello world\"" "Just a reminder"
|
15
|
+
$ cat test.txt
|
16
|
+
hello
|
17
|
+
|
18
|
+
# START StringReplacer Just a reminder -- DO NOT MODIFY
|
19
|
+
Don't forget to say "hello world"
|
20
|
+
# END StringReplacer Just a reminder -- DO NOT MODIFY
|
21
|
+
|
22
|
+
Now I swap it out by using the same <tt>REPLACEMENT_ID</tt>...
|
23
|
+
|
24
|
+
$ string_replacer test.txt "YOU ARE STILL FORGETTING to say \"hello world\"" "Just a reminder"
|
25
|
+
$ cat test.txt
|
26
|
+
hello
|
27
|
+
|
28
|
+
# START StringReplacer Just a reminder -- DO NOT MODIFY
|
29
|
+
YOU ARE STILL FORGETTING to say "hello world"
|
30
|
+
# END StringReplacer Just a reminder -- DO NOT MODIFY
|
31
|
+
|
32
|
+
And finally, to demonstrate that <tt>REPLACEMENT_ID</tt> is case-sensitive...
|
33
|
+
|
34
|
+
$ string_replacer test.txt "Nevermind" "Just a Reminder"
|
35
|
+
$ cat test.txt
|
36
|
+
hello
|
37
|
+
|
38
|
+
# START StringReplacer Just a reminder -- DO NOT MODIFY
|
39
|
+
YOU ARE STILL FORGETTING to say "hello world"
|
40
|
+
# END StringReplacer Just a reminder -- DO NOT MODIFY
|
41
|
+
|
42
|
+
# START StringReplacer Just a Reminder -- DO NOT MODIFY
|
43
|
+
Nevermind
|
44
|
+
# END StringReplacer Just a Reminder -- DO NOT MODIFY
|
45
|
+
|
46
|
+
If you don't specify <tt>REPLACEMENT_ID</tt>, it will default to "1."
|
47
|
+
|
48
|
+
== Real-world usage
|
49
|
+
|
50
|
+
http://brighterplanet.com uses it to unmask the memcached 1.4.4 package on http://engineyard.com AppCloud (i.e. Amazon EC2) instances.
|
51
|
+
|
52
|
+
We have this in our before_restart.rb hook:
|
53
|
+
|
54
|
+
###############################################################
|
55
|
+
# make sure we have string_replacer installed at the system level
|
56
|
+
###############################################################
|
57
|
+
sudo 'gem install string_replacer --no-rdoc --no-ri'
|
58
|
+
|
59
|
+
###############################################################
|
60
|
+
# install memcached 1.4.4, which is not available by default
|
61
|
+
###############################################################
|
62
|
+
memcached_server_version = '1.4.4'
|
63
|
+
unless `/usr/bin/memcached -h`.include?(memcached_server_version)
|
64
|
+
sudo "/usr/bin/string_replacer /etc/portage/package.keywords/local \"=net-misc/memcached-#{memcached_server_version}\" \"make sure memcached #{memcached_server_version} is available\""
|
65
|
+
sudo "emerge net-misc/memcached"
|
66
|
+
end
|
67
|
+
sudo "cp #{release_path}/deploy/memcached/memcached.#{node[:environment][:name]} /etc/conf.d/memcached"
|
68
|
+
|
69
|
+
Before:
|
70
|
+
|
71
|
+
$ cat /etc/portage/package.keywords/local
|
72
|
+
=dev-lang/ruby-1.8.7_p174
|
73
|
+
|
74
|
+
After:
|
75
|
+
|
76
|
+
$ cat /etc/portage/package.keywords/local
|
77
|
+
=dev-lang/ruby-1.8.7_p174
|
78
|
+
|
79
|
+
# START StringReplacer make sure memcached 1.4.4 is available -- DO NOT MODIFY
|
80
|
+
=net-misc/memcached-1.4.4
|
81
|
+
# END StringReplacer make sure memcached 1.4.4 is available -- DO NOT MODIFY
|
82
|
+
|
83
|
+
== Obvious flaws
|
84
|
+
|
85
|
+
* assumes whitespace is ignored
|
86
|
+
* assumes # means comment
|
87
|
+
* no idea how it would work on big files
|
88
|
+
* there is probably a unix program that has done this since the 1970s
|
89
|
+
|
90
|
+
== Copyright
|
91
|
+
|
92
|
+
Copyright (c) 2010 Seamus Abshere. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "string_replacer"
|
8
|
+
gem.summary = %Q{Repeatedly replace text in a file without disturbing the rest of the file.}
|
9
|
+
gem.description = %Q{For example, if you need to make sure that a particular package is unmasked in /etc/portage/package.keywords/local, you can use string_replacer to put it there without disturbing the other lines.}
|
10
|
+
gem.email = "seamus@abshere.net"
|
11
|
+
gem.homepage = "http://github.com/seamusabshere/string_replacer"
|
12
|
+
gem.authors = ["Seamus Abshere"]
|
13
|
+
gem.executables = ['string_replacer']
|
14
|
+
# sabshere 9/22/10 currently no tests
|
15
|
+
# gem.add_development_dependency "shoulda", ">= 0"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
# sabshere 9/22/10 currently no tests
|
24
|
+
# require 'rake/testtask'
|
25
|
+
# Rake::TestTask.new(:test) do |test|
|
26
|
+
# test.libs << 'lib' << 'test'
|
27
|
+
# test.pattern = 'test/**/test_*.rb'
|
28
|
+
# test.verbose = true
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# begin
|
32
|
+
# require 'rcov/rcovtask'
|
33
|
+
# Rcov::RcovTask.new do |test|
|
34
|
+
# test.libs << 'test'
|
35
|
+
# test.pattern = 'test/**/test_*.rb'
|
36
|
+
# test.verbose = true
|
37
|
+
# end
|
38
|
+
# rescue LoadError
|
39
|
+
# task :rcov do
|
40
|
+
# abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# task :test => :check_dependencies
|
45
|
+
#
|
46
|
+
# task :default => :test
|
47
|
+
|
48
|
+
require 'rake/rdoctask'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "string_replacer #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/string_replacer
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
class StringReplacer
|
4
|
+
NEWLINE = "AijQA6tD1wkWqgvLzXD"
|
5
|
+
START_MARKER = '# START StringReplacer %s -- DO NOT MODIFY'
|
6
|
+
END_MARKER = "# END StringReplacer %s -- DO NOT MODIFY#{NEWLINE}"
|
7
|
+
|
8
|
+
attr_accessor :path
|
9
|
+
def initialize(path)
|
10
|
+
@path = path
|
11
|
+
end
|
12
|
+
|
13
|
+
def replace!(string, replacement_id, after_line)
|
14
|
+
replacement_id = 1 unless replacement_id.to_s.strip.length > 0
|
15
|
+
after_line = nil unless after_line.to_s.strip.length > 0
|
16
|
+
new_path = "#{path}.new"
|
17
|
+
backup_path = "#{path}.bak"
|
18
|
+
current_start_marker = START_MARKER % replacement_id.to_s
|
19
|
+
current_end_marker = END_MARKER % replacement_id.to_s
|
20
|
+
string_with_markers = current_start_marker + NEWLINE + string + NEWLINE + current_end_marker
|
21
|
+
text = IO.read(path).gsub("\n", NEWLINE)
|
22
|
+
if text.include? current_start_marker
|
23
|
+
text.sub! /#{Regexp.escape current_start_marker}.*#{Regexp.escape current_end_marker}/, string_with_markers
|
24
|
+
elsif after_line
|
25
|
+
text.sub! /(#{Regexp.escape after_line}#{Regexp.escape NEWLINE})/, '\1' + string_with_markers
|
26
|
+
else
|
27
|
+
text << NEWLINE << string_with_markers
|
28
|
+
end
|
29
|
+
text.gsub! NEWLINE, "\n"
|
30
|
+
File.open(new_path, 'w') { |f| f.write text }
|
31
|
+
FileUtils.mv path, backup_path
|
32
|
+
FileUtils.mv new_path, path
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{string_replacer}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Seamus Abshere"]
|
12
|
+
s.date = %q{2010-09-22}
|
13
|
+
s.default_executable = %q{string_replacer}
|
14
|
+
s.description = %q{For example, if you need to make sure that a particular package is unmasked in /etc/portage/package.keywords/local, you can use string_replacer to put it there without disturbing the other lines.}
|
15
|
+
s.email = %q{seamus@abshere.net}
|
16
|
+
s.executables = ["string_replacer"]
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".gitignore",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"bin/string_replacer",
|
29
|
+
"lib/string_replacer.rb",
|
30
|
+
"string_replacer.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_string_replacer.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/seamusabshere/string_replacer}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Repeatedly replace text in a file without disturbing the rest of the file.}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_string_replacer.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
else
|
50
|
+
end
|
51
|
+
else
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_replacer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Seamus Abshere
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-22 00:00:00 -05:00
|
19
|
+
default_executable: string_replacer
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: For example, if you need to make sure that a particular package is unmasked in /etc/portage/package.keywords/local, you can use string_replacer to put it there without disturbing the other lines.
|
23
|
+
email: seamus@abshere.net
|
24
|
+
executables:
|
25
|
+
- string_replacer
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- .document
|
33
|
+
- .gitignore
|
34
|
+
- LICENSE
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- bin/string_replacer
|
39
|
+
- lib/string_replacer.rb
|
40
|
+
- string_replacer.gemspec
|
41
|
+
- test/helper.rb
|
42
|
+
- test/test_string_replacer.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/seamusabshere/string_replacer
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Repeatedly replace text in a file without disturbing the rest of the file.
|
77
|
+
test_files:
|
78
|
+
- test/helper.rb
|
79
|
+
- test/test_string_replacer.rb
|