filename_cleaner 0.2.0 → 0.3.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 +5 -0
- data/CHANGELOGS.md +8 -2
- data/README.md +20 -17
- data/lib/filename_cleaner/cli.rb +23 -26
- data/lib/filename_cleaner/filename_cleaner.rb +32 -14
- data/lib/filename_cleaner/version.rb +1 -1
- data/test/lib/filename_cleaner/test_filename_cleaner.rb +51 -43
- data/test/test_helper.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f6078a186a0c9e84d192701867662ad2ce071bd
|
4
|
+
data.tar.gz: 43354655bc00fcb28cd45bd232a6c7eeb2ac82e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bc0cf808b4f785de98469185023e17b5cab828f58d7369a16429bd76897e397a41925f746093a1ef207c4184cf635859af47dfa9ca902024756a4b25c016ed9
|
7
|
+
data.tar.gz: 2f60628983d6bef4f9e6df80c8eac533a07181b335807d52f6b0fc211a02a86d1455a67be19422b67343b73e799946379a9293de4ba589c5c6102175d19964bc
|
data/.travis.yml
ADDED
data/CHANGELOGS.md
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
### Changelogs
|
2
2
|
|
3
|
+
#### 0.3.0
|
4
|
+
|
5
|
+
- Cleanup and simplify the apis
|
6
|
+
- Rename `--dry-run` to `--commit`
|
7
|
+
- Remove the `--non-exts` options to only support file with extension
|
8
|
+
|
3
9
|
#### 0.2.0
|
4
10
|
|
5
11
|
- Cleanup the APIs
|
6
12
|
* rename `sanitize_filename` to `sanitize_name_with_extension`
|
7
|
-
* add `sanitize_name`
|
8
|
-
-
|
13
|
+
* add `sanitize_name` for name that does not have any extension
|
14
|
+
- Make non-public apis private
|
9
15
|
- Add core_ext/object/blank.rb
|
10
16
|
|
11
17
|
#### 0.1.1
|
data/README.md
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
## filename_cleaner
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/filename_cleaner.svg)]
|
4
|
-
[![Dependency Status](https://gemnasium.com/agilecreativity/filename_cleaner.png)]
|
5
|
-
[![Code Climate](https://codeclimate.com/github/agilecreativity/filename_cleaner.png)]
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/filename_cleaner.svg)][gem]
|
4
|
+
[![Dependency Status](https://gemnasium.com/agilecreativity/filename_cleaner.png)][gemnasium]
|
5
|
+
[![Code Climate](https://codeclimate.com/github/agilecreativity/filename_cleaner.png)][codeclimate]
|
6
|
+
[![Build Status](https://secure.travis-ci.org/agilecreativity/filename_cleaner.png)][travis-ci]
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
[gem]: http://badge.fury.io/rb/filename_cleaner
|
9
|
+
[gemnasium]: https://gemnasium.com/agilecreativity/filename_cleaner
|
10
|
+
[codeclimate]: https://codeclimate.com/github/agilecreativity/filename_cleaner
|
11
|
+
[travis-ci]: http://travis-ci.org/agilecreativity/filename_cleaner
|
12
|
+
|
13
|
+
Quickly rename list of files with extension and replace any special characters with
|
14
|
+
with any given string.
|
9
15
|
|
10
16
|
Currently any string that are not one of letters (a..z, A..Z),
|
11
17
|
numbers (0..9), _ (underscore), - (dash), and ' ' spaces string
|
@@ -23,13 +29,12 @@ Just type `filename_cleaner` without any options to see the list of help
|
|
23
29
|
|
24
30
|
```
|
25
31
|
Usage:
|
26
|
-
filename_cleaner
|
32
|
+
filename_cleaner rename
|
27
33
|
|
28
34
|
Options:
|
29
35
|
-b, [--base-dir=BASE_DIR] # Base directory
|
30
36
|
# Default: . (current directory)
|
31
37
|
-e, [--exts=one two three] # List of extensions to search for
|
32
|
-
-f, [--non-exts=one two three] # List of files without extension to search for
|
33
38
|
-n, [--inc-words=one two three] # List of words to be included in the result if any
|
34
39
|
-x, [--exc-words=one two three] # List of words to be excluded from the result if any
|
35
40
|
-i, [--ignore-case], [--no-ignore-case] # Match case insensitively
|
@@ -39,24 +44,23 @@ Options:
|
|
39
44
|
-v, [--version], [--no-version] # Display version information
|
40
45
|
-s, [--sep-char=SEP_CHAR] # Separator char to use
|
41
46
|
# Default: .
|
42
|
-
-
|
43
|
-
# Default: true
|
47
|
+
-c, [--commit], [--no-commit] # Commit your changes
|
44
48
|
|
45
|
-
Sanitize
|
49
|
+
Sanitize and rename file with special characters
|
46
50
|
```
|
47
51
|
|
48
52
|
To perform the dry-run without make any changes to the file system:
|
49
53
|
|
50
54
|
```
|
51
55
|
cd ~/projects/files
|
52
|
-
filename_cleaner
|
56
|
+
filename_cleaner rename --base-dir . --exts java rb --recursive --sep-char _
|
53
57
|
```
|
54
58
|
|
55
59
|
To make your change permanent:
|
56
60
|
|
57
61
|
```
|
58
62
|
cd ~/projects/files
|
59
|
-
filename_cleaner
|
63
|
+
filename_cleaner rename --base-dir . --exts java rb --recursive --sep-char _ --commit
|
60
64
|
```
|
61
65
|
|
62
66
|
#### Us as library in your project
|
@@ -80,12 +84,12 @@ Example Usage:
|
|
80
84
|
```ruby
|
81
85
|
require 'filename_cleaner'
|
82
86
|
# work with the file that have extension
|
83
|
-
|
84
|
-
puts
|
87
|
+
new_name = FilenameCleaner.sanitize_name_with_extension('some b@d fil$name.txt', '_')
|
88
|
+
puts new_name # => 'some_b_d_fil_name.txt'
|
85
89
|
|
86
90
|
# or to work with the file without extension
|
87
|
-
|
88
|
-
puts
|
91
|
+
new_name = FilenameCleaner.sanitize_name('some b@d fil$name.txt', '_')
|
92
|
+
puts new_name # => 'some_b_d_fil_name_txt'
|
89
93
|
```
|
90
94
|
|
91
95
|
### Contributing
|
@@ -99,5 +103,4 @@ GitHub pull requests are even better!.
|
|
99
103
|
4. Push to the branch (`git push origin my-new-feature`)
|
100
104
|
5. Create new Pull Request
|
101
105
|
|
102
|
-
[agile_utils]: https://rubygems.org/gems/agile_utils
|
103
106
|
[rubocop]: https://github.com/bbatsov/rubocop
|
data/lib/filename_cleaner/cli.rb
CHANGED
@@ -2,13 +2,11 @@ require 'thor'
|
|
2
2
|
require 'agile_utils'
|
3
3
|
require 'fileutils'
|
4
4
|
require_relative '../filename_cleaner'
|
5
|
-
|
6
5
|
module FilenameCleaner
|
7
6
|
class CLI < Thor
|
8
|
-
desc '
|
7
|
+
desc 'rename', 'Sanitize and rename file with special characters'
|
9
8
|
method_option *AgileUtils::Options::BASE_DIR
|
10
9
|
method_option *AgileUtils::Options::EXTS
|
11
|
-
method_option *AgileUtils::Options::NON_EXTS
|
12
10
|
method_option *AgileUtils::Options::INC_WORDS
|
13
11
|
method_option *AgileUtils::Options::EXC_WORDS
|
14
12
|
method_option *AgileUtils::Options::IGNORE_CASE
|
@@ -20,31 +18,30 @@ module FilenameCleaner
|
|
20
18
|
desc: 'Separator char to use',
|
21
19
|
default: '.'
|
22
20
|
|
23
|
-
method_option :
|
21
|
+
method_option :commit,
|
24
22
|
type: :boolean,
|
25
|
-
aliases: '-
|
26
|
-
desc: '
|
27
|
-
default:
|
28
|
-
def
|
23
|
+
aliases: '-c',
|
24
|
+
desc: 'Commit your changes',
|
25
|
+
default: false
|
26
|
+
def rename
|
29
27
|
opts = options.symbolize_keys
|
30
28
|
if opts[:version]
|
31
29
|
puts "You are using Filename Cleaner version #{FilenameCleaner::VERSION}"
|
32
30
|
exit
|
33
31
|
end
|
34
|
-
|
32
|
+
sanitize_and_rename(opts)
|
35
33
|
end
|
36
34
|
|
37
35
|
desc 'usage', 'Display help screen'
|
38
36
|
def usage
|
39
37
|
puts <<-EOS
|
40
38
|
Usage:
|
41
|
-
filename_cleaner
|
39
|
+
filename_cleaner rename
|
42
40
|
|
43
41
|
Options:
|
44
42
|
-b, [--base-dir=BASE_DIR] # Base directory
|
45
|
-
# Default: . (current directory)
|
43
|
+
# Default: . (current directory name)
|
46
44
|
-e, [--exts=one two three] # List of extensions to search for
|
47
|
-
-f, [--non-exts=one two three] # List of files without extension to search for
|
48
45
|
-n, [--inc-words=one two three] # List of words to be included in the result if any
|
49
46
|
-x, [--exc-words=one two three] # List of words to be excluded from the result if any
|
50
47
|
-i, [--ignore-case], [--no-ignore-case] # Match case insensitively
|
@@ -54,8 +51,9 @@ Options:
|
|
54
51
|
-v, [--version], [--no-version] # Display version information
|
55
52
|
-s, [--sep-char=SEP_CHAR] # Separator char to use
|
56
53
|
# Default: .
|
57
|
-
-
|
58
|
-
|
54
|
+
-c, [--commit], [--no-commit] # Commit your changes
|
55
|
+
|
56
|
+
Sanitize and rename file with special characters
|
59
57
|
EOS
|
60
58
|
end
|
61
59
|
|
@@ -63,8 +61,8 @@ Options:
|
|
63
61
|
|
64
62
|
private
|
65
63
|
|
66
|
-
def
|
67
|
-
files = CodeLister.files(options
|
64
|
+
def sanitize_and_rename(options = {})
|
65
|
+
files = CodeLister.files(options)
|
68
66
|
if files.empty?
|
69
67
|
puts "No match found for your options :#{options}"
|
70
68
|
else
|
@@ -72,22 +70,21 @@ Options:
|
|
72
70
|
puts "FYI: process : #{index + 1} of #{files.size}"
|
73
71
|
dirname = File.dirname(File.expand_path(file))
|
74
72
|
filename = File.basename(file)
|
75
|
-
sanitized_name = FilenameCleaner::
|
73
|
+
sanitized_name = FilenameCleaner::sanitize(filename, options[:sep_char], true)
|
76
74
|
old_name = File.expand_path(file)
|
77
75
|
new_name = File.expand_path([dirname, sanitized_name].join(File::SEPARATOR))
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
puts "FYI: new name: #{new_name}"
|
76
|
+
if new_name != old_name
|
77
|
+
puts "FYI: old name: #{old_name}"
|
78
|
+
puts "FYI: new name: #{new_name}"
|
79
|
+
if options[:commit]
|
83
80
|
FileUtils.mv old_name, new_name
|
84
|
-
else
|
85
|
-
puts "FYI: same file #{old_name}"
|
86
81
|
end
|
87
82
|
else
|
88
|
-
puts
|
83
|
+
puts "FYI: same file #{old_name}"
|
89
84
|
end
|
90
|
-
|
85
|
+
end
|
86
|
+
unless options[:commit]
|
87
|
+
puts 'No changes will take place as this is a dry run, to commit your change, please use --commit option'
|
91
88
|
end
|
92
89
|
end
|
93
90
|
end
|
@@ -1,25 +1,43 @@
|
|
1
1
|
module FilenameCleaner
|
2
2
|
DOT = '.'
|
3
3
|
class << self
|
4
|
-
# Sanitize the name without any extension
|
5
|
-
def sanitize_name(name, sep_char = '.')
|
6
|
-
replace_dot!(sanitize_with_dot(name), sep_char)
|
7
|
-
end
|
8
4
|
|
9
|
-
# Sanitize
|
5
|
+
# Sanitize the any name with or without any extension
|
10
6
|
#
|
11
|
-
# @param [String]
|
12
|
-
# @
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
# @param [String] name the input string
|
8
|
+
# @param [String] sep_char the separator char to be used
|
9
|
+
# @return [String] output string with special chars replaced withe specified string
|
10
|
+
def sanitize(name, sep_char = '.', have_extension = false)
|
11
|
+
if have_extension
|
12
|
+
sanitize_name_with_extension(name, sep_char)
|
13
|
+
else
|
14
|
+
sanitize_name_without_extension(name, sep_char)
|
15
|
+
end
|
18
16
|
end
|
19
17
|
|
20
18
|
private
|
21
19
|
|
22
|
-
#
|
20
|
+
# Sanitize the any name with or without any extension
|
21
|
+
#
|
22
|
+
# @param [String] name the input string
|
23
|
+
# @param [String] sep_char the separator char to be used
|
24
|
+
# @return [String] output string with special chars replaced withe specified string
|
25
|
+
def sanitize_name_without_extension(name, sep_char = '.')
|
26
|
+
replace_dot(sanitize_with_dot(name), sep_char)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Sanitize filename that works with file with extension
|
30
|
+
#
|
31
|
+
# @param [String] name the input filename with extension
|
32
|
+
# @return [String] the output file with special characters replaced
|
33
|
+
def sanitize_name_with_extension(name, sep_char = '.')
|
34
|
+
extension = File.extname(name)
|
35
|
+
name_only = File.basename(name, ".*")
|
36
|
+
name_only = replace_dot(sanitize_with_dot(name_only), sep_char)
|
37
|
+
"#{name_only}#{extension}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Replace the multiple special characters with a dot string
|
23
41
|
#
|
24
42
|
# @param [String] name input file
|
25
43
|
# @return [String] the new name with special characters replaced or removed.
|
@@ -43,7 +61,7 @@ module FilenameCleaner
|
|
43
61
|
end
|
44
62
|
|
45
63
|
# replace 'dot' string with a given string if specified
|
46
|
-
def replace_dot
|
64
|
+
def replace_dot(string, replace = nil)
|
47
65
|
string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
|
48
66
|
string
|
49
67
|
end
|
@@ -1,58 +1,66 @@
|
|
1
1
|
require_relative '../../test_helper'
|
2
2
|
describe FilenameCleaner do
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# it 'works with text with extension' do
|
9
|
-
# FilenameCleaner.sanitize_with_dot('filename.txt').must_equal 'filename.txt'
|
10
|
-
# end
|
11
|
-
# it 'strips the end of string if not letters or numbers' do
|
12
|
-
# FilenameCleaner.sanitize_with_dot('filename .txt').must_equal 'filename.txt'
|
13
|
-
# FilenameCleaner.sanitize_with_dot('filename .txt').must_equal 'filename.txt'
|
14
|
-
# FilenameCleaner.sanitize_with_dot('filename !.txt').must_equal 'filename.txt'
|
15
|
-
# end
|
16
|
-
# end
|
17
|
-
context '#sanitize_name_with_extension' do
|
18
|
-
describe 'file with extension' do
|
19
|
-
it 'replaces mutilple consecutive chars with one' do
|
20
|
-
FilenameCleaner.sanitize_name_with_extension('some!!!$file$:%.txt').must_equal 'some.file.txt'
|
3
|
+
context '#sanitize' do
|
4
|
+
describe 'without extension' do
|
5
|
+
it 'works with simple input' do
|
6
|
+
FilenameCleaner.sanitize('any txt').must_equal 'any.txt'
|
7
|
+
FilenameCleaner.sanitize('any txt', '-').must_equal 'any-txt'
|
21
8
|
end
|
22
|
-
|
23
|
-
|
9
|
+
|
10
|
+
it 'works with text containing the dot' do
|
11
|
+
FilenameCleaner.sanitize('text with a dot.txt').must_equal 'text.with.a.dot.txt'
|
12
|
+
FilenameCleaner.sanitize('text with a dot.txt', '_').must_equal 'text_with_a_dot_txt'
|
24
13
|
end
|
25
|
-
it '
|
26
|
-
FilenameCleaner.
|
14
|
+
it 'replaces many consecutive special characters with one' do
|
15
|
+
FilenameCleaner.sanitize('text with!@**! multiple chars').must_equal 'text.with.multiple.chars'
|
16
|
+
FilenameCleaner.sanitize('text with!@**! multiple chars', '_').must_equal 'text_with_multiple_chars'
|
27
17
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
FilenameCleaner.sanitize_name_with_extension('some!!!$file$:%.').must_equal 'some.file'
|
18
|
+
it 'strips all special characters at the end' do
|
19
|
+
FilenameCleaner.sanitize('filename .txt_').must_equal 'filename.txt'
|
20
|
+
FilenameCleaner.sanitize('filename .txt_!$#$#', '_').must_equal 'filename_txt'
|
32
21
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
end
|
23
|
+
describe 'with extension' do
|
24
|
+
context 'file with extension' do
|
25
|
+
it 'replaces mutilple consecutive chars with one' do
|
26
|
+
FilenameCleaner.sanitize('some!!!$file$:%.txt', '.', true).must_equal 'some.file.txt'
|
27
|
+
end
|
28
|
+
it 'works with default separator' do
|
29
|
+
FilenameCleaner.sanitize('some file.txt', '.', true).must_equal 'some.file.txt'
|
36
30
|
end
|
37
|
-
it 'works with
|
38
|
-
FilenameCleaner.
|
31
|
+
it 'works with non-default separator' do
|
32
|
+
FilenameCleaner.sanitize('some file.txt', '_', true).must_equal 'some_file.txt'
|
39
33
|
end
|
40
34
|
end
|
41
|
-
context
|
42
|
-
it '
|
43
|
-
FilenameCleaner.
|
35
|
+
context'file without extension' do
|
36
|
+
it 'replaces mutilple consecutive chars with one' do
|
37
|
+
FilenameCleaner.sanitize('some!!!$file$:%', '.', true).must_equal 'some.file'
|
44
38
|
end
|
45
|
-
|
46
|
-
|
39
|
+
context 'using default separator' do
|
40
|
+
it 'works with simple input' do
|
41
|
+
FilenameCleaner.sanitize('Gemfile', '.', true).must_equal 'Gemfile'
|
42
|
+
end
|
43
|
+
it 'works with complex input' do
|
44
|
+
FilenameCleaner.sanitize('File$without!extension', '.', true).must_equal 'File.without.extension'
|
45
|
+
end
|
47
46
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
context 'with non-default separator char' do
|
48
|
+
it 'works with simple input' do
|
49
|
+
FilenameCleaner.sanitize('Gemfile', '_', true).must_equal 'Gemfile'
|
50
|
+
end
|
51
|
+
it 'works with complex input' do
|
52
|
+
FilenameCleaner.sanitize('File$without!extension', '-', true).must_equal 'File-without-extension'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
context 'special characters at boundary' do
|
56
|
+
it 'does not strip special characters that from extension' do
|
57
|
+
FilenameCleaner.sanitize('filename!!#@.txt!!', '.', true).must_equal 'filename.txt!!'
|
58
|
+
end
|
59
|
+
it 'strips the special characters that come before the extension' do
|
60
|
+
FilenameCleaner.sanitize('filename !#@!.txt', '_', true).must_equal 'filename.txt'
|
61
|
+
end
|
54
62
|
end
|
55
63
|
end
|
56
|
-
|
64
|
+
end
|
57
65
|
end
|
58
66
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filename_cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Burin Choomnuan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -204,6 +204,7 @@ files:
|
|
204
204
|
- ".gitignore"
|
205
205
|
- ".rubocop.yml"
|
206
206
|
- ".ruby-version"
|
207
|
+
- ".travis.yml"
|
207
208
|
- ".yardopts"
|
208
209
|
- CHANGELOGS.md
|
209
210
|
- Gemfile
|