Class: Cask::Artifact::Relocated Private

Inherits:
AbstractArtifact show all
Includes:
OS::Linux::Cask::Artifact::Relocated
Defined in:
cask/artifact/relocated.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Superclass for all artifacts which have a source and a target location.

Direct Known Subclasses

Moved, Symlinked

Constant Summary

Constants inherited from AbstractArtifact

AbstractArtifact::DirectivesType

Instance Attribute Summary

Attributes inherited from AbstractArtifact

#cask

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractArtifact

#cask_sandbox, #config, dirmethod, dsl_key, english_article, english_name, read_script_arguments, #requires_sudo?, #run_cask_sandbox, #sort_order, #staged_path_join_executable, #to_args

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

#initialize(cask, source, target: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'cask/artifact/relocated.rb', line 44

def initialize(cask, source, target: nil)
  # Keep `to_args` (and so the JSON API) free of an empty target stanza.
  if target.nil?
    super(cask, source)
  else
    super(cask, source, { target: })
  end

  @source = T.let(nil, T.nilable(Pathname))
  @source_string = T.let(source.to_s, String)
  @target = T.let(nil, T.nilable(Pathname))
  @target_string = T.let(target.to_s, String)
end

Class Method Details

.from_args(cask, source_string, target_hash = nil) ⇒ T.attached_class

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (T.attached_class)


20
21
22
23
24
25
26
27
28
29
# File 'cask/artifact/relocated.rb', line 20

def self.from_args(cask, source_string, target_hash = nil)
  target = if target_hash
    raise CaskInvalidError, cask unless target_hash.is_a?(Hash)

    ::Utils::Data.assert_valid_keys(target_hash, :target)
    target_hash[:target]
  end

  new(cask, source_string, target:)
end

Instance Method Details

#add_altname_metadata(file, altname, command:) ⇒ SystemCommand::Result?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Try to make the asset searchable under the target name. Spotlight respects this attribute for many filetypes, but ignores it for App bundles. Alfred 2.2 respects it even for App bundles.

Parameters:

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'cask/artifact/relocated.rb', line 90

def (file, altname, command:)
  return if altname.to_s.casecmp(file.basename.to_s)&.zero?

  odebug "Adding #{ALT_NAME_ATTRIBUTE} metadata"
  altnames = command.run("/usr/bin/xattr",
                         args:         ["-p", ALT_NAME_ATTRIBUTE, file],
                         print_stderr: false).stdout.sub(/\A\((.*)\)\Z/, '\1')
  odebug "Existing metadata is: #{altnames}"
  altnames.concat(", ") unless altnames.empty?
  altnames.concat(%Q("#{altname}"))
  altnames = "(#{altnames})"

  # Some packages are shipped as u=rx (e.g. Bitcoin Core)
  command.run!("chmod",
               args: ["--", "u+rw", file, file.realpath],
               sudo: nil)

  command.run!("/usr/bin/xattr",
               args:         ["-w", ALT_NAME_ATTRIBUTE, altnames, file],
               print_stderr: false,
               sudo:         nil)
end

#resolve_target(target, base_dir: config.public_send(self.class.dirmethod)) ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



32
33
34
35
36
37
38
39
40
41
# File 'cask/artifact/relocated.rb', line 32

def resolve_target(target, base_dir: config.public_send(self.class.dirmethod))
  target = Pathname(target)

  if target.relative?
    return target.expand_path if target.descend.first.to_s == "~"
    return base_dir/target if base_dir
  end

  target
end

#source ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



59
60
61
62
63
64
65
66
# File 'cask/artifact/relocated.rb', line 59

def source
  @source ||= begin
    base_path = cask.staged_path
    only_path = cask.url&.only_path.presence
    base_path = base_path.join(only_path) if only_path
    base_path.join(@source_string)
  end
end

#summarize ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



81
82
83
84
# File 'cask/artifact/relocated.rb', line 81

def summarize
  target_string = @target_string.empty? ? "" : " -> #{@target_string}"
  "#{@source_string}#{target_string}"
end

#target ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



69
70
71
# File 'cask/artifact/relocated.rb', line 69

def target
  @target ||= resolve_target(@target_string.presence || source.basename)
end

#to_a ⇒ Array<T.anything>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



74
75
76
77
78
# File 'cask/artifact/relocated.rb', line 74

def to_a
  [@source_string].tap do |ary|
    ary << { target: @target_string } unless @target_string.empty?
  end
end