From 5d0957ac81029f1b583112e3567435d9c2b47583 Mon Sep 17 00:00:00 2001 From: miccal Date: Sun, 30 Aug 2020 10:44:20 +0800 Subject: [PATCH] cask/artifact/symlinked: allow --force to overwrite symbolic links --- Library/Homebrew/cask/artifact/symlinked.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/cask/artifact/symlinked.rb b/Library/Homebrew/cask/artifact/symlinked.rb index fd40619fad..1feeda027a 100644 --- a/Library/Homebrew/cask/artifact/symlinked.rb +++ b/Library/Homebrew/cask/artifact/symlinked.rb @@ -40,7 +40,7 @@ module Cask private - def link(**options) + def link(force: false, **options) unless source.exist? raise CaskError, "It seems the #{self.class.link_type_english_name.downcase} " \ @@ -48,9 +48,16 @@ module Cask end if target.exist? - raise CaskError, - "It seems there already exists #{self.class.english_article} " \ - "#{self.class.english_name} at '#{target}'; not overwriting." + message = "It seems there is already #{self.class.english_article} " \ + "#{self.class.english_name} at '#{target}'" + + if force && target.symlink? && \ + (target.realpath == source.realpath || target.realpath.to_s.start_with?("#{cask.caskroom_path}/")) + opoo "#{message}; overwriting." + target.delete + else + raise CaskError, "#{message}." + end end ohai "Linking #{self.class.english_name} '#{source.basename}' to '#{target}'."