tty-file 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +302 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tty-file.rb +1 -0
- data/lib/tty/file.rb +497 -0
- data/lib/tty/file/create_file.rb +104 -0
- data/lib/tty/file/differ.rb +78 -0
- data/lib/tty/file/download_file.rb +56 -0
- data/lib/tty/file/version.rb +7 -0
- data/tasks/console.rake +10 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +29 -0
- data/tty-file.gemspec +29 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ad05816965df5179021ae1e70048eac3ae58db78
|
4
|
+
data.tar.gz: 35e7da73870c9bbb6d01a35584bb0537c110903e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3dbfc53a540c8f43c130274c02fcf3c2a9cfb0d50cab7b7d0a1e1ba2ab8c92073a61b0c4bf4c0108d005abfc353d65e10190169c689b772c54becfee96bc12e6
|
7
|
+
data.tar.gz: 92a918fd84a1e846d04c374644781f940f28ea0705df7f9ab0da5df7e87f9d0e39c2bd08621b021eed86da4cb7b2e4703ba5430b64860c571c5fe856ba7308cc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
sudo: false
|
4
|
+
cache: bundler
|
5
|
+
script: "bundle exec rake ci"
|
6
|
+
rvm:
|
7
|
+
- 2.0.0
|
8
|
+
- 2.1.10
|
9
|
+
- 2.2.5
|
10
|
+
- 2.3.1
|
11
|
+
- ruby-head
|
12
|
+
- rbx
|
13
|
+
matrix:
|
14
|
+
include:
|
15
|
+
- rvm: jruby
|
16
|
+
env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false -Xcompat.version=2.0'
|
17
|
+
- rvm: jruby-9.1.2.0
|
18
|
+
env: JRUBY_OPTS='--server -Xcompile.invokedynamic=false'
|
19
|
+
allow_failures:
|
20
|
+
- rvm: ruby-head
|
21
|
+
- rvm: jruby
|
22
|
+
- rvm: rbx
|
23
|
+
fast_finish: true
|
24
|
+
branches:
|
25
|
+
only: master
|
26
|
+
notifications:
|
27
|
+
email: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at [email]. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Piotr Murach
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,302 @@
|
|
1
|
+
# TTY::File [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/tty-file.svg)][gem]
|
3
|
+
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-file.svg?branch=master)][travis]
|
4
|
+
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-file/badges/gpa.svg)][codeclimate]
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-file/badge.svg)][coverage]
|
6
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-file.svg?branch=master)][inchpages]
|
7
|
+
|
8
|
+
[gitter]: https://gitter.im/piotrmurach/tty
|
9
|
+
[gem]: http://badge.fury.io/rb/tty-file
|
10
|
+
[travis]: http://travis-ci.org/piotrmurach/tty-file
|
11
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-file
|
12
|
+
[coverage]: https://coveralls.io/github/piotrmurach/tty-file
|
13
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-file
|
14
|
+
|
15
|
+
> File manipulation utility methods
|
16
|
+
|
17
|
+
## Motivation
|
18
|
+
|
19
|
+
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.
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'tty-file'
|
27
|
+
```
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install tty-file
|
36
|
+
|
37
|
+
## Contents
|
38
|
+
|
39
|
+
* [1. Usage](#1-usage)
|
40
|
+
* [2. Interface](#2-interface)
|
41
|
+
* [2.1. binary?](#21-binary)
|
42
|
+
* [2.2. chmod](#22-chmod)
|
43
|
+
* [2.3. copy_file](#23-copy_file)
|
44
|
+
* [2.4. create_file](#24-create_file)
|
45
|
+
* [2.5. diff](#25-diff)
|
46
|
+
* [2.6. download_file](#26-download_file)
|
47
|
+
* [2.7. inject_into_file](#27-inject_into_file)
|
48
|
+
* [2.8. replace_in_file](#28-replace_in_file)
|
49
|
+
* [2.9. append_to_file](#29-apend_to_file)
|
50
|
+
* [2.10. prepend_to_file](#30-prepend_to_file)
|
51
|
+
* [2.11. remove_file](#211-remove_file)
|
52
|
+
|
53
|
+
## 1. Usage
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
TTY::File.replace_in_file('Gemfile', /gem 'rails'/, "gem 'hanami'")
|
57
|
+
```
|
58
|
+
|
59
|
+
## 2. Interface
|
60
|
+
|
61
|
+
The following are methods available for creating and manipulating files.
|
62
|
+
|
63
|
+
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`.
|
64
|
+
|
65
|
+
### 2.1. binary?
|
66
|
+
|
67
|
+
To check whether a file is a binary file, i.e. image, executable etc. do:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
TTY::File.binary?('image.png') # => true
|
71
|
+
```
|
72
|
+
|
73
|
+
### 2.2. chmod
|
74
|
+
|
75
|
+
To change file modes use `chmod` like so:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
TTY::File.chmod('filename.rb', 0777)
|
79
|
+
```
|
80
|
+
|
81
|
+
There are number of constants available to represent common mode bits such as `TTY::File::U_R`, `TTY::File::O_X` and can be used as follows:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
TTY::File.chmod('filename.rb', TTY::File::U_R | TTY::File::O_X)
|
85
|
+
```
|
86
|
+
|
87
|
+
Apart from traditional octal number definition for file permissions, you can use more convenient permission notation accepted by Unix `chmod` command:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
TTY::File.chmod('filename.rb', 'u=wrx,g+x')
|
91
|
+
```
|
92
|
+
|
93
|
+
The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits. The `a` symbol is equivalent to `ugo`.
|
94
|
+
|
95
|
+
### 2.3. copy_file
|
96
|
+
|
97
|
+
Copies a file content from relative source to relative destination.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
TTY::File.copy_file 'Gemfile', 'Gemfile.bak'
|
101
|
+
```
|
102
|
+
|
103
|
+
If you provide a block then the file content is yielded:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
TTY::File.copy_file('Gemfile', 'app/Gemfile') do |content|
|
107
|
+
"https://rubygems.org\n" + content
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
If the source file is an `ERB` template then you can provide `:context` in which the file gets evaluted or if `TTY::File` gets included as a module then approprite object context will be used by default. To use `:context` do:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
variables = OpenStruct.new
|
115
|
+
variables[:foo] = 'bar'
|
116
|
+
|
117
|
+
TTY::File.copy_file('templates/application.html.erb', context: variables)
|
118
|
+
```
|
119
|
+
|
120
|
+
You can also specifie template name surrounding any dynamic variables with `%` to be evaluted:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
variables = OpenStruct.new
|
124
|
+
variables[:file_name] = 'foo'
|
125
|
+
|
126
|
+
TTY::File.copy_file('templates/%file_name%.rb', context: variables)
|
127
|
+
# => Creates templates/foo.rb
|
128
|
+
```
|
129
|
+
|
130
|
+
If the destination is a directory, then copies source inside that directory.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
TTY::File.copy_file 'docs/README.md', 'app'
|
134
|
+
```
|
135
|
+
|
136
|
+
If the destination file already exists, a prompt menu will be displayed to enquire about action:
|
137
|
+
|
138
|
+
If you wish to preserve original owner, group, permission and modified time use `:preserve` option:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
TTY::File.copy_file 'docs/README.md', 'app', preserve: true
|
142
|
+
```
|
143
|
+
|
144
|
+
### 2.4. create_file
|
145
|
+
|
146
|
+
To create a file at a given destination with the given content use `create_file`:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
TTY::File.create_file 'docs/README.md', '## Title header'
|
150
|
+
```
|
151
|
+
|
152
|
+
On collision with already existing file, a menu is displayed:
|
153
|
+
|
154
|
+
You can force to always overwrite file with `:force` option or always skip by providing `:skip`.
|
155
|
+
|
156
|
+
### 2.5. diff
|
157
|
+
|
158
|
+
To compare files line by line in a system independent way use `diff`:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
TTY::File.diff('file_a', 'file_b')
|
162
|
+
# =>
|
163
|
+
# @@ -1,4 +1,4 @@
|
164
|
+
# aaa
|
165
|
+
# -bbb
|
166
|
+
# +xxx
|
167
|
+
# ccc
|
168
|
+
```
|
169
|
+
|
170
|
+
You can also pass additional arguments such as `:format`, `:context_lines` and `:threshold`.
|
171
|
+
|
172
|
+
Accepted formats are `:old`, `:unified`, `:context`, `:ed`, `:reverse_ed`, by default the `:unified` format is used.
|
173
|
+
|
174
|
+
The `:context_lines` specifies how many extra lines around the differing lines to include in the output. By default its 3 lines.
|
175
|
+
|
176
|
+
The `:threshold` sets maximum file size in bytes, by default files larger than `10Mb` are not processed.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
TTY::File.diff('file_a', 'file_b', format: :old)
|
180
|
+
# =>
|
181
|
+
# 1,4c1,4
|
182
|
+
# < aaa
|
183
|
+
# < bbb
|
184
|
+
# < ccc
|
185
|
+
# ---
|
186
|
+
# > aaa
|
187
|
+
# > xxx
|
188
|
+
# > ccc
|
189
|
+
```
|
190
|
+
|
191
|
+
Equally, you can perform a comparison between a file content and a string content like so:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
TTY::File.diff('/path/to/file', 'some long text')
|
195
|
+
```
|
196
|
+
|
197
|
+
### 2.6. download_file
|
198
|
+
|
199
|
+
To download a content from a given address and to save at a given relative location do:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md")
|
203
|
+
```
|
204
|
+
|
205
|
+
If you pass a block then the content will be yielded to allow modification:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md") do |content|
|
209
|
+
content.gsub("\n", " ")
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
By default `download_file` will follow maximum 3 redirects. This can be changed by passing `:limit` option:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md", limit: 5)
|
217
|
+
# => raises TTY::File::DownloadError
|
218
|
+
```
|
219
|
+
|
220
|
+
### 2.7. inject_into_file
|
221
|
+
|
222
|
+
Inject content into a file at a given location
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
TTY::File.inject_into_file 'filename.rb', after: "Code below this line\n" do
|
226
|
+
"text to add"
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
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.
|
231
|
+
|
232
|
+
### 2.8. replace_in_file
|
233
|
+
|
234
|
+
Replace content of a file matching condition.
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
TTY::File.replace_in_file 'filename.rb', /matching condition/, 'replacement'
|
238
|
+
```
|
239
|
+
|
240
|
+
### 2.9. append_to_file
|
241
|
+
|
242
|
+
Appends text to a file. You can provide the text as a second argument:
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
TTY::File.append_to_file('Gemfile', "gem 'tty'")
|
246
|
+
```
|
247
|
+
|
248
|
+
or inside a block:
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
TTY::File.append_to_file('Gemfile') do
|
252
|
+
"gem 'tty'"
|
253
|
+
end
|
254
|
+
```
|
255
|
+
|
256
|
+
### 2.10. prepend_to_file
|
257
|
+
|
258
|
+
Prepends text to a file. You can provide the text as a second argument:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
TTY::File.prepend_to_file('Gemfile', "gem 'tty'")
|
262
|
+
```
|
263
|
+
|
264
|
+
or inside a block:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
TTY::File.prepend_to_file('Gemfile') do
|
268
|
+
"gem 'tty'"
|
269
|
+
end
|
270
|
+
```
|
271
|
+
|
272
|
+
### 2.11. remove_file
|
273
|
+
|
274
|
+
To remove a file do:
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
TTY::File.remove_file 'doc/README.md'
|
278
|
+
```
|
279
|
+
|
280
|
+
You can also pass in `:force` to remove file ignoring any errors:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
TTY::File.remove_file 'doc/README.md', force: true
|
284
|
+
```
|
285
|
+
|
286
|
+
## Development
|
287
|
+
|
288
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
289
|
+
|
290
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
291
|
+
|
292
|
+
## Contributing
|
293
|
+
|
294
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-file. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
295
|
+
|
296
|
+
## License
|
297
|
+
|
298
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
299
|
+
|
300
|
+
## Copyright
|
301
|
+
|
302
|
+
Copyright (c) 2016 Piotr Murach. See LICENSE for further details.
|