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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e4c0aad87875fbeee0976b1682cb3b2f30e0c3c
4
- data.tar.gz: 8af35d698b9762048de68a92aa8af73d3e21a85e
3
+ metadata.gz: 5f6078a186a0c9e84d192701867662ad2ce071bd
4
+ data.tar.gz: 43354655bc00fcb28cd45bd232a6c7eeb2ac82e8
5
5
  SHA512:
6
- metadata.gz: 894666704a1dfe9bdfbe320ac027cc6873ef65fa5e81bb13380f2894df2084027895b623fdf69bc34b7fa1d30e4e80281714a2e2eac541e08ff29872cadaebdc
7
- data.tar.gz: 4ff5423c8deb7ce03ed0ff9728d812155e3be8e80aef7d6b27454c6e0cd3294e4a7ec095c30fcdc532bc20fc3ac4d0e55c559b624c34a4d32e5453faac70c5f1
6
+ metadata.gz: 7bc0cf808b4f785de98469185023e17b5cab828f58d7369a16429bd76897e397a41925f746093a1ef207c4184cf635859af47dfa9ca902024756a4b25c016ed9
7
+ data.tar.gz: 2f60628983d6bef4f9e6df80c8eac533a07181b335807d52f6b0fc211a02a86d1455a67be19422b67343b73e799946379a9293de4ba589c5c6102175d19964bc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.2
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
- - Don't check for extension if we are dealing with a directory name
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)](http://badge.fury.io/rb/filename_cleaner)
4
- [![Dependency Status](https://gemnasium.com/agilecreativity/filename_cleaner.png)](https://gemnasium.com/agilecreativity/filename_cleaner)
5
- [![Code Climate](https://codeclimate.com/github/agilecreativity/filename_cleaner.png)](https://codeclimate.com/github/agilecreativity/filename_cleaner)
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
- Quickly rename list of files (with or without extensions) and replace any special characters with
8
- a specific valid string (or separator char).
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 clean
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
- -d, [--dry-run], [--no-dry-run] # Perform a dry run only
43
- # Default: true
47
+ -c, [--commit], [--no-commit] # Commit your changes
44
48
 
45
- Sanitize filename
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 clean --base-dir . --extes java rb --recursive --sep-char '_'
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 clean --base-dir . --extes java rb --recursive --sep-char '_' --no-dry-run
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
- clean_name = FilenameCleaner::sanitize_name_with_extension('some b@d fil$name.txt', '_')
84
- puts clean_name # => 'some_b_d_fil_name.txt'
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
- clean_name = FilenameCleaner::sanitize_name('some b@d fil$name.txt', '_')
88
- puts clean_name # => 'some_b_d_fil_name_txt'
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
@@ -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 'clean', 'Remove special characters in the list of files'
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 :dry_run,
21
+ method_option :commit,
24
22
  type: :boolean,
25
- aliases: '-d',
26
- desc: 'Perform a dry run only',
27
- default: true
28
- def clean
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
- run(opts)
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 clean [OPTIONS]
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
- -d, [--dry-run], [--no-dry-run] # Perform a dry run only
58
- # Default: true
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 run(options = {})
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::sanitize_name_with_extension(filename, options[:sep_char])
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
- if !options[:dry_run]
80
- if new_name != old_name
81
- puts "FYI: old name: #{old_name}"
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 'No changes will be applied as this is a dry-run'
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 filename that have the extension
5
+ # Sanitize the any name with or without any extension
10
6
  #
11
- # @param [String] filename the input filename with extension
12
- # @return [String] the output file with special characters replaced.
13
- def sanitize_name_with_extension(filename, sep_char = '.')
14
- extension = File.extname(filename)
15
- name_only = File.basename(filename, ".*")
16
- name_only = replace_dot!(sanitize_with_dot(name_only), sep_char)
17
- "#{name_only}#{extension}"
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
- # Replace the multipe special characters with a dot
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!(string, replace = nil)
64
+ def replace_dot(string, replace = nil)
47
65
  string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
48
66
  string
49
67
  end
@@ -1,3 +1,3 @@
1
1
  module FilenameCleaner
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,58 +1,66 @@
1
1
  require_relative '../../test_helper'
2
2
  describe FilenameCleaner do
3
- # context '#sanitize_with_dot' do
4
- # it 'works with simple input' do
5
- # FilenameCleaner.sanitize_with_dot('any txt').must_equal 'any.txt'
6
- # FilenameCleaner.sanitize_with_dot('this is a long filename.txt').must_equal 'this.is.a.long.filename.txt'
7
- # end
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
- it 'works with default separator' do
23
- FilenameCleaner.sanitize_name_with_extension('some file.txt').must_equal 'some.file.txt'
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 'works with non-default separator' do
26
- FilenameCleaner.sanitize_name_with_extension('some file.txt', '_').must_equal 'some_file.txt'
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
- end
29
- describe 'file without extension' do
30
- it 'replaces mutilple consecutive chars with one' do
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
- context 'using default separator' do
34
- it 'works with simple input' do
35
- FilenameCleaner.sanitize_name_with_extension('Gemfile').must_equal 'Gemfile'
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 complex input' do
38
- FilenameCleaner.sanitize_name_with_extension('File$without!extension').must_equal 'File.without.extension'
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 'with non-default separator char' do
42
- it 'works with simple input' do
43
- FilenameCleaner.sanitize_name_with_extension('Gemfile', '_').must_equal 'Gemfile'
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
- it 'works with complex input' do
46
- FilenameCleaner.sanitize_name_with_extension('File$without!extension', '-').must_equal 'File-without-extension'
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
- end
49
- context 'end of the filename' do
50
- it 'strips the end of string if not letters or numbers' do
51
- FilenameCleaner.sanitize_name_with_extension('filename .txt').must_equal 'filename.txt'
52
- FilenameCleaner.sanitize_name_with_extension('filename .txt').must_equal 'filename.txt'
53
- FilenameCleaner.sanitize_name_with_extension('filename !.txt').must_equal 'filename.txt'
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
- end
64
+ end
57
65
  end
58
66
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'minitest'
1
2
  require 'minitest/autorun'
2
3
  require 'minitest/pride'
3
4
  require 'minitest-spec-context'
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.2.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-13 00:00:00.000000000 Z
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