tty-file 0.6.0 → 0.10.0
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +63 -2
- data/README.md +191 -80
- data/lib/tty-file.rb +1 -3
- data/lib/tty/file.rb +383 -242
- data/lib/tty/file/compare_files.rb +70 -0
- data/lib/tty/file/create_file.rb +35 -19
- data/lib/tty/file/differ.rb +47 -25
- data/lib/tty/file/digest_file.rb +5 -5
- data/lib/tty/file/download_file.rb +10 -10
- data/lib/tty/file/read_backward_file.rb +0 -1
- data/lib/tty/file/version.rb +2 -2
- metadata +32 -50
- data/.gitignore +0 -10
- data/.rspec +0 -3
- data/.travis.yml +0 -26
- data/CODE_OF_CONDUCT.md +0 -49
- data/Gemfile +0 -9
- data/Rakefile +0 -10
- data/appveyor.yml +0 -26
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/tasks/console.rake +0 -10
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-file.gemspec +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e1d9e2c1d0c438b74128707623835e44da4ec23dcdebfe0ba88afb5284c730a7
|
4
|
+
data.tar.gz: 63c7b68f347b2199502240cbdbfdf20264f90a95ece1ea50f60a78d2c3a6c73f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ab6abf98fa2bcdb58f7f0eb2d3518e1184607863fedfeb2a1de34b21eeb876d3e7c9ce2eb0c48198b98dfaae7d281c49447a7dfe872a48e19d07493fdb0c020
|
7
|
+
data.tar.gz: 9925c5f71b7a0c8f9afb369abb6b83df9c6700d87884a3b9e4ce2dd77c02c2e0d9848803979ac599e431c0f282419ba2be72ea241de10479930989bc7073ed57
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,68 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.10.0] - 2020-07-27
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add diff option to file conflict menu to show file contents differences
|
7
|
+
* Add :header option to #diff to show two-line header for compared files
|
8
|
+
* Add git-like hunk coloring when displaying diff lines in unified format
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
* Change to only use keyword arguments in all methods
|
12
|
+
* Change #tail_file to accept :lines as keyword parameter
|
13
|
+
* Change to update tty-prompt & pastel dependencies
|
14
|
+
* Change prompt to be quiet and provide no output after selection
|
15
|
+
* Change #diff_files to stop raising on binary or large files
|
16
|
+
|
17
|
+
## [v0.9.0] - 2020-04-28
|
18
|
+
|
19
|
+
### Changed
|
20
|
+
* Change #chmod custom permission implementation for built-in FileUtils one
|
21
|
+
* Change to update tty-prompt dependency
|
22
|
+
* Change gemspec to include metadata and stop loading test artefacts
|
23
|
+
|
24
|
+
### Fixed
|
25
|
+
* Fix Ruby 2.7 hash to keywords conversion warnings
|
26
|
+
|
27
|
+
## [v0.8.0] - 2019-07-25
|
28
|
+
|
29
|
+
### Added
|
30
|
+
* Add #read_to_char for reading file content limited by bytes until a valid char
|
31
|
+
|
32
|
+
### Fixed
|
33
|
+
* Fix #binary? to correctly identify large UTF-8 files as non-binary
|
34
|
+
|
35
|
+
## [v0.7.1] - 2019-05-06
|
36
|
+
|
37
|
+
### Changed
|
38
|
+
* Change all methods to accept Pathname instances as argument by Chris Hoffman(@yarmiganosca)
|
39
|
+
|
40
|
+
### Fixed
|
41
|
+
* Fix Ruby 2.6.0 ERB#new arguments deprecation warnings
|
42
|
+
* Fix #replace_in_file to handle character encoding
|
43
|
+
|
44
|
+
## [v0.7.0] - 2018-12-17
|
45
|
+
|
46
|
+
### Added
|
47
|
+
* Add :secure option to #remove_file call
|
48
|
+
* Add #safe_append_to_file, #safe_prepend_to_file, #safe_inject_into_file
|
49
|
+
|
50
|
+
### Changed
|
51
|
+
* Change #replace_in_file, #append_to_file, #prepend_to_file, and #inject_into_file to perform operation unsafely without checking prior content
|
52
|
+
* Change to load gemspec files directly
|
53
|
+
* Change to update tty-prompt
|
54
|
+
* Change to freeze strings
|
55
|
+
* Change to relax tty-prompt & diff-lcs version constraints
|
56
|
+
|
57
|
+
### Fixed
|
58
|
+
* Fixed windows tests
|
59
|
+
|
3
60
|
## [v0.6.0] - 2018-05-21
|
4
61
|
|
5
62
|
### Changed
|
6
63
|
* Change identical files conflict message from blue to cyan for readability
|
7
64
|
* Change replace_in_file to stop raising error and allow forcing file overwrite
|
8
|
-
* Change replace_in_file, inject_into_file, prepend_to_file, append_to_file to return true
|
9
|
-
when operation is performed successfully, false otherwise
|
65
|
+
* Change replace_in_file, inject_into_file, prepend_to_file, append_to_file to return true when operation is performed successfully, false otherwise
|
10
66
|
* Update tty-prompt dependency
|
11
67
|
* Change download_file to preserve query parameters
|
12
68
|
|
@@ -64,6 +120,11 @@
|
|
64
120
|
|
65
121
|
* Initial implementation and release
|
66
122
|
|
123
|
+
[v0.10.0]: https://github.com/piotrmurach/tty-file/compare/v0.9.0...v0.10.0
|
124
|
+
[v0.9.0]: https://github.com/piotrmurach/tty-file/compare/v0.8.0...v0.9.0
|
125
|
+
[v0.8.0]: https://github.com/piotrmurach/tty-file/compare/v0.7.1...v0.8.0
|
126
|
+
[v0.7.1]: https://github.com/piotrmurach/tty-file/compare/v0.7.0...v0.7.1
|
127
|
+
[v0.7.0]: https://github.com/piotrmurach/tty-file/compare/v0.6.0...v0.7.0
|
67
128
|
[v0.6.0]: https://github.com/piotrmurach/tty-file/compare/v0.5.0...v0.6.0
|
68
129
|
[v0.5.0]: https://github.com/piotrmurach/tty-file/compare/v0.4.0...v0.5.0
|
69
130
|
[v0.4.0]: https://github.com/piotrmurach/tty-file/compare/v0.3.0...v0.4.0
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://cdn.rawgit.com/piotrmurach/tty/master/images/tty.png" alt="tty logo" /></a>
|
3
|
+
</div>
|
4
|
+
|
1
5
|
# TTY::File [][gitter]
|
2
6
|
|
3
7
|
[][gem]
|
@@ -26,7 +30,7 @@ Though Ruby's `File` and `FileUtils` libraries provide very robust apis for deal
|
|
26
30
|
Add this line to your application's Gemfile:
|
27
31
|
|
28
32
|
```ruby
|
29
|
-
gem
|
33
|
+
gem "tty-file"
|
30
34
|
```
|
31
35
|
|
32
36
|
And then execute:
|
@@ -48,11 +52,11 @@ Or install it yourself as:
|
|
48
52
|
* [2.5. create_file](#25-create_file)
|
49
53
|
* [2.6. copy_dir](#26-copy_dir)
|
50
54
|
* [2.7. create_dir](#27-create_dir)
|
51
|
-
* [2.8.
|
55
|
+
* [2.8. diff_files](#28-diff_files)
|
52
56
|
* [2.9. download_file](#29-download_file)
|
53
57
|
* [2.10. inject_into_file](#210-inject_into_file)
|
54
58
|
* [2.11. replace_in_file](#211-replace_in_file)
|
55
|
-
* [2.12. append_to_file](#212-
|
59
|
+
* [2.12. append_to_file](#212-append_to_file)
|
56
60
|
* [2.13. prepend_to_file](#213-prepend_to_file)
|
57
61
|
* [2.14. remove_file](#214-remove_file)
|
58
62
|
* [2.15. tail_file](#215-tail_file)
|
@@ -60,7 +64,7 @@ Or install it yourself as:
|
|
60
64
|
## 1. Usage
|
61
65
|
|
62
66
|
```ruby
|
63
|
-
TTY::File.replace_in_file(
|
67
|
+
TTY::File.replace_in_file("Gemfile", /gem 'rails'/, "gem 'hanami'")
|
64
68
|
```
|
65
69
|
|
66
70
|
## 2. Interface
|
@@ -74,7 +78,7 @@ If you wish to silence verbose output use `verbose: false`. Similarly if you wis
|
|
74
78
|
To check whether a file is a binary file, i.e. image, executable etc. do:
|
75
79
|
|
76
80
|
```ruby
|
77
|
-
TTY::File.binary?(
|
81
|
+
TTY::File.binary?("image.png") # => true
|
78
82
|
```
|
79
83
|
|
80
84
|
### 2.2. checksum_file
|
@@ -89,7 +93,7 @@ Among the supported message digest algorithms are:
|
|
89
93
|
For example, to create a digest for a string using `SHA1` do:
|
90
94
|
|
91
95
|
```ruby
|
92
|
-
TTY::File.checksum_file("Some content\nThe end",
|
96
|
+
TTY::File.checksum_file("Some content\nThe end", "sha1")
|
93
97
|
# => "289388f187404135e6c15b21460442cf867180dd"
|
94
98
|
```
|
95
99
|
|
@@ -98,19 +102,19 @@ TTY::File.checksum_file("Some content\nThe end", 'sha1')
|
|
98
102
|
To change file modes use `chmod`, like so:
|
99
103
|
|
100
104
|
```ruby
|
101
|
-
TTY::File.chmod(
|
105
|
+
TTY::File.chmod("filename.rb", 0777)
|
102
106
|
```
|
103
107
|
|
104
108
|
There are a number of constants available to represent common mode bits such as `TTY::File::U_R` and `TTY::File::O_X`, and they can be used as follows:
|
105
109
|
|
106
110
|
```ruby
|
107
|
-
TTY::File.chmod(
|
111
|
+
TTY::File.chmod("filename.rb", TTY::File::U_R | TTY::File::O_X)
|
108
112
|
```
|
109
113
|
|
110
114
|
Apart from traditional octal number definition for file permissions, you can use the more convenient permission notation used by the Unix `chmod` command:
|
111
115
|
|
112
116
|
```ruby
|
113
|
-
TTY::File.chmod(
|
117
|
+
TTY::File.chmod("filename.rb", "u=wrx,g+x")
|
114
118
|
```
|
115
119
|
|
116
120
|
The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits. The `a` symbol is equivalent to `ugo`.
|
@@ -120,13 +124,13 @@ The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits.
|
|
120
124
|
Copies a file's contents from a relative source to a relative destination.
|
121
125
|
|
122
126
|
```ruby
|
123
|
-
TTY::File.copy_file
|
127
|
+
TTY::File.copy_file "Gemfile", "Gemfile.bak"
|
124
128
|
```
|
125
129
|
|
126
130
|
If you provide a block then the file content is yielded:
|
127
131
|
|
128
132
|
```ruby
|
129
|
-
TTY::File.copy_file(
|
133
|
+
TTY::File.copy_file("Gemfile", "app/Gemfile") do |content|
|
130
134
|
"https://rubygems.org\n" + content
|
131
135
|
end
|
132
136
|
```
|
@@ -135,25 +139,25 @@ If the source file is an `ERB` template then you can provide a `:context` in whi
|
|
135
139
|
|
136
140
|
```ruby
|
137
141
|
variables = OpenStruct.new
|
138
|
-
variables[:foo] =
|
142
|
+
variables[:foo] = "bar"
|
139
143
|
|
140
|
-
TTY::File.copy_file(
|
144
|
+
TTY::File.copy_file("templates/application.html.erb", context: variables)
|
141
145
|
```
|
142
146
|
|
143
|
-
You can also specify the template name surrounding any dynamic variables with `%` to be
|
147
|
+
You can also specify the template name surrounding any dynamic variables with `%` to be evaluated:
|
144
148
|
|
145
149
|
```ruby
|
146
150
|
variables = OpenStruct.new
|
147
|
-
variables[:file_name] =
|
151
|
+
variables[:file_name] = "foo"
|
148
152
|
|
149
|
-
TTY::File.copy_file(
|
153
|
+
TTY::File.copy_file("templates/%file_name%.rb", context: variables)
|
150
154
|
# => Creates templates/foo.rb
|
151
155
|
```
|
152
156
|
|
153
157
|
If the destination is a directory, then copies source inside that directory.
|
154
158
|
|
155
159
|
```ruby
|
156
|
-
TTY::File.copy_file
|
160
|
+
TTY::File.copy_file "docs/README.md", "app"
|
157
161
|
```
|
158
162
|
|
159
163
|
If the destination file already exists, a prompt menu will be displayed to enquire about action:
|
@@ -161,24 +165,50 @@ If the destination file already exists, a prompt menu will be displayed to enqui
|
|
161
165
|
If you wish to preserve original owner, group, permission and modified time use `:preserve` option:
|
162
166
|
|
163
167
|
```ruby
|
164
|
-
TTY::File.copy_file
|
168
|
+
TTY::File.copy_file "docs/README.md", "app", preserve: true
|
165
169
|
```
|
166
170
|
|
167
171
|
### 2.5. create_file
|
168
172
|
|
169
|
-
To create a file at a given destination with
|
173
|
+
To create a file at a given destination with some content use `create_file`:
|
170
174
|
|
171
175
|
```ruby
|
172
|
-
TTY::File.create_file
|
176
|
+
TTY::File.create_file "file-a/README.md", content
|
177
|
+
```
|
178
|
+
|
179
|
+
On collision with already existing file, a menu gets displayed:
|
180
|
+
|
181
|
+
```
|
182
|
+
collision examples/file-a
|
183
|
+
Overwrite examples/file-a? (enter "h" for help) [y,d,n,q,h]
|
173
184
|
```
|
174
185
|
|
175
|
-
|
186
|
+
The `d` option allows to compare the changes:
|
187
|
+
|
188
|
+
```
|
189
|
+
--- a/examples/file-a
|
190
|
+
+++ b/examples/file-a
|
191
|
+
@@ -1,8 +1,9 @@
|
192
|
+
aaaaa
|
193
|
+
bbbbb
|
194
|
+
-ccccc
|
195
|
+
+xxxxx
|
196
|
+
+
|
197
|
+
ddddd
|
198
|
+
eeeee
|
199
|
+
fffff
|
200
|
+
-ggggg
|
201
|
+
+yyyyy
|
202
|
+
Overwrite examples/file-a? (enter "h" for help) [y,d,n,q,h]
|
203
|
+
````
|
176
204
|
|
177
205
|
You can force to always overwrite file with `:force` option or always skip by providing `:skip`.
|
178
206
|
|
207
|
+
There is [examples/overwrite.rb](examples/overwrite.rb) that demonstrates diffing file with new content.
|
208
|
+
|
179
209
|
### 2.6. copy_dir
|
180
210
|
|
181
|
-
To recursively copy a directory of files from source to destination location use `copy_directory` or its alias
|
211
|
+
To recursively copy a directory of files from source to destination location use `copy_directory` or its alias `copy_dir`.
|
182
212
|
|
183
213
|
Assuming you have the following directory structure:
|
184
214
|
|
@@ -190,10 +220,10 @@ Assuming you have the following directory structure:
|
|
190
220
|
# %name%.rb
|
191
221
|
```
|
192
222
|
|
193
|
-
|
223
|
+
You can copy `doc` folder to `docs` by invoking:
|
194
224
|
|
195
225
|
```ruby
|
196
|
-
TTY::File.copy_directory(
|
226
|
+
TTY::File.copy_directory("doc", "docs", context: ...)
|
197
227
|
```
|
198
228
|
|
199
229
|
The `context` needs to respond to `name` message and given it returns `foo` value the following directory gets created:
|
@@ -209,13 +239,13 @@ The `context` needs to respond to `name` message and given it returns `foo` valu
|
|
209
239
|
If you only need to copy top level files use option `recursive: false`:
|
210
240
|
|
211
241
|
```ruby
|
212
|
-
TTY::File.copy_directory(
|
242
|
+
TTY::File.copy_directory("doc", "docs", recursive: false)
|
213
243
|
```
|
214
244
|
|
215
245
|
By passing `:exclude` option you can instruct the method to ignore any files including the given pattern:
|
216
246
|
|
217
247
|
```ruby
|
218
|
-
TTY::File.copy_directory(
|
248
|
+
TTY::File.copy_directory("doc", "docs", exclude: "subcommands")
|
219
249
|
```
|
220
250
|
|
221
251
|
### 2.7. create_dir
|
@@ -223,21 +253,21 @@ TTY::File.copy_directory('doc', 'docs', exclude: 'subcommands')
|
|
223
253
|
To create directory use `create_directory` or its alias `create_dir` passing as a first argument file path:
|
224
254
|
|
225
255
|
```ruby
|
226
|
-
TTY::File.create_dir(/path/to/directory)
|
256
|
+
TTY::File.create_dir("/path/to/directory")
|
227
257
|
```
|
228
258
|
|
229
|
-
|
259
|
+
Or a data structure describing the directory tree including any files with or without content:
|
230
260
|
|
231
261
|
```ruby
|
232
262
|
tree =
|
233
|
-
|
234
|
-
|
235
|
-
[
|
236
|
-
|
237
|
-
|
238
|
-
[
|
263
|
+
"app" => [
|
264
|
+
"README.md",
|
265
|
+
["Gemfile", "gem 'tty-file'"],
|
266
|
+
"lib" => [
|
267
|
+
"cli.rb",
|
268
|
+
["file_utils.rb", "require 'tty-file'"]
|
239
269
|
]
|
240
|
-
|
270
|
+
"spec" => []
|
241
271
|
]
|
242
272
|
```
|
243
273
|
|
@@ -256,50 +286,93 @@ TTY::File.create_dir(tree)
|
|
256
286
|
As a second argument you can provide a parent directory, otherwise current directory will be assumed:
|
257
287
|
|
258
288
|
```ruby
|
259
|
-
|
289
|
+
TTY::File.create_dir(tree, "/path/to/parent/dir")
|
260
290
|
```
|
261
291
|
|
262
|
-
### 2.8.
|
292
|
+
### 2.8. diff_files
|
263
293
|
|
264
294
|
To compare files line by line in a system independent way use `diff`, or `diff_files`:
|
265
295
|
|
266
296
|
```ruby
|
267
|
-
TTY::File.
|
268
|
-
# =>
|
269
|
-
# @@ -1,4 +1,4 @@
|
270
|
-
# aaa
|
271
|
-
# -bbb
|
272
|
-
# +xxx
|
273
|
-
# ccc
|
297
|
+
print TTY::File.diff_files("file-a", "file-b")
|
274
298
|
```
|
275
299
|
|
276
|
-
|
300
|
+
Printing output to console would result in:
|
301
|
+
|
302
|
+
```
|
303
|
+
diff examples/file-a and examples/file-b
|
304
|
+
--- examples/file-a
|
305
|
+
+++ examples/file-b
|
306
|
+
@@ -1,8 +1,9 @@
|
307
|
+
aaaaa
|
308
|
+
bbbbb
|
309
|
+
-ccccc
|
310
|
+
+xxxxx
|
311
|
+
+
|
312
|
+
ddddd
|
313
|
+
eeeee
|
314
|
+
fffff
|
315
|
+
-ggggg
|
316
|
+
+yyyyy
|
317
|
+
```
|
277
318
|
|
278
|
-
|
319
|
+
You can also pass additional parameters such as:
|
279
320
|
|
280
|
-
|
321
|
+
* `:format` - accepted values are `:unified`, `:old`, `:context` and `:ed`. Defaults to `:unified` as seen in the output above - similar to git tool.
|
322
|
+
* `:lines` - how many extra lines to include in the output around the compared lines. Defaults to `3` lines.
|
323
|
+
* `:threshold` - set maximum file size in bytes. By default files larger than `10Mb` are no processed.
|
324
|
+
* `:header` - controls display of two-line files comparison. By default `true`.
|
281
325
|
|
282
|
-
|
326
|
+
Changing format to `:old`, removing context lines and skipping log output:
|
283
327
|
|
284
328
|
```ruby
|
285
|
-
TTY::File.
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
329
|
+
TTY::File.diff_files("file_a", "file_b", format: :old, lines: 0, verbose: false)
|
330
|
+
```
|
331
|
+
|
332
|
+
Results in the following output:
|
333
|
+
|
334
|
+
```
|
335
|
+
<<< examples/file-a
|
336
|
+
>>> examples/file-b
|
337
|
+
3c3,4
|
338
|
+
< ccccc
|
339
|
+
---
|
340
|
+
> xxxxx
|
341
|
+
>
|
342
|
+
|
343
|
+
7c8
|
344
|
+
< ggggg
|
345
|
+
---
|
346
|
+
> yyyyy
|
295
347
|
```
|
296
348
|
|
297
|
-
|
349
|
+
In addition, you can perform a comparison between a file and a string or between two strings. For example, comparing file with content:
|
298
350
|
|
299
351
|
```ruby
|
300
|
-
TTY::File.
|
352
|
+
TTY::File.diff_files("file-a", "new\nlong\ntext")
|
301
353
|
```
|
302
354
|
|
355
|
+
Will output:
|
356
|
+
|
357
|
+
```
|
358
|
+
diff a/examples/file-a and b/examples/file-a
|
359
|
+
--- a/examples/file-a
|
360
|
+
+++ b/examples/file-a
|
361
|
+
@@ -1,8 +1,4 @@
|
362
|
+
-aaaaa
|
363
|
+
-bbbbb
|
364
|
+
-ccccc
|
365
|
+
-ddddd
|
366
|
+
-eeeee
|
367
|
+
-fffff
|
368
|
+
-ggggg
|
369
|
+
+new
|
370
|
+
+long
|
371
|
+
+text
|
372
|
+
````
|
373
|
+
|
374
|
+
Please run [examples/diff.rb](examples/diff.rb) to see how output works.
|
375
|
+
|
303
376
|
### 2.9. download_file
|
304
377
|
|
305
378
|
To download a content from a given address and to save at a given relative location do:
|
@@ -328,32 +401,46 @@ TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md", limi
|
|
328
401
|
Inject content into a file at a given location and return `true` when performed successfully, `false` otherwise.
|
329
402
|
|
330
403
|
```ruby
|
331
|
-
TTY::File.inject_into_file
|
404
|
+
TTY::File.inject_into_file "filename.rb", "text to add", after: "Code below this line\n"
|
332
405
|
```
|
333
406
|
|
334
|
-
|
407
|
+
Or using a block:
|
335
408
|
|
336
409
|
```ruby
|
337
|
-
TTY::File.inject_into_file
|
410
|
+
TTY::File.inject_into_file "filename.rb", after: "Code below this line\n" do
|
338
411
|
"text to add"
|
339
412
|
end
|
340
413
|
```
|
341
414
|
|
342
|
-
You can also use Regular Expressions in `:after` or `:before` to match file location.
|
415
|
+
You can also use Regular Expressions in `:after` or `:before` to match file location.
|
416
|
+
|
417
|
+
By default, this method will always inject content into file, regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually inserting text:
|
418
|
+
|
419
|
+
```ruby
|
420
|
+
TTY::File.inject_into_file("filename.rb", "text to add", after: "Code below this line\n"
|
421
|
+
```
|
422
|
+
|
423
|
+
Alternatively, use `safe_inject_into_file` to check if the text can be safely inserted.
|
424
|
+
|
425
|
+
```ruby
|
426
|
+
TTY::File.safe_inject_into_file("Gemfile", "gem 'tty'")
|
427
|
+
```
|
428
|
+
|
429
|
+
The [append_to_file](#212-append_to_file) and [prepend_to_file](#213-prepend_to_file) allow you to add content at the end and the begging of a file.
|
343
430
|
|
344
431
|
### 2.11. replace_in_file
|
345
432
|
|
346
433
|
Replace content of a file matching condition by calling `replace_in_file` or `gsub_file`, which returns `true` when substitutions are performed successfully, `false` otherwise.
|
347
434
|
|
348
435
|
```ruby
|
349
|
-
TTY::File.replace_in_file
|
436
|
+
TTY::File.replace_in_file "filename.rb", /matching condition/, "replacement"
|
350
437
|
```
|
351
438
|
|
352
439
|
The replacement content can be provided in a block
|
353
440
|
|
354
441
|
```ruby
|
355
|
-
TTY::File.gsub_file
|
356
|
-
|
442
|
+
TTY::File.gsub_file "filename.rb", /matching condition/ do
|
443
|
+
"replacement"
|
357
444
|
end
|
358
445
|
```
|
359
446
|
|
@@ -362,45 +449,69 @@ end
|
|
362
449
|
Appends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
|
363
450
|
|
364
451
|
```ruby
|
365
|
-
TTY::File.append_to_file(
|
452
|
+
TTY::File.append_to_file("Gemfile", "gem 'tty'")
|
366
453
|
```
|
367
454
|
|
368
|
-
|
455
|
+
Or inside a block:
|
369
456
|
|
370
457
|
```ruby
|
371
|
-
TTY::File.append_to_file(
|
458
|
+
TTY::File.append_to_file("Gemfile") do
|
372
459
|
"gem 'tty'"
|
373
460
|
end
|
374
461
|
```
|
375
462
|
|
463
|
+
By default, this method will always append content regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually appending:
|
464
|
+
|
465
|
+
```ruby
|
466
|
+
TTY::File.append_to_file("Gemfile", "gem 'tty'", force: false)
|
467
|
+
```
|
468
|
+
|
469
|
+
Alternatively, use `safe_append_to_file` to check if the text can be safely appended.
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
TTY::File.safe_append_to_file("Gemfile", "gem 'tty'")
|
473
|
+
```
|
474
|
+
|
376
475
|
### 2.13. prepend_to_file
|
377
476
|
|
378
477
|
Prepends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
|
379
478
|
|
380
479
|
```ruby
|
381
|
-
TTY::File.prepend_to_file(
|
480
|
+
TTY::File.prepend_to_file("Gemfile", "gem 'tty'")
|
382
481
|
```
|
383
482
|
|
384
|
-
|
483
|
+
Or inside a block:
|
385
484
|
|
386
485
|
```ruby
|
387
|
-
TTY::File.prepend_to_file(
|
486
|
+
TTY::File.prepend_to_file("Gemfile") do
|
388
487
|
"gem 'tty'"
|
389
488
|
end
|
390
489
|
```
|
391
490
|
|
491
|
+
By default, this method will always prepend content regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually prepending:
|
492
|
+
|
493
|
+
```ruby
|
494
|
+
TTY::File.prepend_to_file("Gemfile", "gem 'tty'", force: false)
|
495
|
+
```
|
496
|
+
|
497
|
+
Alternatively, use `safe_prepend_to_file` to check if the text can be safely appended.
|
498
|
+
|
499
|
+
```ruby
|
500
|
+
TTY::File.safe_prepend_to_file("Gemfile", "gem 'tty'")
|
501
|
+
```
|
502
|
+
|
392
503
|
### 2.14. remove_file
|
393
504
|
|
394
505
|
To remove a file do:
|
395
506
|
|
396
507
|
```ruby
|
397
|
-
TTY::File.remove_file
|
508
|
+
TTY::File.remove_file "doc/README.md"
|
398
509
|
```
|
399
510
|
|
400
511
|
You can also pass in `:force` to remove file ignoring any errors:
|
401
512
|
|
402
513
|
```ruby
|
403
|
-
TTY::File.remove_file
|
514
|
+
TTY::File.remove_file "doc/README.md", force: true
|
404
515
|
```
|
405
516
|
|
406
517
|
### 2.15. tail_file
|
@@ -408,14 +519,14 @@ TTY::File.remove_file 'doc/README.md', force: true
|
|
408
519
|
To read the last 10 lines from a file do:
|
409
520
|
|
410
521
|
```ruby
|
411
|
-
TTY::File.tail_file
|
412
|
-
# => [
|
522
|
+
TTY::File.tail_file "doc/README.md"
|
523
|
+
# => ["## Copyright", "Copyright (c) 2016-2017", ...]
|
413
524
|
```
|
414
525
|
|
415
526
|
You can also pass a block:
|
416
527
|
|
417
528
|
```ruby
|
418
|
-
TTY::File.tail_file(
|
529
|
+
TTY::File.tail_file("doc/README.md") do |line|
|
419
530
|
puts line
|
420
531
|
end
|
421
532
|
```
|
@@ -423,7 +534,7 @@ end
|
|
423
534
|
To change how many lines are read pass a second argument:
|
424
535
|
|
425
536
|
```ruby
|
426
|
-
TTY::File.tail_file(
|
537
|
+
TTY::File.tail_file("doc/README.md", 15)
|
427
538
|
```
|
428
539
|
|
429
540
|
## Development
|
@@ -442,4 +553,4 @@ The gem is available as open source under the terms of the [MIT License](http://
|
|
442
553
|
|
443
554
|
## Copyright
|
444
555
|
|
445
|
-
Copyright (c) 2016
|
556
|
+
Copyright (c) 2016 Piotr Murach. See LICENSE for further details.
|