txt_file_mutator 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/VERSION +1 -1
- data/lib/txt_file_mutator.rb +47 -4
- data/pkg/txt_file_mutator-0.1.0.gem +0 -0
- data/pkg/txt_file_mutator-0.2.0.gem +0 -0
- data/pkg/txt_file_mutator-0.2.1.gem +0 -0
- data/test/test_insert_before_and_after.rb +30 -0
- data/txt_file_mutator.gemspec +64 -0
- metadata +7 -2
data/CHANGELOG
CHANGED
@@ -1,2 +1,8 @@
|
|
1
1
|
13-11-2009: Initial release
|
2
2
|
|
3
|
+
Using http://rubular.com/ for testing the regexps! Let me know of any corner cases or bugs you find! :)
|
4
|
+
|
5
|
+
TODO:
|
6
|
+
* test insert_after and before only executed if not existing before! avoid content inserted multiple times before or after!
|
7
|
+
* also have option for multiple or only once!
|
8
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/txt_file_mutator.rb
CHANGED
@@ -1,16 +1,54 @@
|
|
1
1
|
module TextFileMutator
|
2
|
-
|
3
|
-
|
2
|
+
|
3
|
+
def self.has_multiple_content_before?(file_content, content, before)
|
4
|
+
res = file_content =~ /(#{content}\s*#{content})\s*#{before}/
|
5
|
+
!res.nil?
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.has_content_before?(file_content, content, before)
|
9
|
+
res = file_content =~ /(#{content})\s*#{before}/
|
10
|
+
!res.nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def self.has_multiple_content_after?(file_content, content, after)
|
15
|
+
res = file_content =~ /#{after}\s*(#{content}\s*#{content})/
|
16
|
+
!res.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.has_content_after?(file_content, content, after)
|
20
|
+
res = file_content =~ /#{after}\s*(#{content})/
|
21
|
+
!res.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def self.insert_before(file, line, txt_before, exist_check=true)
|
26
|
+
content = File.read(file)
|
27
|
+
if exist_check
|
28
|
+
return if has_content_before?(content, txt_before, line)
|
29
|
+
end
|
30
|
+
gsub_content_file content, file, /(#{Regexp.escape(line)})/ do |match|
|
4
31
|
"#{txt_before}\n#{match}"
|
5
32
|
end
|
6
33
|
end
|
7
34
|
|
8
|
-
def self.insert_after(file, line, txt_after)
|
9
|
-
|
35
|
+
def self.insert_after(file, line, txt_after, exist_check=true)
|
36
|
+
content = File.read(file)
|
37
|
+
if exist_check
|
38
|
+
return if has_content_after?(content, txt_after, line)
|
39
|
+
end
|
40
|
+
gsub_content_file file, /(#{Regexp.escape(line)})/ do |match|
|
10
41
|
"#{match}\n#{txt_after}"
|
11
42
|
end
|
12
43
|
end
|
13
44
|
|
45
|
+
def self.remove_duplicate_lines(file)
|
46
|
+
content = File.read(file)
|
47
|
+
content.gsub!(/^(.*)(\r?\n\1)+$/, '\1')
|
48
|
+
File.open(path, 'wb') { |file| file.write(content) }
|
49
|
+
end
|
50
|
+
|
51
|
+
|
14
52
|
def self.comment_gem_config(file, line)
|
15
53
|
gsub_file file, /^\s*[^#]\s*config.gem.+'#{Regexp.escape(line)}'$/ do |match|
|
16
54
|
"# " + "#{match}"
|
@@ -43,6 +81,11 @@ module TextFileMutator
|
|
43
81
|
|
44
82
|
|
45
83
|
protected
|
84
|
+
def self.gsub_content_file(content, path, regexp, *args, &block)
|
85
|
+
content.gsub!(regexp, *args, &block)
|
86
|
+
File.open(path, 'wb') { |file| file.write(content) }
|
87
|
+
end
|
88
|
+
|
46
89
|
def self.gsub_file(path, regexp, *args, &block)
|
47
90
|
content = File.read(path).gsub(regexp, *args, &block)
|
48
91
|
File.open(path, 'wb') { |file| file.write(content) }
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'txt_file_mutator'
|
2
|
+
require "test/unit"
|
3
|
+
|
4
|
+
class TestTxtFileMutator < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@tfm = TextFileMutator
|
8
|
+
@file = 'DATA_REMOVE.txt'
|
9
|
+
puts @tfm.instance_methods
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
## Nothing really
|
14
|
+
end
|
15
|
+
|
16
|
+
def has_content?(file, content)
|
17
|
+
content = File.read(file)
|
18
|
+
res = content =~ /#{content}/
|
19
|
+
!res.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_insert_before
|
23
|
+
content = 'BEFORE'
|
24
|
+
@tfm.insert_before @file, 'before', content
|
25
|
+
@tfm.insert_before @file, 'before', content
|
26
|
+
|
27
|
+
assert_equal false, @tfm.has_multiple_content_before?(File.read(@file), content, 'before'), "Same content inserted should not be repeated after multiple insert before"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,64 @@
|
|
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{txt_file_mutator}
|
8
|
+
s.version = "0.2.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = %q{2009-11-14}
|
13
|
+
s.description = %q{includes convenience method to do txt file mutations including simple code refactorings such as commenting a line}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"CHANGELOG",
|
24
|
+
"LICENSE",
|
25
|
+
"README",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/txt_file_mutator.rb",
|
30
|
+
"pkg/txt_file_mutator-0.1.0.gem",
|
31
|
+
"pkg/txt_file_mutator-0.2.0.gem",
|
32
|
+
"pkg/txt_file_mutator-0.2.1.gem",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb",
|
35
|
+
"spec/txt_file_mutator_spec.rb",
|
36
|
+
"txt_file_mutator.gemspec"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/kristianmandrup/txt_file_mutator}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.5}
|
42
|
+
s.summary = %q{mutate a text file in various ways}
|
43
|
+
s.test_files = [
|
44
|
+
"spec/spec_helper.rb",
|
45
|
+
"spec/txt_file_mutator_spec.rb",
|
46
|
+
"test/test_insert_before_and_after.rb",
|
47
|
+
"test/test_remove_line_w_content.rb",
|
48
|
+
"test/test_txt_file_mutator.rb"
|
49
|
+
]
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: txt_file_mutator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kristian Mandrup
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-14 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,9 +42,13 @@ files:
|
|
42
42
|
- Rakefile
|
43
43
|
- VERSION
|
44
44
|
- lib/txt_file_mutator.rb
|
45
|
+
- pkg/txt_file_mutator-0.1.0.gem
|
46
|
+
- pkg/txt_file_mutator-0.2.0.gem
|
47
|
+
- pkg/txt_file_mutator-0.2.1.gem
|
45
48
|
- spec/spec.opts
|
46
49
|
- spec/spec_helper.rb
|
47
50
|
- spec/txt_file_mutator_spec.rb
|
51
|
+
- txt_file_mutator.gemspec
|
48
52
|
has_rdoc: true
|
49
53
|
homepage: http://github.com/kristianmandrup/txt_file_mutator
|
50
54
|
licenses: []
|
@@ -76,5 +80,6 @@ summary: mutate a text file in various ways
|
|
76
80
|
test_files:
|
77
81
|
- spec/spec_helper.rb
|
78
82
|
- spec/txt_file_mutator_spec.rb
|
83
|
+
- test/test_insert_before_and_after.rb
|
79
84
|
- test/test_remove_line_w_content.rb
|
80
85
|
- test/test_txt_file_mutator.rb
|