property :name, String, regex: /^[\w-]+$/, name_property: true
property :options, String

action :install do
  execute "installing cask #{new_resource.name}" do
    command "/usr/local/bin/brew cask install #{new_resource.name} #{new_resource.options}"
    user Homebrew.owner
    environment lazy { { 'HOME' => ::Dir.home(Homebrew.owner), 'USER' => Homebrew.owner } }
    not_if { casked? }
  end
end

action :uninstall do
  execute "uninstalling cask #{new_resource.name}" do
    command "/usr/local/bin/brew cask uninstall #{new_resource.name}"
    user Homebrew.owner
    environment lazy { { 'HOME' => ::Dir.home(Homebrew.owner), 'USER' => Homebrew.owner } }
    only_if { casked? }
  end
end

action_class do
  alias_method :action_cask, :action_install
  alias_method :action_uncask, :action_uninstall

  def casked?
    shell_out('/usr/local/bin/brew cask list 2>/dev/null', user: Homebrew.owner).stdout.split.include?(name)
  end
end

property :name, String, regex: %r(^[\w/-]+$), name_property: true
property :options, String

action :install do
  execute "installing cask #{new_resource.name}" do
    command "/usr/local/bin/brew cask install #{new_resource.name} #{new_resource.options}"
    user Homebrew.owner
    environment lazy { { 'HOME' => ::Dir.home(Homebrew.owner), 'USER' => Homebrew.owner } }
    not_if { casked? }
  end
end

action :uninstall do
  execute "uninstalling cask #{new_resource.name}" do
    command "/usr/local/bin/brew cask uninstall #{new_resource.name}"
    user Homebrew.owner
    environment lazy { { 'HOME' => ::Dir.home(Homebrew.owner), 'USER' => Homebrew.owner } }
    only_if { casked? }
  end
end

action_class do
  alias_method :action_cask, :action_install
  alias_method :action_uncask, :action_uninstall

  def casked?
    unscoped_name = name.split('/').last
    shell_out('/usr/local/bin/brew cask list 2>/dev/null', user: Homebrew.owner).stdout.split.include?(unscoped_name)
  end
end

Allow Cask name to be scoped to tap
Allow Cask name to be scoped to tap

If multiple taps include the same cask name, then the only way to
install/uninstall is by specifying the name scoped with the tap name
(e.g. myuser/mytap/mycask). However, `brew cask list` only shows the
unscoped name.

Signed-off-by: Andrew Marshall <02e0a999c50b1f88df7a8f5a04e1b76b35ea6a88@johnandrewmarshall.com>
