require "agile_utils" require "code_lister" require "fileutils" require_relative "../ebook_renamer" module EbookRenamer class CLI < Thor desc "rename", "Rename multiple ebook files (pdf,epub,mobi) from a given directory" method_option *AgileUtils::Options::BASE_DIR method_option *AgileUtils::Options::RECURSIVE method_option :sep_string, aliases: "-s", desc: "Separator string between words in output filename", default: "." method_option :commit, aliases: "-c", desc: "Make change permanent", type: :boolean, default: false method_option *AgileUtils::Options::VERSION def rename opts = options.symbolize_keys # Explicitly add the :exts options opts[:exts] = %w[pdf epub mobi] if opts[:version] puts "You are using EbookRenamer version #{EbookRenamer::VERSION}" exit end execute(opts) end desc "usage", "Display help screen" def usage puts <<-EOS Usage: ebook_renamer Options: -b, [--base-dir=BASE_DIR] # Base directory # Default: . (current directory) -r, [--recursive], [--no-recursive] # Search for files recursively # Default: true -s, [--sep-string=SEP_STRING] # Separator string between words in output filename # Default: . -c, [--commit], [--no-commit] # Make change permanent -v, [--version], [--no-version] # Display version information Rename multiple ebook files (pdf,epub,mobi) from a given directory EOS end default_task :usage private # Rename the file from the given directory # Using the with the argurment options as follow # # :base_dir - the base directory # :recursive - perform the rename recursively (true|false) # :commit - make the rename permanent (true|false) # :exts - list of extensions to be processed default to ['epub,mobi,pdf'] # # @param args [Hash] options argument def execute(options = {}) # meta_binary = "ebook-meta" input_files = CodeLister.files(options) if input_files.empty? puts "No file found for your option: #{options}" return end unless options[:commit] puts "-----------------------------------------------------------" puts " FYI: no changes as this is a dry-run, please use --commit " puts "-----------------------------------------------------------" end FileUtils.chdir(options[:base_dir]) input_files.each_with_index do |file, index| extension = File.extname(file) begin meta_binary = "ebook-meta" metadata = meta(file, meta_binary) hash = meta_to_hash(metadata) # Skip the process, if we don't have the title if hash["title"] == File.basename(file, ".*") puts "#{index + 1} of #{input_files.size} skip name: #{File.expand_path(file)} [no title found]" next end old_name = File.expand_path(file) full_name = formatted_name(hash, sep_char: " by ") new_name = "#{File.dirname(file)}/#{sanitize_filename(full_name, options[:sep_string])}#{extension}" new_name = File.expand_path(new_name) if old_name != new_name puts "#{index + 1} of #{input_files.size} old name : #{old_name}" puts "#{index + 1} of #{input_files.size} new name : #{new_name}" FileUtils.mv(old_name, new_name) if options[:commit] else puts "#{index + 1} of #{input_files.size} skip name: #{old_name}" end rescue RuntimeError => e puts e.backtrace next end end end end end