refactor artifacts

- abstracts out common logic, much code removed
- fixes one bug/inconsistency: some artifacts failed to
  "return false" when the target exists in "preflight_checks".
- one trivial functional change: artifact type and quotation marks
  added in all user feedback (quotes needed b/c of pathnames
  containing spaces)
This commit is contained in:
Roland Walker 2014-01-03 17:08:09 -05:00
parent 0d421c432f
commit a4149be6ef
13 changed files with 80 additions and 285 deletions

View File

@ -1,6 +1,7 @@
module Cask::Artifact; end
require 'cask/artifact/base'
require 'cask/artifact/symlinked'
require 'cask/artifact/app'
require 'cask/artifact/block'

View File

@ -1,41 +1,5 @@
class Cask::Artifact::App < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:link].any?
end
def install
@cask.artifacts[:link].each { |app| link(app) }
end
def uninstall
@cask.artifacts[:link].each { |app| unlink(app) }
end
def link(app_relative_path)
source = @cask.destination_path.join(app_relative_path)
target = Cask.appdir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.directory? && !target.symlink?
ohai "It seems there is already an app at #{target}; not linking."
return false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(app_relative_path)
linked_path = Cask.appdir.join(Pathname(app_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing link: #{linked_path}"
linked_path.delete
end
class Cask::Artifact::App < Cask::Artifact::Symlinked
def self.artifact_dsl_key
:link
end
end

View File

@ -1,4 +1,29 @@
class Cask::Artifact::Base
def self.artifact_name
@artifact_name ||= self.name.sub(%r{^.*:}, '').gsub(%r{(.)([A-Z])}, '\1_\2').downcase
end
def self.artifact_english_name
@artifact_english_name ||= self.name.sub(%r{^.*:}, '').gsub(%r{(.)([A-Z])}, '\1 \2')
end
def self.artifact_english_article
@artifact_english_article ||= self.artifact_english_name.match(%r{^[aeiou]}i) ? 'an' : 'a'
end
def self.artifact_dsl_key
@artifact_dsl_key ||= self.artifact_name.to_sym
end
def self.artifact_dirmethod
@artifact_dirmethod ||= "#{self.artifact_name}dir".to_sym
end
def self.me?(cask)
cask.artifacts[self.artifact_dsl_key].any?
end
def initialize(cask, command=Cask::SystemCommand)
@cask = cask
@command = command

View File

@ -1,41 +1,2 @@
class Cask::Artifact::Colorpicker < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:colorpicker].any?
end
def install
@cask.artifacts[:colorpicker].each { |colorpicker| link(colorpicker) }
end
def uninstall
@cask.artifacts[:colorpicker].each { |colorpicker| unlink(colorpicker) }
end
def link(colorpicker_relative_path)
source = @cask.destination_path.join(colorpicker_relative_path)
target = Cask.colorpickerdir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking colorpicker #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.exist? && !target.symlink?
ohai "It seems there is already a colorpicker at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(colorpicker_relative_path)
linked_path = Cask.colorpickerdir.join(Pathname(colorpicker_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing colorpicker link: #{linked_path}"
linked_path.delete
end
end
class Cask::Artifact::Colorpicker < Cask::Artifact::Symlinked
end

View File

@ -1,41 +1,2 @@
class Cask::Artifact::Font < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:font].any?
end
def install
@cask.artifacts[:font].each { |font| link(font) }
end
def uninstall
@cask.artifacts[:font].each { |font| unlink(font) }
end
def link(font_relative_path)
source = @cask.destination_path.join(font_relative_path)
target = Cask.fontdir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking font #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.exist? && !target.symlink?
ohai "It seems there is already a font at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(font_relative_path)
linked_path = Cask.fontdir.join(Pathname(font_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing font link: #{linked_path}"
linked_path.delete
end
end
class Cask::Artifact::Font < Cask::Artifact::Symlinked
end

View File

@ -1,8 +1,4 @@
class Cask::Artifact::NestedContainer < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:nested_container].any?
end
def install
@cask.artifacts[:nested_container].each { |container| extract(container) }
end

View File

@ -1,6 +1,7 @@
class Cask::Artifact::Pkg < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:install].any?
# this class actually covers two keys, :install and :uninstall
def self.artifact_dsl_key
:install
end
def install

View File

@ -1,41 +1,5 @@
class Cask::Artifact::Prefpane < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:prefpane].any?
end
def install
@cask.artifacts[:prefpane].each { |prefpane| link(prefpane) }
end
def uninstall
@cask.artifacts[:prefpane].each { |prefpane| unlink(prefpane) }
end
def link(prefpane_relative_path)
source = @cask.destination_path.join(prefpane_relative_path)
target = Cask.prefpanedir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking prefPane #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.directory? && !target.symlink?
ohai "It seems there is already an prefpane at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(prefpane_relative_path)
linked_path = Cask.prefpanedir.join(Pathname(prefpane_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing prefPane link: #{linked_path}"
linked_path.delete
end
class Cask::Artifact::Prefpane < Cask::Artifact::Symlinked
def self.artifact_english_name
'Preference Pane'
end
end

View File

@ -1,41 +1,5 @@
class Cask::Artifact::Qlplugin < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:qlplugin].any?
end
def install
@cask.artifacts[:qlplugin].each { |qlplugin| link(qlplugin) }
end
def uninstall
@cask.artifacts[:qlplugin].each { |qlplugin| unlink(qlplugin) }
end
def link(qlplugin_relative_path)
source = @cask.destination_path.join(qlplugin_relative_path)
target = Cask.qlplugindir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking QuickLook plugin #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.directory? && !target.symlink?
ohai "It seems there is already a QuickLook plugin at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(qlplugin_relative_path)
linked_path = Cask.qlplugindir.join(Pathname(qlplugin_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing QuickLook plugin link: #{linked_path}"
linked_path.delete
end
class Cask::Artifact::Qlplugin < Cask::Artifact::Symlinked
def self.artifact_english_name
'QuickLook Plugin'
end
end

View File

@ -1,41 +1,2 @@
class Cask::Artifact::Service < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:service].any?
end
def install
@cask.artifacts[:service].each { |service| link(service) }
end
def uninstall
@cask.artifacts[:service].each { |service| unlink(service) }
end
def link(service_relative_path)
source = @cask.destination_path.join(service_relative_path)
target = Cask.servicedir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking service #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.exist? && !target.symlink?
ohai "It seems there is already a service at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(service_relative_path)
linked_path = Cask.servicedir.join(Pathname(service_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing service link: #{linked_path}"
linked_path.delete
end
end
class Cask::Artifact::Service < Cask::Artifact::Symlinked
end

View File

@ -0,0 +1,36 @@
class Cask::Artifact::Symlinked < Cask::Artifact::Base
def link(artifact_relative_path)
source = @cask.destination_path.join(artifact_relative_path)
target = Cask.send(self.class.artifact_dirmethod).join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def unlink(artifact_relative_path)
linked_path = Cask.send(self.class.artifact_dirmethod).join(Pathname(artifact_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing #{self.class.artifact_english_name} link: '#{linked_path}'"
linked_path.delete
end
end
def install
@cask.artifacts[self.class.artifact_dsl_key].each { |artifact| link(artifact) }
end
def uninstall
@cask.artifacts[self.class.artifact_dsl_key].each { |artifact| unlink(artifact) }
end
def preflight_checks(source, target)
if target.exist? && !target.symlink?
ohai "It seems there is already #{self.class.artifact_english_article} #{self.class.artifact_english_name} at '#{target}'; not linking."
return false
end
unless source.exist?
raise "it seems the symlink source is not there: '#{source}'"
end
true
end
end

View File

@ -1,41 +1,2 @@
class Cask::Artifact::Widget < Cask::Artifact::Base
def self.me?(cask)
cask.artifacts[:widget].any?
end
def install
@cask.artifacts[:widget].each { |widget| link(widget) }
end
def uninstall
@cask.artifacts[:widget].each { |widget| unlink(widget) }
end
def link(widget_relative_path)
source = @cask.destination_path.join(widget_relative_path)
target = Cask.widgetdir.join(source.basename)
return unless preflight_checks(source, target)
ohai "Linking Widget #{source.basename} to #{target}"
@command.run!('/bin/ln', :args => ['-hfs', source, target])
end
def preflight_checks(source, target)
if target.directory? && !target.symlink?
ohai "It seems there is already a Widget at #{target}; not linking."
false
end
unless source.exist?
raise "it seems the symlink source is not there: #{source}"
end
true
end
def unlink(widget_relative_path)
linked_path = Cask.widgetdir.join(Pathname(widget_relative_path).basename)
if linked_path.exist? && linked_path.symlink?
ohai "Removing Widget link: #{linked_path}"
linked_path.delete
end
end
class Cask::Artifact::Widget < Cask::Artifact::Symlinked
end

View File

@ -71,7 +71,7 @@ describe Cask::Artifact::App do
TestHelper.must_output(self, lambda {
Cask::Artifact::App.new(cask).install
}, "==> It seems there is already an app at #{Cask.appdir.join('Caffeine.app')}; not linking.")
}, "==> It seems there is already an App at '#{Cask.appdir.join('Caffeine.app')}'; not linking.")
(Cask.appdir/'Caffeine.app').wont_be :symlink?
end
@ -83,7 +83,7 @@ describe Cask::Artifact::App do
TestHelper.must_output(self, lambda {
Cask::Artifact::App.new(cask).install
}, "==> Linking Caffeine.app to #{Cask.appdir.join('Caffeine.app')}")
}, "==> Linking App 'Caffeine.app' to '#{Cask.appdir.join('Caffeine.app')}'")
File.readlink(Cask.appdir/'Caffeine.app').wont_equal '/tmp'
end