file_renamer 0.1.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6fe61d9eb0216d0be9aea5e4a1fc248faddac51a955334fddb57a45ebd24c8c
4
- data.tar.gz: e4d291324489972119092196e1821e0c05e1fdf9302edbef8ce5e495728e705c
3
+ metadata.gz: abfc8171a09153f98a5e713ef7a70a91882c9db60434101921714cf4dc1c467b
4
+ data.tar.gz: 4c04c4eec580a8460db6d9f2197c8c25cab8443cebd75e890229baef4a17c4e1
5
5
  SHA512:
6
- metadata.gz: 8bf4eefa65ded92112fc40b1c3a3fda55e525bcf11464958ecc66080086121b9525151a0311986bcc8294236cd9a74a5009712a11058bfdb538ec611452bcc92
7
- data.tar.gz: ed435b66f5eed781d750ee9c62f6f818a9e923288ba740758b806b22f6f5acb345a1a28624effe7e72a042a74a3c5c74e6c86a5d961400d478d5f8799a5cfab3
6
+ metadata.gz: c67b670abd019667dcdb11530240f76b3429005018049b6295d7f683529e248827edf4820495b7d11e5d1f42af44a4308fd96388188f4e737aba47d9f2d748cd
7
+ data.tar.gz: bff076f6cdaf1c2896e2a6e6ae8f6f95330679511973a916ac33de1cbd85de8ef7b3e4dcf708019b9c53aea5011654b63e35fbaf76b960da768bfd5af71f0401
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in file_renamer.gemspec
6
+ gem "rspec", "~> 3.10"
7
+ gem "rake", "~> 13.0"
8
+ gem "bundler", "~> 2.1"
9
+ gem "thor", "~> 1.0"
10
+ gem 'simplecov', require: false, group: :test
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.4.4)
5
+ docile (1.3.2)
6
+ rake (13.0.1)
7
+ rspec (3.10.0)
8
+ rspec-core (~> 3.10.0)
9
+ rspec-expectations (~> 3.10.0)
10
+ rspec-mocks (~> 3.10.0)
11
+ rspec-core (3.10.0)
12
+ rspec-support (~> 3.10.0)
13
+ rspec-expectations (3.10.0)
14
+ diff-lcs (>= 1.2.0, < 2.0)
15
+ rspec-support (~> 3.10.0)
16
+ rspec-mocks (3.10.0)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.10.0)
19
+ rspec-support (3.10.0)
20
+ simplecov (0.19.1)
21
+ docile (~> 1.1)
22
+ simplecov-html (~> 0.11)
23
+ simplecov-html (0.12.3)
24
+ thor (1.0.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 2.1)
31
+ rake (~> 13.0)
32
+ rspec (~> 3.10)
33
+ simplecov
34
+ thor (~> 1.0)
35
+
36
+ BUNDLED WITH
37
+ 2.1.4
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
- ![](https://ruby-gem-downloads-badge.herokuapp.com/file_renamer?&style=plastic)
2
- ![](http://badges.github.io/stability-badges/dist/experimental.svg)
1
+ ![Downloads](https://ruby-gem-downloads-badge.herokuapp.com/file_renamer?&style=plastic)
2
+ [![Build Status](https://img.shields.io/travis/tee0zed/file_renamer)](https://travis-ci.org/github/tee0zed/file_renamer)
3
+ [![Code Coverage](https://img.shields.io/badge/SimpleCov-%2099.46%25-brightgreen)](https://img.shields.io/badge/SimpleCov-%2099.46%25-green)
4
+ [![Stability]( https://img.shields.io/badge/stability%20-stable-brightgreen)]( https://img.shields.io/badge/stability%20-stable-brightgreen)
5
+
3
6
 
4
7
  # FileRenamer
5
8
 
@@ -27,7 +30,7 @@ Or install it yourself as:
27
30
 
28
31
  ### From terminal
29
32
 
30
- filerenamer [options]
33
+ file_renamer [options]
31
34
  -d --dir (optional)
32
35
  -p --prefix (optional)
33
36
  -e --extension (optional)
@@ -62,12 +65,19 @@ Or install it yourself as:
62
65
 
63
66
  ### In your project
64
67
 
65
- FileRenamer::Renamer.rename!(params)
68
+ FileRenamer::PathProcessor.run!(params)
69
+
66
70
  where
67
- - params[:dir] - for execution directory (required)
68
- - params[:prefix] - for filename's prefixes
69
- - params[:ext] - for filename's extensions
70
- - params[:name] - for filename's new names (required)
71
+ - params[:dir] - for execution directory (required)
72
+ - params[:prefix] - for filename's prefixes
73
+ - params[:ext] - for filename's extensions
74
+ - params[:name] - for filename's new names (required)
75
+
76
+ or
77
+ - corrected_params[:dir] - for execution directory (required)
78
+ - corrected_params[:prefix] - for filename's prefixes
79
+ - corrected_params[:ext] - for filename's extensions
80
+ - corrected_params[:name] - for filename's new names (required)
71
81
 
72
82
  ## Development
73
83
 
@@ -94,4 +104,4 @@ in project folder to run all specs
94
104
 
95
105
  MIT License.
96
106
 
97
- 2020 Tee Zed
107
+ 2020 Tee Zed
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  desc 'Spec all'
3
4
  task :spec_all do
4
- system("rspec spec/*")
5
- end
5
+ system('rspec spec/*')
6
+ end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require_relative '../lib/path_processor'
6
+ require_relative '../lib/name_alterer'
7
+ require_relative '../lib/params_corrector'
8
+
9
+ begin
10
+ params = {}
11
+ OptionParser.new do |opts|
12
+ opts.on('-n STRING', '--name', 'new filename (don\'t use spaces!)', String)
13
+ opts.on('-p STRING', '--prefix', 'prefix of the filename', String)
14
+ opts.on('-e STRING', '--ext', 'extension of the filename', String)
15
+ opts.on('-d STRING', '--dir', 'working directory (current by default)', String)
16
+ end.parse!(into: params)
17
+ rescue OptionParser::MissingArgument
18
+ puts 'Argument error.'
19
+ end
20
+
21
+ begin
22
+ session = FileRenamer::PathProcessor.new({ params: params })
23
+ rescue StandardError => e
24
+ abort "#{e.message}"
25
+ end
26
+
27
+ session.rename_files!
28
+
29
+ puts "#{session.counter} paths renamed!"
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FileRenamer
4
+ class NameAlterer
5
+ attr_reader :filename,
6
+ :number,
7
+ :name
8
+
9
+ def initialize
10
+ @filename
11
+ @number
12
+ @name
13
+ end
14
+
15
+ def renamed_filename(args)
16
+ init_params(args)
17
+ name_with_extension
18
+ end
19
+
20
+ private
21
+
22
+ def init_params(args)
23
+ @filename = args[:filename]
24
+ @number = args[:number]
25
+ @name = args[:new_name]
26
+ end
27
+
28
+ def name_with_extension
29
+ filename.gsub(/^\w+./, "#{numbered_name}.")
30
+ end
31
+
32
+ def numbered_name
33
+ number.zero? ? name.to_s : "#{name}_#{number}"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FileRenamer
4
+ class ParamsCorrector
5
+ SLASH = Gem.win_platform? ? '\\' : '/'
6
+ FILENAME_REGEXP = /^[a-zA-Z_0-9-]+$/.freeze
7
+ EXTENSION_REGEXP = /^[a-zA-Z0-9_.]{1,6}$/.freeze
8
+
9
+ attr_accessor :params
10
+
11
+ def initialize
12
+ @params
13
+ end
14
+
15
+ def corrected_params(params)
16
+ init_params(params)
17
+ params_correction!
18
+ self.params
19
+ end
20
+
21
+ def init_params(params)
22
+ self.params = params
23
+ end
24
+
25
+ private
26
+
27
+ def params_correction!
28
+ params[:dir] = correct_dir(params[:dir])
29
+ params[:name] = correct_name(params[:name])
30
+ params[:prefix] = correct_prefix(params[:prefix])
31
+ params[:ext] = correct_ext(params[:ext])
32
+ end
33
+
34
+ def correct_dir(dir)
35
+ if dir.nil?
36
+ Dir.pwd + SLASH
37
+ elsif File.directory?(dir)
38
+ dir += SLASH if dir[-1] != SLASH
39
+ dir
40
+ else
41
+ raise StandardError.new "Directory must exist!"
42
+ end
43
+ end
44
+
45
+ def correct_name(name)
46
+ if name.to_s.match?(FILENAME_REGEXP)
47
+ name.strip.chomp
48
+ else
49
+ raise StandardError.new "Incorrect name!"
50
+ end
51
+ end
52
+
53
+ def correct_prefix(prefix)
54
+ prefix.to_s.strip
55
+ end
56
+
57
+ def correct_ext(ext)
58
+ if ext.to_s.match?(EXTENSION_REGEXP)
59
+ ext[0] == '.' ? ext : ext.prepend('.')
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'name_alterer'
4
+ require_relative 'params_corrector'
5
+
6
+ module FileRenamer
7
+ class PathProcessor
8
+ attr_accessor :counter
9
+
10
+ attr_reader :name_alterer,
11
+ :extension,
12
+ :name,
13
+ :prefix,
14
+ :directory,
15
+ :paths,
16
+ :params
17
+
18
+ def self.run!(args = {})
19
+ self.new(args).rename_files!
20
+ end
21
+
22
+ def initialize(args)
23
+ @params = args[:corrected_params] || ParamsCorrector.new.corrected_params(args[:params])
24
+ @name_alterer = args.fetch(:name_alterer, NameAlterer.new)
25
+
26
+ init_corrected_params
27
+
28
+ @paths = paths
29
+ @counter = 0
30
+ end
31
+
32
+ def rename_files!
33
+ paths.each do |path|
34
+ process_path(path) if should_be_renamed?(path)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def init_corrected_params
41
+ @extension = params[:ext]
42
+ @name = params[:name]
43
+ @directory = params[:dir]
44
+ @prefix = params[:prefix]
45
+ end
46
+
47
+ def process_path(path)
48
+ old_filename = slice_filename(path)
49
+ renamed_filename = name_alterer.renamed_filename({ filename: old_filename,
50
+ number: counter,
51
+ new_name: name })
52
+
53
+ renamed_path = renamed_path(path, old_filename, renamed_filename)
54
+
55
+ rename_file!(path, renamed_path)
56
+ self.counter += 1
57
+ end
58
+
59
+ def rename_file!(path, renamed_path)
60
+ File.rename(path, renamed_path)
61
+ end
62
+
63
+ def renamed_path(path, filename, new_filename)
64
+ path.gsub(filename, new_filename)
65
+ end
66
+
67
+ def should_be_renamed?(path)
68
+ matches_pattern?(path) && is_file?(path)
69
+ end
70
+
71
+ def paths
72
+ Dir["#{directory}*"].sort
73
+ end
74
+
75
+ def matches_pattern?(path)
76
+ slice_filename(path).match?(Regexp.new("^#{prefix}.*#{extension}$"))
77
+ end
78
+
79
+ def is_file?(path)
80
+ !File.directory?(path)
81
+ end
82
+
83
+ def slice_filename(path)
84
+ path.slice(%r{([^/|\\]+$)})
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'NameAlterer#renamed_filename' do
6
+ context 'when single file with one extension rename' do
7
+ let(:params_1) { { filename: 'IMG_123.jpg', number: 0, new_name: 'new_name' } }
8
+ let(:path_alterer_1) { FileRenamer::NameAlterer.new }
9
+
10
+ it 'renames only name' do
11
+ expect(path_alterer_1.renamed_filename(params_1)).to eq 'new_name.jpg'
12
+ end
13
+ end
14
+
15
+ context 'when second file with one extension rename' do
16
+ let(:params_2) { { filename: 'IMG_123.jpg', number: 1, new_name: 'new_name' } }
17
+ let(:path_alterer_2) { FileRenamer::NameAlterer.new }
18
+
19
+ it 'adds number to name' do
20
+ expect(path_alterer_2.renamed_filename(params_2)).to eq 'new_name_1.jpg'
21
+ end
22
+ end
23
+
24
+ context 'when single file with two extensions rename' do
25
+ let(:params_3) { { filename: 'IMG_123.html.erb', number: 0, new_name: 'new_name' } }
26
+ let(:path_alterer_3) { FileRenamer::NameAlterer.new }
27
+
28
+ it 'renames only name' do
29
+ expect(path_alterer_3.renamed_filename(params_3)).to eq 'new_name.html.erb'
30
+ end
31
+ end
32
+
33
+ context 'when second file with two extensions rename' do
34
+ let(:params_4) { { filename: 'IMG_123.html.erb', number: 2, new_name: 'new_name' } }
35
+ let(:path_alterer_4) { FileRenamer::NameAlterer.new }
36
+
37
+ it 'adds number to name' do
38
+ expect(path_alterer_4.renamed_filename(params_4)).to eq 'new_name_2.html.erb'
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'ParamsCorrector#corrected_params' do
6
+ before :each do
7
+ @corrector = FileRenamer::ParamsCorrector.new
8
+ end
9
+
10
+ context 'when extension parameter correction' do
11
+ let(:params) { { ext: 'jpg', dir: './spec/fixtures/dir/', prefix: nil, name: 'new_name' } }
12
+ it 'return ext string with dot if ext not nil' do
13
+ corrected_params = @corrector.corrected_params(params)
14
+ expect(corrected_params[:ext]).to eq '.jpg'
15
+ end
16
+
17
+ let(:params1) { { ext: '.jpg', dir: './spec/fixtures/dir/', prefix: nil, name: 'new_name' } }
18
+ it 'return ext string with dot if ext not nil' do
19
+ corrected_params = @corrector.corrected_params(params1)
20
+ expect(corrected_params[:ext]).to eq '.jpg'
21
+ end
22
+
23
+ let(:params2) { { ext: 'jpganyany', dir: './spec/fixtures/dir/', prefix: nil, name: 'new_name' } }
24
+ it 'return nil if ext not matches regexp' do
25
+ corrected_params = @corrector.corrected_params(params2)
26
+ expect(corrected_params[:ext]).to eq nil
27
+ end
28
+
29
+ let(:params3) { { ext: nil, dir: './spec/fixtures/dir/', prefix: nil, name: 'new_name' } }
30
+ it 'return nil if ext nil' do
31
+ corrected_params = @corrector.corrected_params(params3)
32
+ expect(corrected_params[:ext]).to eq nil
33
+ end
34
+ end
35
+
36
+ context 'when dir parameter correction' do
37
+ let(:params) { { dir: './spec/fixtures/dir/', ext: nil, name: 'new_name' } }
38
+ it 'return path string if dir exists' do
39
+ corrected_params = @corrector.corrected_params(params)
40
+ expect(corrected_params[:dir]).to eq './spec/fixtures/dir/'
41
+ end
42
+
43
+ let(:params1) { { dir: './spec/fixtures/dir2/', ext: nil, name: 'new_name' } }
44
+ it 'raise error if dir does not exists' do
45
+ expect{ @corrector.corrected_params(params1) }.to raise_error(StandardError, "Directory must exist!")
46
+ end
47
+
48
+ let(:params2) { { dir: './spec/fixtures/dir', ext: nil, name: 'new_name' } }
49
+ it 'adds slash to the end of path' do
50
+ corrected_params = @corrector.corrected_params(params2)
51
+ expect(corrected_params[:dir]).to eq './spec/fixtures/dir/'
52
+ end
53
+ end
54
+
55
+ context 'when prefix parameter correction' do
56
+ let(:params) { { prefix: " DCIM\n", ext: nil, dir: './spec/fixtures/dir/', name: 'new_name' } }
57
+ it 'strips prefix string if prefix exists' do
58
+ corrected_params = @corrector.corrected_params(params)
59
+ expect(corrected_params[:prefix]).to eq 'DCIM'
60
+ end
61
+
62
+ let(:params1) { { dir: './spec/fixtures/dir/', prefix: nil, ext: nil, name: 'new_name' } }
63
+ it 'return empty string if prefix does not exists' do
64
+ corrected_params = @corrector.corrected_params(params1)
65
+ expect(corrected_params[:prefix]).to eq ''
66
+ end
67
+ end
68
+
69
+ context 'when name parameter correction' do
70
+ let(:params1) { { dir: './spec/fixtures/dir/', name: nil, ext: nil } }
71
+ it 'raise error if name does not exists' do
72
+ expect{ @corrector.corrected_params(params1) }.to raise_error(StandardError, "Incorrect name!")
73
+ end
74
+
75
+ let(:params2) { { dir: './spec/fixtures/dir/', name: 'name any any ///&&&', ext: nil } }
76
+ it 'raise error if name does not matches regexp' do
77
+ expect{ @corrector.corrected_params(params2) }.to raise_error(StandardError, "Incorrect name!")
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: false
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ describe 'PathProcessor#rename_files' do
6
+ before(:each) do
7
+ i = 1
8
+ 3.times do
9
+ File.write("./spec/fixtures/dir/file#{i}.txt", '')
10
+ i += 1
11
+ end
12
+
13
+ i = 1
14
+ 3.times do
15
+ File.write("./spec/fixtures/dir/pict#{i}.jpg", '')
16
+ i += 1
17
+ end
18
+ end
19
+
20
+ after(:each) do
21
+ Dir['./spec/fixtures/dir/*'].each do |f|
22
+ File.delete(f)
23
+ end
24
+ end
25
+
26
+ context 'when prefix and extension passed' do
27
+ let(:args) { { params: { dir: './spec/fixtures/dir/', prefix: 'file', ext: 'txt', name: 'new_name' } } }
28
+
29
+ it 'renames only file*.txt' do
30
+ FileRenamer::PathProcessor.run!(args)
31
+ expect(Dir['./spec/fixtures/dir/*']).to match_array %w[
32
+ ./spec/fixtures/dir/new_name.txt
33
+ ./spec/fixtures/dir/new_name_1.txt
34
+ ./spec/fixtures/dir/new_name_2.txt
35
+ ./spec/fixtures/dir/pict1.jpg
36
+ ./spec/fixtures/dir/pict2.jpg
37
+ ./spec/fixtures/dir/pict3.jpg
38
+ ]
39
+ end
40
+ end
41
+
42
+ context 'when only ext passed' do
43
+ let(:args) { { params: { dir: './spec/fixtures/dir/', prefix: nil, ext: 'jpg', name: 'new_name' } } }
44
+
45
+ it 'renames only *.jpeg' do
46
+ FileRenamer::PathProcessor.run!(args)
47
+ expect(Dir['./spec/fixtures/dir/*']).to match_array %w[
48
+ ./spec/fixtures/dir/file1.txt
49
+ ./spec/fixtures/dir/file2.txt
50
+ ./spec/fixtures/dir/file3.txt
51
+ ./spec/fixtures/dir/new_name.jpg
52
+ ./spec/fixtures/dir/new_name_1.jpg
53
+ ./spec/fixtures/dir/new_name_2.jpg
54
+ ]
55
+ end
56
+ end
57
+
58
+ context 'when only prefix passed' do
59
+ let(:args) { { params: { dir: './spec/fixtures/dir/', prefix: 'pict', ext: nil, name: 'new_name' } } }
60
+
61
+ it 'renames only pict*' do
62
+ FileRenamer::PathProcessor.run!(args)
63
+ expect(Dir['./spec/fixtures/dir/*']).to match_array %w[
64
+ ./spec/fixtures/dir/file1.txt
65
+ ./spec/fixtures/dir/file2.txt
66
+ ./spec/fixtures/dir/file3.txt
67
+ ./spec/fixtures/dir/new_name.jpg
68
+ ./spec/fixtures/dir/new_name_1.jpg
69
+ ./spec/fixtures/dir/new_name_2.jpg
70
+ ]
71
+ end
72
+ end
73
+
74
+ context 'when prefix and ext doesnt match any of files' do
75
+ let(:args) { { params: { dir: './spec/fixtures/dir/', prefix: 'pict', ext: 'txt', name: 'new_name' } } }
76
+
77
+ it 'renames nothing' do
78
+ FileRenamer::PathProcessor.run!(args)
79
+ expect(Dir['./spec/fixtures/dir/*']).to match_array %w[
80
+ ./spec/fixtures/dir/file1.txt
81
+ ./spec/fixtures/dir/file2.txt
82
+ ./spec/fixtures/dir/file3.txt
83
+ ./spec/fixtures/dir/pict1.jpg
84
+ ./spec/fixtures/dir/pict2.jpg
85
+ ./spec/fixtures/dir/pict3.jpg
86
+ ]
87
+ end
88
+ end
89
+
90
+ context 'when prefix and ext doesnt match any of files' do
91
+ let(:args) { { params: { dir: './spec/fixtures/dir/', prefix: 'file', ext: 'jpg', name: 'new_name' } } }
92
+
93
+ it 'renames nothing' do
94
+ FileRenamer::PathProcessor.run!(args)
95
+ expect(Dir['./spec/fixtures/dir/*']).to match_array %w[
96
+ ./spec/fixtures/dir/file1.txt
97
+ ./spec/fixtures/dir/file2.txt
98
+ ./spec/fixtures/dir/file3.txt
99
+ ./spec/fixtures/dir/pict1.jpg
100
+ ./spec/fixtures/dir/pict2.jpg
101
+ ./spec/fixtures/dir/pict3.jpg
102
+ ]
103
+ end
104
+ end
105
+ end
@@ -1,3 +1,8 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'file_renamer'
3
- require 'path'
1
+ # frozen_string_literal: true
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+
5
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
6
+ require 'params_corrector'
7
+ require 'name_alterer'
8
+ require 'path_processor'
metadata CHANGED
@@ -1,80 +1,93 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_renamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
- - Taras Zhuk
7
+ - Tee Zed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-29 00:00:00.000000000 Z
11
+ date: 2020-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.0'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.0'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: '13.0'
34
34
  type: :development
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: '2.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '3.10'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
55
- description: File Renamer allows to rename bunch of files of similar extension or
56
- filename prefix all at once.
57
- email:
58
- - tee@zed@gmail.com
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description:
70
+ email: tee0zed@gmail.com
59
71
  executables:
60
- - filerenamer
72
+ - file_renamer
61
73
  extensions: []
62
74
  extra_rdoc_files: []
63
75
  files:
64
- - ".rspec"
76
+ - Gemfile
77
+ - Gemfile.lock
65
78
  - LICENSE.txt
66
79
  - README.md
67
80
  - Rakefile
68
- - bin/filerenamer
81
+ - bin/file_renamer
69
82
  - bin/setup
70
- - lib/file_renamer.rb
71
- - lib/file_renamer/version.rb
72
- - lib/path.rb
73
- - spec/integral_spec.rb
74
- - spec/path_spec.rb
75
- - spec/renamer_spec.rb
83
+ - lib/name_alterer.rb
84
+ - lib/params_corrector.rb
85
+ - lib/path_processor.rb
86
+ - spec/name_alterer_spec.rb
87
+ - spec/params_corrector_spec.rb
88
+ - spec/path_processor_spec.rb
76
89
  - spec/spec_helper.rb
77
- homepage: https://github.com/tee0zed/file_renamer
90
+ homepage: http://github.com/tee0zed/file_renamer/
78
91
  licenses:
79
92
  - MIT
80
93
  metadata: {}
@@ -85,22 +98,17 @@ require_paths:
85
98
  - bin
86
99
  required_ruby_version: !ruby/object:Gem::Requirement
87
100
  requirements:
88
- - - "~>"
101
+ - - ">="
89
102
  - !ruby/object:Gem::Version
90
- version: '2.0'
103
+ version: '0'
91
104
  required_rubygems_version: !ruby/object:Gem::Requirement
92
105
  requirements:
93
106
  - - ">="
94
107
  - !ruby/object:Gem::Version
95
108
  version: '0'
96
109
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.7.3
110
+ rubygems_version: 3.1.2
99
111
  signing_key:
100
112
  specification_version: 4
101
- summary: Multiple files renamer script.
102
- test_files:
103
- - spec/integral_spec.rb
104
- - spec/path_spec.rb
105
- - spec/renamer_spec.rb
106
- - spec/spec_helper.rb
113
+ summary: Bulk file renaming tool
114
+ test_files: []
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'optparse'
4
- require_relative '../lib/file_renamer'
5
- require_relative '../lib/path'
6
-
7
- begin
8
-
9
- params = {}
10
- OptionParser.new do |opts|
11
- opts.on('-n', '--name STRING', String)
12
- opts.on('-p', '--prefix STRING', String)
13
- opts.on('-e', '--ext STRING', String)
14
- opts.on('-d', '--dir STRING', String)
15
- end.parse!(into: params)
16
-
17
- rescue OptionParser::MissingArgument
18
- puts "Argument error."
19
- end
20
-
21
- FileRenamer::Renamer.rename!(params)
22
-
23
- puts "#{FileRenamer::Path.get_count} paths renamed!"
@@ -1,80 +0,0 @@
1
- require_relative 'file_renamer/version.rb'
2
- require_relative 'path'
3
-
4
- module FileRenamer
5
- class Renamer
6
- SLASH = Gem.win_platform? ? '\\' : '/'
7
- FILENAME_REGEXP = /^[a-zA-Z_0-9 -]+$/
8
- EXTENSION_REGEXP = /^[a-zA-Z0-9]{1,4}$/
9
-
10
- attr_reader :params, :paths
11
-
12
- def self.rename!(params)
13
- session = FileRenamer::Renamer.new(params)
14
- session.get_paths
15
- session.rename_files
16
- end
17
-
18
- def initialize(params)
19
- @params = params
20
- @paths = []
21
- end
22
-
23
- def get_paths
24
- params_correction!
25
-
26
- Dir["#{params[:dir]}*"].sort.each do |path|
27
- @paths << FileRenamer::Path.new(path, params)
28
- end
29
- end
30
-
31
- def rename_files
32
- @paths.each { |p| p.rename_file! if p.correct_path? }
33
- end
34
-
35
- private
36
-
37
- def params_correction!
38
- correct_dir
39
-
40
- abort "New name must exist!" unless params[:name]
41
- correct_name
42
-
43
- correct_prefix
44
- correct_ext
45
-
46
- abort "Incorrect name!" unless @params[:name]
47
- abort "Directory must exist!" unless @params[:dir]
48
- end
49
-
50
- def correct_dir
51
- if @params[:dir].nil?
52
- @params[:dir] = Dir.pwd
53
- @params[:dir] << SLASH
54
- else
55
- @params[:dir] << SLASH unless @params[:dir][-1] == SLASH
56
- @params[:dir] = nil unless File.directory?(@params[:dir])
57
- end
58
- end
59
-
60
- def correct_name
61
- if @params[:name] && @params[:name].match?(FILENAME_REGEXP)
62
- @params[:name] = @params[:name].strip.gsub(' ', '_')
63
- else
64
- @params[:name] = nil
65
- end
66
- end
67
-
68
- def correct_prefix
69
- @params[:prefix] = @params[:prefix].strip if @params[:prefix]
70
- end
71
-
72
- def correct_ext
73
- if @params[:ext] && @params[:ext].match?(EXTENSION_REGEXP)
74
- @params[:ext] = '.' + @params[:ext] unless @params[:ext][0] == '.'
75
- else
76
- @params[:ext] = nil
77
- end
78
- end
79
- end
80
- end
@@ -1,3 +0,0 @@
1
- module FileRenamer
2
- VERSION = "0.1.3"
3
- end
@@ -1,55 +0,0 @@
1
-
2
- module FileRenamer
3
- class Path
4
- FILENAME_REGEX = /([^\/|\\]+$)/
5
-
6
- @@counter = 0
7
-
8
- attr_reader :path, :prefix, :ext, :new_name
9
-
10
- def self.get_count
11
- @@counter
12
- end
13
-
14
- def self.reset_counter
15
- @@counter = 0
16
- end
17
-
18
- def initialize(path, params)
19
- @path = path
20
- @new_name = params[:name]
21
- @prefix = params[:prefix]
22
- @ext = params[:ext]
23
- end
24
-
25
- def correct_path?
26
- @path.slice(FILENAME_REGEX)
27
- .match?(Regexp.new("^#{prefix}.*#{ext}$")) &&
28
- !File.directory?(@path)
29
- end
30
-
31
- def rename_file!
32
- File.rename(@path, renamed_path(@@counter))
33
- @@counter += 1
34
- end
35
-
36
- private
37
-
38
- def renamed_path(num)
39
- filename = @path.slice(FILENAME_REGEX)
40
-
41
- name =
42
- if num == 0
43
- "#{@new_name}"
44
- else
45
- "#{@new_name}_#{num}"
46
- end
47
-
48
- name << '.' if filename.match?('\.')
49
-
50
- new_name = filename.gsub(/^\w+./, "#{name}")
51
-
52
- @path.gsub(filename, new_name)
53
- end
54
- end
55
- end
@@ -1,85 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "FileRenamer" do
4
- context "integral test" do
5
- before(:each) {
6
- i = 1
7
- 3.times do
8
- File.write("./spec/fixtures/dir/file#{i}.txt", "")
9
- i += 1
10
- end
11
-
12
- i = 1
13
- 3.times do
14
- File.write("./spec/fixtures/dir/pict#{i}.jpg", "")
15
- i += 1
16
- end
17
- }
18
-
19
- after(:each) {
20
- Dir["./spec/fixtures/dir/*"].each do |f|
21
- File.delete(f) if File.exists?(f)
22
- end
23
- }
24
-
25
- let(:params1) {{ dir: "./spec/fixtures/dir/", prefix: "file", ext: "txt", name: "new_name" }}
26
- it 'renames files correctly' do
27
- FileRenamer::Path.reset_counter
28
- FileRenamer::Renamer.rename!(params1)
29
- expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/new_name.txt",
30
- "./spec/fixtures/dir/new_name_1.txt",
31
- "./spec/fixtures/dir/new_name_2.txt",
32
- "./spec/fixtures/dir/pict1.jpg",
33
- "./spec/fixtures/dir/pict2.jpg",
34
- "./spec/fixtures/dir/pict3.jpg"]
35
- end
36
-
37
- let(:params2) {{ dir: "./spec/fixtures/dir/", prefix: "pict", ext: "txt", name: "new_name" }}
38
- it 'doesnt renames if prefix and ext doesnt match any of files' do
39
- FileRenamer::Path.reset_counter
40
- FileRenamer::Renamer.rename!(params2)
41
- expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
42
- "./spec/fixtures/dir/file2.txt",
43
- "./spec/fixtures/dir/file3.txt",
44
- "./spec/fixtures/dir/pict1.jpg",
45
- "./spec/fixtures/dir/pict2.jpg",
46
- "./spec/fixtures/dir/pict3.jpg"]
47
- end
48
-
49
- let(:params3) {{ dir: "./spec/fixtures/dir/", prefix: nil, ext: "jpg", name: "new_name" }}
50
- it 'renames files correctly if only ext passed' do
51
- FileRenamer::Path.reset_counter
52
- FileRenamer::Renamer.rename!(params3)
53
- expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
54
- "./spec/fixtures/dir/file2.txt",
55
- "./spec/fixtures/dir/file3.txt",
56
- "./spec/fixtures/dir/new_name.jpg",
57
- "./spec/fixtures/dir/new_name_1.jpg",
58
- "./spec/fixtures/dir/new_name_2.jpg"]
59
- end
60
-
61
- let(:params4) {{ dir: "./spec/fixtures/dir/", prefix: "file", ext: "jpg", name: "new_name" }}
62
- it 'doesnt renames if prefix and ext doesnt match any of files' do
63
- FileRenamer::Path.reset_counter
64
- FileRenamer::Renamer.rename!(params4)
65
- expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
66
- "./spec/fixtures/dir/file2.txt",
67
- "./spec/fixtures/dir/file3.txt",
68
- "./spec/fixtures/dir/pict1.jpg",
69
- "./spec/fixtures/dir/pict2.jpg",
70
- "./spec/fixtures/dir/pict3.jpg"]
71
- end
72
-
73
- let(:params5) {{ dir: "./spec/fixtures/dir/", prefix: 'pict', ext: nil, name: "new_name" }}
74
- it 'renames files correctly if only prefix passed' do
75
- FileRenamer::Path.reset_counter
76
- FileRenamer::Renamer.rename!(params5)
77
- expect(Dir["./spec/fixtures/dir/*"]).to match_array ["./spec/fixtures/dir/file1.txt",
78
- "./spec/fixtures/dir/file2.txt",
79
- "./spec/fixtures/dir/file3.txt",
80
- "./spec/fixtures/dir/new_name.jpg",
81
- "./spec/fixtures/dir/new_name_1.jpg",
82
- "./spec/fixtures/dir/new_name_2.jpg"]
83
- end
84
- end
85
- end
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
- require 'byebug'
3
- describe "FileRenamer" do
4
- context "Path#correct_path?" do
5
- let(:params1) {{ name: 'new_name', prefix: nil, ext: nil }}
6
- let(:no_opt_params1) { FileRenamer::Path.new('/Documents/DCIM_123.jpg', params1)}
7
- let(:no_opt_params2) { FileRenamer::Path.new('/Documents/ANOTHER_1123133.jpg', params1)}
8
- it "return true for all paths without options" do
9
- expect(no_opt_params1.correct_path?).to eq true
10
- expect(no_opt_params2.correct_path?).to eq true
11
- end
12
-
13
- let(:params2) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
14
- let(:with_options_params_correct1) { FileRenamer::Path.new('/Documents/DCIM_1asdasdggqeda23.jpg', params2) }
15
- let(:with_options_params_correct2) { FileRenamer::Path.new('/Documents/DCIM_1f12fsa23.jpg', params2) }
16
- it "return true if options matches path" do
17
- expect(with_options_params_correct1.correct_path?).to eq true
18
- expect(with_options_params_correct2.correct_path?).to eq true
19
- end
20
-
21
- let(:params3) {{ name: 'new_name', prefix: 'DCIM', ext: nil }}
22
- let(:with_prefix_correct1) { FileRenamer::Path.new('/Documents/DCIM_1asdads123223.jpg', params3) }
23
- let(:with_prefix_correct2) { FileRenamer::Path.new('/Documents/DCIM_1addsa23.png', params3) }
24
- it "return true if prefix matches path" do
25
- expect(with_prefix_correct1.correct_path?).to eq true
26
- expect(with_prefix_correct2.correct_path?).to eq true
27
- end
28
-
29
- let(:params4) {{ name: 'new_name', prefix: nil, ext: '.jpg' }}
30
- let(:with_ext_correct1) { FileRenamer::Path.new('/Documents/ANOTHER_124143.jpg', params4) }
31
- let(:with_ext_correct2) { FileRenamer::Path.new('/Documents/IMG_12122133.jpg', params4) }
32
- it "return true if extension matches path" do
33
- expect(with_ext_correct1.correct_path?).to eq true
34
- expect(with_ext_correct2.correct_path?).to eq true
35
- end
36
-
37
- let(:params5) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
38
- let(:with_prefix_incorrect1) { FileRenamer::Path.new('/Documents/IDCIMMG_114223.jpg', params5) }
39
- let(:with_prefix_incorrect2) { FileRenamer::Path.new('/Documents/IMGDCIMDCIM_12131123.jpg', params5) }
40
- it "return false if prefix not matches path" do
41
- expect(with_prefix_incorrect1.correct_path?).to eq false
42
- expect(with_prefix_incorrect2.correct_path?).to eq false
43
- end
44
-
45
- let(:params6) {{ name: 'new_name', prefix: 'IMG', ext: '.jpg' }}
46
- let(:with_ext_incorrect1) { FileRenamer::Path.new('/Documents/IMG_123.png', params6) }
47
- let(:with_ext_incorrect2) { FileRenamer::Path.new('/Documents/IMG_123.txt', params6) }
48
- it "return false if extension not matches path" do
49
- expect(with_ext_incorrect1.correct_path?).to eq false
50
- expect(with_ext_incorrect2.correct_path?).to eq false
51
- end
52
- end
53
-
54
- context 'Path#renamed_path' do
55
- let(:params7) {{ name: 'new_name', prefix: nil, ext: '.jpg' }}
56
- let(:with_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.jpg', params7) }
57
- it 'renames file correctly if extension param passed' do
58
- expect(with_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.jpg"
59
- end
60
-
61
- let(:params8) {{ name: 'new_name', prefix: nil, ext: nil }}
62
- let(:without_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.jpg', params8) }
63
- it 'renames file correctly if extension param not passed' do
64
- expect(without_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.jpg"
65
- end
66
-
67
- let(:params9) {{ name: 'new_name', prefix: nil, ext: nil }}
68
- let(:filename_w_two_dots_with_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.html.erb', params9) }
69
- it 'renames file with two extensions correctly if extension param passed' do
70
- expect(filename_w_two_dots_with_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.html.erb"
71
- end
72
-
73
- let(:params10) {{ name: 'new_name', prefix: 'DCIM', ext: '.jpg' }}
74
- let(:filename_w_two_dots_without_ext_params) { FileRenamer::Path.new('/Documents/IMG_123.html.erb', params10) }
75
- it 'renames file with two extensions correctly if extension param not passed' do
76
- expect(filename_w_two_dots_without_ext_params.send(:renamed_path, 0)).to eq "/Documents/new_name.html.erb"
77
- end
78
- end
79
- end
@@ -1,126 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'FileRenamer' do
4
- context 'Renamer#get_paths' do
5
- before(:each) {
6
- i = 1
7
- 3.times do
8
- File.write("./spec/fixtures/dir/file#{i}.txt", "")
9
- i += 1
10
- end
11
-
12
- i = 1
13
- 3.times do
14
- File.write("./spec/fixtures/dir/pict#{i}.jpg", "")
15
- i += 1
16
- end
17
- }
18
-
19
- after(:each) {
20
- Dir["./spec/fixtures/dir/*"].each do |f|
21
- File.delete(f) if File.exists? f
22
- end
23
- }
24
-
25
- let(:params1) {{ dir: "./spec/fixtures/dir/", ext: nil, prefix: nil, name: 'new_name' }}
26
- let(:obj1) { FileRenamer::Renamer.new(params1) }
27
- it 'returns all Path objects' do
28
- obj1.get_paths
29
-
30
- expect(obj1.paths.count).to eq 6
31
- expect(obj1.paths.all?{|o| o.is_a? FileRenamer::Path }).to eq true
32
- expect(obj1.paths.map(&:path)).to match_array ["./spec/fixtures/dir/pict3.jpg",
33
- "./spec/fixtures/dir/pict2.jpg",
34
- "./spec/fixtures/dir/pict1.jpg",
35
- "./spec/fixtures/dir/file1.txt",
36
- "./spec/fixtures/dir/file2.txt",
37
- "./spec/fixtures/dir/file3.txt" ]
38
-
39
- end
40
- end
41
-
42
- context 'Renamer#correct_ext' do
43
- let(:params) {{ ext: 'jpg' }}
44
- let(:obj) { FileRenamer::Renamer.new(params) }
45
- it 'return ext string with dot if ext not nil' do
46
- obj.send(:correct_ext)
47
- expect(obj.params[:ext]).to eq '.jpg'
48
- end
49
-
50
- let(:params1) {{ ext: 'jpganyany' }}
51
- let(:obj1) { FileRenamer::Renamer.new(params1) }
52
- it 'return nil if ext not matches regexp' do
53
- obj1.send(:correct_ext)
54
- expect(obj1.params[:ext]).to eq nil
55
- end
56
-
57
- let(:params2) {{ ext: nil }}
58
- let(:obj2) { FileRenamer::Renamer.new(params2) }
59
- it 'return nil if ext nil' do
60
- obj2.send(:correct_ext)
61
- expect(obj2.params[:ext]).to eq nil
62
- end
63
- end
64
-
65
- context 'Renamer#correct_dir' do
66
- let(:params) {{ dir: './spec/fixtures/dir/' }}
67
- let(:obj) { FileRenamer::Renamer.new(params) }
68
- it 'return dir string if dir exists' do
69
- obj.send(:correct_dir)
70
- expect(obj.params[:dir]).to eq './spec/fixtures/dir/'
71
- end
72
-
73
- let(:params1) {{ dir: './spec/fixtures/dir2/' }}
74
- let(:obj1) { FileRenamer::Renamer.new(params1) }
75
- it 'return nil if dir does not exists' do
76
- obj1.send(:correct_dir)
77
- expect(obj1.params[:dir]).to eq nil
78
- end
79
-
80
- let(:params2) {{ dir: './spec/fixtures/dir' }}
81
- let(:obj2) { FileRenamer::Renamer.new(params2) }
82
- it 'add slash to the end of path' do
83
- obj2.send(:correct_dir)
84
- expect(obj2.params[:dir]).to eq './spec/fixtures/dir/'
85
- end
86
- end
87
-
88
- context 'Renamer#correct_prefix' do
89
- let(:params) {{ prefix: " DCIM\n" }}
90
- let(:obj) { FileRenamer::Renamer.new(params) }
91
- it 'strips prefix string if prefix exists' do
92
- obj.send(:correct_prefix)
93
- expect(obj.params[:prefix]).to eq 'DCIM'
94
- end
95
-
96
- let(:params1) {{ prefix: nil }}
97
- let(:obj1) { FileRenamer::Renamer.new(params1) }
98
- it 'return nil if prefix does not exists' do
99
- obj1.send(:correct_prefix)
100
- expect(obj1.params[:prefix]).to eq nil
101
- end
102
- end
103
-
104
- context 'Renamer#correct_name' do
105
- let(:params) {{ name: "new name " }}
106
- let(:obj) { FileRenamer::Renamer.new(params) }
107
- it 'deletes spaces from filename and strips' do
108
- obj.send(:correct_name)
109
- expect(obj.params[:name]).to eq 'new_name'
110
- end
111
-
112
- let(:params1) {{ name: nil }}
113
- let(:obj1) { FileRenamer::Renamer.new(params1) }
114
- it 'return nil if name does not exists' do
115
- obj1.send(:correct_name)
116
- expect(obj1.params[:name]).to eq nil
117
- end
118
-
119
- let(:params2) {{ name: 'name any any ////&&$^' }}
120
- let(:obj2) { FileRenamer::Renamer.new(params1) }
121
- it 'return nil if name does not matches regexp' do
122
- obj1.send(:correct_name)
123
- expect(obj1.params[:name]).to eq nil
124
- end
125
- end
126
- end