fileman 0.1.30646 → 0.1.32104

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: febf89801ec699944102973dcad88b0fa961a31c
4
- data.tar.gz: 32d397a79ee25dd9794480af74a84bf7faec60ac
3
+ metadata.gz: bc7e6ea25aa65085ca2533678d88e224b2e7645f
4
+ data.tar.gz: e3519620666ff6a33bf6ebcb3bc46169a054b1ef
5
5
  SHA512:
6
- metadata.gz: b323254d9bb0b9f00ed6310ee43fc58598d6cd89eb8b82e7db143477c886d986df47d72da8834902ad3152fdbab651030718afb285d049704ae4540a89124e39
7
- data.tar.gz: df382817c7b46163957c1876ee8d8aabc1a578bdf780567d0c88afe504de031cba5a01d7648c667aa2c6b717e80416cc7b5bdfafce335b84bd77fb5d682c5e20
6
+ metadata.gz: c4653d4d441f9b0b751a6821b205aaa7b9f77e33ed74bd6b14feb2bf6a4c8179f1c9cda7664a5e4fcd5543be37f5d8f28b6d588e18acf53157fe272f70e05d58
7
+ data.tar.gz: 90a3e93d990cd04b519f1fdb6cb77a3cfde980fe23ee332cd4a7ed4d046a6e554f2a5a0f65ba7d2f17ac42699c9523df518d68cea5b60e0adecee23ae151b2b3
@@ -5,8 +5,7 @@ module Fileman
5
5
  module_function
6
6
 
7
7
  def remove(folder_path)
8
- new_name = Fileman.rename_r(folder_path, 'a', {:include_files => true, :ignore_ext => true})
9
- new_path = File.expand_path("../#{new_name}", folder_path)
8
+ new_path = Fileman.rename(folder_path, 'a', {:include_files => true, :ignore_ext => true, :recursive => true})
10
9
  FileUtils.remove_dir new_path, true
11
10
  end
12
11
  end
@@ -3,53 +3,74 @@ require 'fileutils'
3
3
  module Fileman
4
4
  module_function
5
5
 
6
- # Rename folder and all its subfolders
6
+ # Rename file or folder. If the item is a folder, subfolders and files can optionally
7
+ # be recursively renamed to.
7
8
  #
8
9
  # Arguments:
9
10
  # ----------
10
- # +folder_path+ Folder's path
11
- # +new_name+ New name that is used to rename the folder and all its subfolder.
12
- # If there is multiple subfolders, then a incremetal number is appened
11
+ # +item_path+ Item's path
12
+ # +new_name+ New name that is used to rename the item. If the item is a folder,
13
+ # subfolders and files can optionally be recursively renamed to.
14
+ # If there is multiple subfolders, then an incremetal number is appened
13
15
  # straight after the 'new_name'(e.g. if new_name is 'a', and there are
14
16
  # 2 subfolder 'subf_a' and 'subf_b', then the new names will respectively
15
17
  # be 'a1' and 'a2')
16
18
  # +options+ Optional parameters defined as follow:
17
- # +:include_files+ If true, all files will also be renamed. Extensions will be removed.
18
- # Default is false
19
- # +:ignore_ext+ If true, and if 'include_files' is also true, then file's extensions
20
- # are ignored. Default is false
19
+ # +:recursive+ Default is false. If true, and if the item is a folder, then all the
20
+ # folder's content is also renamed
21
+ # +:include_files+ Default is false. If true, all files will also be renamed. In that case
22
+ # the 'recursive' option is implicitly set to true
23
+ # +:ignore_ext+ Default is false. If true, and if 'include_files' is also true, then
24
+ # file's extensions are removed.
21
25
  #
22
26
  # Returns the new name of the root folder
23
- def rename_r(folder_path, new_name, options={})
27
+ def rename(item_path, new_name, options={})
24
28
  include_files = !options[:include_files].nil? && options[:include_files]
29
+ recursive = include_files ? true : (!options[:recursive].nil? && options[:recursive])
25
30
  ignore_ext = !options[:ignore_ext].nil? && options[:ignore_ext]
26
31
  increment_name = lambda { |inc| inc == 0 ? new_name : "#{new_name}#{inc}" }
27
32
 
28
- rename_item = lambda { |f|
29
- parent_folder = File.expand_path("../", f)
30
- file_ext = ignore_ext ? '' : File.extname(f)
31
- counter = 0
32
- while File.exists?(File.join(parent_folder, "#{increment_name.call(counter)}#{file_ext}")) do
33
- counter+=1
33
+ try_rename_item = lambda { |counter, file, parent_folder, file_ext|
34
+ final_name = nil
35
+ begin
36
+ while File.exists?(File.join(parent_folder, "#{increment_name.call(counter)}#{file_ext}")) do
37
+ counter+=1
38
+ end
39
+ final_name = increment_name.call(counter)
40
+ final_name = "#{final_name}#{file_ext}" unless ignore_ext
41
+ FileUtils.mv file, File.join(parent_folder, final_name)
42
+ rescue Errno::EACCES => eaccess # permission denied
43
+ final_name = nil
44
+ rescue Exception => e
45
+ raise e
34
46
  end
35
- final_name = increment_name.call(counter)
36
- final_name = "#{final_name}#{file_ext}" unless ignore_ext
37
- FileUtils.mv f, File.join(parent_folder, final_name)
38
47
  final_name
39
48
  }
40
49
 
41
- final_root_folder_new_name = new_name
42
- all_items = Dir.glob("#{folder_path}/**/*", File::FNM_DOTMATCH) - %w[. ..]
43
- items_per_folder = all_items.group_by { |x| File.expand_path('../',x)}
44
-
45
- sorted_items = items_per_folder.map { |k,v| v.sort_by{|y| y}.reverse }.flatten.reverse
46
- (sorted_items << folder_path).each { |f|
47
- if File.directory?(f)
48
- final_root_folder_new_name = rename_item.call(f)
49
- elsif include_files && File.file?(f)
50
- rename_item.call(f)
50
+ rename_item = lambda { |i|
51
+ parent_folder = File.expand_path("../", i)
52
+ file_ext = ignore_ext ? '' : File.extname(i)
53
+ counter = 0
54
+ final_name = nil
55
+ while final_name == nil
56
+ final_name = try_rename_item.call(counter, i, parent_folder, file_ext)
57
+ counter += 1
58
+ end
59
+ File.join(parent_folder, final_name)
60
+ }
61
+
62
+ recursively_rename_item = lambda { |i|
63
+ item_is_dir = File.directory? i
64
+ new_item_path = rename_item.call(i) unless !item_is_dir && !include_files # rename current item
65
+
66
+ if item_is_dir && recursive # if current is a folder, rename all its children
67
+ all_folder_children = Dir.glob("#{new_item_path}/*", File::FNM_DOTMATCH) - ["#{new_item_path}/.", "#{new_item_path}/.."]
68
+ all_folder_children.each { |c| recursively_rename_item.call(c) }
51
69
  end
70
+
71
+ new_item_path
52
72
  }
53
- return final_root_folder_new_name
73
+
74
+ return recursively_rename_item.call(item_path)
54
75
  end
55
76
  end
@@ -18,8 +18,9 @@ def rename_help(rename_command)
18
18
  text = "usage: fileman #{rename_command} <path> <new-name> [<args>]\n" +
19
19
  "\n" +
20
20
  "Optional arguments are:\n" +
21
- " -i\t Include files so they are also renamed\n" +
22
- " -e\t Remove files extension. Only valid if -i option is also specified"
21
+ " -r\t Recursive rename. That means that if the path points to a folder, all its content will also be renamed\n" +
22
+ " -i\t Include files so they are also renamed. This option implicitly uses recursive rename(no need to use the -r option)\n" +
23
+ " -e\t Remove files extension. Only valid if the -i option is also specified"
23
24
  puts text.colorize(:yellow)
24
25
  end
25
26
 
@@ -55,6 +56,8 @@ def get_rename_options
55
56
  options[:include_files] = true
56
57
  when 'e'
57
58
  options[:ignore_ext] = true
59
+ when 'r'
60
+ options[:recursive] = true
58
61
  end
59
62
  }
60
63
  }
@@ -71,6 +74,7 @@ def get_rename_arguments
71
74
  options = get_rename_options
72
75
  arguments[:include_files] = options[:include_files]
73
76
  arguments[:ignore_ext] = options[:ignore_ext]
77
+ arguments[:recursive] = options[:recursive]
74
78
 
75
79
  return arguments
76
80
  end
@@ -87,7 +91,8 @@ def rename
87
91
  options = {}
88
92
  options[:include_files] = true if args[:include_files]
89
93
  options[:ignore_ext] = true if args[:ignore_ext]
90
- Fileman.rename_r(args[:path], args[:name], options) unless args[:error]
94
+ options[:recursive] = true if args[:recursive]
95
+ Fileman.rename(args[:path], args[:name], options) unless args[:error]
91
96
  end
92
97
  end
93
98
 
@@ -1 +1 @@
1
- module Fileman
2
1
  VERSION ="0.1.30646"
2
+ module Fileman
3
3
  VERSION ="0.1.32104"
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- describe "#Fileman.rename_r" do
3
+ describe "#Fileman.rename" do
4
4
  let(:folder_name) { "TestFolder" }
5
5
  let(:new_folder_name) { "a" }
6
6
 
@@ -20,7 +20,7 @@ describe "#Fileman.rename_r" do
20
20
 
21
21
  describe 'rename folder' do
22
22
 
23
- it 'rename the folder and all its subfolders recursively' do
23
+ it 'rename the folder' do
24
24
  File.directory?(folder_name).must_equal true
25
25
  File.directory?("#{folder_name}/SubFolder_1").must_equal true
26
26
  File.directory?("#{folder_name}/SubFolder_2").must_equal true
@@ -28,7 +28,25 @@ describe "#Fileman.rename_r" do
28
28
  File.file?("#{folder_name}/SubFolder_1/test_file_1.txt").must_equal true
29
29
  File.file?("#{folder_name}/SubFolder_2/test_file.txt").must_equal true
30
30
 
31
- Fileman.rename_r folder_name, new_folder_name
31
+ Fileman.rename folder_name, new_folder_name
32
+
33
+ File.directory?("a").must_equal true
34
+ File.directory?("a/SubFolder_1").must_equal true
35
+ File.directory?("a/SubFolder_2").must_equal true
36
+ File.file?("a/SubFolder_1/test_file.txt").must_equal true
37
+ File.file?("a/SubFolder_1/test_file_1.txt").must_equal true
38
+ File.file?("a/SubFolder_2/test_file.txt").must_equal true
39
+ end
40
+
41
+ it 'rename the folder and all its subfolders recursively if the :recursive option is true' do
42
+ File.directory?(folder_name).must_equal true
43
+ File.directory?("#{folder_name}/SubFolder_1").must_equal true
44
+ File.directory?("#{folder_name}/SubFolder_2").must_equal true
45
+ File.file?("#{folder_name}/SubFolder_1/test_file.txt").must_equal true
46
+ File.file?("#{folder_name}/SubFolder_1/test_file_1.txt").must_equal true
47
+ File.file?("#{folder_name}/SubFolder_2/test_file.txt").must_equal true
48
+
49
+ Fileman.rename folder_name, new_folder_name, {:recursive => true}
32
50
 
33
51
  File.directory?("a").must_equal true
34
52
  File.directory?("a/a").must_equal true
@@ -38,7 +56,7 @@ describe "#Fileman.rename_r" do
38
56
  File.file?("a/a1/test_file.txt").must_equal true
39
57
  end
40
58
 
41
- it 'rename the folder and all its subfolders recursively, including all files ' +
59
+ it 'rename the folder and all its subfolders recursively, including all files, if both the :include_files and :recursive options are true' +
42
60
  'if option :include_files is true' do
43
61
  File.directory?(folder_name).must_equal true
44
62
  File.directory?("#{folder_name}/SubFolder_1").must_equal true
@@ -47,7 +65,7 @@ describe "#Fileman.rename_r" do
47
65
  File.file?("#{folder_name}/SubFolder_1/test_file_1.txt").must_equal true
48
66
  File.file?("#{folder_name}/SubFolder_2/test_file.txt").must_equal true
49
67
 
50
- Fileman.rename_r folder_name, new_folder_name, {:include_files => true}
68
+ Fileman.rename folder_name, new_folder_name, {:include_files => true, :recursive => true}
51
69
 
52
70
  File.directory?("a").must_equal true
53
71
  File.directory?("a/a").must_equal true
@@ -58,7 +76,7 @@ describe "#Fileman.rename_r" do
58
76
  end
59
77
 
60
78
  it 'rename the folder and all its subfolders recursively, including all files ' +
61
- 'WITHOUT their extensions if both options :include_files and :ignore_ext are true' do
79
+ 'WITHOUT their extensions if options :include_files, :ignore_ext, and :recursive are true' do
62
80
  File.directory?(folder_name).must_equal true
63
81
  File.directory?("#{folder_name}/SubFolder_1").must_equal true
64
82
  File.directory?("#{folder_name}/SubFolder_2").must_equal true
@@ -66,7 +84,7 @@ describe "#Fileman.rename_r" do
66
84
  File.file?("#{folder_name}/SubFolder_1/test_file_1.txt").must_equal true
67
85
  File.file?("#{folder_name}/SubFolder_2/test_file.txt").must_equal true
68
86
 
69
- Fileman.rename_r folder_name, new_folder_name, {:include_files => true, :ignore_ext => true}
87
+ Fileman.rename folder_name, new_folder_name, {:include_files => true, :ignore_ext => true, :recursive => true}
70
88
 
71
89
  File.directory?("a").must_equal true
72
90
  File.directory?("a/a").must_equal true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fileman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.30646
4
+ version: 0.1.32104
5
5
  platform: ruby
6
6
  authors:
7
7
  - nicolasdao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2014-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler