txt_file_mutator 0.2.2 → 0.2.4

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/.gitignore CHANGED
@@ -1,5 +1,3 @@
1
1
  .DS_Store
2
- log/*.log
3
- tmp/**/*
4
- config/database.yml
5
- db/*.sqlite3
2
+ pkg/*.gem
3
+ *.gemspec
data/CHANGELOG CHANGED
@@ -1,8 +1,17 @@
1
- 13-11-2009: Initial release
1
+ TODO:
2
+ Enhance with a convenient DSL using closures (yield)
3
+
4
+ with_files 'file.txt', 'file2.txt' do |files|
5
+ files.remove_lines 'abc', 'def'
6
+ files.comment_lines 'xyz', 'gfe'
7
+ ...
8
+ end
9
+
10
+ Test it!
2
11
 
12
+ 29-01-2010: Improved Documentation
13
+ 29-09-2009: Initial release
3
14
  Using http://rubular.com/ for testing the regexps! Let me know of any corner cases or bugs you find! :)
4
15
 
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!
16
+
8
17
 
data/README CHANGED
@@ -1,17 +1,97 @@
1
1
  txt_file_mutator
2
2
  ================
3
3
  Kristian Mandrup, kmandrup@gmail.com
4
- ---
5
4
 
6
5
  Contains utility functions for doing simple file mutations/transformations such as:
7
6
 
8
- * Inserting content BEFORE a line with some specific content
9
- * Inserting content AFTER a line with some specific content
7
+ * Inserting content BEFORE a line with some specific content (before-marker)
8
+ Has option (default=true) to only insert if no pre-existing same content before the before-marker (to disallow multiple executions to have stacking effects!)
9
+ - insert_before [path]
10
+
11
+ TextFileMutator.insert_after [path], [find-txt], [insert txt]
12
+
13
+ TextFileMutator.comment_line 'my/path/file.txt', '# before this', 'insert this!'
14
+
15
+ * Inserting content AFTER a line with some specific content (functions same as insert_before)
16
+ - insert_after
17
+
18
+ TextFileMutator.insert_after [path], [find-txt], [insert txt]
19
+
20
+ TextFileMutator.comment_line 'my/path/file.txt', '# after this', 'insert this!'
21
+
10
22
 
11
23
  * Commenting a line with some specific content
12
- * Commenting a config.gem statement for a specific gem
24
+ - comment_line
25
+
26
+ TextFileMutator.comment_line [path], [txt]
27
+
28
+ TextFileMutator.comment_line 'my/path/file.txt', 'comment this'
29
+
30
+ * Comment a config.gem statement for a specific gem
31
+ - comment_config_gem
32
+
33
+ TextFileMutator.comment_config_gem [path], [txt]
34
+
35
+ TextFileMutator.comment_config_gem 'my/path/file.txt', 'my-gem'
36
+
37
+ * Comment a gem 'my-gem' statement
38
+ - comment_rails_3_config_gem
39
+
13
40
 
14
41
  * Remove a line
42
+ - remove_line
43
+
44
+ TextFileMutator.remove_line [file_path], [txt]
45
+
46
+ TextFileMutator.remove_line 'my/path/file.txt', '# remove this line'
47
+
15
48
  * Remove a require statement
49
+ - remove_require
50
+
51
+ TextFileMutator.remove_require [file_path], [require string]
52
+
53
+ TextFileMutator.remove_require 'my/path/file.txt', 'rails/generators'
54
+
55
+ * Replace line
56
+ TextFileMutator.replace_line [file_path], [old-string] [replace-string]
57
+
58
+ Replaces each old-string found with replace-string
59
+
60
+ * Remove duplicate lines
61
+ TextFileMutator.remove_duplicate_lines [file_path]
62
+
63
+ TextFileMutator.remove_duplicate_lines 'my/path/file.txt'
64
+
65
+ * Append line
66
+ TextFileMutator.append_line [file_path], [content]
67
+
68
+ TextFileMutator.append_line '/my/path/file.txt', "this line..."
69
+
70
+ Investigation functions
71
+ * has_content?
72
+ TextFileMutator.has_content? file_content, content
73
+
74
+ # returns true if 'blip' occurs at least once in the string
75
+ TextFileMutator.has_content? file('my_file.txt).read, 'blip'
76
+
77
+ * has_content_before?
78
+
79
+ TextFileMutator.has_content_before? file_content, content, before
80
+
81
+ # returns true if 'blip' occurs at least once before '# before
82
+ TextFileMutator.has_multiple_content_before? file('my_file.txt).read, 'blip', '# before me' me'
83
+
84
+ * has_multiple_content_before?
85
+
86
+ TextFileMutator.has_multiple_content_before? file_content, content, before
87
+
88
+ # returns true if 'blip' occurs at least 2 times before the string '# before'
89
+ TextFileMutator.has_multiple_content_before? file('my_file.txt).read, 'blip', '# before me' me'
90
+
91
+ * has_content_after?
92
+ 'Reverse' of has_content_before?
93
+
94
+ * has_multiple_content_after?
95
+ 'Reverse' of has_multiple_content_before?
16
96
 
17
97
  (more to come in later versions...!)
data/TODO ADDED
@@ -0,0 +1,7 @@
1
+ Enhance with a convenient DSL using closures (yield)
2
+
3
+ with_files 'file.txt', 'file2.txt' do |files|
4
+ files.remove_lines 'abc', 'def'
5
+ files.comment_lines 'xyz', 'gfe'
6
+ ...
7
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.4
@@ -1,5 +1,7 @@
1
+ require 'rake'
2
+
1
3
  module TextFileMutator
2
-
4
+
3
5
  def self.has_multiple_content_before?(file_content, content, before)
4
6
  res = file_content =~ /(#{content}\s*#{content})\s*#{before}/
5
7
  !res.nil?
@@ -10,6 +12,10 @@ module TextFileMutator
10
12
  !res.nil?
11
13
  end
12
14
 
15
+ def self.has_content?(file_content, content)
16
+ res = file_content =~ /#{content}/
17
+ !res.nil?
18
+ end
13
19
 
14
20
  def self.has_multiple_content_after?(file_content, content, after)
15
21
  res = file_content =~ /#{after}\s*(#{content}\s*#{content})/
@@ -21,7 +27,6 @@ module TextFileMutator
21
27
  !res.nil?
22
28
  end
23
29
 
24
-
25
30
  def self.insert_before(file, line, txt_before, exist_check=true)
26
31
  content = File.read(file)
27
32
  if exist_check
@@ -48,15 +53,21 @@ module TextFileMutator
48
53
  File.open(path, 'wb') { |file| file.write(content) }
49
54
  end
50
55
 
51
-
52
56
  def self.comment_gem_config(file, line)
53
- gsub_file file, /^\s*[^#]\s*config.gem.+'#{Regexp.escape(line)}'$/ do |match|
54
- "# " + "#{match}"
55
- end
57
+ comment_line_w_contents(file, 'config.gem', line)
58
+ end
59
+
60
+ def self.comment_rails3_gem_config(file, line)
61
+ comment_line_w_contents(file, 'gem', line)
56
62
  end
57
63
 
58
64
  def self.remove_require(file, line)
59
- gsub_file file, /^\s*require\s+'#{Regexp.escape(line)}'$/ do |match|
65
+ remove_line_w_contents file, 'require', line
66
+ end
67
+
68
+ # TODO: support both '' and ""
69
+ def self.remove_line_w_contents(file, part1, part2)
70
+ gsub_file file, /^\s*#{Regexp.escape(part1)}\s+'#{Regexp.escape(part2)}'$/ do |match|
60
71
  ""
61
72
  end
62
73
  end
@@ -67,6 +78,12 @@ module TextFileMutator
67
78
  end
68
79
  end
69
80
 
81
+ def self.comment_line_w_contents(file, part1, part2)
82
+ gsub_file file, /^\s*[^#]\s*#{Regexp.escape(part1)}.+(#{Regexp.escape(part2)}$)/i do |match|
83
+ "# " + "#{match}"
84
+ end
85
+ end
86
+
70
87
  def self.remove_content(file, line)
71
88
  gsub_file file, /(#{Regexp.escape(line)})/i do |match|
72
89
  ""
@@ -74,21 +91,150 @@ module TextFileMutator
74
91
  end
75
92
 
76
93
  def self.remove_line(file, line)
77
- gsub_file file, /^.*#{Regexp.escape(line)}.*$)/i do |match|
78
- ""
94
+ replace_line file, line, ""
95
+ end
96
+
97
+ def self.replace_line(file, line, replacement)
98
+ gsub_file file, /^.*#{Regexp.escape(line)}.*$/i do |match|
99
+ "#{replacement}"
79
100
  end
80
101
  end
81
102
 
103
+ def self.append_line(path, content)
104
+ File.open(path, 'a') do |f|
105
+ f.puts(content)
106
+ end
107
+ end
82
108
 
83
109
  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
110
+
111
+ def self.gsub_content_file(content, path, regexp, *args, &block)
112
+ content.gsub!(regexp, *args, &block)
113
+ File.open(path, 'wb') { |file| file.write(content) }
114
+ end
88
115
 
89
116
  def self.gsub_file(path, regexp, *args, &block)
90
117
  content = File.read(path).gsub(regexp, *args, &block)
91
118
  File.open(path, 'wb') { |file| file.write(content) }
92
119
  end
93
120
 
94
- end
121
+ end
122
+
123
+ module Rake
124
+ class FileList
125
+ def has_multiple_content_before?(content, before)
126
+ each do |file|
127
+ t = TextFileMutator.has_multiple_content_before?(file.read, content, before)
128
+ return t if t
129
+ end
130
+ false
131
+ end
132
+
133
+ def has_content_before?(content, before)
134
+ each do |file|
135
+ t = TextFileMutator.has_multiple_content_after?(file.read, content, before)
136
+ return t if t
137
+ end
138
+ false
139
+ end
140
+
141
+ def has_any_content?(content)
142
+ each do |file|
143
+ t = TextFileMutator.has_multiple_content_after?(file.read, content)
144
+ return t if t
145
+ end
146
+ false
147
+ end
148
+
149
+ def have_all_content?(content)
150
+ each do |file|
151
+ t = TextFileMutator.has_multiple_content_after?(file.read, content)
152
+ return false if !t
153
+ end
154
+ true
155
+ end
156
+
157
+ def has_any_multiple_content_after?(content, after)
158
+ each do |file|
159
+ t = TextFileMutator.has_multiple_content_after?(file.read, content, after)
160
+ return t if t
161
+ end
162
+ false
163
+ end
164
+
165
+ def has_any_content_after?(content, after)
166
+ each do |file|
167
+ t = TextFileMutator.has_any_content_after?(file.read, content, after)
168
+ return t if t
169
+ end
170
+ false
171
+ end
172
+
173
+ def insert_before(file, line, txt_before, exist_check=true)
174
+ each do |file|
175
+ TextFileMutator.insert_before line, txt_before, exist_check
176
+ end
177
+ end
178
+
179
+ def insert_after(file, line, txt_after, exist_check=true)
180
+ each do |file|
181
+ TextFileMutator.insert_after line, txt_after, exist_check
182
+ end
183
+ end
184
+
185
+ def remove_duplicate_lines(file)
186
+ each do |file|
187
+ TextFileMutator.remove_duplicate_lines
188
+ end
189
+ end
190
+
191
+ def remove_require(file, line)
192
+ each do |file|
193
+ TextFileMutator.remove_require file, line
194
+ end
195
+ end
196
+
197
+ # TODO: support both '' and ""
198
+ def remove_line_w_contents(file, part1, part2)
199
+ each do |file|
200
+ TextFileMutator.remove_line_w_contents file, part1, part2
201
+ end
202
+ end
203
+
204
+ def comment_line(file, line)
205
+ each do |file|
206
+ TextFileMutator.comment_line file, line
207
+ end
208
+ end
209
+
210
+ def comment_line_w_contents(file, part1, part2)
211
+ each do |file|
212
+ TextFileMutator.comment_line_w_contents file, part1, part2
213
+ end
214
+ end
215
+
216
+ def remove_content(line)
217
+ each do |file|
218
+ TextFileMutator.remove_content file, line
219
+ end
220
+ end
221
+
222
+ def remove_line(line)
223
+ each do |file|
224
+ TextFileMutator.remove_line file, line
225
+ end
226
+ end
227
+
228
+ def replace_line(line, replacement)
229
+ each do |file|
230
+ TextFileMutator.replace_line file, line, replacement
231
+ end
232
+ end
233
+
234
+ def append_line(content)
235
+ each do |file|
236
+ TextFileMutator.append_line file, content
237
+ end
238
+ end
239
+ end
240
+ end
@@ -13,18 +13,32 @@ class TestTxtFileMutator < Test::Unit::TestCase
13
13
  ## Nothing really
14
14
  end
15
15
 
16
- def has_content?(file, content)
17
- content = File.read(file)
18
- res = content =~ /#{content}/
19
- !res.nil?
20
- end
21
-
22
16
  def test_insert_before
23
17
  content = 'BEFORE'
24
18
  @tfm.insert_before @file, 'before', content
25
19
  @tfm.insert_before @file, 'before', content
26
20
 
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"
21
+ assert_equal false, @tfm.has_multiple_content_before?(File.read(@file), content, 'before'), "Same content inserted should not be repeated after multiple insert_before"
22
+ end
23
+
24
+ def test_insert_after
25
+ content = 'BEFORE'
26
+ @tfm.insert_after @file, 'after', content
27
+ @tfm.insert_after @file, 'after', content
28
+
29
+ assert_equal false, @tfm.has_multiple_content_after?(File.read(@file), content, 'after'), "Same content inserted should not be repeated after multiple insert_after"
28
30
  end
29
31
 
32
+
33
+ def test_replace_line
34
+ content = 'replace me'
35
+ replace = 'with this replacement'
36
+ @tfm.replace_line @file, content, replace
37
+
38
+ new_content = File.read(@file)
39
+ assert_equal false, @tfm.has_content?(new_content, content), "File should not contain original content"
40
+ assert_equal true, @tfm.has_content?(new_content, replace), "File should contain replacement"
41
+ end
42
+
43
+
30
44
  end
@@ -0,0 +1,25 @@
1
+ require 'txt_file_mutator'
2
+ require "test/unit"
3
+ require "rake"
4
+
5
+ class TestTxtFileMutator < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @tfm = TextFileMutator
9
+ @file = 'DATA.txt'
10
+ puts @tfm.instance_methods
11
+ end
12
+
13
+ def teardown
14
+ ## Nothing really
15
+ end
16
+
17
+ # include method
18
+ def test_comment_line
19
+ FileList.new('DATA_REMOVE.txt') do |files|
20
+ files.remove_line 'remove'
21
+ end
22
+ end
23
+ end
24
+
25
+
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{txt_file_mutator}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2009-11-14}
12
+ s.date = %q{2010-01-30}
13
13
  s.description = %q{includes convenience method to do txt file mutations including simple code refactorings such as commenting a line}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README",
18
- "README.rdoc"
18
+ "README.rdoc",
19
+ "TODO"
19
20
  ]
20
21
  s.files = [
21
22
  ".document",
@@ -25,11 +26,9 @@ Gem::Specification.new do |s|
25
26
  "README",
26
27
  "README.rdoc",
27
28
  "Rakefile",
29
+ "TODO",
28
30
  "VERSION",
29
31
  "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
32
  "spec/spec.opts",
34
33
  "spec/spec_helper.rb",
35
34
  "spec/txt_file_mutator_spec.rb",
@@ -44,6 +43,7 @@ Gem::Specification.new do |s|
44
43
  "spec/spec_helper.rb",
45
44
  "spec/txt_file_mutator_spec.rb",
46
45
  "test/test_insert_before_and_after.rb",
46
+ "test/test_on_filelist.rb",
47
47
  "test/test_remove_line_w_content.rb",
48
48
  "test/test_txt_file_mutator.rb"
49
49
  ]
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.2
4
+ version: 0.2.4
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-14 00:00:00 +01:00
12
+ date: 2010-01-30 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,7 @@ extra_rdoc_files:
32
32
  - LICENSE
33
33
  - README
34
34
  - README.rdoc
35
+ - TODO
35
36
  files:
36
37
  - .document
37
38
  - .gitignore
@@ -40,11 +41,9 @@ files:
40
41
  - README
41
42
  - README.rdoc
42
43
  - Rakefile
44
+ - TODO
43
45
  - VERSION
44
46
  - 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
48
47
  - spec/spec.opts
49
48
  - spec/spec_helper.rb
50
49
  - spec/txt_file_mutator_spec.rb
@@ -81,5 +80,6 @@ test_files:
81
80
  - spec/spec_helper.rb
82
81
  - spec/txt_file_mutator_spec.rb
83
82
  - test/test_insert_before_and_after.rb
83
+ - test/test_on_filelist.rb
84
84
  - test/test_remove_line_w_content.rb
85
85
  - test/test_txt_file_mutator.rb