native-packages 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +1 -1
  3. data/README.md +5 -2
  4. data/docs/_guides/building-packages.md +5 -0
  5. data/docs/_guides/distribution-recipes.md +3 -1
  6. data/docs/_guides/getting-started.md +2 -2
  7. data/docs/_guides/github-actions.md +6 -4
  8. data/docs/_guides/hosted-repositories.md +170 -0
  9. data/docs/_guides/multi-platform.md +1 -1
  10. data/docs/_guides/native-recipes.md +6 -2
  11. data/docs/_guides/omarchy.md +131 -0
  12. data/docs/_guides/prereleases.md +1 -1
  13. data/docs/_guides/publishing.md +19 -0
  14. data/docs/_guides/release-signing.md +4 -4
  15. data/docs/_guides/windows-signing.md +259 -0
  16. data/docs/_reference/commands.md +32 -3
  17. data/docs/_reference/configuration.md +33 -3
  18. data/docs/_reference/platforms.md +126 -8
  19. data/docs/_reference/troubleshooting.md +22 -2
  20. data/docs/index.md +1 -1
  21. data/docs/releasing.md +2 -2
  22. data/examples/native-packages-all-formats.yaml +1 -1
  23. data/examples/native-packages-linux-distribution.yaml +76 -0
  24. data/examples/native-packages.yaml +1 -1
  25. data/lib/native_packages/appimage.rb +4 -66
  26. data/lib/native_packages/apt_repository.rb +190 -0
  27. data/lib/native_packages/build.rb +59 -6
  28. data/lib/native_packages/cli.rb +19 -2
  29. data/lib/native_packages/configuration.rb +29 -2
  30. data/lib/native_packages/dnf_repository.rb +131 -0
  31. data/lib/native_packages/flatpak.rb +171 -0
  32. data/lib/native_packages/flatpak_repository.rb +63 -0
  33. data/lib/native_packages/hosted_repository.rb +73 -0
  34. data/lib/native_packages/inspection.rb +3 -2
  35. data/lib/native_packages/macos_signing.rb +20 -2
  36. data/lib/native_packages/omarchy.rb +140 -0
  37. data/lib/native_packages/openpgp.rb +114 -0
  38. data/lib/native_packages/snap.rb +166 -0
  39. data/lib/native_packages/staging.rb +74 -0
  40. data/lib/native_packages/support.rb +1 -1
  41. data/lib/native_packages/windows_signing.rb +284 -0
  42. metadata +15 -1
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "hosted_repository"
4
+
5
+ module NativePackages
6
+ # A signed DNF (and Zypper) repository: packages/ holds the .rpm files and
7
+ # repodata/ the metadata createrepo_c writes, with repomd.xml signed
8
+ # beside it as repomd.xml.asc. Beside them go the public key (NAME.asc) and
9
+ # a NAME.repo file for users to install.
10
+ class DnfRepository < HostedRepository
11
+ TAGS = { 1000 => "name", 1001 => "version", 1002 => "release", 1003 => "epoch", 1022 => "arch", 1044 => "sourcerpm" }.freeze
12
+ # Signature header tags for DSA, RSA, PGP, GPG and OpenPGP signatures.
13
+ SIGNATURES = [267, 268, 1002, 1005, 278].freeze
14
+
15
+ def self.validate(data)
16
+ settings = data["dnf"]
17
+ check(settings, "dnf", KEYS) unless settings.nil?
18
+ end
19
+
20
+ def initialize(configuration)
21
+ super(configuration, configuration.data.fetch("dnf") { raise Error, "configure dnf to publish --to dnf" })
22
+ end
23
+
24
+ # The name, version, release, epoch and architecture of the RPM at
25
+ # `path`, and whether it carries a signature.
26
+ def self.header(path)
27
+ File.open(path, "rb") do |file|
28
+ raise Error, "not an RPM package: #{path}" unless file.read(96)&.start_with?("\xED\xAB\xEE\xDB".b)
29
+ signature = read_header(file, path)
30
+ file.seek((8 - (file.pos % 8)) % 8, IO::SEEK_CUR)
31
+ main = read_header(file, path)
32
+ raise Error, "#{File.basename(path)} is a source package" unless main.key?(1044)
33
+ TAGS.to_h { |tag, key| [key, main[tag]] }.merge("epoch" => main[1003]&.first.to_i, "signed" => SIGNATURES.any? { |tag| signature.key?(tag) })
34
+ end
35
+ end
36
+
37
+ def self.read_header(file, path)
38
+ raise Error, "invalid RPM header: #{path}" unless file.read(8)&.start_with?("\x8E\xAD\xE8\x01".b)
39
+ count, size = file.read(8).unpack("N2")
40
+ index = file.read(count * 16).unpack("N*").each_slice(4).to_a
41
+ store = file.read(size)
42
+ raise Error, "truncated RPM header: #{path}" unless store&.bytesize == size
43
+ index.to_h do |tag, type, offset, length|
44
+ value = case type
45
+ when 4 then store.byteslice(offset, 4 * length).unpack("N*")
46
+ when 6, 8, 9 then store.byteslice(offset..).unpack1("Z*").force_encoding(Encoding::UTF_8)
47
+ end
48
+ [tag, value]
49
+ end
50
+ end
51
+
52
+ # RPM's version order (rpmvercmp): -1, 0 or 1.
53
+ def self.compare(left, right)
54
+ return 0 if left == right
55
+ loop do
56
+ left, right = [left, right].map { |part| part.sub(/\A[^A-Za-z0-9~^]+/, "") }
57
+ # A tilde sorts before everything, even the end.
58
+ if left.start_with?("~") || right.start_with?("~")
59
+ return 1 unless left.start_with?("~")
60
+ return -1 unless right.start_with?("~")
61
+ left, right = left[1..], right[1..]
62
+ next
63
+ end
64
+ if left.start_with?("^") || right.start_with?("^")
65
+ return -1 if left.empty?
66
+ return 1 if right.empty?
67
+ return 1 unless left.start_with?("^")
68
+ return -1 unless right.start_with?("^")
69
+ left, right = left[1..], right[1..]
70
+ next
71
+ end
72
+ break if left.empty? || right.empty?
73
+ numeric = left.match?(/\A\d/)
74
+ pattern = numeric ? /\A\d+/ : /\A[A-Za-z]+/
75
+ mine, theirs = left[pattern], right[pattern]
76
+ # A number is newer than letters.
77
+ return numeric ? 1 : -1 unless theirs
78
+ order = if numeric
79
+ a, b = [mine, theirs].map { |number| number.sub(/\A0+/, "") }
80
+ (a.length <=> b.length).nonzero? || (a <=> b)
81
+ else
82
+ mine <=> theirs
83
+ end
84
+ return order unless order.zero?
85
+ left, right = left[mine.length..], right[theirs.length..]
86
+ end
87
+ return 0 if left.empty? && right.empty?
88
+ left.empty? ? -1 : 1
89
+ end
90
+
91
+ def self.compare_packages(left, right)
92
+ (left.fetch("epoch") <=> right.fetch("epoch")).nonzero? || compare(left.fetch("version"), right.fetch("version")).nonzero? ||
93
+ compare(left.fetch("release"), right.fetch("release"))
94
+ end
95
+
96
+ def publish(rpms, secret: nil, passphrase: nil)
97
+ raise Error, "the build contains no rpm packages to publish --to dnf" if rpms.empty?
98
+ raise Error, "install createrepo_c to publish --to dnf" unless available?("createrepo_c")
99
+ rpms.each do |path|
100
+ self.class.header(path)
101
+ place(path, directory / "packages" / path.basename)
102
+ end
103
+ entries = Pathname.glob(directory / "packages/*.rpm").sort.to_h do |path|
104
+ header = self.class.header(path)
105
+ [path, [header.values_at("name", "arch"), header]]
106
+ end
107
+ entries = prune(entries) { |left, right| self.class.compare_packages(left, right) }
108
+ signer(secret: secret, passphrase: passphrase).session do |signing|
109
+ # gzip, which every dnf and zypper reads; newer createrepo_c defaults to zstd.
110
+ run "createrepo_c", "--quiet", "--no-database", "--general-compress-type=gz", directory
111
+ signing.detach_sign(directory / "repodata/repomd.xml", directory / "repodata/repomd.xml.asc")
112
+ write(directory / "#{name}.asc", signing.public_key(armor: true))
113
+ end
114
+ signed = entries.values.all? { |_, header| header.fetch("signed") }
115
+ write(directory / "#{name}.repo", <<~REPO)
116
+ [#{name}]
117
+ name=#{name}
118
+ baseurl=#{url}
119
+ enabled=1
120
+ repo_gpgcheck=1
121
+ gpgcheck=#{signed ? 1 : 0}
122
+ gpgkey=#{url}/#{name}.asc
123
+ REPO
124
+ puts "Published #{entries.length} package#{"s" unless entries.length == 1} to #{directory}. Install with:", *instructions.map { |line| " #{line}" }
125
+ end
126
+
127
+ def instructions
128
+ ["sudo curl -fsSLo /etc/yum.repos.d/#{name}.repo #{url}/#{name}.repo", "sudo dnf install #{name}"]
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "staging"
4
+
5
+ module NativePackages
6
+ # Builds a single-file Flatpak bundle from the same contents nFPM packages.
7
+ # The app is already built, so this uses flatpak's own build commands
8
+ # (build-finish, build-export, build-bundle) on the staged files, without
9
+ # flatpak-builder, an SDK or the network. Files installed under /usr go to
10
+ # /app; the runtime supplies the libraries.
11
+ #
12
+ # This never writes or submits a Flathub manifest: Flathub does not accept
13
+ # AI-assisted manifests or submissions.
14
+ class Flatpak
15
+ include Support
16
+ include Staging
17
+ # nFPM architecture => Flatpak architecture.
18
+ ARCHES = { "amd64" => "x86_64", "arm64" => "aarch64" }.freeze
19
+ KEYS = %w[id runtime runtime_version sdk command finish_args metainfo branch runtime_repo repository].freeze
20
+ # Reverse-DNS: three or more elements, no hyphen in the last one.
21
+ ID = /\A(?=.{1,255}\z)(?:[A-Za-z_][A-Za-z0-9_-]*\.){2,}[A-Za-z_][A-Za-z0-9_]*\z/
22
+ RUNTIME = /\A(?:[A-Za-z_][A-Za-z0-9_-]*\.)+[A-Za-z_][A-Za-z0-9_]*\z/
23
+ BRANCH = /\A[A-Za-z0-9_][A-Za-z0-9_.-]*\z/
24
+ ICON_DIRECTORIES = %w[share/icons share/pixmaps].freeze
25
+ attr_reader :root, :settings
26
+
27
+ def initialize(configuration)
28
+ @root = configuration.root
29
+ @settings = configuration.data.fetch("flatpak", {})
30
+ end
31
+
32
+ def self.architecture(target)
33
+ ARCHES.fetch(target.fetch("arch")) { raise Error, "flatpak does not support #{target.fetch('arch')}; use #{ARCHES.keys.join(', ')}" }
34
+ end
35
+
36
+ def self.filename(name, version, target) = "#{name}-#{version}-#{architecture(target)}.flatpak"
37
+
38
+ def self.validate(data)
39
+ building = data.fetch("targets").values.any? { |target| target.fetch("formats", []).include?("flatpak") }
40
+ settings = data["flatpak"]
41
+ raise Error, "flatpak targets need a top-level flatpak section" if building && settings.nil?
42
+ return if settings.nil?
43
+ raise Error, "flatpak: expected a mapping" unless settings.is_a?(Hash)
44
+ raise Error, "flatpak: no target builds the flatpak format" unless building
45
+ unknown = settings.keys - KEYS
46
+ raise Error, "flatpak: unknown fields #{unknown.join(', ')}" unless unknown.empty?
47
+ raise Error, "flatpak.id: use a reverse-DNS application ID such as org.example.App" unless settings["id"].is_a?(String) && ID.match?(settings["id"])
48
+ %w[runtime sdk].each do |key|
49
+ next if key == "sdk" && !settings.key?(key)
50
+ raise Error, "flatpak.#{key}: expected a runtime ID such as org.freedesktop.Platform" unless settings[key].is_a?(String) && RUNTIME.match?(settings[key])
51
+ end
52
+ %w[runtime_version branch].each do |key|
53
+ next if key == "branch" && !settings.key?(key)
54
+ raise Error, "flatpak.#{key}: expected a branch such as 24.08 or stable" unless settings[key].is_a?(String) && BRANCH.match?(settings[key])
55
+ end
56
+ command = settings["command"]
57
+ unless command.is_a?(String) && (/\A[A-Za-z0-9_][A-Za-z0-9_.+-]*\z/.match?(command) || command.start_with?("/app/"))
58
+ raise Error, "flatpak.command: name a program in /app/bin, or an absolute /app path"
59
+ end
60
+ arguments = settings.fetch("finish_args", [])
61
+ unless arguments.is_a?(Array) && arguments.all? { |argument| argument.is_a?(String) && argument.start_with?("--") && !argument.start_with?("--command") }
62
+ raise Error, "flatpak.finish_args: use a list of flatpak build-finish options such as --socket=wayland"
63
+ end
64
+ if settings.key?("metainfo") && !(settings["metainfo"].is_a?(String) && Pathname.new(settings["metainfo"]).relative?)
65
+ raise Error, "flatpak.metainfo: expected a path relative to the configuration"
66
+ end
67
+ if settings.key?("runtime_repo") && !(settings["runtime_repo"].is_a?(String) && settings["runtime_repo"].start_with?("https://"))
68
+ raise Error, "flatpak.runtime_repo: expected an https:// .flatpakrepo URL"
69
+ end
70
+ end
71
+
72
+ def id = settings.fetch("id")
73
+ def branch = settings.fetch("branch", "stable")
74
+
75
+ def doctor(target)
76
+ self.class.architecture(target)
77
+ raise Error, "install flatpak to build Flatpak bundles" unless available?("flatpak")
78
+ end
79
+
80
+ # Returns the bundle's path and what went into it.
81
+ def build(package, target, destination, name:, version:, epoch:)
82
+ doctor(target)
83
+ arch = self.class.architecture(target)
84
+ output = destination / self.class.filename(name, version, target)
85
+ Dir.mktmpdir("native-packages-flatpak-") do |directory|
86
+ directory = Pathname.new(directory)
87
+ tree = directory / "tree"
88
+ tree.mkpath
89
+ stage(package, tree, "flatpak")
90
+ outside = tree.children.map { |path| "/#{path.basename}" } - ["/usr"]
91
+ raise Error, "flatpak: Flatpak apps install only under /usr, which becomes /app; move #{outside.join(', ')}" unless outside.empty?
92
+ build = directory / "build"
93
+ build.mkpath
94
+ files = build / "files"
95
+ (tree / "usr").exist? ? (tree / "usr").rename(files) : files.mkpath
96
+ details = integrate(files)
97
+ metadata = ["[Application]", "name=#{id}", "runtime=#{settings.fetch('runtime')}/#{arch}/#{settings.fetch('runtime_version')}"]
98
+ metadata << "sdk=#{settings.fetch('sdk')}/#{arch}/#{settings.fetch('runtime_version')}" if settings["sdk"]
99
+ write(build / "metadata", metadata.join("\n") + "\n")
100
+ # Installed runtimes would add their own permissions to the metadata;
101
+ # hide them so every host writes the same app.
102
+ installations = directory / "installations"
103
+ environment = { "FLATPAK_SYSTEM_DIR" => (installations / "system").to_s, "FLATPAK_USER_DIR" => (installations / "user").to_s }
104
+ command = settings.fetch("command")
105
+ run "flatpak", "build-finish", "--command=#{command}", *settings.fetch("finish_args", []), build, env: environment
106
+ normalize(build, epoch)
107
+ repository = directory / "repository"
108
+ run "flatpak", "build-export", "--arch=#{arch}", "--timestamp=#{Time.at(epoch).utc.iso8601}", "--no-update-summary",
109
+ "--disable-fsync", repository, build, branch, env: environment
110
+ bundle = ["flatpak", "build-bundle", "--arch=#{arch}"]
111
+ bundle << "--runtime-repo=#{settings['runtime_repo']}" if settings["runtime_repo"]
112
+ run(*bundle, repository, output, id, branch, env: environment)
113
+ check(output, arch)
114
+ # The same inputs give the same commit. The bundle file around it
115
+ # also records when it was written, which SOURCE_DATE_EPOCH does not
116
+ # change, so its hash differs between builds.
117
+ commit = (repository / "refs/heads/app/#{id}/#{arch}/#{branch}").read.strip
118
+ [output, details.merge("id" => id, "branch" => branch, "commit" => commit,
119
+ "runtime" => "#{settings.fetch('runtime')}//#{settings.fetch('runtime_version')}", "command" => command, "libraries" => "runtime")]
120
+ end
121
+ end
122
+
123
+ # Fails unless `path` is a Flatpak bundle of this app for `arch`.
124
+ def check(path, arch)
125
+ bytes = path.binread(4096) || ""
126
+ raise Error, "not a Flatpak bundle: #{path}" unless bytes.start_with?("flatpak\0".b)
127
+ raise Error, "#{path.basename} does not hold app/#{id}/#{arch}/#{branch}" unless bytes.include?("app/#{id}/#{arch}/#{branch}".b)
128
+ end
129
+
130
+ # Flatpak exports only desktop entries, icons and metainfo named after
131
+ # the app ID, so rename the package's own to match.
132
+ def integrate(files)
133
+ command = settings.fetch("command")
134
+ executable = command.start_with?("/") ? files / relative_path(command.delete_prefix("/app/")) : files / "bin" / command
135
+ raise Error, "flatpak: command #{command} is not an executable the package installs" unless executable.file? && executable.executable?
136
+ details = { "desktop" => nil, "icon" => nil, "metainfo" => nil }
137
+ entries = Pathname.glob(files / "share/applications/*.desktop")
138
+ raise Error, "flatpak: at most one desktop entry in /usr/share/applications; found #{entries.length}" if entries.length > 1
139
+ if (desktop = entries.first)
140
+ lines = desktop.read.lines
141
+ icon = lines.find { |line| line.start_with?("Icon=") }&.delete_prefix("Icon=")&.strip
142
+ if icon && icon != id
143
+ raise Error, "flatpak: Icon must be a name, not a path" if icon.include?("/")
144
+ icons = ICON_DIRECTORIES.flat_map { |path| Pathname.glob(files / path / "**" / "#{icon}.{svg,png}") }
145
+ raise Error, "flatpak: the package installs no #{icon}.svg or #{icon}.png icon" if icons.empty?
146
+ icons.each { |path| path.rename(path.dirname / "#{id}#{path.extname}") }
147
+ lines = lines.map { |line| line.start_with?("Icon=") && line.delete_prefix("Icon=").strip == icon ? "Icon=#{id}\n" : line }
148
+ end
149
+ desktop.delete
150
+ write(files / "share/applications/#{id}.desktop", lines.join)
151
+ details["desktop"] = "#{id}.desktop"
152
+ details["icon"] = id if icon
153
+ end
154
+ if settings["metainfo"]
155
+ source = root / relative_path(settings["metainfo"])
156
+ raise Error, "flatpak.metainfo: #{settings['metainfo']} does not exist" unless source.file?
157
+ write(files / "share/metainfo/#{id}.metainfo.xml", source.binread)
158
+ end
159
+ metainfo = Pathname.glob(files / "share/{metainfo,appdata}/*.xml")
160
+ unless metainfo.empty?
161
+ named = metainfo.find { |path| path.basename.to_s.start_with?("#{id}.") }
162
+ raise Error, "flatpak: name the metainfo file #{id}.metainfo.xml" unless named
163
+ unless named.read.match?(%r{<id>\s*#{Regexp.escape(id)}(?:\.desktop)?\s*</id>})
164
+ raise Error, "flatpak: #{named.basename} does not declare <id>#{id}</id>"
165
+ end
166
+ details["metainfo"] = named.basename.to_s
167
+ end
168
+ details
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "flatpak"
4
+ require_relative "hosted_repository"
5
+
6
+ module NativePackages
7
+ # A signed, self-hosted Flatpak repository: an OSTree repository that the
8
+ # build's bundles are imported into, served as static files, with a
9
+ # NAME.flatpakrepo and an ID.flatpakref beside it for users to add.
10
+ #
11
+ # This is not Flathub. Flathub does not accept AI-assisted manifests or
12
+ # submissions, so native-packages neither writes nor submits one.
13
+ class FlatpakRepository < HostedRepository
14
+ # Each bundle is a commit without a parent, so a ref has no history to
15
+ # keep: the repository serves the newest commit of each app.
16
+ KEYS = (HostedRepository::KEYS - %w[keep] + %w[title homepage]).freeze
17
+
18
+ def self.validate(data)
19
+ settings = data["flatpak"].is_a?(Hash) ? data["flatpak"]["repository"] : nil
20
+ return if settings.nil?
21
+ check(settings, "flatpak.repository", KEYS)
22
+ %w[title homepage].each do |key|
23
+ raise Error, "flatpak.repository.#{key}: expected a string" if settings.key?(key) && !settings[key].is_a?(String)
24
+ end
25
+ if settings.key?("homepage") && !settings["homepage"].start_with?("https://")
26
+ raise Error, "flatpak.repository.homepage: expected an https:// URL"
27
+ end
28
+ end
29
+
30
+ def initialize(configuration)
31
+ settings = configuration.data.fetch("flatpak", {})["repository"]
32
+ raise Error, "configure flatpak.repository to publish --to flatpak" unless settings
33
+ super(configuration, settings)
34
+ @flatpak = Flatpak.new(configuration)
35
+ end
36
+
37
+ def title = settings.fetch("title", name)
38
+
39
+ def publish(bundles, secret: nil, passphrase: nil)
40
+ raise Error, "the build contains no Flatpak bundles to publish --to flatpak" if bundles.empty?
41
+ missing = %w[flatpak ostree].reject { |tool| available?(tool) }
42
+ raise Error, "install #{missing.join(' and ')} to publish --to flatpak" unless missing.empty?
43
+ directory.mkpath
44
+ run "ostree", "init", "--mode=archive-z2", "--repo=#{directory}" unless (directory / "config").file?
45
+ signer(secret: secret, passphrase: passphrase).session do |signing|
46
+ bundles.each { |bundle| run "flatpak", "build-import-bundle", "--no-update-summary", *signing.flatpak_options, directory, bundle }
47
+ # Prune drops the objects only replaced commits used.
48
+ run "flatpak", "build-update-repo", *signing.flatpak_options, "--title=#{title}", "--default-branch=#{@flatpak.branch}",
49
+ "--prune", "--generate-static-deltas", directory
50
+ key = [signing.public_key].pack("m0")
51
+ homepage = settings["homepage"] ? ["Homepage=#{settings['homepage']}"] : []
52
+ write(directory / "#{name}.flatpakrepo", (["[Flatpak Repo]", "Title=#{title}", "Url=#{url}/", *homepage,
53
+ "DefaultBranch=#{@flatpak.branch}", "GPGKey=#{key}"].join("\n") + "\n"))
54
+ runtime = @flatpak.settings["runtime_repo"] ? ["RuntimeRepo=#{@flatpak.settings['runtime_repo']}"] : []
55
+ write(directory / "#{@flatpak.id}.flatpakref", (["[Flatpak Ref]", "Name=#{@flatpak.id}", "Branch=#{@flatpak.branch}", "Title=#{title}",
56
+ "Url=#{url}/", "SuggestRemoteName=#{name}", *homepage, *runtime, "IsRuntime=false", "GPGKey=#{key}"].join("\n") + "\n"))
57
+ end
58
+ puts "Published #{bundles.length} Flatpak bundle#{"s" unless bundles.length == 1} to #{directory}. Install with:", *instructions.map { |line| " #{line}" }
59
+ end
60
+
61
+ def instructions = ["flatpak install --user #{url}/#{@flatpak.id}.flatpakref"]
62
+ end
63
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "openpgp"
4
+
5
+ module NativePackages
6
+ # A signed package repository that native-packages writes into a directory,
7
+ # such as a GitHub Pages checkout, to be served as static files. Writing is
8
+ # all it does: the release process commits and pushes the directory.
9
+ class HostedRepository
10
+ include Support
11
+ KEYS = %w[directory url keep signing_key].freeze
12
+ KEEP = 3
13
+ attr_reader :root, :name, :settings, :directory
14
+
15
+ # Checks the settings shared by every hosted repository, under `label`.
16
+ def self.check(settings, label, keys)
17
+ raise Error, "#{label}: expected a mapping" unless settings.is_a?(Hash)
18
+ unknown = settings.keys - keys
19
+ raise Error, "#{label}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
20
+ raise Error, "#{label}.directory: name the directory to write the repository into" unless settings["directory"].is_a?(String) && !settings["directory"].empty?
21
+ unless settings["url"].is_a?(String) && %r{\Ahttps://[^\s/]+(?:/\S*)?\z}.match?(settings["url"])
22
+ raise Error, "#{label}.url: expected the https:// URL the directory is served at"
23
+ end
24
+ keep = settings.fetch("keep", KEEP)
25
+ raise Error, "#{label}.keep: expected how many versions of each package to keep (1 or more)" unless keep.is_a?(Integer) && keep.positive?
26
+ OpenPGP.fingerprint(settings["signing_key"], "#{label}.signing_key")
27
+ end
28
+
29
+ def initialize(configuration, settings)
30
+ @root = configuration.root
31
+ @name = configuration.name
32
+ @settings = settings
33
+ @directory = Pathname.new(settings.fetch("directory")).expand_path(root)
34
+ end
35
+
36
+ def url = settings.fetch("url").chomp("/")
37
+ def keep = settings.fetch("keep", KEEP)
38
+
39
+ def signer(secret: nil, passphrase: nil)
40
+ OpenPGP.new(OpenPGP.fingerprint(settings.fetch("signing_key"), "signing_key"), secret: secret, passphrase: passphrase)
41
+ end
42
+
43
+ # Copies `path` to `destination`, refusing to change a published file.
44
+ def place(path, destination)
45
+ if destination.exist?
46
+ raise Error, "#{destination.relative_path_from(directory)} is already published with different contents" unless sha256(destination) == sha256(path)
47
+ return
48
+ end
49
+ destination.dirname.mkpath
50
+ FileUtils.cp(path, destination)
51
+ destination.chmod(0o644)
52
+ end
53
+
54
+ # Deletes all but the newest `keep` versions in each group of `entries`
55
+ # (path => [group, version, ...]), ordered by `compare`, and returns
56
+ # the entries that remain.
57
+ def prune(entries, &compare)
58
+ removed = entries.group_by { |_, details| details.first }.values.flat_map do |versions|
59
+ versions.sort { |left, right| compare.call(right.last[1], left.last[1]) }.drop(keep).map(&:first)
60
+ end
61
+ removed.each do |path|
62
+ path.delete
63
+ puts "Removed #{path.relative_path_from(directory)}"
64
+ parent = path.dirname
65
+ while parent != directory && parent.empty?
66
+ parent.rmdir
67
+ parent = parent.dirname
68
+ end
69
+ end
70
+ entries.except(*removed)
71
+ end
72
+ end
73
+ end
@@ -115,8 +115,9 @@ module NativePackages
115
115
  depends << (format == "deb" ? "libc6 (>= #{floor})" : "glibc >= #{floor}")
116
116
  end
117
117
  override["depends"] = depends.uniq
118
- elsif format == "appimage"
119
- # Nothing is bundled: the host supplies these, as for deb and rpm.
118
+ elsif %w[appimage flatpak snap].include?(format)
119
+ # Nothing is bundled: the host (AppImage), the Flatpak runtime or the
120
+ # snap's base supplies these.
120
121
  elsif !external.empty? && package.fetch("overrides", {}).fetch(format, {}).fetch("depends", package.fetch("depends", [])).empty?
121
122
  raise Error, "#{format}: declare dependencies for #{external.join(', ')}"
122
123
  end
@@ -114,14 +114,14 @@ module NativePackages
114
114
  end
115
115
 
116
116
  def notarize(package)
117
- execute "hdiutil", "verify", package
117
+ verify_image(package)
118
118
  execute "codesign", "--force", "--timestamp", "--keychain", @keychain,
119
119
  "--sign", @credentials.fetch("APPLE_SIGNING_IDENTITY"), package
120
120
  execute "codesign", "--verify", "--strict", package
121
121
  response = submit(package)
122
122
  execute "xcrun", "stapler", "staple", package
123
123
  execute "xcrun", "stapler", "validate", package
124
- execute "hdiutil", "verify", package
124
+ verify_image(package)
125
125
  puts "Apple accepted #{package.basename}; its notarization ticket is stapled and validated."
126
126
  { "signature" => "developer-id", "notarization" => "accepted", "submission_id" => response.fetch("id"), "stapled" => true }
127
127
  end
@@ -179,6 +179,24 @@ module NativePackages
179
179
  output
180
180
  end
181
181
 
182
+ # hdiutil can still hold a freshly written image for a moment and refuse
183
+ # to verify it with "Resource temporarily unavailable"; that is no fault
184
+ # of the image, so it gets a few more tries.
185
+ def verify_image(package)
186
+ attempts = 0
187
+ begin
188
+ execute "hdiutil", "verify", package
189
+ rescue Error => error
190
+ attempts += 1
191
+ raise unless error.message.include?("Resource temporarily unavailable") && attempts < 5
192
+
193
+ sleep verify_delay * attempts
194
+ retry
195
+ end
196
+ end
197
+
198
+ def verify_delay = 2
199
+
182
200
  def execute(*arguments)
183
201
  capture(*arguments)
184
202
  rescue Error => error
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "build"
4
+
5
+ module NativePackages
6
+ # Writes a first Omarchy recipe (omacom/omarchy-pkgs) from the app's AUR -bin
7
+ # templates. Omarchy's bot then opens every later version bump itself, from
8
+ # the upstream declaration in .omarchy/package.json, so this runs once.
9
+ class Omarchy
10
+ include Support
11
+ ARCHITECTURES = %w[x86_64 aarch64].freeze
12
+ # The policy RawMakase and superwhisper-bin use: hourly, self-merging updates that wait a day
13
+ # after each GitHub release and go to every channel at once.
14
+ POLICY = { "source" => "local", "auto_merge" => true, "release_ring" => "fast", "min_release_age" => "24h" }.freeze
15
+ PROBE = "999.0.0"
16
+ # Releases are tagged vMAJOR.MINOR.PATCH; the watch must match the whole tag.
17
+ TAG_PATTERN = "v(?P<version>[0-9]+(?:\\.[0-9]+)*)"
18
+
19
+ attr_reader :configuration, :root
20
+
21
+ def initialize(configuration)
22
+ @configuration = configuration
23
+ @root = configuration.root
24
+ end
25
+
26
+ def recipe(checkout, release:, package: nil)
27
+ configuration.validate
28
+ version = version_arg(release)
29
+ name = package || bin_package
30
+ templates = configuration.data.fetch("templates").select { |path, _| path.start_with?("arch/#{name}/") }
31
+ raise Error, "no template for arch/#{name}/PKGBUILD" unless templates.key?("arch/#{name}/PKGBUILD")
32
+ checkout = Pathname.new(checkout).expand_path
33
+ raise Error, "#{checkout} is not an omarchy-pkgs checkout (no pkgbuilds/)" unless (checkout / "pkgbuilds").directory?
34
+ destination = checkout / "pkgbuilds" / name
35
+ raise Error, "#{destination} exists; after the first PR Omarchy's bot updates it" if destination.exist? || destination.symlink?
36
+
37
+ build = Build.new(configuration)
38
+ metadata = build.metadata(version, release: true).merge("PKGREL" => 1)
39
+ checksums = build.release_checksums(metadata)
40
+ configuration.data.fetch("assets").each_key do |key|
41
+ digest = checksums[metadata.fetch("#{key}_FILE")]
42
+ metadata["#{key}_SHA256"] = digest if digest
43
+ end
44
+
45
+ Dir.mktmpdir(".#{name}-", checkout / "pkgbuilds") do |temporary|
46
+ staging = Pathname.new(temporary) / name
47
+ templates.each do |path, source|
48
+ template = root / relative_path(source)
49
+ content = render(template.binread, metadata)
50
+ content = follow_pkgver(content, version) if File.basename(path) == "PKGBUILD"
51
+ write(staging / relative_path(path).relative_path_from(Pathname.new("arch/#{name}")), content, executable: template.executable?)
52
+ end
53
+ upstream = declaration(staging, name, version, checksums)
54
+ write(staging / ".omarchy/package.json", JSON.pretty_generate(POLICY.merge("upstream" => upstream)) + "\n")
55
+ staging.rename(destination)
56
+ end
57
+ puts "Wrote #{destination}"
58
+ end
59
+
60
+ # The one AUR binary recipe, unless the caller names it.
61
+ def bin_package
62
+ names = configuration.data.fetch("templates").keys.filter_map { |path| path[%r{\Aarch/([^/]+-bin)/PKGBUILD\z}, 1] }
63
+ raise Error, "no arch/NAME-bin/PKGBUILD template; add one or pass --package" if names.empty?
64
+ raise Error, "several -bin templates (#{names.join(', ')}); pass --package" if names.length > 1
65
+ names.first
66
+ end
67
+
68
+ # Omarchy's bot rewrites only pkgver, pkgrel and the checksums, so every
69
+ # source URL has to be written in terms of pkgver.
70
+ def follow_pkgver(content, version)
71
+ literal = /(?<![0-9.])#{Regexp.escape(version)}(?![0-9])/
72
+ content.gsub(/^source(?:_[A-Za-z0-9_]+)?=\(.*?\)/m) { |array| array.gsub(literal, "${pkgver}") }
73
+ end
74
+
75
+ def declaration(staging, name, version, checksums)
76
+ pkgbuild = staging / "PKGBUILD"
77
+ run "bash", "-n", pkgbuild
78
+ upstream = configuration.project.upstream
79
+ sources = {}
80
+ architectures = nil
81
+ ARCHITECTURES.each do |arch|
82
+ recipe = inspect_recipe(pkgbuild, arch)
83
+ architectures ||= recipe.fetch("arch")
84
+ raise Error, "PKGBUILD pkgname must be #{name}" unless recipe.fetch("pkgname") == [name]
85
+ raise Error, "PKGBUILD pkgver must be #{version}" unless recipe.fetch("pkgver") == [version]
86
+ unless !architectures.empty? && (architectures - ARCHITECTURES).empty?
87
+ raise Error, "PKGBUILD arch must list x86_64 and/or aarch64"
88
+ end
89
+ if recipe.fetch("source").any? { |entry| entry.split("::", 2).last.match?(%r{\A[a-z+]+://}) }
90
+ raise Error, "move downloads into source_<arch>: each architecture downloads its own release assets"
91
+ end
92
+ next unless architectures.include?(arch)
93
+
94
+ urls = recipe.fetch("source_#{arch}").map { |entry| entry.split("::", 2).last }
95
+ sums = recipe.fetch("sha256sums_#{arch}")
96
+ raise Error, "source_#{arch} must download the release assets" if urls.empty?
97
+ raise Error, "sha256sums_#{arch} must match source_#{arch}" unless sums.length == urls.length
98
+ prefix = "#{upstream}/releases/download/v#{version}/"
99
+ urls.each_with_index do |url, index|
100
+ raise Error, "source_#{arch} leaves a variable unexpanded; use double quotes: #{url}" if url.include?("$")
101
+ raise Error, "source_#{arch} must come from #{prefix}: #{url}" unless url.start_with?(prefix)
102
+ file = url.delete_prefix(prefix)
103
+ raise Error, "#{file} is not listed in the release checksums" unless checksums.key?(file)
104
+ raise Error, "sha256sums_#{arch} disagrees with the release checksums for #{file}" unless sums[index] == checksums.fetch(file)
105
+ end
106
+ sources[arch] = urls
107
+ end
108
+ # Prove the URLs move with pkgver, as they will when the bot bumps it.
109
+ probe = staging / "PKGBUILD.probe"
110
+ probe.write(pkgbuild.read.sub(/^pkgver=.*$/, "pkgver=#{PROBE}"))
111
+ begin
112
+ sources.each do |arch, urls|
113
+ moved = inspect_recipe(probe, arch).fetch("source_#{arch}").map { |entry| entry.split("::", 2).last }
114
+ raise Error, "source_#{arch} does not follow pkgver" unless moved == urls.map { |url| url.gsub(version, PROBE) }
115
+ end
116
+ ensure
117
+ probe.delete
118
+ end
119
+ # The watch takes each new tag's SHA-256 from the digest GitHub reports
120
+ # for the asset at that exact URL, which is why every source above must
121
+ # be a release download and match the release's own checksums.
122
+ { "watch" => { "github" => configuration.data.fetch("release").fetch("repository"), "pattern" => TAG_PATTERN } }
123
+ end
124
+
125
+ # Source the recipe the way omarchy-pkgs' tooling does: CARCH set and
126
+ # nothing else from the environment.
127
+ def inspect_recipe(path, arch)
128
+ script = <<~'BASH'
129
+ source "$1" >/dev/null
130
+ emit() { printf '%s' "$1"; shift; printf '\t%s' "$@"; printf '\n'; }
131
+ emit pkgname "${pkgname[@]}"; emit pkgver "$pkgver"; emit arch "${arch[@]}"; emit source "${source[@]}"
132
+ for name in source_x86_64 source_aarch64 sha256sums_x86_64 sha256sums_aarch64; do
133
+ declare -n values="$name"; emit "$name" "${values[@]}"; unset -n values
134
+ done
135
+ BASH
136
+ output = capture("env", "-i", "PATH=/usr/bin:/bin", "CARCH=#{arch}", "bash", "-c", script, "bash", path.basename, chdir: path.dirname)
137
+ output.lines.to_h { |line| key, *values = line.chomp.split("\t", -1); [key, values.reject(&:empty?)] }
138
+ end
139
+ end
140
+ end