Module: Pray::LockfileIO

Included in:
Pray
Defined in:
lib/pray/lockfile.rb

Class Method Summary collapse

Class Method Details

.build_lockfile(manifest_hash, environment, project_root, manifest_sources, manifest_targets, rendered, packages, source_revisions, source_host_keys) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pray/lockfile.rb', line 150

def build_lockfile(manifest_hash, environment, project_root, manifest_sources, manifest_targets, rendered, packages, source_revisions, source_host_keys)
  Lockfile.new(
    manifest_hash: manifest_hash,
    environment: environment,
    source: manifest_sources.map do |source|
      LockSource.new(
        name: source.name,
        kind: source.kind,
        url: source.url,
        revision: source_revisions[source.name],
        host_key_fingerprint: source_host_keys[source.name]
      )
    end,
    package: packages.map do |package|
      LockedPackage.new(
        name: package.declaration.name,
        version: package.spec.version,
        source: package.declaration.source,
        path: relative_lockfile_path(project_root, package.root),
        tree_hash: package.tree_hash,
        artifact_hash: package.artifact_hash,
        artifact: normalize_lockfile_artifact(project_root, package.artifact, package.root),
        exports: package.selected_exports,
        dependencies: package.spec.dependencies.map(&:name),
        signer_fingerprint: package.signer_fingerprint
      )
    end,
    target: manifest_targets.map { |target| LockedTarget.new(name: target.name, outputs: target.outputs) },
    managed_span: rendered.flat_map(&:managed_spans)
  ).canonicalized
end

.format_relative_lockfile_path(relative) ⇒ Object



239
240
241
242
# File 'lib/pray/lockfile.rb', line 239

def format_relative_lockfile_path(relative)
  text = relative.to_s.tr("\\", "/")
  (text == "." || text.start_with?("./")) ? text : "./#{text}"
end

.from_hash(data) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pray/lockfile.rb', line 182

def from_hash(data)
  Lockfile.new(
    prayfile_lock: data["prayfile_lock"],
    spec: data["spec"],
    generated_by: data["generated_by"],
    manifest_hash: data["manifest_hash"],
    environment: data["environment"],
    source: Array(data["source"]).map do |entry|
      LockSource.new(
        name: entry["name"],
        kind: entry["kind"],
        url: entry["url"],
        revision: entry["revision"],
        host_key_fingerprint: entry["host_key_fingerprint"]
      )
    end,
    package: Array(data["package"]).map do |entry|
      LockedPackage.new(
        name: entry["name"],
        version: entry["version"],
        source: entry["source"],
        path: entry["path"],
        tree_hash: entry["tree_hash"],
        artifact_hash: entry["artifact_hash"],
        artifact: entry["artifact"],
        exports: entry["exports"] || [],
        dependencies: entry["dependencies"] || [],
        signer_fingerprint: entry["signer_fingerprint"]
      )
    end,
    target: Array(data["target"]).map { |entry| LockedTarget.new(name: entry["name"], outputs: entry["outputs"] || []) },
    managed_span: Array(data["managed_span"]).map do |entry|
      ManagedSpanRecord.new(
        id: entry["id"],
        target: entry["target"],
        open_line: entry["open_line"],
        close_line: entry["close_line"],
        ideal_checksum: entry["ideal_checksum"],
        package: entry["package"],
        export: entry["export"],
        source_checksum: entry["source_checksum"],
        silenced: entry["silenced"]
      )
    end
  )
end

.lockfile_hash(lockfile) ⇒ Object



129
130
131
# File 'lib/pray/lockfile.rb', line 129

def lockfile_hash(lockfile)
  Hashing.sha256_prefixed(serialize_lockfile(lockfile))
end

.lockfile_to_hash(lockfile) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pray/lockfile.rb', line 64

def lockfile_to_hash(lockfile)
  {
    "prayfile_lock" => lockfile.prayfile_lock,
    "spec" => lockfile.spec,
    "generated_by" => lockfile.generated_by,
    "manifest_hash" => lockfile.manifest_hash,
    "source" => lockfile.source.map { |entry| source_to_hash(entry) },
    "package" => lockfile.package.map { |entry| package_to_hash(entry) },
    "target" => lockfile.target.map { |entry| {"name" => entry.name, "outputs" => entry.outputs} },
    "managed_span" => lockfile.managed_span.map { |entry| managed_span_to_hash(entry) }
  }
end

.lockfile_to_toml(lockfile) ⇒ Object



60
61
62
# File 'lib/pray/lockfile.rb', line 60

def lockfile_to_toml(lockfile)
  LockfileSerialize.lockfile_to_toml(lockfile)
end

.lockfiles_equivalent?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/pray/lockfile.rb', line 146

def lockfiles_equivalent?(left, right)
  left.equivalent_to?(right)
end

.managed_span_to_hash(entry) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pray/lockfile.rb', line 100

def managed_span_to_hash(entry)
  {
    "id" => entry.id,
    "target" => entry.target,
    "open_line" => entry.open_line,
    "close_line" => entry.close_line,
    "ideal_checksum" => entry.ideal_checksum,
    "package" => entry.package,
    "export" => entry.export,
    "source_checksum" => entry.source_checksum,
    "silenced" => entry.silenced
  }
end

.normalize_lockfile_artifact(project_root, artifact, package_root) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/pray/lockfile.rb', line 244

def normalize_lockfile_artifact(project_root, artifact, package_root)
  return artifact unless artifact.start_with?("path:")

  path_text = artifact.delete_prefix("path:")
  path = Pathname(path_text)
  relative = if path.absolute?
    relative_lockfile_path(project_root, path)
  else
    relative_lockfile_path(project_root, package_root)
  end
  "path:#{relative}"
end

.package_to_hash(entry) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pray/lockfile.rb', line 84

def package_to_hash(entry)
  hash = {
    "name" => entry.name,
    "version" => entry.version,
    "path" => entry.path,
    "tree_hash" => entry.tree_hash,
    "artifact_hash" => entry.artifact_hash,
    "artifact" => entry.artifact,
    "exports" => entry.exports,
    "dependencies" => entry.dependencies
  }
  hash["source"] = entry.source unless entry.source.nil?
  hash["signer_fingerprint"] = entry.signer_fingerprint if entry.signer_fingerprint
  hash
end

.parse_lockfile(text) ⇒ Object



114
115
116
117
118
119
# File 'lib/pray/lockfile.rb', line 114

def parse_lockfile(text)
  data = TomlRB.parse(text)
  from_hash(data)
rescue TomlRB::ParseError => error
  raise Error.parse("lockfile", error.message)
end

.read_lockfile(path) ⇒ Object



121
122
123
# File 'lib/pray/lockfile.rb', line 121

def read_lockfile(path)
  parse_lockfile(File.read(path))
end

.relative_lockfile_path(project_root, path) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/pray/lockfile.rb', line 229

def relative_lockfile_path(project_root, path)
  absolute = Pathname(path).absolute? ? Pathname(path) : Pathname(project_root).join(path)
  normalized_root = Pathname(project_root).cleanpath
  normalized_absolute = absolute.cleanpath
  relative = normalized_absolute.relative_path_from(normalized_root)
  format_relative_lockfile_path(relative)
rescue ArgumentError
  format_relative_lockfile_path(Pathname(path).cleanpath)
end

.serialize_lockfile(lockfile) ⇒ Object



125
126
127
# File 'lib/pray/lockfile.rb', line 125

def serialize_lockfile(lockfile)
  lockfile_to_toml(lockfile.canonicalized)
end

.source_to_hash(entry) ⇒ Object



77
78
79
80
81
82
# File 'lib/pray/lockfile.rb', line 77

def source_to_hash(entry)
  hash = {"name" => entry.name, "kind" => entry.kind, "url" => entry.url}
  hash["revision"] = entry.revision if entry.revision
  hash["host_key_fingerprint"] = entry.host_key_fingerprint if entry.host_key_fingerprint
  hash
end

.write_lockfile(path, lockfile) ⇒ Object



133
134
135
# File 'lib/pray/lockfile.rb', line 133

def write_lockfile(path, lockfile)
  File.write(path, serialize_lockfile(lockfile))
end

.write_lockfile_if_changed(path, lockfile) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/pray/lockfile.rb', line 137

def write_lockfile_if_changed(path, lockfile)
  serialized = serialize_lockfile(lockfile)
  if File.exist?(path) && File.binread(path) == serialized
    return
  end

  File.write(path, serialized)
end