Class: Cask::DSL::Rename Private

Inherits:
Object show all
Defined in:
cask/dsl/rename.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.

Class corresponding to the rename stanza.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ 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:



12
13
14
15
16
17
18
19
20
21
22
# File 'cask/dsl/rename.rb', line 12

def initialize(from, to)
  [from, to].each do |path|
    path = Pathname(path)
    if path.absolute? || path.each_filename.any?("..")
      raise ArgumentError, "'rename' requires paths within the staged cask"
    end
  end

  @from = from
  @to = to
end

Instance Attribute Details

#from ⇒ String (readonly)

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:



9
10
11
# File 'cask/dsl/rename.rb', line 9

def from
  @from
end

#to ⇒ String (readonly)

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:



9
10
11
# File 'cask/dsl/rename.rb', line 9

def to
  @to
end

Instance Method Details

#pairs ⇒ Hash{Symbol => 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:



63
64
65
# File 'cask/dsl/rename.rb', line 63

def pairs
  { from:, to: }
end

#perform_rename(staged_path) ⇒ 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.

This method returns an undefined value.

Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'cask/dsl/rename.rb', line 25

def perform_rename(staged_path)
  return unless staged_path.exist?

  # Find files matching the glob pattern
  matching_files = if @from.include?("*")
    staged_path.glob(@from)
  else
    [staged_path.join(@from)].select(&:exist?)
  end

  return if matching_files.empty?

  # Rename the first matching file to the target path
  source_file = matching_files.first
  return if source_file.nil?

  if source_file.relative_path_from(staged_path).each_filename.any?("..")
    raise ArgumentError, "'rename' requires paths within the staged cask"
  end

  target_file = staged_path.join(@to)

  [source_file, target_file].each do |path|
    path.dirname.ascend do |parent|
      break if parent == staged_path

      raise ArgumentError, "'rename' path contains a symlink" if parent.symlink?
    end
  end

  # Ensure target directory exists
  target_file.dirname.mkpath

  # Perform the rename
  source_file.rename(target_file.to_s) if source_file.exist?
end