tty-file 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/tty/file.rb +35 -21
- data/lib/tty/file/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/unit/append_to_file_spec.rb +96 -71
- data/spec/unit/binary_spec.rb +19 -1
- data/spec/unit/checksum_file_spec.rb +9 -0
- data/spec/unit/chmod_spec.rb +65 -51
- data/spec/unit/copy_directory_spec.rb +110 -96
- data/spec/unit/copy_file_spec.rb +117 -102
- data/spec/unit/create_directory_spec.rb +83 -69
- data/spec/unit/create_file_spec.rb +96 -82
- data/spec/unit/diff_spec.rb +97 -83
- data/spec/unit/download_file_spec.rb +49 -35
- data/spec/unit/inject_into_file_spec.rb +162 -148
- data/spec/unit/prepend_to_file_spec.rb +109 -83
- data/spec/unit/remove_file_spec.rb +45 -31
- data/spec/unit/replace_in_file_spec.rb +127 -113
- data/spec/unit/tail_file_spec.rb +61 -47
- data/tty-file.gemspec +4 -4
- metadata +12 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d22dc89aced7b8ab2593a332739545706562d2683f93e9a88ea7fdec5a06453
|
4
|
+
data.tar.gz: c94f59cbaaccf16ce290e08ca44ad9c4ebfb7f105d953db9c6ba0dc00de369da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a583724c8f93ae02a69cea8c171076f53305fea40b7e6cf04d9de0ec3a0d275847a1d5a3da87ae42453b40835d15165ca7b32156c47b6d5e280b05cd07b3d2c
|
7
|
+
data.tar.gz: 0e737e0619ef7db2d1cc18507b768ca4d62d88cef2dbf12501f8b855598c5aaaed11ecc88b87505a9e4cfd759fc1bfb83afc2fc1b293f6a9e56405482823668e
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.7.1] - 2019-05-06
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change all methods to accept Pathname instances as argument by Chris Hoffman(@yarmiganosca)
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
* Fix Ruby 2.6.0 ERB#new arguments deprecation warnings
|
10
|
+
* Fix #replace_in_file to handle character encoding
|
11
|
+
|
3
12
|
## [v0.7.0] - 2018-12-17
|
4
13
|
|
5
14
|
### Added
|
@@ -79,6 +88,7 @@
|
|
79
88
|
|
80
89
|
* Initial implementation and release
|
81
90
|
|
91
|
+
[v0.7.1]: https://github.com/piotrmurach/tty-file/compare/v0.7.0...v0.7.1
|
82
92
|
[v0.7.0]: https://github.com/piotrmurach/tty-file/compare/v0.6.0...v0.7.0
|
83
93
|
[v0.6.0]: https://github.com/piotrmurach/tty-file/compare/v0.5.0...v0.6.0
|
84
94
|
[v0.5.0]: https://github.com/piotrmurach/tty-file/compare/v0.4.0...v0.5.0
|
data/lib/tty/file.rb
CHANGED
@@ -37,7 +37,7 @@ module TTY
|
|
37
37
|
|
38
38
|
# Check if file is binary
|
39
39
|
#
|
40
|
-
# @param [String] relative_path
|
40
|
+
# @param [String, Pathname] relative_path
|
41
41
|
# the path to file to check
|
42
42
|
#
|
43
43
|
# @example
|
@@ -66,7 +66,7 @@ module TTY
|
|
66
66
|
|
67
67
|
# Create checksum for a file, io or string objects
|
68
68
|
#
|
69
|
-
# @param [File,IO,String] source
|
69
|
+
# @param [File, IO, String, Pathname] source
|
70
70
|
# the source to generate checksum for
|
71
71
|
# @param [String] mode
|
72
72
|
# @param [Hash[Symbol]] options
|
@@ -92,7 +92,7 @@ module TTY
|
|
92
92
|
|
93
93
|
# Change file permissions
|
94
94
|
#
|
95
|
-
# @param [String] relative_path
|
95
|
+
# @param [String, Pathname] relative_path
|
96
96
|
# @param [Integer,String] permisssions
|
97
97
|
# @param [Hash[Symbol]] options
|
98
98
|
# @option options [Symbol] :noop
|
@@ -130,7 +130,7 @@ module TTY
|
|
130
130
|
|
131
131
|
# Create directory structure
|
132
132
|
#
|
133
|
-
# @param [String, Hash] destination
|
133
|
+
# @param [String, Pathname, Hash] destination
|
134
134
|
# the path or data structure describing directory tree
|
135
135
|
#
|
136
136
|
# @example
|
@@ -155,8 +155,8 @@ module TTY
|
|
155
155
|
# @api public
|
156
156
|
def create_directory(destination, *args, **options)
|
157
157
|
parent = args.size.nonzero? ? args.pop : nil
|
158
|
-
if destination.is_a?(String)
|
159
|
-
destination = { destination => [] }
|
158
|
+
if destination.is_a?(String) || destination.is_a?(Pathname)
|
159
|
+
destination = { destination.to_s => [] }
|
160
160
|
end
|
161
161
|
|
162
162
|
destination.each do |dir, files|
|
@@ -183,7 +183,7 @@ module TTY
|
|
183
183
|
|
184
184
|
# Create new file if doesn't exist
|
185
185
|
#
|
186
|
-
# @param [String] relative_path
|
186
|
+
# @param [String, Pathname] relative_path
|
187
187
|
# @param [String|nil] content
|
188
188
|
# the content to add to file
|
189
189
|
# @param [Hash] options
|
@@ -200,6 +200,7 @@ module TTY
|
|
200
200
|
#
|
201
201
|
# @api public
|
202
202
|
def create_file(relative_path, *args, **options, &block)
|
203
|
+
relative_path = relative_path.to_s
|
203
204
|
content = block_given? ? block[] : args.join
|
204
205
|
|
205
206
|
CreateFile.new(self, relative_path, content, options).call
|
@@ -220,6 +221,7 @@ module TTY
|
|
220
221
|
# vars[:name] = 'foo'
|
221
222
|
# copy_file 'templates/%name%.rb', 'app/%name%.rb', context: vars
|
222
223
|
#
|
224
|
+
# @param [String, Pathname] source_path
|
223
225
|
# @param [Hash] options
|
224
226
|
# @option options [Symbol] :context
|
225
227
|
# the binding to use for the template
|
@@ -233,7 +235,8 @@ module TTY
|
|
233
235
|
#
|
234
236
|
# @api public
|
235
237
|
def copy_file(source_path, *args, **options, &block)
|
236
|
-
|
238
|
+
source_path = source_path.to_s
|
239
|
+
dest_path = (args.first || source_path).to_s.sub(/\.erb$/, '')
|
237
240
|
|
238
241
|
ctx = if (vars = options[:context])
|
239
242
|
vars.instance_eval('binding')
|
@@ -242,7 +245,12 @@ module TTY
|
|
242
245
|
end
|
243
246
|
|
244
247
|
create_file(dest_path, options) do
|
245
|
-
|
248
|
+
version = ERB.version.scan(/\d+\.\d+\.\d+/)[0]
|
249
|
+
template = if version.to_f >= 2.2
|
250
|
+
ERB.new(::File.binread(source_path), trim_mode: "-", eoutvar: "@output_buffer")
|
251
|
+
else
|
252
|
+
ERB.new(::File.binread(source_path), nil, "-", "@output_buffer")
|
253
|
+
end
|
246
254
|
content = template.result(ctx)
|
247
255
|
content = block[content] if block
|
248
256
|
content
|
@@ -288,6 +296,7 @@ module TTY
|
|
288
296
|
# command.rb
|
289
297
|
# README
|
290
298
|
#
|
299
|
+
# @param [String, Pathname] source_path
|
291
300
|
# @param [Hash[Symbol]] options
|
292
301
|
# @option options [Symbol] :preserve
|
293
302
|
# If true, the owner, group, permissions and modified time
|
@@ -303,9 +312,10 @@ module TTY
|
|
303
312
|
#
|
304
313
|
# @api public
|
305
314
|
def copy_directory(source_path, *args, **options, &block)
|
315
|
+
source_path = source_path.to_s
|
306
316
|
check_path(source_path)
|
307
317
|
source = escape_glob_path(source_path)
|
308
|
-
dest_path = args.first || source
|
318
|
+
dest_path = (args.first || source).to_s
|
309
319
|
opts = {recursive: true}.merge(options)
|
310
320
|
pattern = opts[:recursive] ? ::File.join(source, '**') : source
|
311
321
|
glob_pattern = ::File.join(pattern, '*')
|
@@ -327,8 +337,8 @@ module TTY
|
|
327
337
|
|
328
338
|
# Diff files line by line
|
329
339
|
#
|
330
|
-
# @param [String] path_a
|
331
|
-
# @param [String] path_b
|
340
|
+
# @param [String, Pathname] path_a
|
341
|
+
# @param [String, Pathname] path_b
|
332
342
|
# @param [Hash[Symbol]] options
|
333
343
|
# @option options [Symbol] :format
|
334
344
|
# the diffining output format
|
@@ -384,9 +394,9 @@ module TTY
|
|
384
394
|
# is provided in place of destination, the content of
|
385
395
|
# of the uri is yielded.
|
386
396
|
#
|
387
|
-
# @param [String] uri
|
397
|
+
# @param [String, Pathname] uri
|
388
398
|
# the URI address
|
389
|
-
# @param [String] dest
|
399
|
+
# @param [String, Pathname] dest
|
390
400
|
# the relative path to save
|
391
401
|
# @param [Hash[Symbol]] options
|
392
402
|
# @param options [Symbol] :limit
|
@@ -403,7 +413,8 @@ module TTY
|
|
403
413
|
#
|
404
414
|
# @api public
|
405
415
|
def download_file(uri, *args, **options, &block)
|
406
|
-
|
416
|
+
uri = uri.to_s
|
417
|
+
dest_path = (args.first || ::File.basename(uri)).to_s
|
407
418
|
|
408
419
|
unless uri =~ %r{^https?\://}
|
409
420
|
copy_file(uri, dest_path, options)
|
@@ -425,7 +436,7 @@ module TTY
|
|
425
436
|
|
426
437
|
# Prepend to a file
|
427
438
|
#
|
428
|
-
# @param [String] relative_path
|
439
|
+
# @param [String, Pathname] relative_path
|
429
440
|
# @param [Array[String]] content
|
430
441
|
# the content to preped to file
|
431
442
|
#
|
@@ -456,7 +467,7 @@ module TTY
|
|
456
467
|
|
457
468
|
# Append to a file
|
458
469
|
#
|
459
|
-
# @param [String] relative_path
|
470
|
+
# @param [String, Pathname] relative_path
|
460
471
|
# @param [Array[String]] content
|
461
472
|
# the content to append to file
|
462
473
|
#
|
@@ -490,7 +501,7 @@ module TTY
|
|
490
501
|
|
491
502
|
# Inject content into file at a given location
|
492
503
|
#
|
493
|
-
# @param [String] relative_path
|
504
|
+
# @param [String, Pathname] relative_path
|
494
505
|
#
|
495
506
|
# @param [Hash] options
|
496
507
|
# @option options [Symbol] :before
|
@@ -552,6 +563,7 @@ module TTY
|
|
552
563
|
# Replace content of a file matching string, returning false
|
553
564
|
# when no substitutions were performed, true otherwise.
|
554
565
|
#
|
566
|
+
# @param [String, Pathname] relative_path
|
555
567
|
# @options [Hash[String]] options
|
556
568
|
# @option options [Symbol] :force
|
557
569
|
# replace content even if present
|
@@ -572,7 +584,7 @@ module TTY
|
|
572
584
|
# @api public
|
573
585
|
def replace_in_file(relative_path, *args, **options, &block)
|
574
586
|
check_path(relative_path)
|
575
|
-
contents = ::File.
|
587
|
+
contents = ::File.read(relative_path)
|
576
588
|
replacement = (block ? block[] : args[1..-1].join).gsub('\0', '')
|
577
589
|
match = Regexp.escape(replacement)
|
578
590
|
status = nil
|
@@ -584,7 +596,7 @@ module TTY
|
|
584
596
|
if options.fetch(:force, true) || !(contents =~ /^#{match}(\r?\n)*/m)
|
585
597
|
status = contents.gsub!(*args, &block)
|
586
598
|
if !status.nil?
|
587
|
-
::File.open(relative_path, '
|
599
|
+
::File.open(relative_path, 'w') do |file|
|
588
600
|
file.write(contents)
|
589
601
|
end
|
590
602
|
end
|
@@ -598,6 +610,7 @@ module TTY
|
|
598
610
|
|
599
611
|
# Remove a file or a directory at specified relative path.
|
600
612
|
#
|
613
|
+
# @param [String, Pathname] relative_path
|
601
614
|
# @param [Hash[:Symbol]] options
|
602
615
|
# @option options [Symbol] :noop
|
603
616
|
# pretend removing file
|
@@ -613,6 +626,7 @@ module TTY
|
|
613
626
|
#
|
614
627
|
# @api public
|
615
628
|
def remove_file(relative_path, *args, **options)
|
629
|
+
relative_path = relative_path.to_s
|
616
630
|
log_status(:remove, relative_path, options.fetch(:verbose, true),
|
617
631
|
options.fetch(:color, :red))
|
618
632
|
|
@@ -625,7 +639,7 @@ module TTY
|
|
625
639
|
|
626
640
|
# Provide the last number of lines from a file
|
627
641
|
#
|
628
|
-
# @param [String] relative_path
|
642
|
+
# @param [String, Pathname] relative_path
|
629
643
|
# the relative path to a file
|
630
644
|
#
|
631
645
|
# @param [Integer] num_lines
|
data/lib/tty/file/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -38,6 +38,10 @@ module Helpers
|
|
38
38
|
::File.join(dir_path('tmp'), filename.to_s)
|
39
39
|
end
|
40
40
|
|
41
|
+
def tmp_pathname(filename = nil)
|
42
|
+
Pathname.new(tmp_path(filename))
|
43
|
+
end
|
44
|
+
|
41
45
|
def exists_and_identical?(source, dest)
|
42
46
|
dest_path = tmp_path(dest)
|
43
47
|
expect(::File.exist?(dest_path)).to be(true)
|
@@ -1,85 +1,110 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe TTY::File,
|
4
|
-
|
5
|
-
file
|
6
|
-
|
7
|
-
expect(File.read(file)).to eq([
|
8
|
-
"gem 'nokogiri'\n",
|
9
|
-
"gem 'rails', '5.0.0'\n",
|
10
|
-
"gem 'rack', '>=1.0'\n",
|
11
|
-
"gem 'tty'"
|
12
|
-
].join)
|
13
|
-
end
|
3
|
+
RSpec.describe TTY::File, "#append_to_file" do
|
4
|
+
shared_context "appending to files" do
|
5
|
+
it "appends to file" do
|
6
|
+
file = path_factory.call('Gemfile')
|
14
7
|
|
15
|
-
|
16
|
-
file = tmp_path('Gemfile')
|
17
|
-
TTY::File.append_to_file(file, "gem 'tty'\n", "gem 'rake'", verbose: false)
|
18
|
-
expect(File.read(file)).to eq([
|
19
|
-
"gem 'nokogiri'\n",
|
20
|
-
"gem 'rails', '5.0.0'\n",
|
21
|
-
"gem 'rack', '>=1.0'\n",
|
22
|
-
"gem 'tty'\n",
|
23
|
-
"gem 'rake'"
|
24
|
-
].join)
|
25
|
-
end
|
8
|
+
TTY::File.append_to_file(file, "gem 'tty'", verbose: false)
|
26
9
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
"gem 'tty'"
|
35
|
-
].join)
|
36
|
-
end
|
10
|
+
expect(File.read(file)).to eq([
|
11
|
+
"gem 'nokogiri'\n",
|
12
|
+
"gem 'rails', '5.0.0'\n",
|
13
|
+
"gem 'rack', '>=1.0'\n",
|
14
|
+
"gem 'tty'"
|
15
|
+
].join)
|
16
|
+
end
|
37
17
|
|
38
|
-
|
39
|
-
|
40
|
-
TTY::File.append_to_file(file, "gem 'rack', '>=1.0'\n", force: false, verbose: false)
|
41
|
-
expect(::File.read(file)).to eq([
|
42
|
-
"gem 'nokogiri'\n",
|
43
|
-
"gem 'rails', '5.0.0'\n",
|
44
|
-
"gem 'rack', '>=1.0'\n",
|
45
|
-
].join)
|
46
|
-
end
|
18
|
+
it "appends multiple lines to file" do
|
19
|
+
file = path_factory.call('Gemfile')
|
47
20
|
|
48
|
-
|
49
|
-
file = tmp_path('Gemfile')
|
50
|
-
TTY::File.safe_append_to_file(file, "gem 'rack', '>=1.0'\n", verbose: false)
|
21
|
+
TTY::File.append_to_file(file, "gem 'tty'\n", "gem 'rake'", verbose: false)
|
51
22
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
23
|
+
expect(File.read(file)).to eq([
|
24
|
+
"gem 'nokogiri'\n",
|
25
|
+
"gem 'rails', '5.0.0'\n",
|
26
|
+
"gem 'rack', '>=1.0'\n",
|
27
|
+
"gem 'tty'\n",
|
28
|
+
"gem 'rake'"
|
29
|
+
].join)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "appends content in a block" do
|
33
|
+
file = path_factory.call('Gemfile')
|
34
|
+
|
35
|
+
TTY::File.append_to_file(file, verbose: false) { "gem 'tty'"}
|
36
|
+
|
37
|
+
expect(File.read(file)).to eq([
|
38
|
+
"gem 'nokogiri'\n",
|
39
|
+
"gem 'rails', '5.0.0'\n",
|
40
|
+
"gem 'rack', '>=1.0'\n",
|
41
|
+
"gem 'tty'"
|
42
|
+
].join)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "doesn't append if already present" do
|
46
|
+
file = path_factory.call('Gemfile')
|
47
|
+
|
48
|
+
TTY::File.append_to_file(file, "gem 'rack', '>=1.0'\n", force: false, verbose: false)
|
58
49
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
50
|
+
expect(::File.read(file)).to eq([
|
51
|
+
"gem 'nokogiri'\n",
|
52
|
+
"gem 'rails', '5.0.0'\n",
|
53
|
+
"gem 'rack', '>=1.0'\n",
|
54
|
+
].join)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "appends safely checking if content already present" do
|
58
|
+
file = path_factory.call('Gemfile')
|
59
|
+
|
60
|
+
TTY::File.safe_append_to_file(file, "gem 'rack', '>=1.0'\n", verbose: false)
|
61
|
+
|
62
|
+
expect(::File.read(file)).to eq([
|
63
|
+
"gem 'nokogiri'\n",
|
64
|
+
"gem 'rails', '5.0.0'\n",
|
65
|
+
"gem 'rack', '>=1.0'\n",
|
66
|
+
].join)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "appends multiple times by default" do
|
70
|
+
file = path_factory.call('Gemfile')
|
71
|
+
|
72
|
+
TTY::File.append_to_file(file, "gem 'tty'\n", verbose: false)
|
73
|
+
TTY::File.append_to_file(file, "gem 'tty'\n", verbose: false)
|
74
|
+
|
75
|
+
expect(::File.read(file)).to eq([
|
76
|
+
"gem 'nokogiri'\n",
|
77
|
+
"gem 'rails', '5.0.0'\n",
|
78
|
+
"gem 'rack', '>=1.0'\n",
|
79
|
+
"gem 'tty'\n",
|
80
|
+
"gem 'tty'\n"
|
81
|
+
].join)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "logs action" do
|
85
|
+
file = path_factory.call('Gemfile')
|
86
|
+
expect {
|
87
|
+
TTY::File.add_to_file(file, "gem 'tty'")
|
88
|
+
}.to output(/\e\[32mappend\e\[0m.*Gemfile/).to_stdout_from_any_process
|
89
|
+
end
|
90
|
+
|
91
|
+
it "logs action without color" do
|
92
|
+
file = path_factory.call('Gemfile')
|
93
|
+
expect {
|
94
|
+
TTY::File.add_to_file(file, "gem 'tty'", color: false)
|
95
|
+
}.to output(/\s+append.*Gemfile/).to_stdout_from_any_process
|
96
|
+
end
|
70
97
|
end
|
71
98
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}.to output(/\e\[32mappend\e\[0m.*Gemfile/).to_stdout_from_any_process
|
99
|
+
context "when passed a String instance for the file argument" do
|
100
|
+
let(:path_factory) { method(:tmp_path) }
|
101
|
+
|
102
|
+
include_context "appending to files"
|
77
103
|
end
|
78
104
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
}.to output(/\s+append.*Gemfile/).to_stdout_from_any_process
|
105
|
+
context "when passed a Pathname instance for the file argument" do
|
106
|
+
let(:path_factory) { method(:tmp_pathname) }
|
107
|
+
|
108
|
+
include_context "appending to files"
|
84
109
|
end
|
85
110
|
end
|