Module: Pray::LockfileSerialize

Defined in:
lib/pray/lockfile_serialize.rb

Class Method Summary collapse

Class Method Details

.append_scalars(lines, entries) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pray/lockfile_serialize.rb', line 97

def append_scalars(lines, entries)
  entries.each do |key, value|
    lines << case value
    when String
      "#{key} = #{format_string(value)}"
    when Integer
      "#{key} = #{value}"
    when TrueClass, FalseClass
      "#{key} = #{value}"
    else
      raise ArgumentError, "unsupported lockfile scalar type for #{key}"
    end
  end
end

.append_section(lines, section_lines) ⇒ Object



30
31
32
33
34
# File 'lib/pray/lockfile_serialize.rb', line 30

def append_section(lines, section_lines)
  return if section_lines.empty?

  lines.concat(section_lines, [""])
end

.format_managed_span(entry) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pray/lockfile_serialize.rb', line 78

def format_managed_span(entry)
  lines = ["[[managed_span]]"]
  append_scalars(
    lines,
    [
      ["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]
    ]
  )
  lines
end

.format_package(entry) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pray/lockfile_serialize.rb', line 49

def format_package(entry)
  lines = ["[[package]]"]
  scalars = [
    ["name", entry.name],
    ["version", entry.version]
  ]
  scalars << ["source", entry.source] unless entry.source.nil?
  scalars.concat(
    [
      ["path", entry.path],
      ["tree_hash", entry.tree_hash],
      ["artifact_hash", entry.artifact_hash],
      ["artifact", entry.artifact]
    ]
  )
  append_scalars(lines, scalars)
  lines << "exports = #{format_string_array(entry.exports)}"
  lines << "dependencies = #{format_string_array(entry.dependencies)}"
  lines << "signer_fingerprint = #{format_string(entry.signer_fingerprint)}" if entry.signer_fingerprint
  lines
end

.format_source(entry) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pray/lockfile_serialize.rb', line 36

def format_source(entry)
  lines = ["[[source]]"]
  scalars = [
    ["name", entry.name],
    ["kind", entry.kind],
    ["url", entry.url]
  ]
  scalars << ["revision", entry.revision] if entry.revision
  scalars << ["host_key_fingerprint", entry.host_key_fingerprint] if entry.host_key_fingerprint
  append_scalars(lines, scalars)
  lines
end

.format_string(value) ⇒ Object



112
113
114
115
# File 'lib/pray/lockfile_serialize.rb', line 112

def format_string(value)
  escaped = value.gsub("\\", "\\\\").gsub("\"", "\\\"")
  "\"#{escaped}\""
end

.format_string_array(values) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/pray/lockfile_serialize.rb', line 117

def format_string_array(values)
  return "[]" if values.empty?
  return "[#{format_string(values.first)}]" if values.length == 1

  items = values.map { |value| "    #{format_string(value)}" }.join(",\n")
  "[\n#{items},\n]"
end

.format_target(entry) ⇒ Object



71
72
73
74
75
76
# File 'lib/pray/lockfile_serialize.rb', line 71

def format_target(entry)
  lines = ["[[target]]"]
  append_scalars(lines, [["name", entry.name]])
  lines << "outputs = #{format_string_array(entry.outputs)}"
  lines
end

.lockfile_to_toml(lockfile) ⇒ Object



7
8
9
# File 'lib/pray/lockfile_serialize.rb', line 7

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

.serialize_lockfile_text(lockfile) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pray/lockfile_serialize.rb', line 11

def serialize_lockfile_text(lockfile)
  lines = [
    "prayfile_lock = #{format_string(lockfile.prayfile_lock)}",
    "spec = #{format_string(lockfile.spec)}",
    "generated_by = #{format_string(lockfile.generated_by)}",
    "manifest_hash = #{format_string(lockfile.manifest_hash)}"
  ]
  lines << "environment = #{format_string(lockfile.environment)}" if lockfile.environment
  lines << ""

  lockfile.source.each { |entry| append_section(lines, format_source(entry)) }
  lockfile.package.each { |entry| append_section(lines, format_package(entry)) }
  lockfile.target.each { |entry| append_section(lines, format_target(entry)) }
  lockfile.managed_span.each { |entry| append_section(lines, format_managed_span(entry)) }

  lines.pop while lines.last == ""
  "#{lines.join("\n")}\n"
end