Class: Gitenv::Symlink
- Inherits:
-
Object
- Object
- Gitenv::Symlink
- Defined in:
- lib/gitenv/actions/symlink.rb
Defined Under Namespace
Classes: Action
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(context, file, options = {}) ⇒ Symlink
constructor
A new instance of Symlink.
- #link ⇒ Object
- #link_backup ⇒ Object
- #status ⇒ Object
- #target ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(context, file, options = {}) ⇒ Symlink
Returns a new instance of Symlink.
28 29 30 31 32 33 |
# File 'lib/gitenv/actions/symlink.rb', line 28 def initialize context, file, = {} @context, @file = context, file @as, @overwrite, @backup = [:as], [:overwrite], [:backup] @mkdir = .fetch :mkdir, true @backup = true if @overwrite && !.key?(:backup) end |
Instance Method Details
#apply ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/gitenv/actions/symlink.rb', line 35 def apply FileUtils.mkdir_p File.dirname(link) if @mkdir backup_exists = File.exist? link_backup FileUtils.mv link, link_backup if @backup && File.exist?(link) && !backup_exists FileUtils.rm link if @overwrite && File.exist?(link) && !backup_exists # TODO: only if link points somewhere else File.symlink target, link unless File.exist?(link) end |
#link ⇒ Object
71 72 73 |
# File 'lib/gitenv/actions/symlink.rb', line 71 def link @link ||= File.join(*[ @context.to, link_name].compact) end |
#link_backup ⇒ Object
75 76 77 |
# File 'lib/gitenv/actions/symlink.rb', line 75 def link_backup @link_backup ||= "#{link}.orig" end |
#status ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gitenv/actions/symlink.rb', line 47 def status backup_notice = if @backup; " backup the file and"; end if File.symlink? link current_target = File. File.readlink(link) if @overwrite == false || current_target == target Status.ok 'ok' elsif !@overwrite Status.error "exists but points to #{current_target}; enable overwrite if you want to replace it" elsif @backup && File.exist?(link_backup) Status.error "already exists with backup copy" else Status.warning "currently points to #{current_target}; apply will#{backup_notice} overwrite" end elsif File.exist? link if @overwrite Status.warning "exists but is not a symlink; apply will#{backup_notice} overwrite" else Status.error "exists but is not a symlink; apply will ignore" end else Status.missing "is not set up; apply will create the link" end end |
#target ⇒ Object
79 80 81 |
# File 'lib/gitenv/actions/symlink.rb', line 79 def target @target ||= File.join(*[ @context.from, @file ].compact) end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/gitenv/actions/symlink.rb', line 43 def to_s "#{Paint[link, :cyan]} #{Paint['->', :bold]} #{target}" end |