Sha256: 4d4097e4db73bad779b4b9483c2b9e59aa989a3c17048ffcb053ee4172c81bd3

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

class Caco::FileLink < Trailblazer::Operation
  pass :link_exist?
  pass :target_exist?
  step :link_same_target?,
    Output(:success) => End(:success),
    Output(:failure) => Track(:success)
  step :ensure_target!
  step :create_link!

  def link_exist?(ctx, link:, **)
    ctx[:link_exist] = !!File.lstat(link) rescue false
    ctx[:link_realpath] = File.realdirpath(link) rescue false
    ctx[:link_exist]
  end

  def target_exist?(ctx, target:, **)
    ctx[:target_exist] = File.exist?(target)
    ctx[:target_realpath] = File.realdirpath(target) rescue nil
    ctx[:target_exist]
  end

  def link_same_target?(ctx, target_realpath:, link_realpath:, **)
    ctx[:link_same_target] = (target_realpath == link_realpath)
  end

  def ensure_target!(ctx, target_exist:, ensure_target: false, **)
    return false if !target_exist && ensure_target
    true
  end

  def create_link!(ctx, target:, link:, link_exist:, link_same_target:, **)
    ctx[:force] = (link_exist && !link_same_target)
    FileUtils.ln_s target, link, force: ctx[:force]
    ctx[:link_created] = true
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
caco-0.1.0 lib/caco/file_link.rb