class Caligari::Application
This class is instantiated and used to run a single invocation of the `caligari` application.
Constants
- FLAGS
The flags to be passed to `Pathname#fnmatch?` to filter exclusions.
- HOME
The `Pathname` representation of the current user's home directory.
Public Class Methods
Instantiates a new {Application} instance.
@param paths [Array<String>]
the paths for this {Application} to {#run} on
@param options [Hash]
the hash of options to apply to this {Application}
@return [self]
the initialized {Application} instance
# File lib/caligari/application.rb, line 24 def initialize(*paths, **options) @paths = paths.map { |path| Pathname.new(path) } options.each do |key, value| instance_variable_set(:"@#{key}", value) end collect_exclusions end
Public Instance Methods
Runs Caligari
.
@return [self]
# File lib/caligari/application.rb, line 35 def run @paths.each do |path| fail "#{path} is not a valid path" unless path.directory? if @traverse Dir.chdir(path) do each_file(Pathname.new('.')) { |file| link(file) } end else each_file(path) { |file| link(file) } end end self end
Private Instance Methods
Collects the list of exclusion patterns for Caligari
from a combination of XDG
configuration directories and the given CLI exclusions.
@note Exclusion patterns are used to filter the files to act upon via the
`Pathname#fnmatch?` instance method. The actual filtering, however, takes place within {#each_file}.
@return [Array<String>]
the array of patterns to exclude
@see each_file
# File lib/caligari/application.rb, line 60 def collect_exclusions XDG::CONFIG_FILES.each do |file| config = YAML::Store.new(file) @exclude |= config.transaction { config.fetch(:exclude, []) } end @exclude end
Recursively iterates over each file in the given `path`, excluding those which match any of the exclusion patterns collected via the {#collect_exclusions} private instance method. The given `block` is executed for each file.
@param path [Pathname]
the base directory to iterate over
@yieldparam entry [Pathname]
the representation of a file
@return [Array<Pathname>]
@see collect_exclusions
# File lib/caligari/application.rb, line 80 def each_file(path, &block) path.each_child do |entry| next if @exclude.any? { |pattern| entry.fnmatch?(pattern, FLAGS) } entry.directory? ? each_file(entry, &block) : yield(entry) end end
Symbolically links the given `file` to the current user's home directory.
@param file [Pathname]
the `Pathname` representation of a file to link
@return [Boolean]
`true` if the given `file` was symbolically linked, `false` otherwise
# File lib/caligari/application.rb, line 93 def link(file) return false unless file.exist? dest = HOME + file.dirname unless dest.exist? report "path: #{dest}" dest.mkpath unless @simulate end dest += file.basename if @force && dest.file? report "rm: #{dest}" dest.delete unless @simulate end report "ln: #{dest}" if @simulate || !dest.file? if @simulate || dest.file? false else dest.make_symlink(file.expand_path) true end end
Reports the given `message` on the given `stream` if Caligari
has been configured to run in verbose or simulation mode.
@param message [#to_s]
the message to report
@param stream [#puts]
the stream to write a message to
@return [nil]
# File lib/caligari/application.rb, line 122 def report(message, stream = $stdout) return unless @verbose || @simulate stream.puts message.to_s end