Module: Pray::ManifestJson

Defined in:
lib/pray/manifest_json.rb

Class Method Summary collapse

Class Method Details

.encode_array(array) ⇒ Object



107
108
109
# File 'lib/pray/manifest_json.rb', line 107

def encode_array(array)
  "[#{array.map { |value| encode_value(value) }.join(",")}]"
end

.encode_compact(manifest) ⇒ Object



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

def encode_compact(manifest)
  encode_object(manifest_fields(manifest))
end

.encode_object(hash) ⇒ Object



102
103
104
105
# File 'lib/pray/manifest_json.rb', line 102

def encode_object(hash)
  entries = hash.map { |key, value| "#{encode_string(key)}:#{encode_value(value)}" }
  "{#{entries.join(",")}}"
end

.encode_string(text) ⇒ Object



124
125
126
# File 'lib/pray/manifest_json.rb', line 124

def encode_string(text)
  "\"#{text.gsub("\\", "\\\\").gsub("\"", "\\\"").gsub("\n", "\\n").gsub("\r", "\\r").gsub("\t", "\\t")}\""
end

.encode_value(value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pray/manifest_json.rb', line 111

def encode_value(value)
  case value
  when Hash then encode_object(value)
  when Array then encode_array(value)
  when String then encode_string(value)
  when TrueClass, FalseClass then value.to_s
  when NilClass then "null"
  when Integer then value.to_s
  else
    raise Error.manifest("unsupported JSON value: #{value.inspect}")
  end
end

.entry_fields(entry) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/pray/manifest_json.rb', line 50

def entry_fields(entry)
  if entry.kind == "package"
    {"kind" => "package", "name" => entry.name}
  else
    {"kind" => "local", "path" => entry.path}
  end
end

.local_fields(entry) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/pray/manifest_json.rb', line 81

def local_fields(entry)
  fields = {
    "path" => entry.path,
    "position" => entry.position,
    "optional" => entry.optional
  }
  fields["bound"] = true if entry.bound
  fields
end

.manifest_fields(manifest) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pray/manifest_json.rb', line 11

def manifest_fields(manifest)
  fields = {
    "prayfile_version" => manifest.prayfile_version,
    "sources" => manifest.sources.map { |source| source_fields(source) },
    "targets" => manifest.targets.map { |target| target_fields(target) },
    "packages" => manifest.packages.map { |package| package_fields(package) },
    "local" => manifest.local.map { |entry| local_fields(entry) },
    "render" => render_fields(manifest.render)
  }
  fields["symbols"] = manifest.symbols.sort.to_h unless manifest.symbols.empty?
  fields
end

.package_fields(package) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pray/manifest_json.rb', line 58

def package_fields(package)
  fields = {
    "name" => package.name,
    "constraint" => package.constraint,
    "source" => package.source,
    "exports" => package.exports,
    "targets" => package.targets,
    "features" => package.features,
    "groups" => package.groups,
    "optional" => package.optional,
    "path" => package.path,
    "git" => package.git,
    "tag" => package.tag,
    "rev" => package.rev,
    "tarball" => package.tarball,
    "oci" => package.oci
  }
  fields["file"] = package.file if package.file
  fields["roles"] = package.roles unless package.roles.nil? || package.roles.empty?
  fields["bound"] = true if package.bound
  fields
end

.render_fields(render) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/pray/manifest_json.rb', line 91

def render_fields(render)
  {
    "mode" => render.mode,
    "conflict" => render.conflict,
    "churn" => render.churn,
    "header" => render.header,
    "section_markers" => render.section_markers,
    "line_endings" => render.line_endings
  }
end

.source_fields(source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pray/manifest_json.rb', line 24

def source_fields(source)
  fields = {
    "name" => source.name,
    "kind" => source.kind,
    "url" => source.url
  }
  fields["subdir"] = source.subdir if source.subdir
  fields["rev"] = source.rev if source.rev
  fields["tag"] = source.tag if source.tag
  fields
end

.target_fields(target) ⇒ Object



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

def target_fields(target)
  {
    "name" => target.name,
    "outputs" => target.outputs,
    "skills" => target.skills,
    "commands" => target.commands,
    "rules" => target.rules,
    "max_bytes" => target.max_bytes,
    "mode" => target.mode || "legacy",
    "scoped" => target.scoped || false,
    "entries" => (target.entries || []).map { |entry| entry_fields(entry) }
  }
end