native-packages 0.11.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.
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "staging"
4
+
5
+ module NativePackages
6
+ # Builds a .snap from the same contents nFPM packages: the files become the
7
+ # snap's root, beside a generated meta/snap.yaml, packed with mksquashfs
8
+ # as `snap pack` packs a prime directory. No snapcraft, LXD or network is
9
+ # involved, and no extension (such as gnome) is applied, so the snap uses
10
+ # its base snap's libraries and whatever the package installs.
11
+ class Snap
12
+ include Support
13
+ include Staging
14
+ # nFPM architecture => Snap architecture.
15
+ ARCHES = { "amd64" => "amd64", "arm64" => "arm64", "arm7" => "armhf", "ppc64le" => "ppc64el", "s390x" => "s390x",
16
+ "riscv64" => "riscv64" }.freeze
17
+ KEYS = %w[name summary description title license base confinement grade icon apps plugs slots layout environment store].freeze
18
+ APP_KEYS = %w[command desktop plugs slots environment daemon command-chain common-id restart-condition stop-command
19
+ install-mode autostart].freeze
20
+ NAME = /\A(?=.{2,40}\z)(?=[a-z0-9-]*[a-z])[a-z0-9]+(?:-[a-z0-9]+)*\z/
21
+ APP = /\A[a-zA-Z0-9](?:-?[a-zA-Z0-9])*\z/
22
+ CHANNEL = %r{\A(?:[a-z0-9][a-z0-9._-]*/)?(?:stable|candidate|beta|edge)(?:/[a-z0-9][a-z0-9._-]*)?\z}
23
+ CREDENTIALS = "SNAPCRAFT_STORE_CREDENTIALS"
24
+ attr_reader :root, :settings, :name
25
+
26
+ def initialize(configuration)
27
+ @root = configuration.root
28
+ @settings = configuration.data.fetch("snap", {})
29
+ @name = settings.fetch("name", configuration.name)
30
+ end
31
+
32
+ def self.architecture(target)
33
+ ARCHES.fetch(target.fetch("arch")) { raise Error, "snap does not support #{target.fetch('arch')}; use #{ARCHES.keys.join(', ')}" }
34
+ end
35
+
36
+ def self.validate(data)
37
+ building = data.fetch("targets").values.any? { |target| target.fetch("formats", []).include?("snap") }
38
+ settings = data["snap"]
39
+ raise Error, "snap targets need a top-level snap section" if building && settings.nil?
40
+ return if settings.nil?
41
+ raise Error, "snap: expected a mapping" unless settings.is_a?(Hash)
42
+ raise Error, "snap: no target builds the snap format" unless building
43
+ unknown = settings.keys - KEYS
44
+ raise Error, "snap: unknown fields #{unknown.join(', ')}" unless unknown.empty?
45
+ name = settings.fetch("name", data.fetch("nfpm").fetch("name"))
46
+ raise Error, "snap.name: use 2 to 40 lowercase letters, digits and single hyphens, as #{name.inspect} is not" unless name.is_a?(String) && NAME.match?(name)
47
+ raise Error, "snap.base: expected a base such as core24" unless settings["base"].is_a?(String) && /\A(?:core\d{2}|bare)\z/.match?(settings["base"])
48
+ raise Error, "snap.confinement: use strict, classic or devmode" unless %w[strict classic devmode].include?(settings.fetch("confinement", "strict"))
49
+ raise Error, "snap.grade: use stable or devel" unless %w[stable devel].include?(settings.fetch("grade", "stable"))
50
+ summary = settings.fetch("summary", data.fetch("nfpm")["description"].to_s.lines.first.to_s.strip)
51
+ raise Error, "snap.summary: write a summary of at most 78 characters" unless summary.is_a?(String) && !summary.empty? && summary.length <= 78
52
+ %w[description title license].each do |key|
53
+ raise Error, "snap.#{key}: expected a string" if settings.key?(key) && !settings[key].is_a?(String)
54
+ end
55
+ %w[plugs slots layout environment].each do |key|
56
+ raise Error, "snap.#{key}: expected a mapping" if settings.key?(key) && !settings[key].is_a?(Hash)
57
+ end
58
+ if settings.key?("icon") && !(settings["icon"].is_a?(String) && settings["icon"].match?(/\.(?:png|svg)\z/))
59
+ raise Error, "snap.icon: name a .png or .svg file the package installs, such as /usr/share/icons/hicolor/scalable/apps/app.svg"
60
+ end
61
+ apps = settings["apps"]
62
+ raise Error, "snap.apps: declare at least one app with a command" unless apps.is_a?(Hash) && !apps.empty?
63
+ apps.each do |app, definition|
64
+ raise Error, "snap.apps: invalid app name #{app.inspect}" unless app.is_a?(String) && APP.match?(app)
65
+ raise Error, "snap.apps.#{app}: expected a mapping" unless definition.is_a?(Hash)
66
+ unknown = definition.keys - APP_KEYS
67
+ raise Error, "snap.apps.#{app}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
68
+ unless definition["command"].is_a?(String) && !definition["command"].strip.empty?
69
+ raise Error, "snap.apps.#{app}.command: name a program the package installs, such as usr/bin/#{name}"
70
+ end
71
+ %w[plugs slots].each do |key|
72
+ next unless definition.key?(key)
73
+ raise Error, "snap.apps.#{app}.#{key}: expected a list of names" unless definition[key].is_a?(Array) && definition[key].all?(String)
74
+ end
75
+ raise Error, "snap.apps.#{app}.desktop: name a .desktop file the package installs" if definition.key?("desktop") && !definition["desktop"].to_s.end_with?(".desktop")
76
+ end
77
+ store = settings["store"]
78
+ return if store.nil?
79
+ raise Error, "snap.store: expected a mapping with channels" unless store.is_a?(Hash) && store.keys == ["channels"]
80
+ channels = store["channels"]
81
+ unless channels.is_a?(Array) && !channels.empty? && channels.all? { |channel| channel.is_a?(String) && CHANNEL.match?(channel) }
82
+ raise Error, "snap.store.channels: list channels such as stable or latest/edge"
83
+ end
84
+ end
85
+
86
+ def filename(version, target) = "#{name}_#{version}_#{self.class.architecture(target)}.snap"
87
+
88
+ def doctor(target)
89
+ self.class.architecture(target)
90
+ raise Error, "install mksquashfs (squashfs-tools) to build snaps" unless available?("mksquashfs")
91
+ end
92
+
93
+ # Returns the snap's path and what went into it.
94
+ def build(package, target, destination, version:, epoch:)
95
+ doctor(target)
96
+ raise Error, "snap versions are at most 32 characters" if version.length > 32
97
+ output = destination / filename(version, target)
98
+ Dir.mktmpdir("native-packages-snap-") do |directory|
99
+ prime = Pathname.new(directory) / "prime"
100
+ prime.mkpath
101
+ stage(package, prime, "snap")
102
+ raise Error, "snap: the package must not install /meta or /snap, which the snap's own metadata uses" if (prime / "meta").exist? || (prime / "snap").exist?
103
+ apps = settings.fetch("apps").to_h { |app, definition| [app, integrate(prime, app, definition)] }
104
+ if settings["icon"]
105
+ icon = inside(prime, settings["icon"], "snap")
106
+ raise Error, "snap.icon: the package installs no #{settings['icon']}" unless icon.file?
107
+ write(prime / "meta/gui/icon#{icon.extname}", icon.binread)
108
+ apps.each_key do |app|
109
+ desktop = prime / "meta/gui/#{app}.desktop"
110
+ write(desktop, desktop.read.gsub(/^Icon=.*$/, "Icon=${SNAP}/meta/gui/icon#{icon.extname}")) if desktop.file?
111
+ end
112
+ end
113
+ write(prime / "meta/snap.yaml", YAML.dump(metadata(package, version, target, apps)).delete_prefix("---\n"))
114
+ normalize(prime, epoch)
115
+ run "mksquashfs", prime, output, "-noappend", "-comp", "xz", "-no-fragments", "-no-progress", "-quiet", "-all-root", "-no-xattrs",
116
+ env: { "SOURCE_DATE_EPOCH" => epoch.to_s }
117
+ check(output)
118
+ [output, { "name" => name, "base" => settings.fetch("base"), "confinement" => settings.fetch("confinement", "strict"),
119
+ "apps" => apps.keys, "libraries" => "base" }]
120
+ end
121
+ end
122
+
123
+ # meta/snap.yaml. The summary, description and license default to the
124
+ # package's.
125
+ def metadata(package, version, target, apps)
126
+ result = { "name" => name, "version" => version, "summary" => settings.fetch("summary", package.fetch("description").lines.first.strip),
127
+ "description" => settings.fetch("description", package.fetch("description")) }
128
+ result["title"] = settings["title"] if settings["title"]
129
+ license = settings.fetch("license", package["license"])
130
+ result["license"] = license if license
131
+ result.merge!("base" => settings.fetch("base"), "grade" => settings.fetch("grade", "stable"),
132
+ "confinement" => settings.fetch("confinement", "strict"), "architectures" => [self.class.architecture(target)], "apps" => apps)
133
+ %w[plugs slots layout environment].each { |key| result[key] = settings[key] if settings[key] }
134
+ result
135
+ end
136
+
137
+ # Checks the app's command and puts its desktop entry in meta/gui, where
138
+ # snapd looks for it, launching the snap's app.
139
+ def integrate(prime, app, definition)
140
+ command = Shellwords.split(definition.fetch("command")).first.delete_prefix("$SNAP/")
141
+ executable = prime / relative_path(command)
142
+ raise Error, "snap.apps.#{app}.command: the package installs no executable #{command}" unless executable.file? && executable.executable?
143
+ if (desktop = definition["desktop"])
144
+ source = inside(prime, desktop.start_with?("/") ? desktop : "/#{desktop}", "snap")
145
+ raise Error, "snap.apps.#{app}.desktop: the package installs no #{desktop}" unless source.file?
146
+ launcher = app == name ? name : "#{name}.#{app}"
147
+ entry = source.read.gsub(/^Exec=(\S+)/) { "Exec=#{launcher}" }
148
+ write(prime / "meta/gui/#{app}.desktop", entry)
149
+ end
150
+ definition.except("desktop")
151
+ end
152
+
153
+ # Fails unless `path` is a squashfs image, the format of every snap.
154
+ def check(path)
155
+ raise Error, "not a snap (squashfs) file: #{path}" unless path.binread(4) == "hsqs".b
156
+ end
157
+
158
+ def publish(paths)
159
+ channels = settings.dig("store", "channels") or raise Error, "configure snap.store.channels to publish --to snapcraft"
160
+ raise Error, "set #{CREDENTIALS} to a Snap Store login from snapcraft export-login" if ENV.fetch(CREDENTIALS, "").empty?
161
+ raise Error, "install snapcraft to publish to the Snap Store" unless available?("snapcraft")
162
+ raise Error, "the build contains no snaps" if paths.empty?
163
+ paths.each { |path| run "snapcraft", "upload", "--release=#{channels.join(',')}", path }
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NativePackages
4
+ # Lays out a package's nFPM contents as a directory tree, for the formats
5
+ # nFPM does not write (AppImage, Flatpak and Snap), and fixes its modes and
6
+ # times so the same inputs give the same bytes.
7
+ module Staging
8
+ include Support
9
+
10
+ # nFPM overrides replace the contents list when one is supplied.
11
+ def contents(package, packager)
12
+ package.fetch("overrides", {}).fetch(packager, {}).fetch("contents", package.fetch("contents"))
13
+ .reject { |item| item["packager"] && item["packager"] != packager }
14
+ end
15
+
16
+ def inside(tree, destination, packager)
17
+ raise Error, "#{packager}: content destinations must be absolute: #{destination}" unless destination.to_s.start_with?("/")
18
+ tree / relative_path(destination.to_s.delete_prefix("/"))
19
+ end
20
+
21
+ def stage(package, tree, packager)
22
+ contents(package, packager).each do |item|
23
+ type = item.fetch("type", "file")
24
+ next if type == "ghost"
25
+ target = inside(tree, item.fetch("dst"), packager)
26
+ mode = item.dig("file_info", "mode")
27
+ case type
28
+ when "dir"
29
+ target.mkpath
30
+ when "symlink"
31
+ target.dirname.mkpath
32
+ File.symlink(item.fetch("src"), target)
33
+ when "file", "tree", "config", "config|noreplace", "config|missingok"
34
+ source = File.expand_path(item.fetch("src"), root)
35
+ paths = package["disable_globbing"] ? [source].select { |path| File.exist?(path) } : Dir.glob(source).sort
36
+ raise Error, "missing package content: #{source}" if paths.empty?
37
+ # As nFPM does: a glob source, or a destination ending in a slash,
38
+ # makes the destination a directory that takes each match's name.
39
+ pattern = !package["disable_globbing"] && item.fetch("src").match?(/[*?\[{]/)
40
+ single = paths.length == 1 && File.file?(paths.first) && type != "tree" && !pattern && !item.fetch("dst").end_with?("/")
41
+ paths.each do |path|
42
+ if File.directory?(path)
43
+ target.mkpath
44
+ FileUtils.cp_r(Dir.children(path).map { |child| File.join(path, child) }, target)
45
+ else
46
+ file = single ? target : target / File.basename(path)
47
+ file.dirname.mkpath
48
+ FileUtils.cp(path, file)
49
+ file.chmod(mode) if mode
50
+ end
51
+ end
52
+ else
53
+ raise Error, "#{packager}: unsupported content type #{type}"
54
+ end
55
+ end
56
+ end
57
+
58
+ # The same bytes for the same inputs: fixed modes and times.
59
+ def normalize(tree, epoch)
60
+ time = Time.at(epoch)
61
+ Pathname.glob(tree / "**/*", File::FNM_DOTMATCH).each do |path|
62
+ next if path.basename.to_s == "."
63
+ if path.symlink?
64
+ File.lutime(time, time, path)
65
+ else
66
+ path.chmod(path.directory? || path.executable? ? 0o755 : 0o644)
67
+ File.utime(time, time, path)
68
+ end
69
+ end
70
+ tree.chmod(0o755)
71
+ File.utime(time, time, tree)
72
+ end
73
+ end
74
+ end
@@ -12,7 +12,7 @@ require "tmpdir"
12
12
  require "yaml"
13
13
 
14
14
  module NativePackages
15
- VERSION = "0.11.0"
15
+ VERSION = "0.12.0"
16
16
  class Error < StandardError; end
17
17
 
18
18
  module Support
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native-packages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino
@@ -25,6 +25,7 @@ files:
25
25
  - docs/_guides/distribution-recipes.md
26
26
  - docs/_guides/getting-started.md
27
27
  - docs/_guides/github-actions.md
28
+ - docs/_guides/hosted-repositories.md
28
29
  - docs/_guides/multi-platform.md
29
30
  - docs/_guides/native-recipes.md
30
31
  - docs/_guides/omarchy.md
@@ -47,6 +48,7 @@ files:
47
48
  - docs/platforms.md
48
49
  - docs/releasing.md
49
50
  - examples/native-packages-all-formats.yaml
51
+ - examples/native-packages-linux-distribution.yaml
50
52
  - examples/native-packages.yaml
51
53
  - examples/packaging/arch/example-app-bin/PKGBUILD.in
52
54
  - examples/packaging/nfpm.yml
@@ -55,17 +57,25 @@ files:
55
57
  - exe/native-packages
56
58
  - lib/native_packages.rb
57
59
  - lib/native_packages/appimage.rb
60
+ - lib/native_packages/apt_repository.rb
58
61
  - lib/native_packages/build.rb
59
62
  - lib/native_packages/cli.rb
60
63
  - lib/native_packages/configuration.rb
64
+ - lib/native_packages/dnf_repository.rb
65
+ - lib/native_packages/flatpak.rb
66
+ - lib/native_packages/flatpak_repository.rb
67
+ - lib/native_packages/hosted_repository.rb
61
68
  - lib/native_packages/inspection.rb
62
69
  - lib/native_packages/macos_signing.rb
63
70
  - lib/native_packages/native_recipe.rb
64
71
  - lib/native_packages/omarchy.rb
72
+ - lib/native_packages/openpgp.rb
65
73
  - lib/native_packages/project.rb
66
74
  - lib/native_packages/release_signing.rb
67
75
  - lib/native_packages/repositories.rb
68
76
  - lib/native_packages/scaffold.rb
77
+ - lib/native_packages/snap.rb
78
+ - lib/native_packages/staging.rb
69
79
  - lib/native_packages/support.rb
70
80
  - lib/native_packages/windows_signing.rb
71
81
  homepage: https://github.com/crmne/native-packages