tty-file 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/CHANGELOG.md +14 -0
- data/Gemfile +3 -3
- data/README.md +15 -15
- data/appveyor.yml +3 -2
- data/lib/tty/file.rb +19 -13
- data/lib/tty/file/create_file.rb +1 -1
- data/lib/tty/file/download_file.rb +1 -1
- data/lib/tty/file/version.rb +1 -1
- data/tty-file.gemspec +2 -1
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12c95f3b7bd457b553e6592d5a9c74cf3dff2d9a
|
4
|
+
data.tar.gz: adc293eda605ad68b5f45acff1d37c2849050922
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a86b5c70306a91576514af459e7fd3a458858732352cbe68683afa27786576abdc018c4388e4aa00105e85a23de1d0dcd84f02ea715377ef86e7bbee2ff2197
|
7
|
+
data.tar.gz: a484156abd85e985118467b7cfd087c9672882e763fe0a84867b2543d08c4aa2000fc7640d205ffd6c2201fd18d0a79bcc0f83bf128165668ae4d7d06ca52e5c
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.6.0] - 2018-05-21
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change identical files conflict message from blue to cyan for readability
|
7
|
+
* 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
|
10
|
+
* Update tty-prompt dependency
|
11
|
+
* Change download_file to preserve query parameters
|
12
|
+
|
13
|
+
### Fixed
|
14
|
+
* Fix replace_in_file to preserve file original encoding
|
15
|
+
|
3
16
|
## [v0.5.0] - 2018-01-06
|
4
17
|
|
5
18
|
### Changed
|
@@ -51,6 +64,7 @@
|
|
51
64
|
|
52
65
|
* Initial implementation and release
|
53
66
|
|
67
|
+
[v0.6.0]: https://github.com/piotrmurach/tty-file/compare/v0.5.0...v0.6.0
|
54
68
|
[v0.5.0]: https://github.com/piotrmurach/tty-file/compare/v0.4.0...v0.5.0
|
55
69
|
[v0.4.0]: https://github.com/piotrmurach/tty-file/compare/v0.3.0...v0.4.0
|
56
70
|
[v0.3.0]: https://github.com/piotrmurach/tty-file/compare/v0.2.1...v0.3.0
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
|
20
20
|
## Motivation
|
21
21
|
|
22
|
-
Though Ruby's `File` and `FileUtils` provide very robust apis for dealing with files, this library aims to provide level of abstraction that is much convenient with useful logging capabilities.
|
22
|
+
Though Ruby's `File` and `FileUtils` libraries provide very robust apis for dealing with files, this library aims to provide a level of abstraction that is much more convenient, with useful logging capabilities.
|
23
23
|
|
24
24
|
## Installation
|
25
25
|
|
@@ -65,7 +65,7 @@ TTY::File.replace_in_file('Gemfile', /gem 'rails'/, "gem 'hanami'")
|
|
65
65
|
|
66
66
|
## 2. Interface
|
67
67
|
|
68
|
-
The following are
|
68
|
+
The following methods are available for creating and manipulating files.
|
69
69
|
|
70
70
|
If you wish to silence verbose output use `verbose: false`. Similarly if you wish to run action without actually triggering any action use `noop: true`.
|
71
71
|
|
@@ -79,14 +79,14 @@ TTY::File.binary?('image.png') # => true
|
|
79
79
|
|
80
80
|
### 2.2. checksum_file
|
81
81
|
|
82
|
-
To generate checksum for a file, IO object or String use `checksum_file`. By default `MD5` algorithm is used which can be changed by passing second argument.
|
82
|
+
To generate a checksum for a file, IO object, or String, use `checksum_file`. By default the `MD5` algorithm is used, which can be changed by passing a second argument.
|
83
83
|
|
84
|
-
Among supported message digest algorithms are:
|
84
|
+
Among the supported message digest algorithms are:
|
85
85
|
|
86
86
|
* `sha`, `sha1`, `sha224`, `sha256`, `sha384`, `sha512`
|
87
87
|
* `md2`, `md4`, `md5`
|
88
88
|
|
89
|
-
For example, to create digest for string using `SHA1` do:
|
89
|
+
For example, to create a digest for a string using `SHA1` do:
|
90
90
|
|
91
91
|
```ruby
|
92
92
|
TTY::File.checksum_file("Some content\nThe end", 'sha1')
|
@@ -95,19 +95,19 @@ TTY::File.checksum_file("Some content\nThe end", 'sha1')
|
|
95
95
|
|
96
96
|
### 2.3. chmod
|
97
97
|
|
98
|
-
To change file modes use `chmod
|
98
|
+
To change file modes use `chmod`, like so:
|
99
99
|
|
100
100
|
```ruby
|
101
101
|
TTY::File.chmod('filename.rb', 0777)
|
102
102
|
```
|
103
103
|
|
104
|
-
There are number of constants available to represent common mode bits such as `TTY::File::U_R
|
104
|
+
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
105
|
|
106
106
|
```ruby
|
107
107
|
TTY::File.chmod('filename.rb', TTY::File::U_R | TTY::File::O_X)
|
108
108
|
```
|
109
109
|
|
110
|
-
Apart from traditional octal number definition for file permissions, you can use more convenient permission notation
|
110
|
+
Apart from traditional octal number definition for file permissions, you can use the more convenient permission notation used by the Unix `chmod` command:
|
111
111
|
|
112
112
|
```ruby
|
113
113
|
TTY::File.chmod('filename.rb', 'u=wrx,g+x')
|
@@ -117,7 +117,7 @@ The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits.
|
|
117
117
|
|
118
118
|
### 2.4. copy_file
|
119
119
|
|
120
|
-
Copies a file
|
120
|
+
Copies a file's contents from a relative source to a relative destination.
|
121
121
|
|
122
122
|
```ruby
|
123
123
|
TTY::File.copy_file 'Gemfile', 'Gemfile.bak'
|
@@ -131,7 +131,7 @@ TTY::File.copy_file('Gemfile', 'app/Gemfile') do |content|
|
|
131
131
|
end
|
132
132
|
```
|
133
133
|
|
134
|
-
If the source file is an `ERB` template then you can provide `:context` in which the file gets
|
134
|
+
If the source file is an `ERB` template then you can provide a `:context` in which the file gets evaluated, or if `TTY::File` gets included as a module then appropriate object context will be used by default. To use `:context` do:
|
135
135
|
|
136
136
|
```ruby
|
137
137
|
variables = OpenStruct.new
|
@@ -140,7 +140,7 @@ variables[:foo] = 'bar'
|
|
140
140
|
TTY::File.copy_file('templates/application.html.erb', context: variables)
|
141
141
|
```
|
142
142
|
|
143
|
-
You can also
|
143
|
+
You can also specify the template name surrounding any dynamic variables with `%` to be evaluted:
|
144
144
|
|
145
145
|
```ruby
|
146
146
|
variables = OpenStruct.new
|
@@ -325,7 +325,7 @@ TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md", limi
|
|
325
325
|
|
326
326
|
### 2.10. inject_into_file
|
327
327
|
|
328
|
-
Inject content into a file at a given location
|
328
|
+
Inject content into a file at a given location and return `true` when performed successfully, `false` otherwise.
|
329
329
|
|
330
330
|
```ruby
|
331
331
|
TTY::File.inject_into_file 'filename.rb', "text to add", after: "Code below this line\n"
|
@@ -343,7 +343,7 @@ You can also use Regular Expressions in `:after` or `:before` to match file loca
|
|
343
343
|
|
344
344
|
### 2.11. replace_in_file
|
345
345
|
|
346
|
-
Replace content of a file matching condition by calling `replace_in_file` or `gsub_file`
|
346
|
+
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
347
|
|
348
348
|
```ruby
|
349
349
|
TTY::File.replace_in_file 'filename.rb', /matching condition/, 'replacement'
|
@@ -359,7 +359,7 @@ end
|
|
359
359
|
|
360
360
|
### 2.12. append_to_file
|
361
361
|
|
362
|
-
Appends text to a file. You can provide the text as a second argument:
|
362
|
+
Appends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
|
363
363
|
|
364
364
|
```ruby
|
365
365
|
TTY::File.append_to_file('Gemfile', "gem 'tty'")
|
@@ -375,7 +375,7 @@ end
|
|
375
375
|
|
376
376
|
### 2.13. prepend_to_file
|
377
377
|
|
378
|
-
Prepends text to a file. You can provide the text as a second argument:
|
378
|
+
Prepends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
|
379
379
|
|
380
380
|
```ruby
|
381
381
|
TTY::File.prepend_to_file('Gemfile', "gem 'tty'")
|
data/appveyor.yml
CHANGED
@@ -9,7 +9,6 @@ test_script:
|
|
9
9
|
- bundle exec rake ci
|
10
10
|
environment:
|
11
11
|
matrix:
|
12
|
-
- ruby_version: "193"
|
13
12
|
- ruby_version: "200"
|
14
13
|
- ruby_version: "200-x64"
|
15
14
|
- ruby_version: "21"
|
@@ -20,6 +19,8 @@ environment:
|
|
20
19
|
- ruby_version: "23-x64"
|
21
20
|
- ruby_version: "24"
|
22
21
|
- ruby_version: "24-x64"
|
22
|
+
- ruby_version: "25"
|
23
|
+
- ruby_version: "25-x64"
|
23
24
|
matrix:
|
24
25
|
allow_failures:
|
25
|
-
- ruby_version: "
|
26
|
+
- ruby_version: "25"
|
data/lib/tty/file.rb
CHANGED
@@ -500,6 +500,7 @@ module TTY
|
|
500
500
|
#
|
501
501
|
# @api public
|
502
502
|
def inject_into_file(relative_path, *args, **options, &block)
|
503
|
+
check_path(relative_path)
|
503
504
|
replacement = block_given? ? block[] : args.join
|
504
505
|
|
505
506
|
flag, match = if options.key?(:after)
|
@@ -515,17 +516,18 @@ module TTY
|
|
515
516
|
replacement + '\0'
|
516
517
|
end
|
517
518
|
|
518
|
-
replace_in_file(relative_path, /#{match}/, content, options.merge(verbose: false))
|
519
|
-
|
520
519
|
log_status(:inject, relative_path, options.fetch(:verbose, true),
|
521
520
|
options.fetch(:color, :green))
|
521
|
+
replace_in_file(relative_path, /#{match}/, content,
|
522
|
+
options.merge(verbose: false))
|
522
523
|
end
|
523
524
|
module_function :inject_into_file
|
524
525
|
|
525
526
|
alias insert_into_file inject_into_file
|
526
527
|
module_function :insert_into_file
|
527
528
|
|
528
|
-
# Replace content of a file matching string
|
529
|
+
# Replace content of a file matching string, returning false
|
530
|
+
# when no substitutions were performed, true otherwise.
|
529
531
|
#
|
530
532
|
# @options [Hash[String]] options
|
531
533
|
# @option options [Symbol] :force
|
@@ -541,26 +543,30 @@ module TTY
|
|
541
543
|
# match = "gem 'hanami'"
|
542
544
|
# end
|
543
545
|
#
|
546
|
+
# @return [Boolean]
|
547
|
+
# true when replaced content, false otherwise
|
548
|
+
#
|
544
549
|
# @api public
|
545
550
|
def replace_in_file(relative_path, *args, **options, &block)
|
546
551
|
check_path(relative_path)
|
547
|
-
contents
|
552
|
+
contents = IO.read(relative_path)
|
548
553
|
replacement = (block ? block[] : args[1..-1].join).gsub('\0', '')
|
554
|
+
match = Regexp.escape(replacement)
|
555
|
+
status = nil
|
549
556
|
|
550
557
|
log_status(:replace, relative_path, options.fetch(:verbose, true),
|
551
558
|
options.fetch(:color, :green))
|
559
|
+
return false if options[:noop]
|
552
560
|
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
end
|
560
|
-
::File.open(relative_path, 'wb') do |file|
|
561
|
-
file.write(contents)
|
561
|
+
if !(contents =~ /^#{match}(\r?\n)*/m) || options[:force]
|
562
|
+
status = contents.gsub!(*args, &block)
|
563
|
+
if !status.nil?
|
564
|
+
::File.open(relative_path, 'wb') do |file|
|
565
|
+
file.write(contents)
|
566
|
+
end
|
562
567
|
end
|
563
568
|
end
|
569
|
+
!status.nil?
|
564
570
|
end
|
565
571
|
module_function :replace_in_file
|
566
572
|
|
data/lib/tty/file/create_file.rb
CHANGED
@@ -36,7 +36,7 @@ module TTY
|
|
36
36
|
|
37
37
|
Net::HTTP.start(uri.host, uri.port,
|
38
38
|
use_ssl: uri.scheme == 'https') do |http|
|
39
|
-
http.request_get(uri.
|
39
|
+
http.request_get(uri.request_uri) do |response|
|
40
40
|
case response
|
41
41
|
when Net::HTTPSuccess
|
42
42
|
response.read_body do |seg|
|
data/lib/tty/file/version.rb
CHANGED
data/tty-file.gemspec
CHANGED
@@ -22,10 +22,11 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.required_ruby_version = '>= 2.0.0'
|
23
23
|
|
24
24
|
spec.add_dependency 'pastel', '~> 0.7.2'
|
25
|
-
spec.add_dependency 'tty-prompt', '~> 0.
|
25
|
+
spec.add_dependency 'tty-prompt', '~> 0.16.1'
|
26
26
|
spec.add_dependency 'diff-lcs', '~> 1.3.0'
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
29
29
|
spec.add_development_dependency 'rake', '~> 10.0'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
spec.add_development_dependency 'webmock', '~> 3.4.0'
|
31
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pastel
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.16.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.16.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: diff-lcs
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.4.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.4.0
|
97
111
|
description: File manipulation utility methods.
|
98
112
|
email:
|
99
113
|
- ''
|