class ConfigmonkeyCli::Application::ManifestAction::SyncLinks

Public Instance Methods

destructive() click to toggle source
# File lib/configmonkey_cli/application/manifest_actions/sync_links.rb, line 43
def destructive
  prefixed_sources = @sources.map do |src|
    File.join(@destination, "#{@prefix}#{File.basename(src)}")
  end.reject{|f| excluded?(f) }

  if @purge
    (Dir["#{File.join(@destination, @prefix)}*"] - prefixed_sources).each do |f|
      thor.remove_file(f)
      # @todo fix for https://github.com/erikhuda/thor/pull/720
      ::FileUtils.rm_rf(f) if File.symlink?(f)
    end
  end

  @sources.each do |src|
    if r = excluded?(src)
      status :excluded, :black, rel(src) << c(" #{r.inspect}", :black)
    else
      thor.create_link("#{@destination}/#{@prefix}#{File.basename(src)}", src, @opts)
    end
  end
end
excluded?(src) click to toggle source
# File lib/configmonkey_cli/application/manifest_actions/sync_links.rb, line 65
def excluded? src
  [*@opts[:exclude]].detect do |filter|
    case filter
    when Proc
      filter.call(src)
    when Regexp
      src.match(filter)
    when String
      src.ends_with?(filter)
    end
  end
end
init(hargs_and_opts = {}) click to toggle source
# File lib/configmonkey_cli/application/manifest_actions/sync_links.rb, line 5
def init hargs_and_opts = {}
  @args, @opts = args_and_opts(hargs_and_opts)
  @opts = @opts.reverse_merge({
    prefix: nil,
    map: "d:dp",
    hard: false,
    exclude: [],
  })
end
prepare() click to toggle source
# File lib/configmonkey_cli/application/manifest_actions/sync_links.rb, line 15
def prepare
  @opts[:force] = app.opts[:default_yes]
  @opts[:symbolic] = !@opts[:hard]

  #-----------------
  @source = @args[0]
  @destination = @args[1]
  map = @opts[:map].split(":")

  # prefix source
  @source = File.join(map[0]["d"] ? thor.destination_root : manifest.directory, @source)
  @destination = File.join(map[1]["d"] ? thor.destination_root : manifest.directory, @destination)

  @sources = Dir[@source]
  @sources = Dir["#{@sources[0]}/*"] if @sources.length == 1 && FileTest.directory?(@sources[0])

  # prefix target link
  if(@opts[:prefix] && map[1].downcase["p"])
    @prefix = "cm--#{manifest.checksum(@opts[:prefix], soft: !map[1]["H"])}--"
    @purge = map[1]["P"]
  end
end
simulate() click to toggle source
# File lib/configmonkey_cli/application/manifest_actions/sync_links.rb, line 38
def simulate
  status :fake, :black, rel(@destination)
  destructive if thor.options[:pretend]
end