file_retriever 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/file_retriever.gemspec +29 -0
- data/lib/file_retriever/.gitignore +4 -0
- data/lib/file_retriever/FileRetriever/.gitignore +3 -0
- data/lib/file_retriever/FileRetriever/.rspec +1 -0
- data/lib/file_retriever/FileRetriever/sources/Errors.rb +9 -0
- data/lib/file_retriever/FileRetriever/sources/FileRetriever.rb +99 -0
- data/lib/file_retriever/FileRetriever/sources/StringExtension.rb +17 -0
- data/lib/file_retriever/FileRetriever/spec/TestFileHandler.rb +91 -0
- data/lib/file_retriever/FileRetriever/spec/TestHelper.rb +37 -0
- data/lib/file_retriever/FileRetriever/spec/file_changer_spec.rb +81 -0
- data/lib/file_retriever/FileRetriever/spec/file_handler_spec.rb +13 -0
- data/lib/file_retriever/FileRetriever/spec/filer_retriever_spec.rb +10 -0
- data/lib/file_retriever/FileRetriever/spec/spec_helper.rb +12 -0
- data/lib/file_retriever/FileRetriever/use_cases/FileChanger.rb +105 -0
- data/lib/file_retriever/version.rb +3 -0
- data/lib/file_retriever.rb +8 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a3e43ec4ce2d1e6e953719c22da2d834866968a7a2aca37eefafc7d275234294
|
4
|
+
data.tar.gz: c7f1a2e78a574294b2d664ae29ec64531b5ffe88cd007a67bc919fbb9e3e27e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a1bcf28039f791011014abd3669072f2b1297e6786b907680eea494d6b658f8e438fbb3765402bb5ffd67081072e0659d61f1cbf3973238c5373c10091cfd68
|
7
|
+
data.tar.gz: 88575a7248190e64e115202e6128ffb302c55602d31159c77de1d70be09697b6e1ce747937a43f44412b2aae031b96b8ab036433489a6ccff90a223f0ed808b4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
file_retriever (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
rake (10.5.0)
|
10
|
+
|
11
|
+
PLATFORMS
|
12
|
+
ruby
|
13
|
+
|
14
|
+
DEPENDENCIES
|
15
|
+
bundler (~> 2.0)
|
16
|
+
file_retriever!
|
17
|
+
rake (~> 10.0)
|
18
|
+
|
19
|
+
BUNDLED WITH
|
20
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 TODO: Write your name
|
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,36 @@
|
|
1
|
+
# FileRetriever
|
2
|
+
coming soon..
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'file_retriever'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install file_retriever
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
coming soon..
|
23
|
+
|
24
|
+
## Development
|
25
|
+
|
26
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
27
|
+
|
28
|
+
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).
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hayao11/file_retriever.
|
33
|
+
|
34
|
+
## License
|
35
|
+
|
36
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "file_retriever"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "file_retriever/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_retriever"
|
8
|
+
spec.version = FileRetriever::VERSION
|
9
|
+
spec.authors = ["Takashi Miyazaki"]
|
10
|
+
spec.email = ["hayao1900@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{This library supports file processing. }
|
13
|
+
spec.description = %q{This library supports file processing. It processes the specified directory recursively and provides file type and path information.}
|
14
|
+
spec.homepage = "https://github.com/hayao11/FileRetriever"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
@@ -0,0 +1,99 @@
|
|
1
|
+
|
2
|
+
class BaseFileRetriever
|
3
|
+
FileInfo = Struct.new(:file_type,:file_name,:abs_path)
|
4
|
+
|
5
|
+
def initialize(skip_file_regs=[])
|
6
|
+
default_skip_regs = ["^\\.+$","\\.DS_Store$"]
|
7
|
+
skip_file_regs.concat(default_skip_regs)
|
8
|
+
@skip_file_names_reg = reg_convert_from_strs(skip_file_regs)
|
9
|
+
end
|
10
|
+
|
11
|
+
def retrieve(base_path,callback)
|
12
|
+
files = dir_entries(base_path)
|
13
|
+
files.each do |file_name|
|
14
|
+
next if skip_file?(file_name)
|
15
|
+
|
16
|
+
abs_path = File.expand_path(file_name,base_path)
|
17
|
+
file_type = ftype?(abs_path)
|
18
|
+
next unless file_exist?(abs_path)
|
19
|
+
|
20
|
+
callback.call(file_type,file_name,abs_path)
|
21
|
+
|
22
|
+
case file_type
|
23
|
+
when :directory
|
24
|
+
retrieve(abs_path,callback)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def retrieved(base_path,result_files=[])
|
30
|
+
files = dir_entries(base_path)
|
31
|
+
files.each do |file_name|
|
32
|
+
next if skip_file?(file_name)
|
33
|
+
|
34
|
+
abs_path = File.expand_path(file_name,base_path)
|
35
|
+
file_type = ftype?(abs_path)
|
36
|
+
next unless file_exist?(abs_path)
|
37
|
+
|
38
|
+
result_files << FileInfo.new(file_type,file_name,abs_path)
|
39
|
+
|
40
|
+
case file_type
|
41
|
+
when :directory
|
42
|
+
retrieved(abs_path,result_files)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
result_files
|
46
|
+
end
|
47
|
+
|
48
|
+
def update_skip_file_names_reg(reg)
|
49
|
+
@skip_file_names_reg = reg
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def ftype?(path)
|
54
|
+
begin
|
55
|
+
File.ftype(path).to_sym
|
56
|
+
rescue => e
|
57
|
+
return :unknown
|
58
|
+
ensure
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def dir_entries(path)
|
63
|
+
Dir.entries(path)
|
64
|
+
end
|
65
|
+
|
66
|
+
def skip_file?(relative_path)
|
67
|
+
return true if @skip_file_names_reg.match(relative_path)
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
|
71
|
+
def reg_convert_from_strs(strs)
|
72
|
+
%r(#{strs.join("|")})
|
73
|
+
end
|
74
|
+
|
75
|
+
def file_exist?(path)
|
76
|
+
File.exist?(path)
|
77
|
+
end
|
78
|
+
|
79
|
+
def read(path)
|
80
|
+
File.read(path)
|
81
|
+
end
|
82
|
+
end#class
|
83
|
+
|
84
|
+
|
85
|
+
if $0 == __FILE__
|
86
|
+
|
87
|
+
skip_dir_names = ["vendor",".git"]
|
88
|
+
file_retriever = BaseFileRetriever.new(skip_dir_names)
|
89
|
+
|
90
|
+
callback = lambda {|file_type,file_name,abs_path|
|
91
|
+
p file_type,file_name,abs_path
|
92
|
+
}
|
93
|
+
|
94
|
+
file_retriever.retrieve(".",callback)
|
95
|
+
p file_retriever.retrieved(".")
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require './sources/Errors.rb'
|
3
|
+
require './sources/StringExtension.rb'
|
4
|
+
|
5
|
+
class TestFileHandler
|
6
|
+
def initialize
|
7
|
+
@skip_reg = set_up_skip_delete_reg
|
8
|
+
end
|
9
|
+
|
10
|
+
def make_test_dir(dir_size,base_dir_name)
|
11
|
+
dir_size.times do |i|
|
12
|
+
path = "#{base_dir_name}/inner_test/inner_dir#{i}"
|
13
|
+
dir_size.times do |n|
|
14
|
+
path += "/test_dir_name#{n}"
|
15
|
+
make_dir(path)
|
16
|
+
make_text(path + "/test.txt")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def force_rm_rf(path,is_serious=false)
|
22
|
+
return unless File.exist?(path)
|
23
|
+
if skip_delete_path?(path)
|
24
|
+
raise Errors::DangerousPathError.new(path)
|
25
|
+
end
|
26
|
+
FileUtils.rm_rf(path) if is_serious
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def set_up_skip_delete_dirs
|
31
|
+
<<-EOS
|
32
|
+
Applications
|
33
|
+
etc
|
34
|
+
Library
|
35
|
+
home
|
36
|
+
Network
|
37
|
+
System
|
38
|
+
net
|
39
|
+
Users
|
40
|
+
private
|
41
|
+
Volumes
|
42
|
+
sbin
|
43
|
+
bin
|
44
|
+
tmp
|
45
|
+
usr
|
46
|
+
EOS
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_up_skip_delete_list
|
50
|
+
set_up_skip_delete_dirs
|
51
|
+
.split_and_strip
|
52
|
+
.map {|s| "/" << s}
|
53
|
+
.concat([".","/","./","../"])
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_up_skip_delete_reg
|
57
|
+
str = '^(\\/+('
|
58
|
+
str << set_up_skip_delete_dirs
|
59
|
+
.split_and_strip
|
60
|
+
.join('|')
|
61
|
+
str << ')?\\/?'
|
62
|
+
str << '|\\.+\\/?'
|
63
|
+
str << ')$'
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def skip_delete_path?(path)
|
68
|
+
if path.match(@skip_reg)
|
69
|
+
return true
|
70
|
+
end
|
71
|
+
return false
|
72
|
+
end
|
73
|
+
|
74
|
+
def make_dir(path)
|
75
|
+
return if File.exist?(path)
|
76
|
+
FileUtils.mkdir_p(path)
|
77
|
+
end
|
78
|
+
|
79
|
+
def make_text(path)
|
80
|
+
return if File.exist?(path)
|
81
|
+
FileUtils.touch(path)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
if $0 == __FILE__
|
87
|
+
test_file_handler = TestFileHandler.new
|
88
|
+
test_file_handler.make_test_dir(10,"test_dir")
|
89
|
+
|
90
|
+
end
|
91
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require './sources/StringExtension.rb'
|
2
|
+
|
3
|
+
class TestHelper
|
4
|
+
class << self
|
5
|
+
def pascal_case?(str)
|
6
|
+
return false if str == nil
|
7
|
+
|
8
|
+
pascal_reg = /(?:(?:[A-Z])(?:[a-z0-9]+))+/
|
9
|
+
if res = str.match(pascal_reg)
|
10
|
+
return true if res.to_s.length == str.length
|
11
|
+
end
|
12
|
+
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_up_skip_retrieve_dirs
|
17
|
+
#describe the directory name you want to skip here.
|
18
|
+
skip_dir_names = <<-EOS
|
19
|
+
node_modules
|
20
|
+
spec
|
21
|
+
Carthage
|
22
|
+
Pods
|
23
|
+
EOS
|
24
|
+
skip_dir_list = skip_dir_names.split_and_strip
|
25
|
+
end
|
26
|
+
|
27
|
+
def snake_strs
|
28
|
+
snake_strs = <<-EOS
|
29
|
+
test_test
|
30
|
+
Abc_Def_Ghi
|
31
|
+
abc_def_ghi_jkl_mn
|
32
|
+
A__B__C
|
33
|
+
a_b_c
|
34
|
+
EOS
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require './use_cases/FileChanger.rb'
|
2
|
+
require './spec/TestFileHandler.rb'
|
3
|
+
require './spec/TestHelper.rb'
|
4
|
+
|
5
|
+
|
6
|
+
shared_context 'file changer init' do |paths|
|
7
|
+
let(:file_changer){ FileChanger.new(paths) }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'FileChanger',must:true do
|
11
|
+
context 'check reg convert' do
|
12
|
+
include_context 'file changer init',[]
|
13
|
+
|
14
|
+
it 'snake case strings change to camel case' do
|
15
|
+
TestHelper.snake_strs.split_and_strip.each do |str|
|
16
|
+
changed_camel = file_changer.send(:snake_to_camel).call(str)
|
17
|
+
match_res = changed_camel.match(/_/)
|
18
|
+
expect(match_res).to eq nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'snake case strings change to pascal case' do
|
23
|
+
TestHelper.snake_strs.split_and_strip.each do |str|
|
24
|
+
changed_camel = file_changer.send(:snake_to_pascal).call(str)
|
25
|
+
if snake_match_res = changed_camel.match(/_/)
|
26
|
+
expect(snake_match_res).to eq nil
|
27
|
+
end
|
28
|
+
|
29
|
+
changed_pascal = file_changer.send(:snake_to_pascal).call(str)
|
30
|
+
if is_pascal = TestHelper.pascal_case?(changed_pascal)
|
31
|
+
expect(is_pascal).to eq true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end#it
|
35
|
+
|
36
|
+
end#context
|
37
|
+
end#desc
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
describe 'FileChanger ',must:true do
|
42
|
+
include_context 'file changer init',TestHelper.set_up_skip_retrieve_dirs
|
43
|
+
|
44
|
+
it 'create directories and retrive snake case' do
|
45
|
+
test_file_handler = TestFileHandler.new
|
46
|
+
change_type = :to_pascal
|
47
|
+
make_dir_base_name = "test_dirs"
|
48
|
+
changed_dir_name = file_changer.send(:snake_to_pascal).call(make_dir_base_name)
|
49
|
+
retrieve_target_path = "."
|
50
|
+
|
51
|
+
if File.exist?(make_dir_base_name)
|
52
|
+
pending "directory is already exist"
|
53
|
+
else
|
54
|
+
make_size = 20
|
55
|
+
test_file_handler.make_test_dir(make_size,make_dir_base_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
#:to_camel or :to_pascal
|
59
|
+
target_dirs = file_changer.retrieve_snake_dirs(retrieve_target_path,change_type)
|
60
|
+
|
61
|
+
if target_dirs.empty?
|
62
|
+
return pending "target_dirs is empty"
|
63
|
+
else
|
64
|
+
p "target-dir-length:#{target_dirs.length}"
|
65
|
+
end
|
66
|
+
|
67
|
+
file_changer.change_dirs_name(target_dirs)
|
68
|
+
|
69
|
+
|
70
|
+
changed_dirs = file_changer.retrieve_snake_dirs(retrieve_target_path,change_type)
|
71
|
+
p "result-target-dir-length:#{changed_dirs.length}"
|
72
|
+
expect(changed_dirs.empty?).to eq true
|
73
|
+
|
74
|
+
if File.exist?(changed_dir_name)
|
75
|
+
test_file_handler.force_rm_rf(changed_dir_name,true)
|
76
|
+
end
|
77
|
+
|
78
|
+
end#context
|
79
|
+
end#desc
|
80
|
+
|
81
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require './spec/TestFileHandler.rb'
|
2
|
+
|
3
|
+
describe 'FileChanger',must:true do
|
4
|
+
it 'skip dengerous path ' do
|
5
|
+
test_file_handler = TestFileHandler.new
|
6
|
+
test_file_handler.set_up_skip_delete_list.each do |s|
|
7
|
+
if test_file_handler.skip_delete_path?(s)
|
8
|
+
expect { test_file_handler.force_rm_rf(s) }.to raise_error(Errors::DangerousPathError)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end#it
|
12
|
+
end#describe
|
13
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require './sources/FileRetriever.rb'
|
2
|
+
|
3
|
+
describe :FileRetriever,must:true do
|
4
|
+
it "retrieved not empty" do
|
5
|
+
skip_dir_list = []
|
6
|
+
file_retriever = FileRetriever.new(skip_dir_list)
|
7
|
+
retrieved = file_retriever.retrieved(".")
|
8
|
+
expect(retrieved.empty?).to eq false
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
RSpec.configure do |config|
|
3
|
+
config.expect_with :rspec do |expectations|
|
4
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
5
|
+
end
|
6
|
+
|
7
|
+
config.mock_with :rspec do |mocks|
|
8
|
+
mocks.verify_partial_doubles = true
|
9
|
+
end
|
10
|
+
|
11
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
12
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require './sources/FileRetriever.rb'
|
3
|
+
require './sources/StringExtension.rb'
|
4
|
+
|
5
|
+
class FileChanger
|
6
|
+
ChangePathInfo = Struct.new(:before,:after)
|
7
|
+
def initialize(skip_file_names=[])
|
8
|
+
default_skip_regs = ["^.git$","vendor"]
|
9
|
+
default_skip_regs.concat(skip_file_names)
|
10
|
+
@file_retriever = FileRetriever.new(default_skip_regs)
|
11
|
+
@snake_case_reg = Regexp.new(/_+./)
|
12
|
+
end
|
13
|
+
|
14
|
+
def retrieve_snake_dirs(base_path,after_type)
|
15
|
+
dirs = []
|
16
|
+
path_change_callback = after_type == :to_camel ? snake_to_camel : snake_to_pascal
|
17
|
+
|
18
|
+
cb = lambda do |ftype,fn,abs_path|
|
19
|
+
case ftype
|
20
|
+
when :directory
|
21
|
+
changed_path = set_up_changed_dir(abs_path,path_change_callback)
|
22
|
+
unless abs_path == changed_path
|
23
|
+
path_info = ChangePathInfo.new(abs_path,changed_path)
|
24
|
+
dirs << path_info
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
@file_retriever.retrieve(base_path,cb)
|
30
|
+
return dirs
|
31
|
+
end
|
32
|
+
|
33
|
+
def change_dirs_name(dirs)
|
34
|
+
dirs.sort {|a,b|
|
35
|
+
a.before.length <=> b.before.length
|
36
|
+
}.reverse.each do |path_info|
|
37
|
+
FileUtils.mv(path_info.before,path_info.after)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def snake_to_camel
|
43
|
+
lambda { |str|
|
44
|
+
str.gsub(@snake_case_reg) do |word|
|
45
|
+
word.upcase.sub(/_+/,'')
|
46
|
+
end
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def snake_to_pascal
|
51
|
+
lambda { |str|
|
52
|
+
return str unless str.match(/_/)
|
53
|
+
str.split(/_/).map{|s| s.capitalize }.join("")
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def split_target(path)
|
58
|
+
splited_path = path.split('/')
|
59
|
+
target = splited_path.pop
|
60
|
+
return splited_path,target
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_up_changed_dir(path,cb)
|
64
|
+
path_data = split_target(path)
|
65
|
+
changed_path = cb.call(path_data.pop)
|
66
|
+
[path_data,changed_path].join('/')
|
67
|
+
end
|
68
|
+
|
69
|
+
def camel_to_pascal(str)
|
70
|
+
reg = /(?:(?:^[a-z]|[A-Z])(?:[a-z0-9]+))/
|
71
|
+
matches = str.scan(reg)
|
72
|
+
changed_str = matches.map{|s|s.capitalize}.join("")
|
73
|
+
changed_str.length == str.length ? changed_str : nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
if $0 == __FILE__
|
78
|
+
|
79
|
+
#describe the directory name you want to skip here.
|
80
|
+
skip_dir_names = <<-EOS
|
81
|
+
Carthage
|
82
|
+
Pods
|
83
|
+
spec
|
84
|
+
node_modules
|
85
|
+
EOS
|
86
|
+
|
87
|
+
#:to_camel or :to_pascal
|
88
|
+
change_type = :to_pascal
|
89
|
+
file_changer = FileChanger.new(skip_dir_names.split_and_strip)
|
90
|
+
|
91
|
+
#specify retrieve target directory. (relative or absolute path)
|
92
|
+
retrieve_target_path = "../pococha/ios/Pokota"
|
93
|
+
abs_path = File.absolute_path(retrieve_target_path)
|
94
|
+
#retrieved directories data
|
95
|
+
to_be_changed_dirs_data = file_changer.retrieve_snake_dirs(abs_path,change_type)
|
96
|
+
|
97
|
+
#change directoy name
|
98
|
+
if to_be_changed_dirs_data.empty?
|
99
|
+
p :snake_not_found
|
100
|
+
else
|
101
|
+
# file_changer.change_dirs_name(to_be_changed_dirs_data)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_retriever
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takashi Miyazaki
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: This library supports file processing. It processes the specified directory
|
42
|
+
recursively and provides file type and path information.
|
43
|
+
email:
|
44
|
+
- hayao1900@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- file_retriever.gemspec
|
58
|
+
- lib/file_retriever.rb
|
59
|
+
- lib/file_retriever/.gitignore
|
60
|
+
- lib/file_retriever/FileRetriever/.gitignore
|
61
|
+
- lib/file_retriever/FileRetriever/.rspec
|
62
|
+
- lib/file_retriever/FileRetriever/sources/Errors.rb
|
63
|
+
- lib/file_retriever/FileRetriever/sources/FileRetriever.rb
|
64
|
+
- lib/file_retriever/FileRetriever/sources/StringExtension.rb
|
65
|
+
- lib/file_retriever/FileRetriever/spec/TestFileHandler.rb
|
66
|
+
- lib/file_retriever/FileRetriever/spec/TestHelper.rb
|
67
|
+
- lib/file_retriever/FileRetriever/spec/file_changer_spec.rb
|
68
|
+
- lib/file_retriever/FileRetriever/spec/file_handler_spec.rb
|
69
|
+
- lib/file_retriever/FileRetriever/spec/filer_retriever_spec.rb
|
70
|
+
- lib/file_retriever/FileRetriever/spec/spec_helper.rb
|
71
|
+
- lib/file_retriever/FileRetriever/use_cases/FileChanger.rb
|
72
|
+
- lib/file_retriever/version.rb
|
73
|
+
homepage: https://github.com/hayao11/FileRetriever
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.0.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: This library supports file processing.
|
96
|
+
test_files: []
|