class MasterDelivery::MasterDelivery

File delivery class

  1. Move the current active files to backup/

  2. Place a symbolic link to the master (or copy of master) in the appropriate directory

Attributes

backup_root[R]

Public Class Methods

new(master_root, backup_root = '') click to toggle source

@param master_root [String] Path to the dir including master dirs. @param backup_root [String] Path to the dir in which backup masters are created.

# File lib/master_delivery.rb, line 30
def initialize(master_root, backup_root = '')
  @master_root = File.expand_path(master_root)
  @backup_root = if backup_root.nil? || backup_root.empty?
                   File.expand_path(master_root + '/backup')
                 else
                   File.expand_path(backup_root)
                 end
end

Public Instance Methods

confirm(basics, params) click to toggle source

@param params [Hash] :type, :dryrun, :yes, :quiet

# File lib/master_delivery.rb, line 76
def confirm(basics, params)
  unless params[:quiet]
    puts MSG_CONFIRMATION_INTRO unless params[:yes]
    print_params(basics, params.slice(:type, :dryrun))
    print_sample(basics)
  end
  print MSG_CONFIRMATION.chomp unless params[:yes] # use print instead of puts for '\n'
  return true if params[:yes] || gets.chomp == 'y'

  puts 'aborted.'
  false
end
deliver(basics, type: :symbolic_link, dryrun: false, verbose: false) click to toggle source

@param master_id [String] Top directory name of master

@param delivery_root [String] Files will be delivered to this prefix location.

If this prefix is empty, it will be placed in the root directory.
This mechanism saves you from having to unnecessarily deepen the directory
hierarchy under master_id.

Example of prefix
  master_id: MID
  master: $master_root/MID/a/b/readme.md
  delivery_root: /Users/xxx/yyy
  delivery: /Users/xxx/yyy/a/b/readme.md

Example of no-prefix
  master_id: MID
  master: $master_root/MID/a/b/readme.md
  delivery_root: (empty)
  delivery: /a/b/readme.md

@param type [symbol] only link (:symbolic_link) or copy master (:regular_file) @param dryrun [boolean] if set this false, FileUtils::DryRun will be used. note: Even if dryrun: true, @backup_dir is actually created! (for name-consistency)

# File lib/master_delivery.rb, line 61
def deliver(basics, type: :symbolic_link, dryrun: false, verbose: false)
  FileUtils.mkdir_p(@backup_root)
  utils = dryrun ? FileUtils::DryRun : FileUtils

  backup_dir = Dir.mktmpdir("#{basics[:master_id]}-original-", @backup_root)
  puts "mkdir -p #{backup_dir}" if verbose
  mfiles = master_files(basics[:master_id])
  mfiles.each do |master|
    tfile = move_to_backup(master, utils, basics, backup_dir, verbose)
    deliver_to_target(master, utils, tfile, type, verbose)
  end
  [mfiles, backup_dir]
end

Private Instance Methods

backup_file_path(master, master_id, backup_dir) click to toggle source
# File lib/master_delivery.rb, line 152
def backup_file_path(master, master_id, backup_dir)
  path = File.expand_path(backup_dir) + relative_master_path(master, master_id)
  Pathname.new(path).cleanpath.to_s
end
deliver_to_target(master, utils, tfile, type, verbose) click to toggle source

Deliver a link to a master (or a copy of a master) in the appropriate directory

# File lib/master_delivery.rb, line 101
def deliver_to_target(master, utils, tfile, type, verbose)
  tfiledir = File.dirname(tfile)
  utils.mkdir_p(tfiledir, verbose: verbose)
  case type
  when :symbolic_link
    utils.ln_s(master, tfiledir, verbose: verbose)
  when :regular_file
    utils.cp(master, tfiledir, verbose: verbose)
  end
end
master_files(master_id) click to toggle source
# File lib/master_delivery.rb, line 137
def master_files(master_id)
  Find.find("#{@master_root}/#{master_id}").select do |m|
    # Reject symbolic links.
    # Some "file?" and "symlink?" mothods do not work as requested
    # because they are determined after following a symbolic link.
    # "link.File.lstat" method does not follow symbolic links,
    # so you can check if it is a symbolic or not.
    File.lstat(m).file?
  end
end
move_to_backup(master, utils, basics, backup_dir, verbose) click to toggle source

Move a master file currently used to backup/

# File lib/master_delivery.rb, line 92
def move_to_backup(master, utils, basics, backup_dir, verbose)
  backupfiledir = File.dirname(backup_file_path(master, basics[:master_id], backup_dir))
  utils.mkdir_p(backupfiledir, verbose: verbose)
  tfile = target_file_path(master, basics)
  utils.mv(tfile, backupfiledir, force: true, verbose: verbose)
  tfile
end
print_params(basics, params) click to toggle source

@param params [Hash] :type, :dryrun

print_sample(basics) click to toggle source
relative_master_path(master, master_id) click to toggle source
# File lib/master_delivery.rb, line 148
def relative_master_path(master, master_id)
  File.expand_path(master).delete_prefix("#{@master_root}/#{master_id}")
end
target_file_path(master, basics) click to toggle source
# File lib/master_delivery.rb, line 157
def target_file_path(master, basics)
  path = relative_master_path(master, basics[:master_id]).prepend(File.expand_path(basics[:delivery_root]))
  Pathname.new(path).cleanpath.to_s
end