Class: ConfigmonkeyCli::Application::ManifestAction::SyncLinks

Inherits:
Base
  • Object
show all
Defined in:
lib/configmonkey_cli/application/manifest_actions/sync_links.rb

Instance Attribute Summary

Attributes inherited from Base

#app, #args, #manifest, #opts, #thor

Instance Method Summary collapse

Methods inherited from Base

#args_and_opts, #exists?, #expand_dst, #expand_src, #initialize, #rel, #to_s

Constructor Details

This class inherits a constructor from ConfigmonkeyCli::Application::ManifestAction::Base

Instance Method Details

#destructiveObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
# 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 = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# 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

#prepareObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

#simulateObject



38
39
40
41
# 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