Class: Command::Clean
Instance Attribute Summary
Attributes inherited from CommandBase
#stream_io
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Clean
Returns a new instance of Clean.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/command/clean.rb', line 18
def initialize
super("[<target> ...] [options]")
@opt.separator "\n \u30FB\u30B5\u30D6\u30BF\u30A4\u30C8\u30EB\u306E\u5909\u66F4\u7B49\u306B\u3088\u308A\u53C2\u7167\u3055\u308C\u306A\u304F\u306A\u3063\u305F\u30B4\u30DF\u30D5\u30A1\u30A4\u30EB\u3092\u524A\u9664\u3057\u307E\u3059\u3002\n \u30FB\u5BFE\u8C61\u5C0F\u8AAC\u3092\u6307\u5B9A\u3057\u306A\u304B\u3063\u305F\u5834\u5408\u306F\u76F4\u524D\u306B\u5909\u63DB\u3057\u305F\u5C0F\u8AAC\u306B\u3064\u3044\u3066\u691C\u67FB\u3057\u307E\u3059\u3002\n\n Examples:\nnarou clean # \u76F4\u524D\u306B\u5909\u63DB\u3057\u305F\u5C0F\u8AAC\u306B\u3064\u3044\u3066\u691C\u67FB\nnarou clean 6\nnarou clean 6 -f # \u5B9F\u969B\u306B\u524A\u9664\nnarou clean --all # \u5168\u5C0F\u8AAC\u306B\u3064\u3044\u3066\u691C\u67FB\nnarou clean -af # \u5168\u5C0F\u8AAC\u306B\u3064\u3044\u3066\u691C\u67FB\u3057\u3066\u3001\u5B9F\u969B\u306B\u524A\u9664\n Options:\n EOS\n\n @opt.on(\"-f\", \"--force\", \"\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u30B4\u30DF\u30D5\u30A1\u30A4\u30EB\u3092\u5B9F\u969B\u306B\u524A\u9664\u3059\u308B\") {\n @options[\"force\"] = true\n }\n @opt.on(\"-n\", \"--dry-run\", \"\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u30B4\u30DF\u30D5\u30A1\u30A4\u30EB\u3092\u8868\u793A\u3059\u308B\") {\n @options[\"force\"] = false\n }\n @opt.on(\"-a\", \"--all\", \"\u5168\u5C0F\u8AAC\u3092\u691C\u67FB\u3059\u308B\") {\n @options[\"all\"] = true\n }\nend\n"
|
Class Method Details
.oneline_help ⇒ Object
14
15
16
|
# File 'lib/command/clean.rb', line 14
def self.oneline_help
"ゴミファイルを削除します"
end
|
Instance Method Details
#clean_all_directories(is_remove) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/command/clean.rb', line 74
def clean_all_directories(is_remove)
Database.instance.each_key do |id|
next if Narou.novel_frozen?(id)
dir = Downloader.get_novel_data_dir_by_target(id)
clean_directory(dir, is_remove)
end
end
|
#clean_directory(dir, is_remove) ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/command/clean.rb', line 82
def clean_directory(dir, is_remove)
return unless dir
return unless File.directory?(dir)
return unless File.exist?(File.join(dir, Downloader::TOC_FILE_NAME))
orphans = find_orphans(dir)
orphans.each do |path|
puts path
FileUtils.remove_entry_secure(path) if is_remove
end
end
|
#execute(argv) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/command/clean.rb', line 45
def execute(argv)
super
if @options["all"]
clean_all_directories(@options["force"])
return
end
if argv.empty?
latest_id = Inventory.load("latest_convert")["id"]
if latest_id
dir = Downloader.get_novel_data_dir_by_target(latest_id)
if dir
clean_directory(dir, @options["force"])
else
error "#{latest_id} は存在しません"
end
end
return
end
tagname_to_ids(argv)
argv.each do |target|
dir = Downloader.get_novel_data_dir_by_target(target)
if dir
clean_directory(dir, @options["force"])
else
error "#{target} は存在しません"
end
end
end
|
#find_orphans(dir) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/command/clean.rb', line 93
def find_orphans(dir)
orphans = []
toc = Downloader.get_toc_data(dir)
items = toc["subtitles"].map { |item| "#{item['index']} #{item['file_subtitle']}" }
Dir.glob(File.join(dir, Downloader::RAW_DATA_DIR_NAME, "*.txt")).each do |path|
orphans.push(path) unless items.include?(File.basename(path, ".*"))
end
Dir.glob(File.join(dir, Downloader::SECTION_SAVE_DIR_NAME, "*.yaml")).each do |path|
orphans.push(path) unless items.include?(File.basename(path, ".*"))
end
orphans
end
|