Class: Command::Clean

Inherits:
CommandBase show all
Defined in:
lib/command/clean.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

#execute!, execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeClean

Returns a new instance of Clean.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/command/clean.rb', line 14

def initialize
  super("[<target> ...] [options]")
  @opt.separator <<-EOS

  ・サブタイトルの変更等により参照されなくなったゴミファイルを削除します。
  ・対象小説を指定しなかった場合は直前に変換した小説について検査します。

  Examples:
narou clean          # 直前に変換した小説について検査
narou clean 6
narou clean 6 -f     # 実際に削除
narou clean --all    # 全小説について検査
narou clean -af      # 全小説について検査して、実際に削除
  Options:
  EOS

  @opt.on("-f", "--force", "指定した小説のゴミファイルを実際に削除する") {
    @options["force"] = true
  }
  @opt.on("-n", "--dry-run", "指定した小説のゴミファイルを表示する") {
    @options["force"] = false
  }
  @opt.on("-a", "--all", "全小説を検査する") {
    @options["all"] = true
  }
end

Class Method Details

.oneline_helpObject



10
11
12
# File 'lib/command/clean.rb', line 10

def self.oneline_help
  "ゴミファイルを削除します"
end

Instance Method Details

#clean_all_directories(is_remove) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/command/clean.rb', line 70

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/command/clean.rb', line 78

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/command/clean.rb', line 41

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/command/clean.rb', line 89

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