Class: Dependabot::Nuget::NativeDiscoveryJsonReader

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discovery_json:) ⇒ NativeDiscoveryJsonReader

Returns a new instance of NativeDiscoveryJsonReader.



132
133
134
135
136
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 132

def initialize(discovery_json:)
  @discovery_json = discovery_json
  @workspace_discovery = T.let(read_workspace_discovery, T.nilable(Dependabot::Nuget::NativeWorkspaceDiscovery))
  @dependency_set = T.let(read_dependency_set, Dependabot::FileParsers::Base::DependencySet)
end

Instance Attribute Details

#dependency_setObject (readonly)

Returns the value of attribute dependency_set.



129
130
131
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 129

def dependency_set
  @dependency_set
end

#workspace_discoveryObject (readonly)

Returns the value of attribute workspace_discovery.



126
127
128
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 126

def workspace_discovery
  @workspace_discovery
end

Class Method Details

.clear_discovery_file_path_from_cache(dependency_files) ⇒ Object



88
89
90
91
92
93
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 88

def self.clear_discovery_file_path_from_cache(dependency_files)
  key = create_cache_key(dependency_files)
  discovery_file_path = discovery_path_cache[key]
  File.delete(discovery_file_path) if discovery_file_path && File.exist?(discovery_file_path)
  discovery_path_cache.delete(key)
end

.create_cache_key(dependency_files) ⇒ Object



100
101
102
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 100

def self.create_cache_key(dependency_files)
  dependency_files.map { |d| d.to_h.except("content") }.to_s
end

.create_discovery_file_path_from_dependency_files(dependency_files) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 66

def self.create_discovery_file_path_from_dependency_files(dependency_files)
  discovery_key = create_cache_key(dependency_files)
  if discovery_path_cache[discovery_key]
    raise "Discovery file path already exists for the given dependency files: #{discovery_key}"
  end

  discovery_counter_cache = T.let(CacheManager.cache("discovery_counter_cache"), T::Hash[String, Integer])
  counter_key = "counter"
  current_counter = discovery_counter_cache[counter_key] || 0
  current_counter += 1
  discovery_counter_cache[counter_key] = current_counter
  incremeted_discovery_file_path = File.join(temp_directory, "discovery.#{current_counter}.json")
  discovery_path_cache[discovery_key] = incremeted_discovery_file_path
  incremeted_discovery_file_path
end

.discovery_json_from_path(discovery_json_path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 114

def self.discovery_json_from_path(discovery_json_path)
  return unless File.exist?(discovery_json_path)

  DependencyFile.new(
    name: Pathname.new(discovery_json_path).cleanpath.to_path,
    directory: temp_directory,
    type: "file",
    content: File.read(discovery_json_path)
  )
end

.discovery_path_cacheObject



20
21
22
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 20

def self.discovery_path_cache
  T.let(CacheManager.cache("discovery_path_cache"), T::Hash[String, String])
end

.discovery_result_cacheObject



15
16
17
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 15

def self.discovery_result_cache
  T.let(CacheManager.cache("discovery_json_cache"), T::Hash[String, NativeDiscoveryJsonReader])
end

.get_discovery_file_path_from_dependency_files(dependency_files) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 53

def self.get_discovery_file_path_from_dependency_files(dependency_files)
  key = create_cache_key(dependency_files)
  discovery_path = discovery_path_cache[key]
  raise "No discovery path found for specified dependency files: #{key}" unless discovery_path

  discovery_path
end

.get_discovery_from_dependency_files(dependency_files) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 29

def self.get_discovery_from_dependency_files(dependency_files)
  key = create_cache_key(dependency_files)
  discovery_json = discovery_result_cache[key]
  raise "No discovery result for specified dependency files: #{key}" unless discovery_json

  discovery_json
end

.set_discovery_from_dependency_files(dependency_files:, discovery:) ⇒ Object



43
44
45
46
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 43

def self.set_discovery_from_dependency_files(dependency_files:, discovery:)
  key = create_cache_key(dependency_files)
  discovery_result_cache[key] = discovery
end

.temp_directoryObject



105
106
107
# File 'lib/dependabot/nuget/native_discovery/native_discovery_json_reader.rb', line 105

def self.temp_directory
  File.join(Dir.tmpdir, ".dependabot")
end