Class: Dependabot::DependencyFile

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/dependency_file.rb

Defined Under Namespace

Classes: ContentEncoding, Operation

Constant Summary collapse

UTF_8_BOM =
[0xEF, 0xBB, 0xBF].pack("C*").force_encoding("UTF-8").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, content:, directory: "/", type: "file", support_file: false, symlink_target: nil, content_encoding: ContentEncoding::UTF_8, deleted: false, operation: Operation::UPDATE) ⇒ DependencyFile



24
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
# File 'lib/dependabot/dependency_file.rb', line 24

def initialize(name:, content:, directory: "/", type: "file",
               support_file: false, symlink_target: nil,
               content_encoding: ContentEncoding::UTF_8, deleted: false, operation: Operation::UPDATE)
  # Remove UTF-8 byte order mark (BOM) from content, if present
  content = content.delete_prefix(UTF_8_BOM) if content && content_encoding == ContentEncoding::UTF_8

  @name = name
  @content = content
  @directory = clean_directory(directory)
  @symlink_target = symlink_target
  @support_file = support_file
  @content_encoding = content_encoding
  @operation = operation

  # Make deleted override the operation. Deleted is kept when operation
  # was introduced to keep compatibility with downstream dependants.
  @operation = Operation::DELETE if deleted

  # Type is used *very* sparingly. It lets the git_modules updater know that
  # a "file" is actually a submodule, and lets our Go updaters know which
  # file represents the main.go.
  # New use cases should be avoided if at all possible (and use the
  # support_file flag instead)
  @type = type

  return unless (type == "symlink") ^ symlink_target

  raise "Symlinks must specify a target!" unless symlink_target
  raise "Only symlinked files must specify a target!" if symlink_target
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def content
  @content
end

#content_encodingObject

Returns the value of attribute content_encoding.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def content_encoding
  @content_encoding
end

#directoryObject

Returns the value of attribute directory.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def directory
  @directory
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def name
  @name
end

#operationObject

Returns the value of attribute operation.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def operation
  @operation
end

#support_fileObject

Returns the value of attribute support_file.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def support_file
  @support_file
end

Returns the value of attribute symlink_target.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def symlink_target
  @symlink_target
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/dependabot/dependency_file.rb', line 10

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/dependabot/dependency_file.rb', line 75

def ==(other)
  return false unless other.instance_of?(self.class)

  my_hash = to_h.reject { |k| k == "support_file" }
  their_hash = other.to_h.reject { |k| k == "support_file" }
  my_hash == their_hash
end

#binary?Boolean



107
108
109
# File 'lib/dependabot/dependency_file.rb', line 107

def binary?
  content_encoding == ContentEncoding::BASE64
end

#decoded_contentObject



111
112
113
114
115
# File 'lib/dependabot/dependency_file.rb', line 111

def decoded_content
  return Base64.decode64(content) if binary?

  content
end

#deletedObject



95
96
97
# File 'lib/dependabot/dependency_file.rb', line 95

def deleted
  @operation == Operation::DELETE
end

#deleted=(deleted) ⇒ Object



99
100
101
# File 'lib/dependabot/dependency_file.rb', line 99

def deleted=(deleted)
  @operation = deleted ? Operation::DELETE : Operation::UPDATE
end

#deleted?Boolean



103
104
105
# File 'lib/dependabot/dependency_file.rb', line 103

def deleted?
  deleted
end

#eql?(other) ⇒ Boolean



87
88
89
# File 'lib/dependabot/dependency_file.rb', line 87

def eql?(other)
  self.==(other)
end

#hashObject



83
84
85
# File 'lib/dependabot/dependency_file.rb', line 83

def hash
  to_h.hash
end

#pathObject



71
72
73
# File 'lib/dependabot/dependency_file.rb', line 71

def path
  Pathname.new(File.join(directory, name)).cleanpath.to_path
end

#support_file?Boolean



91
92
93
# File 'lib/dependabot/dependency_file.rb', line 91

def support_file?
  @support_file
end

#to_hObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dependabot/dependency_file.rb', line 55

def to_h
  details = {
    "name" => name,
    "content" => content,
    "directory" => directory,
    "type" => type,
    "support_file" => support_file,
    "content_encoding" => content_encoding,
    "deleted" => deleted,
    "operation" => operation
  }

  details["symlink_target"] = symlink_target if symlink_target
  details
end