Class: Sq::Refinement::Changeset::FileModification

Inherits:
Object
  • Object
show all
Defined in:
lib/sq/refinement/changeset/file_modification.rb

Overview

Represents a modification to a single file or directory on disk

Constant Summary collapse

DIRECTORY_CHANGE_TYPE =

Returns the change type for directories.

Returns:

  • (Symbol)

    the change type for directories

:'had contents change'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, type:, prior_path: nil, contents_reader: -> {}, prior_contents_reader: -> {}) ⇒ FileModification

Returns a new instance of FileModification.



22
23
24
25
26
27
28
29
30
31
# File 'lib/sq/refinement/changeset/file_modification.rb', line 22

def initialize(path:, type:,
              prior_path: nil,
              contents_reader: -> {},
              prior_contents_reader: -> {})
  @path = path
  @type = type
  @prior_path = prior_path
  @contents_reader = contents_reader
  @prior_contents_reader = prior_contents_reader
end

Instance Attribute Details

#pathPathname (readonly)

Returns the path to the modified file.

Returns:

  • (Pathname)

    the path to the modified file



14
15
16
# File 'lib/sq/refinement/changeset/file_modification.rb', line 14

def path
  @path
end

#prior_pathPathname, Nil (readonly)

Returns the prior path to the modified file, or ‘nil` if it was not renamed or copied.

Returns:

  • (Pathname, Nil)

    the prior path to the modified file, or ‘nil` if it was not renamed or copied



17
18
19
# File 'lib/sq/refinement/changeset/file_modification.rb', line 17

def prior_path
  @prior_path
end

#type#to_s (readonly)

Returns the type of change that happened to this file.

Returns:

  • (#to_s)

    the type of change that happened to this file



20
21
22
# File 'lib/sq/refinement/changeset/file_modification.rb', line 20

def type
  @type
end

Instance Method Details

#contentsString

Returns the current contents of the file.

Returns:

  • (String)

    the current contents of the file



123
124
125
126
127
128
129
130
# File 'lib/sq/refinement/changeset/file_modification.rb', line 123

def contents
  @contents ||=
    begin
      @contents_reader[].tap { @contents_reader = nil } || DOES_NOT_EXIST
    rescue StandardError
      DOES_NOT_EXIST
    end
end

#prior_contentsString

Returns the prior contents of the file.

Returns:

  • (String)

    the prior contents of the file



133
134
135
136
137
138
139
140
# File 'lib/sq/refinement/changeset/file_modification.rb', line 133

def prior_contents
  @prior_contents ||=
    begin
      @prior_contents_reader[].tap { @prior_contents_reader = nil } || DOES_NOT_EXIST
    rescue StandardError
      DOES_NOT_EXIST
    end
end

#yaml_diff(keypath) ⇒ String, Nil

Returns a YAML string representing the diff of the file from the prior revision to the current revision at the given keypath in the YAML, or ‘nil` if there is no diff.

Parameters:

  • keypath (Array)

    a list of indices passed to ‘dig`. An empty array is equivalent to the entire YAML document

Returns:

  • (String, Nil)

    a YAML string representing the diff of the file from the prior revision to the current revision at the given keypath in the YAML, or ‘nil` if there is no diff



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sq/refinement/changeset/file_modification.rb', line 75

def yaml_diff(keypath)
  require 'yaml'

  @cached_yaml ||= {}

  dig_yaml = lambda do |yaml, path|
    return yaml if yaml == DOES_NOT_EXIST

    object = @cached_yaml[path] ||= YAML.safe_load(yaml, permitted_classes: [Symbol], aliases: true)
    if keypath.empty?
      object
    elsif object.respond_to?(:dig)
      object.dig(*keypath)
    else # backwards compatibility
      keypath.reduce(object) do |acc, elem|
        acc[elem]
      end
    end
  end

  prior = dig_yaml[prior_contents, :prior]
  current = dig_yaml[contents, :current]

  require 'xcodeproj/differ'

  # rubocop:disable Naming/VariableNumber
  return unless (diff = Xcodeproj::Differ.diff(
    prior,
    current,
    key_1: 'prior_revision',
    key_2: 'current_revision'
  ))
  # rubocop:enable Naming/VariableNumber

  diff.to_yaml.prepend("#{path} changed at keypath #{keypath.inspect}\n")
end