file_crawler 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +1 -15
- data/lib/file_crawler/finder.rb +31 -84
- data/lib/file_crawler/finder/command.rb +2 -0
- data/lib/file_crawler/finder/command/move.rb +32 -0
- data/lib/file_crawler/finder/command/search.rb +85 -0
- data/lib/file_crawler/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34a0754d5719d80cd49b03f62407e221c0f209fa
|
4
|
+
data.tar.gz: 07d50c11e8c698159ffe6d06cb54505a84077e09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea7b6219cca07fbba34797ec57982026d07e738a23b0f567bc7ced635fd1063feec1d791d49d52adb28bbeba3da23629b9a4c0e544986d28585a9adc715ca72
|
7
|
+
data.tar.gz: 7545f5913729f65fc58b3039f4583be9ab4c248beaa87ccbf0b6b61aaaa17d0e15f90793c64f5aa7ac224c402c0e34811cff2874791ba6f5afa39dd366afe075
|
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# FileCrawler
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/file_crawler`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
3
|
## Installation
|
8
4
|
|
9
5
|
Add this line to your application's Gemfile:
|
@@ -22,20 +18,10 @@ Or install it yourself as:
|
|
22
18
|
|
23
19
|
## Usage
|
24
20
|
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
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).
|
32
|
-
|
33
21
|
## Contributing
|
34
22
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
36
|
-
|
23
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hirohisa/file_crawler.
|
37
24
|
|
38
25
|
## License
|
39
26
|
|
40
27
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/lib/file_crawler/finder.rb
CHANGED
@@ -1,96 +1,25 @@
|
|
1
|
+
require_relative 'finder/command'
|
2
|
+
|
1
3
|
module FileCrawler
|
2
4
|
|
3
5
|
class Finder
|
4
6
|
attr_accessor :path
|
7
|
+
attr_reader :rows
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
def initialize(path)
|
9
|
-
@path = path
|
10
|
-
select_in_path(path)
|
11
|
-
end
|
12
|
-
|
13
|
-
def rows
|
14
|
-
@rows ||= []
|
15
|
-
end
|
16
|
-
|
17
|
-
def select_in_path(path)
|
18
|
-
tap {
|
19
|
-
@rows = find_rows_in_path(path)
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
def find_rows_in_path(path)
|
24
|
-
Dir.entries(path).select {|item|
|
25
|
-
!item.start_with?('.')
|
26
|
-
}.map {|item|
|
27
|
-
path + '/' + item
|
28
|
-
}
|
29
|
-
end
|
30
|
-
|
31
|
-
def select_with_extension(extension)
|
32
|
-
tap {
|
33
|
-
@rows = find_rows_with_extension(extension, rows)
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
def find_rows_with_extension(extension, resource)
|
38
|
-
find_rows_files(resource).select {|item|
|
39
|
-
extension.any? {|e|
|
40
|
-
extname = File.extname(item).downcase
|
41
|
-
extname == ".#{e.downcase}"
|
42
|
-
}
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
def select_files
|
47
|
-
tap {
|
48
|
-
@rows = find_rows_files(rows)
|
49
|
-
}
|
50
|
-
end
|
51
|
-
|
52
|
-
def find_rows_files(resource)
|
53
|
-
resource.select {|item|
|
54
|
-
!File.directory?(item)
|
55
|
-
}
|
56
|
-
end
|
57
|
-
|
58
|
-
def select_directories
|
59
|
-
tap {
|
60
|
-
@rows = find_rows_directories(rows)
|
61
|
-
}
|
62
|
-
end
|
63
|
-
|
64
|
-
def find_rows_directories(resource)
|
65
|
-
resource.select {|item|
|
66
|
-
File.directory?(item)
|
67
|
-
}
|
68
|
-
end
|
69
|
-
|
70
|
-
def select_with_extension_in_directory(extension)
|
71
|
-
tap {
|
72
|
-
@rows = find_rows_with_extension_in_directories(extension, find_rows_directories(rows))
|
73
|
-
}
|
74
|
-
end
|
75
|
-
|
76
|
-
def find_rows_with_extension_in_directories(extension, directories)
|
77
|
-
directories.select {|directory|
|
78
|
-
variable_to_exist_with_extension_in_directory(extension, directory)
|
79
|
-
}
|
80
|
-
end
|
81
|
-
|
82
|
-
def variable_to_exist_with_extension_in_directory(extension, directory)
|
83
|
-
subfiles = find_rows_in_path(directory)
|
84
|
-
result = find_rows_with_extension(extension, subfiles)
|
85
|
-
return true if result.size > 0
|
9
|
+
include Command::Move
|
10
|
+
include Command::Search
|
86
11
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
12
|
+
def initialize(path = nil)
|
13
|
+
@path = path
|
14
|
+
@rows = []
|
15
|
+
select_in_path(path) unless path.nil?
|
91
16
|
end
|
92
17
|
end
|
93
18
|
|
19
|
+
# @param conditions
|
20
|
+
# directory [Boolean]
|
21
|
+
# exntesion [Array]
|
22
|
+
# extension_in_directory [Array]
|
94
23
|
def self.search(path, conditions = {})
|
95
24
|
finder = FileCrawler::Finder.new(path)
|
96
25
|
|
@@ -108,4 +37,22 @@ module FileCrawler
|
|
108
37
|
finder.rows
|
109
38
|
end
|
110
39
|
|
40
|
+
# conditions
|
41
|
+
# - if dont have extension_in_directory, directory true
|
42
|
+
# - move check? if include, need condition[:force]
|
43
|
+
def self.move(path, destination, conditions = {})
|
44
|
+
raise ArgumentError unless File.directory?(destination)
|
45
|
+
|
46
|
+
search_conditions = {
|
47
|
+
directory: true,
|
48
|
+
extension_in_directory: conditions[:extension_in_directory]
|
49
|
+
}
|
50
|
+
search_conditions[:directory] = nil unless search_conditions[:extension_in_directory].nil?
|
51
|
+
|
52
|
+
directories = search(path, search_conditions)
|
53
|
+
|
54
|
+
finder = FileCrawler::Finder.new
|
55
|
+
finder.move(directories, destination)
|
56
|
+
end
|
57
|
+
|
111
58
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module FileCrawler
|
4
|
+
class Finder
|
5
|
+
module Command
|
6
|
+
module Move
|
7
|
+
|
8
|
+
def move(directories, destination)
|
9
|
+
move_targets = directories.select {|directory|
|
10
|
+
valiable_to_move?(directory, destination)
|
11
|
+
}
|
12
|
+
not_move_targets = directories.select {|directory|
|
13
|
+
!valiable_to_move?(directory, destination)
|
14
|
+
}
|
15
|
+
|
16
|
+
FileUtils.mv(move_targets, destination)
|
17
|
+
|
18
|
+
move_targets.map {|path|
|
19
|
+
destination + '/' + File.basename(path)
|
20
|
+
} + not_move_targets
|
21
|
+
end
|
22
|
+
|
23
|
+
def valiable_to_move?(current_path, destination)
|
24
|
+
return false if current_path == destination
|
25
|
+
|
26
|
+
next_path = destination + '/' + File.basename(current_path)
|
27
|
+
!File.exist?(next_path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module FileCrawler
|
2
|
+
class Finder
|
3
|
+
module Command
|
4
|
+
module Search
|
5
|
+
|
6
|
+
def select_in_path(path)
|
7
|
+
tap {
|
8
|
+
@rows = find_rows_in_path(path)
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_rows_in_path(path)
|
13
|
+
Dir.entries(path).select {|item|
|
14
|
+
!item.start_with?('.')
|
15
|
+
}.map {|item|
|
16
|
+
path + '/' + item
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def select_with_extension(extension)
|
21
|
+
tap {
|
22
|
+
@rows = find_rows_with_extension(extension, rows)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_rows_with_extension(extension, resource)
|
27
|
+
find_rows_files(resource).select {|item|
|
28
|
+
extension.any? {|e|
|
29
|
+
extname = File.extname(item).downcase
|
30
|
+
extname == ".#{e.downcase}"
|
31
|
+
}
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def select_files
|
36
|
+
tap {
|
37
|
+
@rows = find_rows_files(rows)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def find_rows_files(resource)
|
42
|
+
resource.select {|item|
|
43
|
+
!File.directory?(item)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def select_directories
|
48
|
+
tap {
|
49
|
+
@rows = find_rows_directories(rows)
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_rows_directories(resource)
|
54
|
+
resource.select {|item|
|
55
|
+
File.directory?(item)
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def select_with_extension_in_directory(extension)
|
60
|
+
tap {
|
61
|
+
@rows = find_rows_with_extension_in_directories(extension, find_rows_directories(rows))
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def find_rows_with_extension_in_directories(extension, directories)
|
66
|
+
directories.select {|directory|
|
67
|
+
variable_to_exist_with_extension_in_directory(extension, directory)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def variable_to_exist_with_extension_in_directory(extension, directory)
|
72
|
+
subfiles = find_rows_in_path(directory)
|
73
|
+
result = find_rows_with_extension(extension, subfiles)
|
74
|
+
return true if result.size > 0
|
75
|
+
|
76
|
+
subdirectories = find_rows_directories(subfiles).select {|subdirectory|
|
77
|
+
variable_to_exist_with_extension_in_directory(extension, subdirectory)
|
78
|
+
}
|
79
|
+
subdirectories.size > 0
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/lib/file_crawler/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_crawler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hirohisa Kawasaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -64,6 +64,9 @@ files:
|
|
64
64
|
- file_crawler.gemspec
|
65
65
|
- lib/file_crawler.rb
|
66
66
|
- lib/file_crawler/finder.rb
|
67
|
+
- lib/file_crawler/finder/command.rb
|
68
|
+
- lib/file_crawler/finder/command/move.rb
|
69
|
+
- lib/file_crawler/finder/command/search.rb
|
67
70
|
- lib/file_crawler/version.rb
|
68
71
|
homepage: https://github.com/hirohisa/file_crawler
|
69
72
|
licenses:
|