tty-file 0.6.0 → 0.7.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.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +18 -2
  3. data/README.md +58 -16
  4. data/Rakefile +1 -1
  5. data/lib/tty-file.rb +0 -2
  6. data/lib/tty/file.rb +35 -9
  7. data/lib/tty/file/create_file.rb +3 -1
  8. data/lib/tty/file/differ.rb +3 -2
  9. data/lib/tty/file/digest_file.rb +1 -1
  10. data/lib/tty/file/download_file.rb +3 -3
  11. data/lib/tty/file/read_backward_file.rb +0 -1
  12. data/lib/tty/file/version.rb +2 -2
  13. data/spec/fixtures/cli_app/%name%_cli.rb +2 -0
  14. data/spec/fixtures/cli_app/commands/subcommand.rb +2 -0
  15. data/spec/fixtures/cli_app/excluded/%name%_cli.rb +2 -0
  16. data/spec/fixtures/templates/%file_name%.rb +1 -0
  17. data/spec/fixtures/templates/unit_test.rb +1 -0
  18. data/spec/spec_helper.rb +90 -0
  19. data/spec/unit/append_to_file_spec.rb +85 -0
  20. data/spec/unit/binary_spec.rb +206 -0
  21. data/spec/unit/checksum_file_spec.rb +39 -0
  22. data/spec/unit/chmod_spec.rb +78 -0
  23. data/spec/unit/copy_directory_spec.rb +106 -0
  24. data/spec/unit/copy_file_spec.rb +157 -0
  25. data/spec/unit/create_directory_spec.rb +79 -0
  26. data/spec/unit/create_file_spec.rb +116 -0
  27. data/spec/unit/diff_spec.rb +93 -0
  28. data/spec/unit/differ/call_spec.rb +101 -0
  29. data/spec/unit/download_file_spec.rb +54 -0
  30. data/spec/unit/escape_glob_path_spec.rb +14 -0
  31. data/spec/unit/inject_into_file_spec.rb +162 -0
  32. data/spec/unit/prepend_to_file_spec.rb +98 -0
  33. data/spec/unit/remove_file_spec.rb +53 -0
  34. data/spec/unit/replace_in_file_spec.rb +126 -0
  35. data/spec/unit/tail_file_spec.rb +63 -0
  36. data/tasks/console.rake +1 -1
  37. data/tasks/coverage.rake +1 -1
  38. data/tasks/spec.rake +1 -1
  39. data/tty-file.gemspec +5 -4
  40. metadata +30 -13
  41. data/.gitignore +0 -10
  42. data/.rspec +0 -3
  43. data/.travis.yml +0 -26
  44. data/CODE_OF_CONDUCT.md +0 -49
  45. data/Gemfile +0 -9
  46. data/appveyor.yml +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 12c95f3b7bd457b553e6592d5a9c74cf3dff2d9a
4
- data.tar.gz: adc293eda605ad68b5f45acff1d37c2849050922
2
+ SHA256:
3
+ metadata.gz: c45f9ae4c83da383b63e7951bbc66f0e335ef0dcfe1e5c23f1730f7e03a4a828
4
+ data.tar.gz: 9ce415f8dda2fc64aa7d4b288d46bf0ee32942846f7233288b65b88f8f4758c5
5
5
  SHA512:
6
- metadata.gz: 3a86b5c70306a91576514af459e7fd3a458858732352cbe68683afa27786576abdc018c4388e4aa00105e85a23de1d0dcd84f02ea715377ef86e7bbee2ff2197
7
- data.tar.gz: a484156abd85e985118467b7cfd087c9672882e763fe0a84867b2543d08c4aa2000fc7640d205ffd6c2201fd18d0a79bcc0f83bf128165668ae4d7d06ca52e5c
6
+ metadata.gz: ba8743853a4a962d8e7b7dc7ce9c73cff20ede0d506160782605a1187a50facb8b6addd1a5ec2b73ee1f701e8a991afa95ac441010d894fb5b7b4275a6ac0ae6
7
+ data.tar.gz: bc7dad3603aac26bdcdcc2016871a52f1a717fbd67d7067e846c08059eb883b719f06c349db023115dc026a41893e51460654e1f5d1fa93b202e537c53d1d926
@@ -1,12 +1,27 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.7.0] - 2018-12-17
4
+
5
+ ### Added
6
+ * Add :secure option to #remove_file call
7
+ * Add #safe_append_to_file, #safe_prepend_to_file, #safe_inject_into_file
8
+
9
+ ### Changed
10
+ * Change #replace_in_file, #append_to_file, #prepend_to_file, and #inject_into_file to perform operation unsafely without checking prior content
11
+ * Change to load gemspec files directly
12
+ * Change to update tty-prompt
13
+ * Change to freeze strings
14
+ * Change to relax tty-prompt & diff-lcs version constraints
15
+
16
+ ### Fixed
17
+ * Fixed windows tests
18
+
3
19
  ## [v0.6.0] - 2018-05-21
4
20
 
5
21
  ### Changed
6
22
  * Change identical files conflict message from blue to cyan for readability
7
23
  * 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
24
+ * Change replace_in_file, inject_into_file, prepend_to_file, append_to_file to return true when operation is performed successfully, false otherwise
10
25
  * Update tty-prompt dependency
11
26
  * Change download_file to preserve query parameters
12
27
 
@@ -64,6 +79,7 @@
64
79
 
65
80
  * Initial implementation and release
66
81
 
82
+ [v0.7.0]: https://github.com/piotrmurach/tty-file/compare/v0.6.0...v0.7.0
67
83
  [v0.6.0]: https://github.com/piotrmurach/tty-file/compare/v0.5.0...v0.6.0
68
84
  [v0.5.0]: https://github.com/piotrmurach/tty-file/compare/v0.4.0...v0.5.0
69
85
  [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](https://badges.gitter.im/Join%20Chat.svg)][gitter]
2
6
 
3
7
  [![Gem Version](https://badge.fury.io/rb/tty-file.svg)][gem]
@@ -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. diff](#28-diff)
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-apend_to_file)
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)
@@ -140,7 +144,7 @@ variables[:foo] = 'bar'
140
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 evaluted:
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
@@ -190,7 +194,7 @@ Assuming you have the following directory structure:
190
194
  # %name%.rb
191
195
  ```
192
196
 
193
- you can copy `doc` folder to `docs` by invoking:
197
+ You can copy `doc` folder to `docs` by invoking:
194
198
 
195
199
  ```ruby
196
200
  TTY::File.copy_directory('doc', 'docs', context: ...)
@@ -223,10 +227,10 @@ TTY::File.copy_directory('doc', 'docs', exclude: 'subcommands')
223
227
  To create directory use `create_directory` or its alias `create_dir` passing as a first argument file path:
224
228
 
225
229
  ```ruby
226
- TTY::File.create_dir(/path/to/directory)
230
+ TTY::File.create_dir('/path/to/directory')
227
231
  ```
228
232
 
229
- or a data structure describing the directory tree including any files with or without content:
233
+ Or a data structure describing the directory tree including any files with or without content:
230
234
 
231
235
  ```ruby
232
236
  tree =
@@ -256,15 +260,15 @@ TTY::File.create_dir(tree)
256
260
  As a second argument you can provide a parent directory, otherwise current directory will be assumed:
257
261
 
258
262
  ```ruby
259
- TTy::File.create_dir(tree, '/path/to/parent/dir')
263
+ TTY::File.create_dir(tree, '/path/to/parent/dir')
260
264
  ```
261
265
 
262
- ### 2.8. diff
266
+ ### 2.8. diff_files
263
267
 
264
268
  To compare files line by line in a system independent way use `diff`, or `diff_files`:
265
269
 
266
270
  ```ruby
267
- TTY::File.diff('file_a', 'file_b')
271
+ TTY::File.diff_files('file_a', 'file_b')
268
272
  # =>
269
273
  # @@ -1,4 +1,4 @@
270
274
  # aaa
@@ -282,7 +286,7 @@ The `:context_lines` specifies how many extra lines around the differing lines t
282
286
  The `:threshold` sets maximum file size in bytes, by default files larger than `10Mb` are not processed.
283
287
 
284
288
  ```ruby
285
- TTY::File.diff('file_a', 'file_b', format: :old)
289
+ TTY::File.diff_files('file_a', 'file_b', format: :old)
286
290
  # =>
287
291
  # 1,4c1,4
288
292
  # < aaa
@@ -297,7 +301,7 @@ TTY::File.diff('file_a', 'file_b', format: :old)
297
301
  Equally, you can perform a comparison between a file content and a string content like so:
298
302
 
299
303
  ```ruby
300
- TTY::File.diff('/path/to/file', 'some long text')
304
+ TTY::File.diff_files('/path/to/file', 'some long text')
301
305
  ```
302
306
 
303
307
  ### 2.9. download_file
@@ -331,7 +335,7 @@ Inject content into a file at a given location and return `true` when performed
331
335
  TTY::File.inject_into_file 'filename.rb', "text to add", after: "Code below this line\n"
332
336
  ```
333
337
 
334
- or using a block
338
+ Or using a block:
335
339
 
336
340
  ```ruby
337
341
  TTY::File.inject_into_file 'filename.rb', after: "Code below this line\n" do
@@ -339,7 +343,21 @@ TTY::File.inject_into_file 'filename.rb', after: "Code below this line\n" do
339
343
  end
340
344
  ```
341
345
 
342
- You can also use Regular Expressions in `:after` or `:before` to match file location. The `append_to_file` and `prepend_to_file` allow you to add content at the end and the begging of a file.
346
+ You can also use Regular Expressions in `:after` or `:before` to match file location.
347
+
348
+ 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:
349
+
350
+ ```ruby
351
+ TTY::File.inject_into_file('filename.rb', "text to add", after: "Code below this line\n"
352
+ ```
353
+
354
+ Alternatively, use `safe_inject_into_file` to check if the text can be safely inserted.
355
+
356
+ ```ruby
357
+ TTY::File.safe_inject_into_file('Gemfile', "gem 'tty'")
358
+ ```
359
+
360
+ 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
361
 
344
362
  ### 2.11. replace_in_file
345
363
 
@@ -365,7 +383,7 @@ Appends text to a file and returns `true` when performed successfully, `false` o
365
383
  TTY::File.append_to_file('Gemfile', "gem 'tty'")
366
384
  ```
367
385
 
368
- or inside a block:
386
+ Or inside a block:
369
387
 
370
388
  ```ruby
371
389
  TTY::File.append_to_file('Gemfile') do
@@ -373,6 +391,18 @@ TTY::File.append_to_file('Gemfile') do
373
391
  end
374
392
  ```
375
393
 
394
+ 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:
395
+
396
+ ```ruby
397
+ TTY::File.append_to_file('Gemfile', "gem 'tty'", force: false)
398
+ ```
399
+
400
+ Alternatively, use `safe_append_to_file` to check if the text can be safely appended.
401
+
402
+ ```ruby
403
+ TTY::File.safe_append_to_file('Gemfile', "gem 'tty'")
404
+ ```
405
+
376
406
  ### 2.13. prepend_to_file
377
407
 
378
408
  Prepends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
@@ -381,7 +411,7 @@ Prepends text to a file and returns `true` when performed successfully, `false`
381
411
  TTY::File.prepend_to_file('Gemfile', "gem 'tty'")
382
412
  ```
383
413
 
384
- or inside a block:
414
+ Or inside a block:
385
415
 
386
416
  ```ruby
387
417
  TTY::File.prepend_to_file('Gemfile') do
@@ -389,6 +419,18 @@ TTY::File.prepend_to_file('Gemfile') do
389
419
  end
390
420
  ```
391
421
 
422
+ 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:
423
+
424
+ ```ruby
425
+ TTY::File.prepend_to_file('Gemfile', "gem 'tty'", force: false)
426
+ ```
427
+
428
+ Alternatively, use `safe_prepend_to_file` to check if the text can be safely appended.
429
+
430
+ ```ruby
431
+ TTY::File.safe_prepend_to_file('Gemfile', "gem 'tty'")
432
+ ```
433
+
392
434
  ### 2.14. remove_file
393
435
 
394
436
  To remove a file do:
@@ -442,4 +484,4 @@ The gem is available as open source under the terms of the [MIT License](http://
442
484
 
443
485
  ## Copyright
444
486
 
445
- Copyright (c) 2016-2018 Piotr Murach. See LICENSE for further details.
487
+ Copyright (c) 2016 Piotr Murach. See LICENSE for further details.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
4
 
@@ -1,3 +1 @@
1
- # encoding: utf-8
2
-
3
1
  require_relative 'tty/file'
@@ -1,7 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'pastel'
4
- require 'tty-prompt'
5
4
  require 'erb'
6
5
  require 'tempfile'
7
6
  require 'pathname'
@@ -344,7 +343,7 @@ module TTY
344
343
  # @api public
345
344
  def diff(path_a, path_b, **options)
346
345
  threshold = options[:threshold] || 10_000_000
347
- output = ''
346
+ output = []
348
347
 
349
348
  open_tempfile_if_missing(path_a) do |file_a|
350
349
  if ::File.size(file_a) > threshold
@@ -363,7 +362,7 @@ module TTY
363
362
 
364
363
  log_status(:diff, "#{file_a.path} - #{file_b.path}",
365
364
  options.fetch(:verbose, true), options.fetch(:color, :green))
366
- return output if options[:noop]
365
+ return output.join if options[:noop]
367
366
 
368
367
  block_size = file_a.lstat.blksize
369
368
  while !file_a.eof? && !file_b.eof?
@@ -373,7 +372,7 @@ module TTY
373
372
  end
374
373
  end
375
374
  end
376
- output
375
+ output.join
377
376
  end
378
377
  module_function :diff
379
378
 
@@ -447,6 +446,14 @@ module TTY
447
446
  end
448
447
  module_function :prepend_to_file
449
448
 
449
+ # Safely prepend to file checking if content is not already present
450
+ #
451
+ # @api public
452
+ def safe_prepend_to_file(relative_path, *args, **options, &block)
453
+ prepend_to_file(relative_path, *args, **(options.merge(force: false)), &block)
454
+ end
455
+ module_function :safe_prepend_to_file
456
+
450
457
  # Append to a file
451
458
  #
452
459
  # @param [String] relative_path
@@ -473,6 +480,14 @@ module TTY
473
480
  alias add_to_file append_to_file
474
481
  module_function :add_to_file
475
482
 
483
+ # Safely append to file checking if content is not already present
484
+ #
485
+ # @api public
486
+ def safe_append_to_file(relative_path, *args, **options, &block)
487
+ append_to_file(relative_path, *args, **(options.merge(force: false)), &block)
488
+ end
489
+ module_function :safe_append_to_file
490
+
476
491
  # Inject content into file at a given location
477
492
  #
478
493
  # @param [String] relative_path
@@ -526,6 +541,14 @@ module TTY
526
541
  alias insert_into_file inject_into_file
527
542
  module_function :insert_into_file
528
543
 
544
+ # Safely prepend to file checking if content is not already present
545
+ #
546
+ # @api public
547
+ def safe_inject_into_file(relative_path, *args, **options, &block)
548
+ inject_into_file(relative_path, *args, **(options.merge(force: false)), &block)
549
+ end
550
+ module_function :safe_inject_into_file
551
+
529
552
  # Replace content of a file matching string, returning false
530
553
  # when no substitutions were performed, true otherwise.
531
554
  #
@@ -549,7 +572,7 @@ module TTY
549
572
  # @api public
550
573
  def replace_in_file(relative_path, *args, **options, &block)
551
574
  check_path(relative_path)
552
- contents = IO.read(relative_path)
575
+ contents = ::File.binread(relative_path)
553
576
  replacement = (block ? block[] : args[1..-1].join).gsub('\0', '')
554
577
  match = Regexp.escape(replacement)
555
578
  status = nil
@@ -558,7 +581,7 @@ module TTY
558
581
  options.fetch(:color, :green))
559
582
  return false if options[:noop]
560
583
 
561
- if !(contents =~ /^#{match}(\r?\n)*/m) || options[:force]
584
+ if options.fetch(:force, true) || !(contents =~ /^#{match}(\r?\n)*/m)
562
585
  status = contents.gsub!(*args, &block)
563
586
  if !status.nil?
564
587
  ::File.open(relative_path, 'wb') do |file|
@@ -582,6 +605,8 @@ module TTY
582
605
  # remove file ignoring errors
583
606
  # @option options [Symbol] :verbose
584
607
  # log status
608
+ # @option options [Symbol] :secure
609
+ # for secure removing
585
610
  #
586
611
  # @example
587
612
  # remove_file 'doc/README.md'
@@ -591,9 +616,10 @@ module TTY
591
616
  log_status(:remove, relative_path, options.fetch(:verbose, true),
592
617
  options.fetch(:color, :red))
593
618
 
594
- return if options[:noop]
619
+ return if options[:noop] || !::File.exist?(relative_path)
595
620
 
596
- ::FileUtils.rm_r(relative_path, force: options[:force], secure: true)
621
+ ::FileUtils.rm_r(relative_path, force: options[:force],
622
+ secure: options.fetch(:secure, true))
597
623
  end
598
624
  module_function :remove_file
599
625
 
@@ -1,4 +1,6 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty-prompt'
2
4
 
3
5
  module TTY
4
6
  module File
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'diff/lcs'
4
4
  require 'diff/lcs/hunk'
@@ -59,7 +59,7 @@ module TTY
59
59
 
60
60
  # @api public
61
61
  def format_hunks(hunks)
62
- output = ""
62
+ output = []
63
63
  hunks.each_cons(2) do |prev_hunk, current_hunk|
64
64
  begin
65
65
  if current_hunk.overlaps?(prev_hunk)
@@ -72,6 +72,7 @@ module TTY
72
72
  end
73
73
  end
74
74
  output << hunks.last.diff(@format) << "\n" if hunks.last
75
+ output.join
75
76
  end
76
77
  end # Differ
77
78
  end # File
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'stringio'
4
4
  require 'openssl'
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'uri'
4
4
  require 'net/http'
@@ -32,7 +32,7 @@ module TTY
32
32
  # @api private
33
33
  def download(uri, path, limit)
34
34
  raise DownloadError, 'Redirect limit reached!' if limit.zero?
35
- content = ''
35
+ content = []
36
36
 
37
37
  Net::HTTP.start(uri.host, uri.port,
38
38
  use_ssl: uri.scheme == 'https') do |http|
@@ -49,7 +49,7 @@ module TTY
49
49
  end
50
50
  end
51
51
  end
52
- content
52
+ content.join
53
53
  end
54
54
  end # DownloadFile
55
55
  end # File
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  class ReadBackwardFile
@@ -1,7 +1,7 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module TTY
4
4
  module File
5
- VERSION = '0.6.0'.freeze
5
+ VERSION = '0.7.0'
6
6
  end # File
7
7
  end # TTY
@@ -0,0 +1,2 @@
1
+ class AppCLI
2
+ end
@@ -0,0 +1,2 @@
1
+ class Subcommand < Command
2
+ end
@@ -0,0 +1,2 @@
1
+ class AppCLI
2
+ end
@@ -0,0 +1 @@
1
+ <%= foo %>