classifile 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c188cda8d916ed432b19c5d81cf714248cdaebfb0ef07a94d0c4d7daf0f06387
4
+ data.tar.gz: 0aa717603eb6e0c4db4414af494b9dd451c1a720d28d9bc72f4279e7b9adb639
5
+ SHA512:
6
+ metadata.gz: 5f243782cc523bc87873eefbb8d9f76c1bbe46d3efc08ad25903853294af3635f31f6610d5370bb078e492e920bb7b6a77b7b9edbeeb820de36681fdb6bc4e56
7
+ data.tar.gz: 9c7e972a8c91fdb3323a70ee150d8fdaec571eeefba044423ef98f1e1c3953e987aa1d247fc32a25f70bb643e50d54745a4b31da677b981183a8cd363810611c
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # デフォルトの無視対象ファイル
2
+ /shelf/
3
+ /workspace.xml
4
+ # エディターベースの HTTP クライアントリクエスト
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="jdk" jdkName="ruby-3.0.2-p107" jdkType="RUBY_SDK" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, ruby-3.0.2-p107) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.2.27, ruby-3.0.2-p107) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="minitest (v5.14.2, ruby-3.0.2-p107) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.20.1, ruby-3.0.2-p107) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.0.2.0, ruby-3.0.2-p107) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.0.0, ruby-3.0.2-p107) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.3, ruby-3.0.2-p107) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rbs (v1.0.4, ruby-3.0.2-p107) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.1.1, ruby-3.0.2-p107) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.5, ruby-3.0.2-p107) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.20.0, ruby-3.0.2-p107) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.11.0, ruby-3.0.2-p107) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.11.0, ruby-3.0.2-p107) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.0.0, ruby-3.0.2-p107) [gem]" level="application" />
28
+ </component>
29
+ <component name="RakeTasksCache">
30
+ <option name="myRootTask">
31
+ <RakeTaskImpl id="rake" />
32
+ </option>
33
+ </component>
34
+ </module>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/classifile.iml" filepath="$PROJECT_DIR$/.idea/classifile.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/MethodLength:
16
+ Exclude:
17
+ - '*_test.rb'
18
+
19
+ #AsciiComments:
20
+ # Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at @kurehajime. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in classifile.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 kurehajime
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,265 @@
1
+ # Classifile
2
+
3
+ Classifile is a tool for classifying files by Ruby DSL.
4
+
5
+ ## Installation
6
+
7
+ $ gem install classifile
8
+
9
+ ## Usage
10
+
11
+
12
+ 1. Write a DSL.
13
+
14
+ ```ruby
15
+ # dsl.rb
16
+
17
+ dir "Images" do
18
+ image?
19
+ dir "Dogs" do
20
+ include? "dog"
21
+ end
22
+
23
+ dir "Cats" do
24
+ dir "Kittens" do
25
+ include? "kitten"
26
+ end
27
+ include? "cat"
28
+ end
29
+ end
30
+
31
+ dir "Movies" do
32
+ movie?
33
+ end
34
+ ```
35
+
36
+ 2. Prepare the files.
37
+
38
+ ```shell
39
+ temp/
40
+ ├from/
41
+ │ ├─wild_dog.jpg
42
+ │ ├─beautiful_cat.png
43
+ │ ├─cute_kitten.gif
44
+ │ └─dancing_cat.mpg
45
+ └to/
46
+ ```
47
+
48
+ 3. Run the command
49
+
50
+ ```shell
51
+ $ classifile --dsl dsl.rb --from "/temp/from/*" --to "/temp/to"
52
+ ```
53
+
54
+ 4. Done!
55
+
56
+ ```shell
57
+ temp/
58
+ ├from/
59
+ └to/
60
+  ├Images/
61
+  │ ├Dogs/
62
+  │ │ └─wild_dog.jpg
63
+  │ └Cats/
64
+  │  │└─beautiful_cat.png
65
+  │  └Kittens/
66
+  │   └─cute_kitten.gif
67
+  └Movies/
68
+   └─dancing_cat.mpg
69
+ ```
70
+
71
+ ## Syntax
72
+
73
+ ### Classifile rules.
74
+
75
+ 1. The hierarchy of the dir block represents the directory hierarchy.
76
+ 2. If the check fails, exit the block.
77
+ 3. When It complete a block to the end, it will be saved in that directory.
78
+
79
+ ### Block methods
80
+
81
+ #### dir block
82
+
83
+ `dir` block represents a directory.
84
+ A dir block can also be nested.
85
+
86
+ ```ruby
87
+ dir "Images" do
88
+ image?
89
+ dir "Dogs" do
90
+ include? "dog"
91
+ end
92
+ end
93
+ ```
94
+
95
+ `dog.png -> /Images/Dogs/dog.png`
96
+
97
+ #### group block
98
+
99
+ `group` block is a group that does not have a directory.
100
+
101
+ ```ruby
102
+ group "Animals" do
103
+ dir "Dogs" do
104
+ include? "dog"
105
+ end
106
+ end
107
+ ```
108
+
109
+ `dog.png -> /Dogs/dog.png`
110
+
111
+ ### Check methods
112
+
113
+ The check method checks if the file should be stored in that directory.
114
+ If it does not pass the check, it will leave the block immediately.
115
+
116
+ #### include?
117
+
118
+ `include?` method checks if the file name contains one of the strings.
119
+
120
+ ```ruby
121
+ dir "Dogs" do
122
+ include? "dog"
123
+ end
124
+
125
+ dir "Cats" do
126
+ include? "cat" , "kitten"
127
+ end
128
+ ```
129
+
130
+ `dog.png -> /Dogs/dog.png`
131
+ `cat.png -> /Cats/cat.png`
132
+ `kitten.png -> /Cats/kitten.png`
133
+
134
+
135
+ #### end_with?
136
+
137
+ `end_with?` method checks if the file name ends with one of the string.
138
+
139
+ ```ruby
140
+ dir "Archives" do
141
+ end_with? ".zip" , ".gz"
142
+ end
143
+ ```
144
+
145
+ `doc.zip -> /Archives/doc.zip`
146
+
147
+ #### image? / sound? / movie?
148
+
149
+ `image?` method checks if a file is an image or not.
150
+
151
+ `sound?` method checks if a file is an sound or not.
152
+
153
+ `movie?` method checks if a file is an movie or not.
154
+
155
+ ```ruby
156
+ dir "Images" do
157
+ image?
158
+ end
159
+
160
+ dir "Movies" do
161
+ image?
162
+ end
163
+ ```
164
+
165
+ `cat.png -> /Images/cat.png`
166
+ `dog.avi -> /Movies/dog.avi`
167
+
168
+
169
+ #### assert / assert_nil / assert_includes ...
170
+
171
+ You can also use the [minitest](https://docs.ruby-lang.org/ja/2.1.0/class/MiniTest=3a=3aAssertions.html)
172
+ methods.
173
+
174
+ ```ruby
175
+ dir "Archives" do |file|
176
+ assert_includes %w[.zip .gz], file.extname
177
+ end
178
+ ```
179
+
180
+ `doc.zip -> /Archives/doc.zip`
181
+
182
+
183
+ ### Other methods
184
+
185
+ #### empty_dir!
186
+
187
+ If `empty_dir!` is executed, the file will not be saved directly under that directory.
188
+
189
+ In this example, `dog.png` will be saved in `Images/Dogs`. In this example, `dog.png` will be saved in `Images/Dogs`, but `cat.png` will not be saved anywhere.
190
+
191
+ ```ruby
192
+ dir "Images" do
193
+ empty_dir!
194
+ image?
195
+ dir "Dogs" do
196
+ include? "dog"
197
+ end
198
+ end
199
+ ```
200
+
201
+ `doc.png -> /Images/Dogs/dog.png`
202
+ `cat.png -> (not saved) `
203
+
204
+ #### after_save
205
+
206
+ Execute the method after the save is complete.
207
+
208
+ ```ruby
209
+ dir "Markdown" do |_file|
210
+ end_with? ".md"
211
+ after_save :hello
212
+
213
+ def hello
214
+ puts "hello"
215
+ end
216
+ end
217
+ ```
218
+
219
+ `hello.md -> /Markdown/hello.md`
220
+
221
+ and
222
+
223
+ The output is "hello".
224
+
225
+
226
+ ### Block argument
227
+
228
+ File information can be obtained by block argument.
229
+
230
+ ```ruby
231
+ dir "Archives" do |file|
232
+ assert_includes %w[.zip .gz], file.extname
233
+ end
234
+ ```
235
+
236
+ | Property | Description |
237
+ | ---- | ---- |
238
+ | dirname | The directory before the move |
239
+ | basename | File name |
240
+ | pure_basename | File name without extension |
241
+ | extname | Extension |
242
+ | atime | Date and time of made |
243
+ | ctime | Date and time of change |
244
+ | size | File size |
245
+ | to_path | Paths that will be saved |
246
+
247
+
248
+
249
+ ## Development
250
+
251
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
252
+
253
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
254
+
255
+ ## Contributing
256
+
257
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kurehajime/classifile. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/classifile/blob/main/CODE_OF_CONDUCT.md).
258
+
259
+ ## License
260
+
261
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
262
+
263
+ ## Code of Conduct
264
+
265
+ Everyone interacting in the Classifile project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/classifile/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/classifile/const/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "classifile"
7
+ spec.version = Classifile::VERSION
8
+ spec.authors = ["kurehajime"]
9
+ spec.email = ["xiidec@gmail.com"]
10
+
11
+ spec.summary = "classify file by ruby DSL."
12
+ spec.description = "classify file by ruby DSL."
13
+ spec.homepage = "https://github.com/kurehajime/classifile"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/kurehajime/classifile"
19
+ spec.metadata["changelog_uri"] = "https://github.com/kurehajime/classifile/blob/master/README.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # dependency
33
+ spec.add_dependency "minitest", ">= 5.0"
34
+
35
+ spec.add_development_dependency "rake", ">= 13.0"
36
+ spec.add_development_dependency "rubocop", ">= 1.7"
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: "https://github.com/kurehajime/classifile"
40
+ end
data/exe/classifile ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "optparse"
6
+
7
+ @froms = []
8
+
9
+ opt = OptionParser.new
10
+ opt.on("-d", "--dsl PATH", "Dsl path") { |v| @dsl = v }
11
+ opt.on("-f", "--from PATH", "Source path") { |v| @froms << v }
12
+ opt.on("-t", "--to PATH", "Output path") { |v| @to = v }
13
+ opt.on("-p", "--preview", "Preview Mode(don't move,don't copy)") { |v| @preview = v }
14
+ opt.on("-c", "--copy", "Copy Mode(don't move)") { |v| @copy = v }
15
+ opt.on("-h", "--help", "Show this help") do
16
+ puts opt
17
+ exit
18
+ end
19
+
20
+ opt.parse!(ARGV)
21
+ @froms |= ARGV
22
+
23
+ require "classifile"
24
+ exe = Classifile::Execute.new
25
+
26
+ if @preview
27
+ exe.test @dsl, @froms, @to
28
+ elsif @copy
29
+ exe.copy @dsl, @froms, @to
30
+ else
31
+ exe.move @dsl, @froms, @to
32
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/assertions"
4
+
5
+ # Classifile
6
+ module Classifile
7
+ ##
8
+ # Wrapper class for Minitest::Assertions.
9
+ # Wrap and convert to Failed error
10
+ # when minitest's Assert method is called.
11
+ module AssertChecker
12
+ # include Minitest:Assertion
13
+ class Asserter
14
+ include Minitest::Assertions
15
+ attr_accessor :assertions, :failure
16
+
17
+ def initialize
18
+ self.assertions = 0
19
+ self.failure = nil
20
+ end
21
+ end
22
+
23
+ ##
24
+ # Provides assert methods of minitest.
25
+ def method_missing(name, *args)
26
+ @assert = Asserter.new if @assert.nil?
27
+ unless @assert.respond_to?(name) && name.to_s.include?("assert")
28
+ raise NoMethodError.new("Method '#{name}' not found", name)
29
+ end
30
+
31
+ begin
32
+ @assert.send name, *args
33
+ rescue MiniTest::Assertion
34
+ raise Failed unless @gotcha
35
+ end
36
+ end
37
+
38
+ def respond_to_missing?(sym, *)
39
+ if sym.to_s.include?("assert")
40
+ @assert.respond_to?(sym) ? true : super
41
+ else
42
+ super
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Classifile
4
+ ##
5
+ # Check the file extension to see the file type.
6
+ module ExtensionChecker
7
+ attr_accessor :extname
8
+
9
+ def image?
10
+ raise Failed if !@gotcha && !FILE_TYPE_IMAGE.include?(@extname)
11
+ end
12
+
13
+ def sound?
14
+ raise Failed if !@gotcha && !FILE_TYPE_SOUND.include?(@extname)
15
+ end
16
+
17
+ def movie?
18
+ raise Failed if !@gotcha && !FILE_TYPE_MOVIE.include?(@extname)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Classifile
4
+ ##
5
+ # Check the file name
6
+ module NameChecker
7
+ attr_accessor :name
8
+
9
+ ##
10
+ # Whether the file name contains any of the target string
11
+ def include?(*patterns)
12
+ patterns.each do |p|
13
+ return nil if _include?(p)
14
+ end
15
+ raise Failed unless @gotcha
16
+ end
17
+
18
+ ##
19
+ # Whether the file name ends with one of the target strings
20
+ def end_with?(*patterns)
21
+ patterns.each do |p|
22
+ return nil if _end_with?(p)
23
+ end
24
+ raise Failed unless @gotcha
25
+ end
26
+
27
+ private
28
+
29
+ def _include?(pattern)
30
+ @name.downcase.include?(pattern.downcase)
31
+ end
32
+
33
+ def _end_with?(pattern)
34
+ @name.downcase.end_with?(pattern.downcase)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Classifile
4
+ module Classifile
5
+ ##
6
+ # Classify files by DSL
7
+ class Classify
8
+ attr_reader :file, :to_path
9
+
10
+ def run(target_file, to_path, &block)
11
+ raise "TypeError" unless target_file.is_a?(TargetFile)
12
+
13
+ state = State.new(target_file)
14
+ state.to_path = to_path
15
+ state.save_name = target_file.basename
16
+
17
+ begin
18
+ state.instance_exec(state.file, &block)
19
+ rescue Failed
20
+ # Ignored
21
+ end
22
+ state.gotcha
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # extension
5
+ module Classifile
6
+ FILE_TYPE_IMAGE = %w[.gif .jpg .jpeg .jpe .jfif .png .bmp .dib .rle .ico .ai .art .psd .tif .tiff .nsk .webp].freeze
7
+ FILE_TYPE_SOUND = %w[.mp3 .wma .asf .3gp .3g2 .aac .ogg .oga .mov .m4a .alac .flac .wav].freeze
8
+ FILE_TYPE_MOVIE = %w[.avi .flv .mpg .mpeg .mp4 .mkv .mov .qt .wmv .asf .m2ts .ts .m4a .webm .ogm].freeze
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Classifile
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Classifile
4
+ ##
5
+ # Exception raised when a check fails.
6
+ # Exit the dir block it is currently in.
7
+ class Failed < StandardError
8
+ end
9
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Classifile
4
+ module Classifile
5
+ # execute
6
+ class Execute
7
+ ##
8
+ # Classify the files by DSL.
9
+ # However, it does not actually move the file,
10
+ # but outputs the mv command as a string.
11
+ def test(dsl_path, from_paths, to_path)
12
+ classify(dsl_path, from_paths, to_path).each do |ft|
13
+ puts "mv \"#{ft.from}\" \"#{ft.to}\" "
14
+ end
15
+ end
16
+
17
+ ##
18
+ # Classify the files by DSL.
19
+ def move(dsl_path, from_paths, to_path)
20
+ classify(dsl_path, from_paths, to_path).each do |ft|
21
+ FileTools.move(ft.from, ft.to)
22
+ ft.after_save_procs.each(&:call)
23
+ end
24
+ end
25
+
26
+ ##
27
+ # Classify the files by DSL.
28
+ # However, the original file will remain.
29
+ def copy(dsl_path, from_paths, to_path)
30
+ classify(dsl_path, from_paths, to_path).each do |ft|
31
+ FileTools.move(ft.from, ft.to, copy: true)
32
+ ft.after_save_procs.each(&:call)
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Classify files by DSL and return an array of FromTo classes.
38
+ def classify(dsl_path, from_paths, to_path)
39
+ arr = []
40
+ dsl = FileTools.read_dsl(dsl_path)
41
+ Dir.glob(from_paths).each do |from_path|
42
+ arr |= _classify(dsl, from_path, to_path)
43
+ end
44
+
45
+ arr
46
+ end
47
+
48
+ private
49
+
50
+ def _classify(dsl, from_path, to_path)
51
+ arr = []
52
+ cfy = Classify.new
53
+ FileTools.get_file_list(from_path).each do |from_file|
54
+ result = cfy.run(TargetFile.build_by_file(from_file), File.expand_path(to_path)) do
55
+ eval dsl # rubocop:disable all
56
+ end
57
+ arr << result if result
58
+ end
59
+
60
+ arr
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Source and destination classes for file
5
+ class FromTo
6
+ attr_accessor :from, :to, :after_save_procs
7
+
8
+ def initialize(from, to)
9
+ @from = from
10
+ @to = to
11
+ @after_save_procs = []
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ module Classifile
5
+ # File Utility
6
+ class FileTools
7
+ def self.get_file_list(path)
8
+ ex_path = File.expand_path(path) # for Windows
9
+ if !File.exist?(ex_path) # if foo/* pattern
10
+ Dir.glob(ex_path)
11
+ elsif File.ftype(ex_path) == "directory"
12
+ Dir.glob(File.join(ex_path, "*")).sort
13
+ else
14
+ [ex_path]
15
+ end
16
+ end
17
+
18
+ def self.move(src, dest, copy: false)
19
+ dir = File.dirname(dest).to_s
20
+ FileUtils.mkpath(dir)
21
+
22
+ if copy
23
+ FileUtils.copy(src, dest)
24
+ else
25
+ FileUtils.move(src, dest)
26
+ end
27
+ end
28
+
29
+ def self.read_dsl(dsl_path)
30
+ begin
31
+ f = File.open(dsl_path)
32
+ dsl = f.read
33
+ rescue StandardError
34
+ p $ERROR_INFO
35
+ ensure
36
+ # noinspection RubyScope
37
+ f&.close
38
+ end
39
+ # noinspection RubyScope
40
+ dsl
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Classifile
4
+ module Classifile
5
+ ##
6
+ # Class that manages the current state.
7
+ # The DSL code will be executed on an instance of this class
8
+ # A State is created for each dir and group.
9
+ class State
10
+ include AssertChecker
11
+ include ExtensionChecker
12
+ include NameChecker
13
+
14
+ attr_accessor :to_path,
15
+ :save_name,
16
+ :empty,
17
+ :additional_filename,
18
+ :file,
19
+ :after_save_syms
20
+ attr_reader :gotcha
21
+ alias empty? empty
22
+
23
+ def initialize(file)
24
+ @file = file
25
+ @empty = false
26
+ @extname = @file.extname
27
+ @name = @file.basename
28
+ @gotcha = nil
29
+ super()
30
+ end
31
+
32
+ def dir(dir_name, &block)
33
+ return if @gotcha
34
+
35
+ child = make_child dir_name
36
+ child.instance_exec(@file, &block)
37
+
38
+ if child.gotcha
39
+ @gotcha = child.gotcha
40
+ else
41
+ raise Failed if dir_name.empty? || child.empty?
42
+
43
+ gotcha_child(child)
44
+ end
45
+ rescue Failed
46
+ # Ignored
47
+ end
48
+
49
+ def group(_group_name = "", &block)
50
+ dir("", &block)
51
+ end
52
+
53
+ def empty_dir!
54
+ @empty = true
55
+ end
56
+
57
+ def after_save(method_name)
58
+ @after_save_syms << method_name
59
+ end
60
+
61
+ private
62
+
63
+ def gotcha_child(child)
64
+ @gotcha = FromTo.new(File.expand_path(@file.full_path),
65
+ File.expand_path(File.join(child.to_path, child.save_name)))
66
+ child.after_save_syms.each do |sym|
67
+ @gotcha.after_save_procs << child.method(sym)
68
+ end
69
+ end
70
+
71
+ def make_child(dir_name)
72
+ child = clone
73
+ child.to_path = File.join(@to_path, dir_name)
74
+ child.file.to_path = File.join(child.to_path, child.file.basename)
75
+ child.empty = false
76
+ child.after_save_syms = []
77
+ child
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ module Classifile
5
+ ##
6
+ # Class representing the target file
7
+ class TargetFile
8
+ attr_accessor :dirname, :full_path, :basename, :pure_basename, :atime, :ctime, :size, :extname, :to_path
9
+
10
+ def initialize(full_path)
11
+ path_name = Pathname.new(full_path)
12
+ @full_path = full_path
13
+ @dirname = dirname
14
+ @basename = path_name.basename(full_path).to_s
15
+ @pure_basename = path_name.basename(full_path).to_s.split(".")[0]
16
+ @extname = path_name.extname
17
+ end
18
+
19
+ def self.build_by_file(full_path)
20
+ fs = File.stat(full_path)
21
+ tf = TargetFile.new(full_path)
22
+ tf.atime = fs.atime
23
+ tf.ctime = fs.ctime
24
+ tf.size = fs.size
25
+ tf
26
+ end
27
+ end
28
+ end
data/lib/classifile.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "classifile/const/version"
4
+ require_relative "classifile/const/file_types"
5
+ require_relative "classifile/error/failed"
6
+ require_relative "classifile/checker/name_checker"
7
+ require_relative "classifile/checker/extension_checker"
8
+ require_relative "classifile/checker/assert_checker"
9
+ require_relative "classifile/io/file_tools"
10
+ require_relative "classifile/from_to"
11
+ require_relative "classifile/classify"
12
+ require_relative "classifile/target_file"
13
+ require_relative "classifile/state"
14
+ require_relative "classifile/execute"
15
+
16
+ # Classifile
17
+ module Classifile
18
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ dir "Images" do
4
+ image?
5
+ dir "Favorites" do
6
+ empty_dir!
7
+
8
+ dir "Dogs" do
9
+ include? "dog"
10
+ end
11
+
12
+ dir "Cats" do
13
+ dir "Kittens" do
14
+ include? "kitten"
15
+ end
16
+ include? "cat"
17
+ end
18
+ end
19
+
20
+ dir "Birds" do
21
+ include? "bird", "duck", "parrot", "cock", "rooster", "goose", "canary", "mallard", "wild duck", "sea gull",
22
+ "crow", "pheasant", "woodpecker", "peacock", "stork", "turkey", "sparrow", "hawk", "ostrich",
23
+ "swallow", "crane", "bird", "nest", "chickek", "chick", "swan", "pigeon", "lark", "owl", "flamingo",
24
+ "pelican", "penguin", "hen", "eagle"
25
+ end
26
+
27
+ dir "Others" do
28
+ end
29
+ end
30
+
31
+ dir "Movies" do
32
+ movie?
33
+ end
34
+
35
+ dir "Sounds" do
36
+ sound?
37
+ end
38
+
39
+ dir "Documents" do |file|
40
+ end_with? ".txt", ".pdf", ".doc", ".xls", ".ppt", ".docx", ".xlsx", ".pptx"
41
+ if file.atime
42
+ dir file.atime.year.to_s do
43
+ end
44
+ end
45
+ end
46
+
47
+ group "Years Zip Directory ex: /2021/abc.zip " do |file|
48
+ end_with? ".zip"
49
+ if file.atime
50
+ dir file.atime.year.to_s do
51
+ end
52
+ end
53
+ end
54
+
55
+ dir "Markdown" do |_file|
56
+ end_with? ".md"
57
+ after_save :notice
58
+
59
+ def notice
60
+ puts "Please read #{file.to_path}"
61
+ end
62
+ end
@@ -0,0 +1 @@
1
+ # Hello
File without changes
File without changes
File without changes
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classifile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kurehajime
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.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: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ description: classify file by ruby DSL.
56
+ email:
57
+ - xiidec@gmail.com
58
+ executables:
59
+ - classifile
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".idea/.gitignore"
64
+ - ".idea/classifile.iml"
65
+ - ".idea/modules.xml"
66
+ - ".idea/vcs.xml"
67
+ - ".rubocop.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/setup
74
+ - classifile.gemspec
75
+ - exe/classifile
76
+ - lib/classifile.rb
77
+ - lib/classifile/checker/assert_checker.rb
78
+ - lib/classifile/checker/extension_checker.rb
79
+ - lib/classifile/checker/name_checker.rb
80
+ - lib/classifile/classify.rb
81
+ - lib/classifile/const/file_types.rb
82
+ - lib/classifile/const/version.rb
83
+ - lib/classifile/error/failed.rb
84
+ - lib/classifile/execute.rb
85
+ - lib/classifile/from_to.rb
86
+ - lib/classifile/io/file_tools.rb
87
+ - lib/classifile/state.rb
88
+ - lib/classifile/target_file.rb
89
+ - sandbox/dsl/dsl.rb
90
+ - sandbox/from/hello.md
91
+ - sandbox/from/test.txt
92
+ - sandbox/from/xyz.txt
93
+ - sandbox/from/zzz.ini
94
+ homepage: https://github.com/kurehajime/classifile
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/kurehajime/classifile
99
+ source_code_uri: https://github.com/kurehajime/classifile
100
+ changelog_uri: https://github.com/kurehajime/classifile/blob/master/README.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 3.0.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.2.22
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: classify file by ruby DSL.
120
+ test_files: []