Class: PlexSymlinker::Operation
- Inherits:
-
Object
- Object
- PlexSymlinker::Operation
- Defined in:
- lib/plex_symlinker/operation.rb
Instance Attribute Summary collapse
-
#files_base_dir ⇒ Object
readonly
Returns the value of attribute files_base_dir.
-
#symlink_target_dir ⇒ Object
readonly
Returns the value of attribute symlink_target_dir.
-
#symlinks_base_dir ⇒ Object
readonly
Returns the value of attribute symlinks_base_dir.
Instance Method Summary collapse
- #audio_files ⇒ Object
-
#cleanup ⇒ Object
Removes all symlinks from the target folder that don’t have an existing target any more.
- #create_symlinks ⇒ Object
-
#files(dir, extensions = FileTypes::AudioFile.registered_types.keys) ⇒ Array<String>
Searches for files within the given directory and its subdirectories.
-
#initialize(files_base_dir, symlinks_base_dir, symlink_target_dir: nil) ⇒ Operation
constructor
A new instance of Operation.
- #perform ⇒ Object
- #symlinks ⇒ Object
Constructor Details
#initialize(files_base_dir, symlinks_base_dir, symlink_target_dir: nil) ⇒ Operation
Returns a new instance of Operation.
33 34 35 36 37 |
# File 'lib/plex_symlinker/operation.rb', line 33 def initialize(files_base_dir, symlinks_base_dir, symlink_target_dir: nil) @files_base_dir = files_base_dir @symlinks_base_dir = symlinks_base_dir @symlink_target_dir = symlink_target_dir.presence || @files_base_dir end |
Instance Attribute Details
#files_base_dir ⇒ Object (readonly)
Returns the value of attribute files_base_dir.
3 4 5 |
# File 'lib/plex_symlinker/operation.rb', line 3 def files_base_dir @files_base_dir end |
#symlink_target_dir ⇒ Object (readonly)
Returns the value of attribute symlink_target_dir.
3 4 5 |
# File 'lib/plex_symlinker/operation.rb', line 3 def symlink_target_dir @symlink_target_dir end |
#symlinks_base_dir ⇒ Object (readonly)
Returns the value of attribute symlinks_base_dir.
3 4 5 |
# File 'lib/plex_symlinker/operation.rb', line 3 def symlinks_base_dir @symlinks_base_dir end |
Instance Method Details
#audio_files ⇒ Object
53 54 55 |
# File 'lib/plex_symlinker/operation.rb', line 53 def audio_files files(files_base_dir).map(&FileTypes::AudioFile.method(:from_path)) end |
#cleanup ⇒ Object
Removes all symlinks from the target folder that don’t have an existing target any more
TODO: Remove empty directories as well
92 93 94 95 96 97 98 |
# File 'lib/plex_symlinker/operation.rb', line 92 def cleanup PlexSymlinker.logger.info "Removing invalid existing symlinks..." symlinks.each do |link| current_target = link.target.gsub(symlink_target_dir, files_base_dir) File.delete(link.path) unless File.exist?(current_target) end end |
#create_symlinks ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/plex_symlinker/operation.rb', line 66 def create_symlinks PlexSymlinker.logger.info "Creating new symlinks..." progress = ProgressBar.create(total: audio_files.size, output: PlexSymlinker.output) audio_files.each do |file| progress.log " --* #{file.album_artist}/#{file.album}..." FileUtils.mkdir_p(File.join(symlinks_base_dir, file.relative_symlink_dir)) path = file.path.gsub(files_base_dir, symlink_target_dir) symlink_path = File.join(symlinks_base_dir, file.relative_symlink_path) # If we already have a symlink, don't try to create it again. next if File.symlink?(symlink_path) File.symlink(path, symlink_path) progress.increment end end |
#files(dir, extensions = FileTypes::AudioFile.registered_types.keys) ⇒ Array<String>
Searches for files within the given directory and its subdirectories.
49 50 51 |
# File 'lib/plex_symlinker/operation.rb', line 49 def files(dir, extensions = FileTypes::AudioFile.registered_types.keys) Dir[File.join(dir, "**/*.{#{extensions.join(",")}}")] end |
#perform ⇒ Object
61 62 63 64 |
# File 'lib/plex_symlinker/operation.rb', line 61 def perform cleanup create_symlinks end |
#symlinks ⇒ Object
57 58 59 |
# File 'lib/plex_symlinker/operation.rb', line 57 def symlinks files(symlinks_base_dir).map(&Symlink.method(:new)) end |