ezfile 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/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +10 -0
- data/lib/ezfile/version.rb +5 -0
- data/lib/ezfile.rb +194 -0
- data/sig/ezfile.rbs +4 -0
- metadata +59 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1a2f6fbd88171adda8e1e4d3a2003853181a4eac5c009d8c5066583a0168aa13
|
|
4
|
+
data.tar.gz: d1d569ec887eed48ec21c75f531882ba5e1f4fdb1b6fc5ee57b9a25535688934
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 73193bb2241a4843a933eb9d2c983ae80476710b9a9375e68d8d5f686c822c4e5e7635e12171094059413682b3ac0937831997f25e91172264037e798c02d4ac
|
|
7
|
+
data.tar.gz: 43e92b5cc62f6903ce91be0177331a298fd92f8ceaaf7686daf74f93ac08eaf90f7d6b2e98575214493e79ef8130f13b89d2cbafce1a602113c30f337271ee3e
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 aa
|
|
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,77 @@
|
|
|
1
|
+
# Ezfile
|
|
2
|
+
|
|
3
|
+
Ezfile is a file util more safety than another gem `FileUtils`
|
|
4
|
+
|
|
5
|
+
`copy_file` , `mainname`, `move_file`, `rename`, `remainame`
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
gem install ezfile
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require 'ezfile'
|
|
17
|
+
|
|
18
|
+
pwd = Dir.pwd
|
|
19
|
+
|
|
20
|
+
Ezfile.files("*")
|
|
21
|
+
.group_by{|fname| Ezfile.mainname}
|
|
22
|
+
.select{|mname, fs| fs.any{|f| Ezfile.extname(f) == ".ass"}}
|
|
23
|
+
.each do |fname, fs|
|
|
24
|
+
ass = File.combine(pwd, fname +".ass")
|
|
25
|
+
mp4 = File.combine(pwd, fname +".mp4")
|
|
26
|
+
outf = fname+".done.mp4"
|
|
27
|
+
system %Q(ffmpeg -i #{mp4.inspect} -vf subtitles=#{ass.inspect} -c:v h264_nvenc "./out/#{outf}")
|
|
28
|
+
end
|
|
29
|
+
```
|
|
30
|
+
Use `Ezfile.help` to get help.
|
|
31
|
+
```ruby
|
|
32
|
+
Ezfile.help
|
|
33
|
+
|
|
34
|
+
<<-EOF
|
|
35
|
+
require FileUtils and Dir, File.
|
|
36
|
+
methods:
|
|
37
|
+
MOVE :
|
|
38
|
+
::move_file file_path target_dir, mkdir: false
|
|
39
|
+
move file to target directory
|
|
40
|
+
! if directory is non-exist,
|
|
41
|
+
throw an error.
|
|
42
|
+
::move_file file_path, target_dir
|
|
43
|
+
if non-exist, mkdir
|
|
44
|
+
QUERY : its syntax like Explorer Search
|
|
45
|
+
::file_list target_dir = "*" alias ::files
|
|
46
|
+
::dir_list target_dir = "*" alias ::dirs
|
|
47
|
+
show list of files/directories, of target_dir.
|
|
48
|
+
! using Dir.glob(target_dir)
|
|
49
|
+
::glob query
|
|
50
|
+
the same as Dir.glob(query)
|
|
51
|
+
|
|
52
|
+
RENAME :
|
|
53
|
+
::rebasename_file src_file_path, new_basename
|
|
54
|
+
it change a file's basename, and ensure never move it or change its parent node.
|
|
55
|
+
|
|
56
|
+
DELETE :
|
|
57
|
+
::ensure_permanently_delete_file target_file alias ::files
|
|
58
|
+
::ensure_permanently_delete_directory_and_its_descendant target_dir alias ::files
|
|
59
|
+
EOF
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Development
|
|
63
|
+
|
|
64
|
+
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.
|
|
65
|
+
|
|
66
|
+
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).
|
|
67
|
+
|
|
68
|
+
## Contributing
|
|
69
|
+
|
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/saisui/ezfilerb. 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/saisui/ezfilerb/blob/master/CODE_OF_CONDUCT.md).
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
75
|
+
|
|
76
|
+
## Code of Conduct
|
|
77
|
+
|
data/Rakefile
ADDED
data/lib/ezfile.rb
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require_relative "Ezfile/version"
|
|
3
|
+
require 'colorize'
|
|
4
|
+
|
|
5
|
+
module Ezfile
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
# Your code goes here...
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module Ezfile
|
|
11
|
+
class << self
|
|
12
|
+
|
|
13
|
+
# == Exception ==
|
|
14
|
+
# -[x] Dest Dir has a SAME NAME node | 目的地文件夹内存在同名文件/文件夹(节点)
|
|
15
|
+
# -[x] Source File doesn't exist | 源文件不存在
|
|
16
|
+
# -[x] Dest Dir doesn't exist | 目的地文件夹不存在
|
|
17
|
+
# -[x] Path String has a space character | 路径名包含空格
|
|
18
|
+
# -[ ] Abs Path Cannot with Relative Path | 绝对路径不能与相对路径组合
|
|
19
|
+
|
|
20
|
+
# nut_a.black.you.one
|
|
21
|
+
|
|
22
|
+
def test_rebasename path, new_basename
|
|
23
|
+
File.join(File.dirname(File.expand_path(path)), new_basename + File.extname(path))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# -[x] cannot move file or change its parent node | 重命名不改变源文件父节点,不移动源文件。
|
|
27
|
+
def rebasename_file nodepath, new_basename
|
|
28
|
+
|
|
29
|
+
if new_basename.split(/[\/\\]/).size == 0
|
|
30
|
+
return throw puts " wrong ".on_light_yellow + " That's no new basename"
|
|
31
|
+
elsif new_basename.split(/[\/\\]/).size >= 2
|
|
32
|
+
return throw puts " FORBIDDEN ".light_white.on_yellow + " do not move its parent node, don't input a path"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
src_dirpath = File.dirname File.expand_path(nodepath)
|
|
36
|
+
|
|
37
|
+
unless File.exist? nodepath
|
|
38
|
+
return throw puts " ERROR ".light_white.on_red + " File " + "non-exist".red
|
|
39
|
+
end
|
|
40
|
+
new_name = new_basename + File.extname(nodepath)
|
|
41
|
+
unless Dir.entries(File.dirname nodepath).include? new_name
|
|
42
|
+
File.rename nodepath, File.join(src_dirpath, new_name)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# < means headpoint of a basename, > means lastpoint of a basename. like ^ and $ in RegEx
|
|
47
|
+
# basename <name.dot1.dot2.ext>
|
|
48
|
+
# tailname .dot1.dot2.ext> # [A-Za-z_\-\.]*$
|
|
49
|
+
# mainname <name .dot1.dot2
|
|
50
|
+
# headname <name
|
|
51
|
+
|
|
52
|
+
def tailname fname
|
|
53
|
+
Ezfile.basename(fname).match(/((\.[0-9A-Za-z\-_]+)+$)/)[0]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def headname fname
|
|
57
|
+
File.basename(fname)[0...-tailname(fname).size]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def mainname file_name
|
|
61
|
+
File.basename file_name, ".*"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def its_dir_base_ext file_path
|
|
66
|
+
[File.dirname(file_path), File.basename(file_path, ".*"), File.extname(file_path)]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def basename file_name, suffix = ""
|
|
70
|
+
File.basename file_name, suffix
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def move_file file_path, target_dir, mkdir: false, rename: false, rename_fmt: "__%C"
|
|
74
|
+
unless File.exist? file_path
|
|
75
|
+
return throw puts " ERROR ".light_white.on_red + " File " + "non-exist".red
|
|
76
|
+
end
|
|
77
|
+
file_name = File.basename file_path
|
|
78
|
+
|
|
79
|
+
# src_exist = File.exist? file_path
|
|
80
|
+
# src_is_file = File.file? file_path rescue false
|
|
81
|
+
# dest_exist = Dir.exist? target_dir
|
|
82
|
+
# dest_is_dir = File.directory? target_dir rescue false
|
|
83
|
+
# not_same_name = target_dir_entries.include? file_name rescue false
|
|
84
|
+
|
|
85
|
+
isit_should_move = false
|
|
86
|
+
|
|
87
|
+
if Dir.exist? target_dir
|
|
88
|
+
target_dir_entries = Dir.entries target_dir
|
|
89
|
+
if target_dir_entries.include? file_name
|
|
90
|
+
if rename == true
|
|
91
|
+
|
|
92
|
+
rename_count = 0
|
|
93
|
+
# nDigits = rename_fmt.count("#")
|
|
94
|
+
begin
|
|
95
|
+
rename_count += 1
|
|
96
|
+
# rename_postfix = rename_fmt.gsub(/#*/, "%0#{nDigits}d" % rename_count)
|
|
97
|
+
src_dirpath, file_basename, file_ext = its_dir_base_ext(file_path)
|
|
98
|
+
new_basename = file_basename + "__mv_rename_#{rename_count}"
|
|
99
|
+
new_name = new_basename + file_ext
|
|
100
|
+
|
|
101
|
+
new_name_file_path = File.join(src_dirpath, new_name)
|
|
102
|
+
end while target_dir_entries.include? new_name
|
|
103
|
+
|
|
104
|
+
rebasename_file(file_path, new_basename)
|
|
105
|
+
return move_file(new_name_file_path, target_dir)
|
|
106
|
+
end
|
|
107
|
+
return throw puts " ERROR ".light_white.on_red + %Q( === Destination already has a same name node! -- "#{file_name.red}" ===)
|
|
108
|
+
end
|
|
109
|
+
isit_should_move = true
|
|
110
|
+
elsif mkdir == true
|
|
111
|
+
Dir.mkdir target_dir
|
|
112
|
+
isit_should_move = true
|
|
113
|
+
else
|
|
114
|
+
return throw puts " ERROR ".light_white.on_red + " Destination Directory doesn't exist!"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
if isit_should_move
|
|
118
|
+
FileUtils.move file_path, target_dir
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def copy_file file_path, dest_file_path
|
|
123
|
+
if File.exist? dest_file_path
|
|
124
|
+
return throw puts " FORBIDDEN ".light_white.on_yellow + " There is a same name node, cannot " + " COVER ".light_white.on_red.bold + " another existen node, Because will " + " LOSE ".light_white.on_red + " the FILE."
|
|
125
|
+
end
|
|
126
|
+
FileUtils.copy_file file_path, dest_file_path
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def move_file_mkdir file_path, target_dir
|
|
130
|
+
self.move_file file_name, target_dir, mkdir: true
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def file_list dir = "*"
|
|
134
|
+
Dir.glob(dir).select{File.file? _1}
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def dir_list dir = "*"
|
|
138
|
+
Dir.glob(dir).select{File.directory? _1}
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
alias files file_list
|
|
142
|
+
alias dirs dir_list
|
|
143
|
+
alias movefile move_file
|
|
144
|
+
alias movefile_mkdir move_file_mkdir
|
|
145
|
+
alias copyfile copy_file
|
|
146
|
+
|
|
147
|
+
def glob query
|
|
148
|
+
Dir.glob(query)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def ensure_permanently_delete_file file_path
|
|
152
|
+
if File.file? file_path
|
|
153
|
+
FileUtils.remove_file file_path
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
def ensure_permanently_delete_directory_and_its_descendant dir_path
|
|
157
|
+
if File.direcotry? dir_path
|
|
158
|
+
FileUtils.remove_dir dir_path
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def help
|
|
163
|
+
puts <<-EOF
|
|
164
|
+
require #{"FileUtils".red} and #{"Dir".red}, #{"File".red}.
|
|
165
|
+
methods:
|
|
166
|
+
#{" MOVE ".bold.underline.on_red}:
|
|
167
|
+
#{"::move_file".cyan} file_path target_dir, mkdir: false
|
|
168
|
+
move file to target directory
|
|
169
|
+
! if directory is non-exist,
|
|
170
|
+
throw an error.
|
|
171
|
+
#{"::move_file ".cyan} file_path, target_dir
|
|
172
|
+
if non-exist, mkdir
|
|
173
|
+
#{" QUERY ".bold.underline.on_green}: its syntax like #{"Explorer Search".underline}
|
|
174
|
+
#{"::file_list ".cyan} target_dir = "*" alias #{"::files".cyan}
|
|
175
|
+
#{"::dir_list ".cyan} target_dir = "*" alias #{"::dirs".cyan}
|
|
176
|
+
show list of files/directories, of target_dir.
|
|
177
|
+
! using #{"Dir.glob(target_dir)".italic.underline.yellow}
|
|
178
|
+
#{"::its_dir_base_ext ".cyan} file_path
|
|
179
|
+
return an array of file's dirpath basename extname.
|
|
180
|
+
#{"::glob".cyan} query
|
|
181
|
+
the same as #{"Dir.glob(query)".italic.underline.yellow}
|
|
182
|
+
|
|
183
|
+
#{" RENAME ".bold.underline.on_yellow}:
|
|
184
|
+
#{"::rebasename_file".cyan} src_file_path, new_basename
|
|
185
|
+
it change a file's basename, and ensure never move it or change its parent node.
|
|
186
|
+
|
|
187
|
+
#{" DELETE ".bold.underline.on_red}:
|
|
188
|
+
#{"::ensure_permanently_delete_file".cyan} target_file alias #{"::files".cyan}
|
|
189
|
+
#{"::ensure_permanently_delete_directory_and_its_descendant".cyan} target_dir alias #{"::files".cyan}
|
|
190
|
+
EOF
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
end
|
|
194
|
+
end
|
data/sig/ezfile.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ezfile
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- saisui
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-11-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'More Safety than FileUtils. Like #move_file, #copy_file, #rename_file.
|
|
14
|
+
If that destination string including a space, it will be a correct node name. If
|
|
15
|
+
that destination directory has a same name file, move will throw an Error of if
|
|
16
|
+
send a keyvalue -- rename: true, it will be renamed by order Number.'
|
|
17
|
+
email:
|
|
18
|
+
- example@example.com
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- ".rspec"
|
|
24
|
+
- ".standard.yml"
|
|
25
|
+
- CHANGELOG.md
|
|
26
|
+
- LICENSE.txt
|
|
27
|
+
- README.md
|
|
28
|
+
- Rakefile
|
|
29
|
+
- lib/ezfile.rb
|
|
30
|
+
- lib/ezfile/version.rb
|
|
31
|
+
- sig/ezfile.rbs
|
|
32
|
+
homepage: https://github.com/saisui/ezfilerb
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
allowed_push_host: https://rubygems.org
|
|
37
|
+
homepage_uri: https://github.com/saisui/ezfilerb
|
|
38
|
+
source_code_uri: https://github.com/saisui/ezfilerb
|
|
39
|
+
changelog_uri: https://github.com/saisui/ezfilerb/CHANGELOG.md
|
|
40
|
+
post_install_message:
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 2.6.0
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 3.4.21
|
|
56
|
+
signing_key:
|
|
57
|
+
specification_version: 4
|
|
58
|
+
summary: A filetool for windows, It's more safety
|
|
59
|
+
test_files: []
|