Module: Pray::Publish

Defined in:
lib/pray/publish.rb

Class Method Summary collapse

Class Method Details

.join_url(base, path) ⇒ Object



158
159
160
# File 'lib/pray/publish.rb', line 158

def join_url(base, path)
  Registry.join_url(base, path)
end

.load_registry_index(root) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/pray/publish.rb', line 89

def load_registry_index(root)
  path = File.join(root, "v1", "index.json")
  return RegistryIndex.new unless File.exist?(path)

  data = JSON.parse(File.read(path))
  RegistryIndex.new(spec: data["spec"], packages: data["packages"] || [])
end

.load_registry_package_metadata(path, package_name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pray/publish.rb', line 103

def (path, package_name)
  if File.exist?(path)
    data = JSON.parse(File.read(path))
    RegistryPackageMetadata.new(
      name: data["name"],
      versions: Array(data["versions"]).map { |entry| Registry.version_from_hash(entry) }
    )
  else
    RegistryPackageMetadata.new(name: package_name, versions: [])
  end
end

.metadata_to_hash(metadata) ⇒ Object



120
121
122
123
124
125
# File 'lib/pray/publish.rb', line 120

def ()
  {
    "name" => .name,
    "versions" => .versions.map { |entry| version_to_hash(entry) }
  }
end

.publish_to_root(project, root, signer: "local", signer_fingerprint: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pray/publish.rb', line 17

def publish_to_root(project, root, signer: "local", signer_fingerprint: nil)
  root = File.expand_path(root)
  index = load_registry_index(root)
  package_names = index.packages.to_set

  project.packages.each do |package|
    archive_bytes = Archive.build_package_archive_bytes(package)
    artifact_path = registry_artifact_path(package.declaration.name, package.spec.version)
    artifact_output_path = File.join(root, artifact_path)
    write_output_bytes(artifact_output_path, archive_bytes)

     = (root, package.declaration.name)
     = (, package.declaration.name)
    version_entry = published_registry_package_version(
      package,
      signer,
      signer_fingerprint,
      archive_bytes,
      artifact_path
    )
    .versions.reject! { |entry| entry.version == version_entry.version }
    .versions << version_entry
    (, )
    package_names << package.declaration.name
  end

  index.packages = package_names.sort
  write_registry_index(root, index)
end

.publish_to_server(project, server_url, signer: "local", signer_fingerprint: nil) ⇒ Object



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

def publish_to_server(project, server_url, signer: "local", signer_fingerprint: nil)
  project.packages.each do |package|
    archive_bytes = Archive.build_package_archive_bytes(package)
    artifact_path = registry_artifact_path(package.declaration.name, package.spec.version)
    Registry.http_put(join_url(server_url, artifact_path), "application/octet-stream", archive_bytes)

     = RegistryPackageMetadata.new(
      name: package.declaration.name,
      versions: [
        published_registry_package_version(
          package,
          signer,
          signer_fingerprint,
          archive_bytes,
          artifact_path
        )
      ]
    )
    Registry.http_put(
      join_url(server_url, "v1/packages/#{package.declaration.name}.json"),
      "application/json",
      JSON.pretty_generate(())
    )
  end
end

.published_registry_package_version(package, signer, signer_fingerprint, archive_bytes, artifact_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pray/publish.rb', line 73

def published_registry_package_version(package, signer, signer_fingerprint, archive_bytes, artifact_path)
  RegistryPackageVersion.new(
    version: package.spec.version,
    artifact: artifact_path,
    artifact_hash: Hashing.sha256_prefixed(archive_bytes),
    tree_hash: package.tree_hash,
    yanked: false,
    targets: package.spec.targets,
    exports: package.spec.exports.keys,
    signer: signer,
    signer_fingerprint: signer_fingerprint,
    published_at: Time.now.utc.iso8601,
    signature: Registry.registry_artifact_signature(archive_bytes, package.tree_hash, signer)
  )
end

.registry_artifact_path(package_name, version) ⇒ Object



148
149
150
151
# File 'lib/pray/publish.rb', line 148

def registry_artifact_path(package_name, version)
  artifact_name = "#{package_name.tr("/", "-")}-#{version}.praypkg"
  "v1/artifacts/#{package_name}/#{version}/#{artifact_name}"
end

.registry_metadata_path(root, package_name) ⇒ Object



144
145
146
# File 'lib/pray/publish.rb', line 144

def (root, package_name)
  File.join(root, "v1", "packages", "#{package_name}.json")
end

.version_to_hash(entry) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pray/publish.rb', line 127

def version_to_hash(entry)
  hash = {
    "version" => entry.version,
    "artifact" => entry.artifact,
    "yanked" => entry.yanked,
    "targets" => entry.targets,
    "exports" => entry.exports
  }
  hash["artifact_hash"] = entry.artifact_hash if entry.artifact_hash
  hash["tree_hash"] = entry.tree_hash if entry.tree_hash
  hash["signer"] = entry.signer if entry.signer
  hash["signer_fingerprint"] = entry.signer_fingerprint if entry.signer_fingerprint
  hash["published_at"] = entry.published_at if entry.published_at
  hash["signature"] = entry.signature if entry.signature
  hash
end

.write_output_bytes(path, bytes) ⇒ Object



153
154
155
156
# File 'lib/pray/publish.rb', line 153

def write_output_bytes(path, bytes)
  FileUtils.mkdir_p(File.dirname(path))
  File.binwrite(path, bytes)
end

.write_registry_index(root, index) ⇒ Object



97
98
99
100
101
# File 'lib/pray/publish.rb', line 97

def write_registry_index(root, index)
  path = File.join(root, "v1", "index.json")
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.pretty_generate({"spec" => index.spec, "packages" => index.packages}))
end

.write_registry_package_metadata(path, metadata) ⇒ Object



115
116
117
118
# File 'lib/pray/publish.rb', line 115

def (path, )
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.pretty_generate(()))
end