93 lines
2.5 KiB
Ruby
Raw Normal View History

2016-08-18 22:11:42 +03:00
require "hbc/artifact/relocated"
2016-09-24 13:52:43 +02:00
module Hbc
module Artifact
class Moved < Relocated
def self.english_description
"#{artifact_english_name}s"
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def install_phase
each_artifact do |artifact|
load_specification(artifact)
next unless preflight_checks
delete if Utils.path_occupied?(target) && force
move
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def uninstall_phase
each_artifact do |artifact|
load_specification(artifact)
next unless File.exist?(target)
delete
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
private
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def each_artifact
# the sort is for predictability between Ruby versions
@cask.artifacts[self.class.artifact_dsl_key].sort.each do |artifact|
yield artifact
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def move
ohai "Moving #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'"
target.dirname.mkpath
FileUtils.move(source, target)
add_altname_metadata target, source.basename.to_s
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def preflight_checks
if Utils.path_occupied?(target)
if force
ohai(warning_target_exists { |s| s << "overwriting." })
else
ohai(warning_target_exists { |s| s << "not moving." })
return false
end
end
unless source.exist?
message = "It seems the #{self.class.artifact_english_name} source is not there: '#{source}'"
raise CaskError, message
end
true
2016-08-18 22:11:42 +03:00
end
2016-09-24 13:52:43 +02:00
def warning_target_exists
message_parts = [
"It seems there is already #{self.class.artifact_english_article} #{self.class.artifact_english_name} at '#{target}'",
]
yield(message_parts) if block_given?
message_parts.join("; ")
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def delete
ohai "Removing #{self.class.artifact_english_name}: '#{target}'"
raise CaskError, "Cannot remove undeletable #{self.class.artifact_english_name}" if MacOS.undeletable?(target)
2016-09-20 15:11:33 +02:00
2016-09-24 13:52:43 +02:00
if force
Utils.gain_permissions_remove(target, command: @command)
else
target.rmtree
end
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def summarize_artifact(artifact_spec)
load_specification artifact_spec
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
if target.exist?
target_abv = " (#{target.abv})"
else
warning = "Missing #{self.class.artifact_english_name}"
warning = "#{Tty.red}#{warning}#{Tty.reset}: "
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
"#{warning}#{printable_target}#{target_abv}"
end
end
2016-08-18 22:11:42 +03:00
end
end