class Guard::Symlink

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/guard/symlink.rb, line 8
def initialize(options = {})
  opts    = options.dup
  @ignore = opts.delete(:ignore)
  super(opts)
end

Public Instance Methods

run_on_additions(paths) click to toggle source

Called on file(s) additions that the Guard plugin watches.

@param [Array<String>] paths the changes files or paths @raise [:task_has_failed] when run_on_additions has failed @return [Object] the task result

# File lib/guard/symlink.rb, line 32
def run_on_additions(paths)
  link(paths)
end
run_on_removals(paths) click to toggle source

Called on file(s) removals that the Guard plugin watches.

@param [Array<String>] paths the changes files or paths @raise [:task_has_failed] when run_on_removals has failed @return [Object] the task result

# File lib/guard/symlink.rb, line 42
def run_on_removals(paths)
  unlink(paths)
end
start() click to toggle source

Called once when Guard starts. Please override initialize method to init stuff.

@raise [:task_has_failed] when start has failed @return [Object] the task result

# File lib/guard/symlink.rb, line 19
def start
  watchdirs.each do |directory|
    files = package_files(directory)
    link(files)
  end
end

Private Instance Methods

backup(path) click to toggle source
# File lib/guard/symlink.rb, line 155
def backup(path)
  return unless ::File.file?(path)

  backup_file = backup_name(path)

  if ::File.exist?(backup_file)
    raise "Can't link #{path}, destination and #{backup_suffix} already exist!"
  end

  ::File.rename(path, backup_file)
  ::Guard::Compat::UI.info "Created backup #{path} -> #{backup_file}"
end
backup_name(path) click to toggle source
# File lib/guard/symlink.rb, line 177
def backup_name(path)
  "#{path}#{backup_suffix}"
end
backup_restored?(path) click to toggle source
# File lib/guard/symlink.rb, line 168
def backup_restored?(path)
  backup_file = backup_name(path)
  return false unless ::File.exist?(backup_file)
  ::File.delete(path)
  ::File.rename(backup_file, path)
  ::Guard::Compat::UI.info "Removed link by restoring backup #{backup_file} -> #{path}"
  true
end
backup_suffix() click to toggle source
# File lib/guard/symlink.rb, line 181
def backup_suffix
  '.link_backup'
end
directory_empty?(directory) click to toggle source
# File lib/guard/symlink.rb, line 132
def directory_empty?(directory)
  (::Dir.entries(directory) - %w(. ..)).empty?
end
ensure_directory(dir) click to toggle source
# File lib/guard/symlink.rb, line 136
def ensure_directory(dir)
  return if ::File.directory?(dir)
  ::FileUtils.mkdir_p(dir)
end
file_removed?(path) click to toggle source
# File lib/guard/symlink.rb, line 109
def file_removed?(path)
  return true if backup_restored?(path)
  ::File.delete(path)
  # p caller
  ::Guard::Compat::UI.info "Removed link #{path}"
  true
end
package_files(directory) click to toggle source
# File lib/guard/symlink.rb, line 117
def package_files(directory)
  ::Dir.glob(directory + '/**/*').reject { |fn| ::File.directory?(fn) }
end
relative_sub_path(path_org) click to toggle source
# File lib/guard/symlink.rb, line 121
def relative_sub_path(path_org)
  path = path_org.dup
  watchdirs.each do |directory|
    directory += '/'
    next unless path.start_with?(directory)
    path.slice!(directory)
    break
  end
  path
end
remove_empty(directories) click to toggle source
# File lib/guard/symlink.rb, line 91
def remove_empty(directories)
  return if directories.empty?

  parent_directories = []
  directories.uniq.each do |directory|
    next unless ::File.directory?(directory)
    next unless directory_empty?(directory)
    ::Dir.rmdir(directory)
    ::Guard::Compat::UI.info "Removed empty directory #{directory}"

    parent_directory = ::File.dirname(directory)

    next if parent_directories.include?(parent_directory)
    parent_directories.push(parent_directory)
  end
  remove_empty(parent_directories)
end
watchdirs() click to toggle source
# File lib/guard/symlink.rb, line 185
def watchdirs
  @watchdirs ||= ::Guard.state.session.watchdirs
end