tty-file 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/lib/tty/file.rb +75 -85
- data/lib/tty/file/create_file.rb +5 -5
- data/lib/tty/file/differ.rb +5 -4
- data/lib/tty/file/digest_file.rb +4 -4
- data/lib/tty/file/download_file.rb +5 -5
- data/lib/tty/file/version.rb +1 -1
- metadata +19 -56
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/spec/fixtures/cli_app/%name%_cli.rb +0 -2
- data/spec/fixtures/cli_app/commands/subcommand.rb +0 -2
- data/spec/fixtures/cli_app/excluded/%name%_cli.rb +0 -2
- data/spec/fixtures/templates/%file_name%.rb +0 -1
- data/spec/fixtures/templates/unit_test.rb +0 -1
- data/spec/spec_helper.rb +0 -94
- data/spec/unit/append_to_file_spec.rb +0 -110
- data/spec/unit/binary_spec.rb +0 -230
- data/spec/unit/checksum_file_spec.rb +0 -48
- data/spec/unit/chmod_spec.rb +0 -92
- data/spec/unit/copy_directory_spec.rb +0 -120
- data/spec/unit/copy_file_spec.rb +0 -172
- data/spec/unit/create_directory_spec.rb +0 -93
- data/spec/unit/create_file_spec.rb +0 -130
- data/spec/unit/diff_spec.rb +0 -107
- data/spec/unit/differ/call_spec.rb +0 -101
- data/spec/unit/download_file_spec.rb +0 -68
- data/spec/unit/escape_glob_path_spec.rb +0 -14
- data/spec/unit/inject_into_file_spec.rb +0 -176
- data/spec/unit/prepend_to_file_spec.rb +0 -124
- data/spec/unit/read_to_char_spec.rb +0 -24
- data/spec/unit/remove_file_spec.rb +0 -67
- data/spec/unit/replace_in_file_spec.rb +0 -140
- data/spec/unit/tail_file_spec.rb +0 -77
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-file.gemspec +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c14d1cde8381d3b5e40e0b73139df56495bf4247abc1e5b28cfa5cfc17ed94a
|
4
|
+
data.tar.gz: 752eebdc5b63125700de9ed06aa8abd700ab1518a595e26b2bbaa767dadc31c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2625754b9f281800439fbc63eea1b978bf3711ec38eb5040e8279f3ce68de5880c8a58003f2f42b7717f86660f987aa321db128f095cef9a81360b19a020da3f
|
7
|
+
data.tar.gz: e87cd920b9f868ed0774d986b997e697a1351cc83916b05fd148e439b0e9fc5c24ace82d41add7c287dced567ba5ce43010cd58667f04409c3a49d1988fbf38d
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.9.0] - 2020-04-28
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change #chmod custom permission implementation for built-in FileUtils one
|
7
|
+
* Change to update tty-prompt dependency
|
8
|
+
* Change gemspec to include metadata and stop loading test artefacts
|
9
|
+
|
10
|
+
### Fixed
|
11
|
+
* Fix Ruby 2.7 hash to keywords conversion warnings
|
12
|
+
|
3
13
|
## [v0.8.0] - 2019-07-25
|
4
14
|
|
5
15
|
### Added
|
@@ -96,6 +106,7 @@
|
|
96
106
|
|
97
107
|
* Initial implementation and release
|
98
108
|
|
109
|
+
[v0.9.0]: https://github.com/piotrmurach/tty-file/compare/v0.8.0...v0.9.0
|
99
110
|
[v0.8.0]: https://github.com/piotrmurach/tty-file/compare/v0.7.1...v0.8.0
|
100
111
|
[v0.7.1]: https://github.com/piotrmurach/tty-file/compare/v0.7.0...v0.7.1
|
101
112
|
[v0.7.0]: https://github.com/piotrmurach/tty-file/compare/v0.6.0...v0.7.0
|
data/lib/tty/file.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
13
|
-
require_relative
|
3
|
+
require "pastel"
|
4
|
+
require "erb"
|
5
|
+
require "tempfile"
|
6
|
+
require "pathname"
|
7
|
+
|
8
|
+
require_relative "file/create_file"
|
9
|
+
require_relative "file/digest_file"
|
10
|
+
require_relative "file/download_file"
|
11
|
+
require_relative "file/differ"
|
12
|
+
require_relative "file/read_backward_file"
|
13
|
+
require_relative "file/version"
|
14
14
|
|
15
15
|
module TTY
|
16
16
|
module File
|
@@ -41,10 +41,10 @@ module TTY
|
|
41
41
|
# the path to file to check
|
42
42
|
#
|
43
43
|
# @example
|
44
|
-
# binary?(
|
44
|
+
# binary?("Gemfile") # => false
|
45
45
|
#
|
46
46
|
# @example
|
47
|
-
# binary?(
|
47
|
+
# binary?("image.jpg") # => true
|
48
48
|
#
|
49
49
|
# @return [Boolean]
|
50
50
|
# Returns `true` if the file is binary, `false` otherwise
|
@@ -103,18 +103,18 @@ module TTY
|
|
103
103
|
# No operation
|
104
104
|
#
|
105
105
|
# @example
|
106
|
-
# checksum_file(
|
106
|
+
# checksum_file("/path/to/file")
|
107
107
|
#
|
108
108
|
# @example
|
109
|
-
# checksum_file(
|
109
|
+
# checksum_file("Some string content", "md5")
|
110
110
|
#
|
111
111
|
# @return [String]
|
112
112
|
# the generated hex value
|
113
113
|
#
|
114
114
|
# @api public
|
115
115
|
def checksum_file(source, *args, **options)
|
116
|
-
mode = args.size.zero? ?
|
117
|
-
digester = DigestFile.new(source, mode
|
116
|
+
mode = args.size.zero? ? "sha256" : args.pop
|
117
|
+
digester = DigestFile.new(source, mode)
|
118
118
|
digester.call unless options[:noop]
|
119
119
|
end
|
120
120
|
module_function :checksum_file
|
@@ -129,31 +129,19 @@ module TTY
|
|
129
129
|
# @option options [Symbol] :force
|
130
130
|
#
|
131
131
|
# @example
|
132
|
-
# chmod(
|
132
|
+
# chmod("Gemfile", 0755)
|
133
133
|
#
|
134
134
|
# @example
|
135
|
-
# chmod(
|
135
|
+
# chmod("Gemilfe", TTY::File::U_R | TTY::File::U_W)
|
136
136
|
#
|
137
137
|
# @example
|
138
|
-
# chmod(
|
138
|
+
# chmod("Gemfile", "u+x,g+x")
|
139
139
|
#
|
140
140
|
# @api public
|
141
141
|
def chmod(relative_path, permissions, **options)
|
142
|
-
mode = ::File.lstat(relative_path).mode
|
143
|
-
if permissions.to_s =~ /\d+/
|
144
|
-
mode = permissions
|
145
|
-
else
|
146
|
-
permissions.scan(/[ugoa][+-=][rwx]+/) do |setting|
|
147
|
-
who, action = setting[0], setting[1]
|
148
|
-
setting[2..setting.size].each_byte do |perm|
|
149
|
-
mask = const_get("#{who.upcase}_#{perm.chr.upcase}")
|
150
|
-
(action == '+') ? mode |= mask : mode ^= mask
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
142
|
log_status(:chmod, relative_path, options.fetch(:verbose, true),
|
155
|
-
|
156
|
-
::FileUtils.chmod_R(
|
143
|
+
options.fetch(:color, :green))
|
144
|
+
::FileUtils.chmod_R(permissions, relative_path) unless options[:noop]
|
157
145
|
end
|
158
146
|
module_function :chmod
|
159
147
|
|
@@ -163,18 +151,18 @@ module TTY
|
|
163
151
|
# the path or data structure describing directory tree
|
164
152
|
#
|
165
153
|
# @example
|
166
|
-
# create_directory(
|
154
|
+
# create_directory("/path/to/dir")
|
167
155
|
#
|
168
156
|
# @example
|
169
157
|
# tree =
|
170
|
-
#
|
171
|
-
#
|
172
|
-
# [
|
173
|
-
#
|
174
|
-
#
|
175
|
-
# [
|
158
|
+
# "app" => [
|
159
|
+
# "README.md",
|
160
|
+
# ["Gemfile", "gem "tty-file""],
|
161
|
+
# "lib" => [
|
162
|
+
# "cli.rb",
|
163
|
+
# ["file_utils.rb", "require "tty-file""]
|
176
164
|
# ]
|
177
|
-
#
|
165
|
+
# "spec" => []
|
178
166
|
# ]
|
179
167
|
#
|
180
168
|
# create_directory(tree)
|
@@ -198,9 +186,9 @@ module TTY
|
|
198
186
|
|
199
187
|
files.each do |filename, contents|
|
200
188
|
if filename.respond_to?(:each_pair)
|
201
|
-
create_directory(filename, path, options)
|
189
|
+
create_directory(filename, path, **options)
|
202
190
|
else
|
203
|
-
create_file(::File.join(path, filename), contents, options)
|
191
|
+
create_file(::File.join(path, filename), contents, **options)
|
204
192
|
end
|
205
193
|
end
|
206
194
|
end
|
@@ -220,11 +208,11 @@ module TTY
|
|
220
208
|
# forces ovewrite if conflict present
|
221
209
|
#
|
222
210
|
# @example
|
223
|
-
# create_file(
|
211
|
+
# create_file("doc/README.md", "# Title header")
|
224
212
|
#
|
225
213
|
# @example
|
226
|
-
# create_file
|
227
|
-
#
|
214
|
+
# create_file "doc/README.md" do
|
215
|
+
# "# Title Header"
|
228
216
|
# end
|
229
217
|
#
|
230
218
|
# @api public
|
@@ -243,12 +231,12 @@ module TTY
|
|
243
231
|
# destination running it through ERB.
|
244
232
|
#
|
245
233
|
# @example
|
246
|
-
# copy_file
|
234
|
+
# copy_file "templates/test.rb", "app/test.rb"
|
247
235
|
#
|
248
236
|
# @example
|
249
237
|
# vars = OpenStruct.new
|
250
|
-
# vars[:name] =
|
251
|
-
# copy_file
|
238
|
+
# vars[:name] = "foo"
|
239
|
+
# copy_file "templates/%name%.rb", "app/%name%.rb", context: vars
|
252
240
|
#
|
253
241
|
# @param [String, Pathname] source_path
|
254
242
|
# @param [Hash] options
|
@@ -265,15 +253,15 @@ module TTY
|
|
265
253
|
# @api public
|
266
254
|
def copy_file(source_path, *args, **options, &block)
|
267
255
|
source_path = source_path.to_s
|
268
|
-
dest_path = (args.first || source_path).to_s.sub(/\.erb$/,
|
256
|
+
dest_path = (args.first || source_path).to_s.sub(/\.erb$/, "")
|
269
257
|
|
270
258
|
ctx = if (vars = options[:context])
|
271
|
-
vars.instance_eval(
|
259
|
+
vars.instance_eval("binding")
|
272
260
|
else
|
273
|
-
instance_eval(
|
261
|
+
instance_eval("binding")
|
274
262
|
end
|
275
263
|
|
276
|
-
create_file(dest_path, options) do
|
264
|
+
create_file(dest_path, **options) do
|
277
265
|
version = ERB.version.scan(/\d+\.\d+\.\d+/)[0]
|
278
266
|
template = if version.to_f >= 2.2
|
279
267
|
ERB.new(::File.binread(source_path), trim_mode: "-", eoutvar: "@output_buffer")
|
@@ -285,7 +273,8 @@ module TTY
|
|
285
273
|
content
|
286
274
|
end
|
287
275
|
return unless options[:preserve]
|
288
|
-
|
276
|
+
|
277
|
+
copy_metadata(source_path, dest_path, **options)
|
289
278
|
end
|
290
279
|
module_function :copy_file
|
291
280
|
|
@@ -300,7 +289,7 @@ module TTY
|
|
300
289
|
def copy_metadata(src_path, dest_path, **options)
|
301
290
|
stats = ::File.lstat(src_path)
|
302
291
|
::File.utime(stats.atime, stats.mtime, dest_path)
|
303
|
-
chmod(dest_path, stats.mode, options)
|
292
|
+
chmod(dest_path, stats.mode, **options)
|
304
293
|
end
|
305
294
|
module_function :copy_metadata
|
306
295
|
|
@@ -318,7 +307,7 @@ module TTY
|
|
318
307
|
# Invoking:
|
319
308
|
# copy_directory("app", "new_app")
|
320
309
|
# The following directory structure should be created where
|
321
|
-
# name resolves to
|
310
|
+
# name resolves to "cli" value:
|
322
311
|
#
|
323
312
|
# new_app/
|
324
313
|
# cli.rb
|
@@ -345,15 +334,15 @@ module TTY
|
|
345
334
|
check_path(source_path)
|
346
335
|
source = escape_glob_path(source_path)
|
347
336
|
dest_path = (args.first || source).to_s
|
348
|
-
opts = {recursive: true}.merge(options)
|
349
|
-
pattern = opts[:recursive] ? ::File.join(source,
|
350
|
-
glob_pattern = ::File.join(pattern,
|
337
|
+
opts = { recursive: true }.merge(options)
|
338
|
+
pattern = opts[:recursive] ? ::File.join(source, "**") : source
|
339
|
+
glob_pattern = ::File.join(pattern, "*")
|
351
340
|
|
352
341
|
Dir.glob(glob_pattern, ::File::FNM_DOTMATCH).sort.each do |file_source|
|
353
342
|
next if ::File.directory?(file_source)
|
354
343
|
next if opts[:exclude] && file_source.match(opts[:exclude])
|
355
344
|
|
356
|
-
dest = ::File.join(dest_path, file_source.gsub(source_path,
|
345
|
+
dest = ::File.join(dest_path, file_source.gsub(source_path, "."))
|
357
346
|
file_dest = ::Pathname.new(dest).cleanpath.to_s
|
358
347
|
|
359
348
|
copy_file(file_source, file_dest, **options, &block)
|
@@ -446,7 +435,7 @@ module TTY
|
|
446
435
|
dest_path = (args.first || ::File.basename(uri)).to_s
|
447
436
|
|
448
437
|
unless uri =~ %r{^https?\://}
|
449
|
-
copy_file(uri, dest_path, options)
|
438
|
+
copy_file(uri, dest_path, **options)
|
450
439
|
return
|
451
440
|
end
|
452
441
|
|
@@ -456,7 +445,7 @@ module TTY
|
|
456
445
|
content = (block.arity.nonzero? ? block[content] : block[])
|
457
446
|
end
|
458
447
|
|
459
|
-
create_file(dest_path, content, options)
|
448
|
+
create_file(dest_path, content, **options)
|
460
449
|
end
|
461
450
|
module_function :download_file
|
462
451
|
|
@@ -470,19 +459,19 @@ module TTY
|
|
470
459
|
# the content to preped to file
|
471
460
|
#
|
472
461
|
# @example
|
473
|
-
# prepend_to_file(
|
462
|
+
# prepend_to_file("Gemfile", "gem "tty"")
|
474
463
|
#
|
475
464
|
# @example
|
476
|
-
# prepend_to_file(
|
465
|
+
# prepend_to_file("Gemfile") do
|
477
466
|
# "gem 'tty'"
|
478
467
|
# end
|
479
468
|
#
|
480
469
|
# @api public
|
481
470
|
def prepend_to_file(relative_path, *args, **options, &block)
|
482
471
|
log_status(:prepend, relative_path, options.fetch(:verbose, true),
|
483
|
-
|
472
|
+
options.fetch(:color, :green))
|
484
473
|
options.merge!(before: /\A/, verbose: false)
|
485
|
-
inject_into_file(relative_path, *
|
474
|
+
inject_into_file(relative_path, *args, **options, &block)
|
486
475
|
end
|
487
476
|
module_function :prepend_to_file
|
488
477
|
|
@@ -501,19 +490,19 @@ module TTY
|
|
501
490
|
# the content to append to file
|
502
491
|
#
|
503
492
|
# @example
|
504
|
-
# append_to_file(
|
493
|
+
# append_to_file("Gemfile", "gem 'tty'")
|
505
494
|
#
|
506
495
|
# @example
|
507
|
-
# append_to_file(
|
496
|
+
# append_to_file("Gemfile") do
|
508
497
|
# "gem 'tty'"
|
509
498
|
# end
|
510
499
|
#
|
511
500
|
# @api public
|
512
501
|
def append_to_file(relative_path, *args, **options, &block)
|
513
502
|
log_status(:append, relative_path, options.fetch(:verbose, true),
|
514
|
-
|
503
|
+
options.fetch(:color, :green))
|
515
504
|
options.merge!(after: /\z/, verbose: false)
|
516
|
-
inject_into_file(relative_path, *
|
505
|
+
inject_into_file(relative_path, *args, **options, &block)
|
517
506
|
end
|
518
507
|
module_function :append_to_file
|
519
508
|
|
@@ -543,13 +532,13 @@ module TTY
|
|
543
532
|
# log status
|
544
533
|
#
|
545
534
|
# @example
|
546
|
-
# inject_into_file(
|
535
|
+
# inject_into_file("Gemfile", "gem 'tty'", after: "gem 'rack'\n")
|
547
536
|
#
|
548
537
|
# @example
|
549
|
-
# inject_into_file(
|
538
|
+
# inject_into_file("Gemfile", "gem 'tty'\n", "gem 'loaf'", after: "gem 'rack'\n")
|
550
539
|
#
|
551
540
|
# @example
|
552
|
-
# inject_into_file(
|
541
|
+
# inject_into_file("Gemfile", after: "gem 'rack'\n") do
|
553
542
|
# "gem 'tty'\n"
|
554
543
|
# end
|
555
544
|
#
|
@@ -574,7 +563,7 @@ module TTY
|
|
574
563
|
log_status(:inject, relative_path, options.fetch(:verbose, true),
|
575
564
|
options.fetch(:color, :green))
|
576
565
|
replace_in_file(relative_path, /#{match}/, content,
|
577
|
-
options.merge(verbose: false))
|
566
|
+
**options.merge(verbose: false))
|
578
567
|
end
|
579
568
|
module_function :inject_into_file
|
580
569
|
|
@@ -600,10 +589,10 @@ module TTY
|
|
600
589
|
# log status
|
601
590
|
#
|
602
591
|
# @example
|
603
|
-
# replace_in_file(
|
592
|
+
# replace_in_file("Gemfile", /gem 'rails'/, "gem 'hanami'")
|
604
593
|
#
|
605
594
|
# @example
|
606
|
-
# replace_in_file(
|
595
|
+
# replace_in_file("Gemfile", /gem 'rails'/) do |match|
|
607
596
|
# match = "gem 'hanami'"
|
608
597
|
# end
|
609
598
|
#
|
@@ -614,7 +603,7 @@ module TTY
|
|
614
603
|
def replace_in_file(relative_path, *args, **options, &block)
|
615
604
|
check_path(relative_path)
|
616
605
|
contents = ::File.read(relative_path)
|
617
|
-
replacement = (block ? block[] : args[1..-1].join).gsub('\0',
|
606
|
+
replacement = (block ? block[] : args[1..-1].join).gsub('\0', "")
|
618
607
|
match = Regexp.escape(replacement)
|
619
608
|
status = nil
|
620
609
|
|
@@ -625,7 +614,7 @@ module TTY
|
|
625
614
|
if options.fetch(:force, true) || !(contents =~ /^#{match}(\r?\n)*/m)
|
626
615
|
status = contents.gsub!(*args, &block)
|
627
616
|
if !status.nil?
|
628
|
-
::File.open(relative_path,
|
617
|
+
::File.open(relative_path, "w") do |file|
|
629
618
|
file.write(contents)
|
630
619
|
end
|
631
620
|
end
|
@@ -651,7 +640,7 @@ module TTY
|
|
651
640
|
# for secure removing
|
652
641
|
#
|
653
642
|
# @example
|
654
|
-
# remove_file
|
643
|
+
# remove_file "doc/README.md"
|
655
644
|
#
|
656
645
|
# @api public
|
657
646
|
def remove_file(relative_path, *args, **options)
|
@@ -675,12 +664,12 @@ module TTY
|
|
675
664
|
# the number of lines to return from file
|
676
665
|
#
|
677
666
|
# @example
|
678
|
-
# tail_file
|
679
|
-
# # => [
|
667
|
+
# tail_file "filename"
|
668
|
+
# # => ["line 19", "line20", ... ]
|
680
669
|
#
|
681
670
|
# @example
|
682
|
-
# tail_file
|
683
|
-
# # => [
|
671
|
+
# tail_file "filename", 15
|
672
|
+
# # => ["line 19", "line20", ... ]
|
684
673
|
#
|
685
674
|
# @return [Array[String]]
|
686
675
|
#
|
@@ -736,6 +725,7 @@ module TTY
|
|
736
725
|
# @api private
|
737
726
|
def check_path(path)
|
738
727
|
return if ::File.exist?(path)
|
728
|
+
|
739
729
|
raise InvalidPathError, "File path \"#{path}\" does not exist."
|
740
730
|
end
|
741
731
|
private_module_function :check_path
|
@@ -779,7 +769,7 @@ module TTY
|
|
779
769
|
if ::FileTest.file?(object)
|
780
770
|
::File.open(object, &block)
|
781
771
|
else
|
782
|
-
tempfile = Tempfile.new(
|
772
|
+
tempfile = Tempfile.new("tty-file-diff")
|
783
773
|
tempfile << object
|
784
774
|
tempfile.rewind
|
785
775
|
|
data/lib/tty/file/create_file.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "tty-prompt"
|
4
4
|
|
5
5
|
module TTY
|
6
6
|
module File
|
@@ -34,7 +34,7 @@ module TTY
|
|
34
34
|
def call
|
35
35
|
detect_collision do
|
36
36
|
FileUtils.mkdir_p(::File.dirname(relative_path))
|
37
|
-
::File.open(relative_path,
|
37
|
+
::File.open(relative_path, "wb") { |f| f.write(content) }
|
38
38
|
end
|
39
39
|
relative_path
|
40
40
|
end
|
@@ -86,9 +86,9 @@ module TTY
|
|
86
86
|
# @api private
|
87
87
|
def file_collision(relative_path, content)
|
88
88
|
choices = [
|
89
|
-
{ key:
|
90
|
-
{ key:
|
91
|
-
{ key:
|
89
|
+
{ key: "y", name: "yes, overwrite", value: :yes },
|
90
|
+
{ key: "n", name: "no, do not overwrite", value: :no },
|
91
|
+
{ key: "q", name: "quit, abort", value: :quit }
|
92
92
|
]
|
93
93
|
answer = prompt.expand("Overwrite #{relative_path}?", choices)
|
94
94
|
interpret_answer(answer)
|