kettle-sponsors 0.1.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,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
5
+ module Kettle
6
+ module Sponsors
7
+ # Deterministic SVG for one sponsor. Same input always yields the same bytes.
8
+ module BadgeRenderer
9
+ VARIANTS = %i[desktop phone].freeze
10
+ FONT = "system-ui,sans-serif"
11
+
12
+ module_function
13
+
14
+ def svg(sponsor, variant: :desktop)
15
+ raise ArgumentError, "unknown variant #{variant.inspect}" unless VARIANTS.include?(variant)
16
+
17
+ phone = variant == :phone
18
+ scale = phone ? 0.8 : 1.0
19
+ since = sponsor.since ? "since #{sponsor.since.iso8601[0, 7]}" : "sponsor"
20
+ desc = sponsor.description
21
+ desc = "#{desc[0, 40]}…" if phone && desc.length > 40
22
+ w = ([190, (sponsor.name.length * 9) + 40, (desc.length * 6) + 28].max * scale).round
23
+ h = ((desc.empty? ? 40 : 54) * scale).round
24
+ label = [sponsor.name, since, desc].reject(&:empty?).join(", ")
25
+ s = ->(n) { (n * scale).round(1).to_s.sub(/\.0\z/, "") }
26
+ extra = desc.empty? ? "" : %(\n <text x="#{s[14]}" y="#{s[46]}" font-family="#{FONT}" font-size="#{s[10]}" fill="#c9d1d9">#{esc(desc)}</text>)
27
+ <<~SVG
28
+ <svg xmlns="http://www.w3.org/2000/svg" width="#{w}" height="#{h}" viewBox="0 0 #{w} #{h}" role="img" aria-label="#{esc(label)}">
29
+ <rect x=".5" y=".5" width="#{w - 1}" height="#{h - 1}" rx="6" fill="#0d1117" stroke="#30363d"/>
30
+ <text x="#{s[14]}" y="#{s[18]}" font-family="#{FONT}" font-size="#{s[14]}" font-weight="700" fill="#f0f6fc">#{esc(sponsor.name)}</text>
31
+ <text x="#{s[14]}" y="#{s[32]}" font-family="#{FONT}" font-size="#{s[10]}" fill="#8b949e">#{esc(since)}</text>#{extra}
32
+ </svg>
33
+ SVG
34
+ end
35
+
36
+ def esc(text)
37
+ CGI.escapeHTML(text.to_s)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+ require "json"
5
+
6
+ module Kettle
7
+ module Sponsors
8
+ # Checksum of only the active sponsors, so unrelated config edits and
9
+ # expired sponsors are accounted for exactly once.
10
+ module Checksum
11
+ FILENAME = ".checksum"
12
+
13
+ module_function
14
+
15
+ def compute(sponsors, today: Date.today)
16
+ payload = sponsors.select { |s| s.active?(today) }.map(&:to_h)
17
+ Digest::SHA256.hexdigest(JSON.generate(payload))
18
+ end
19
+
20
+ def current?(dir, sum)
21
+ path = File.join(dir, FILENAME)
22
+ File.file?(path) && File.read(path).strip == sum
23
+ end
24
+
25
+ def write(dir, sum)
26
+ File.write(File.join(dir, FILENAME), "#{sum}\n")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Kettle
6
+ module Sponsors
7
+ # kettle-sponsors [--config PATH] [--out DIR] [--check | --verify] [--force]
8
+ # kettle-sponsors publish --from PATH --registry PATH
9
+ class CLI
10
+ def initialize(argv, out: $stdout)
11
+ @argv = argv
12
+ @out = out
13
+ @config = CONFIG_PATH
14
+ @dir = LOCAL_DIR
15
+ @mode = :render
16
+ @force = false
17
+ @from = nil
18
+ @registry = nil
19
+ end
20
+
21
+ # @return [Integer] exit status. With --check: 0 when badges are current, 1 when stale.
22
+ def run
23
+ @mode = :publish if @argv.first == "publish" && @argv.shift
24
+ parser.parse!(@argv)
25
+ return publish if @mode == :publish
26
+
27
+ sponsors = Config.load(@config)
28
+ case @mode
29
+ when :check then check(sponsors)
30
+ when :verify then verify(sponsors)
31
+ else render(sponsors)
32
+ end
33
+ rescue Error, OptionParser::ParseError => e
34
+ warn("kettle-sponsors: #{e.message}")
35
+ 2
36
+ end
37
+
38
+ private
39
+
40
+ def publish
41
+ raise Error, "publish requires --from and --registry" unless @from && @registry
42
+
43
+ changed = Registry.publish(from: File.expand_path(@from), registry: File.expand_path(@registry))
44
+ @out.puts(changed ? "registry updated: #{@registry}" : "registry unchanged")
45
+ 0
46
+ end
47
+
48
+ def check(sponsors)
49
+ stale = Renderer.stale?(sponsors, out: @dir)
50
+ @out.puts(stale ? "sponsors changed" : "sponsors unchanged")
51
+ stale ? 1 : 0
52
+ end
53
+
54
+ def verify(sponsors)
55
+ results = Verifier.verify(sponsors)
56
+ results.each { |url, ok| @out.puts("#{ok ? "ok " : "MISSING"} #{url}") }
57
+ results.value?(false) ? 1 : 0
58
+ end
59
+
60
+ def render(sponsors)
61
+ written = Renderer.render(sponsors, out: @dir, force: @force)
62
+ @out.puts(written.empty? ? "sponsor badges up to date" : "rendered #{written.size} file(s)")
63
+ 0
64
+ end
65
+
66
+ def parser
67
+ OptionParser.new do |o|
68
+ o.banner = "Usage: kettle-sponsors [options]"
69
+ o.on("--config PATH", "kettle-jem.yml or registry (default #{CONFIG_PATH})") { |v| @config = v }
70
+ o.on("--out DIR", "output directory (default #{LOCAL_DIR})") { |v| @dir = v }
71
+ o.on("--check", "exit 1 if badges are stale, 0 if current") { @mode = :check }
72
+ o.on("--verify", "HEAD-check central badge URLs (network)") { @mode = :verify }
73
+ o.on("--from PATH", "publish: source config with sponsors") { |v| @from = v }
74
+ o.on("--registry PATH", "publish: registry file to merge into") { |v| @registry = v }
75
+ o.on("--force", "render even when unchanged") { @force = true }
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "date"
5
+
6
+ module Kettle
7
+ module Sponsors
8
+ # Loads sponsors from a kettle-jem config file or a standalone registry.
9
+ module Config
10
+ module_function
11
+
12
+ # @param path [String] kettle-jem.yml (sponsors under readme.corporate_sponsors)
13
+ # or a registry file with a top-level +sponsors:+ list.
14
+ # @return [Array<Sponsor>] all sponsors, active or not, in file order
15
+ def load(path)
16
+ return [] unless File.file?(path)
17
+
18
+ data = YAML.safe_load_file(path, permitted_classes: [Date], aliases: true) || {}
19
+ entries = data["sponsors"] || data.dig("readme", "corporate_sponsors") ||
20
+ data.dig("sponsorships", "corporate") || data["corporate_sponsors"]
21
+ sponsors = Array(entries).map { |entry| Sponsor.from_h(entry) }
22
+ dupes = sponsors.group_by(&:slug).select { |_, list| list.size > 1 }.keys
23
+ raise Error, "sponsor slug collision: #{dupes.join(", ")}" unless dupes.empty?
24
+
25
+ sponsors
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
5
+ module Kettle
6
+ module Sponsors
7
+ # Builds the linked, responsive badge markup:
8
+ # <a href><picture><source phone><img desktop></picture></a>
9
+ # Badges are concatenated without whitespace so they tile as a grid.
10
+ module Markup
11
+ module_function
12
+
13
+ # @param base [String] URL or relative path of the directory holding the SVGs
14
+ def badge(sponsor, base:, width: "25%")
15
+ base = base.chomp("/")
16
+ alt = CGI.escapeHTML("Sponsor: #{sponsor.name}#{" - #{sponsor.description}" unless sponsor.description.empty?}")
17
+ picture = %(<picture><source media="(max-width: 600px)" srcset="#{base}/phone/#{sponsor.slug}.svg">) +
18
+ %(<img src="#{base}/#{sponsor.slug}.svg" width="#{width}" align="top" alt="#{alt}"></picture>)
19
+ return picture if sponsor.href.empty?
20
+
21
+ %(<a href="#{CGI.escapeHTML(sponsor.href)}">#{picture}</a>)
22
+ end
23
+
24
+ def badges(sponsors, central_base: CENTRAL_BASE, local_base: LOCAL_DIR, width: "25%", today: Date.today)
25
+ sponsors.select { |s| s.active?(today) }.map do |s|
26
+ badge(s, base: s.local? ? local_base : central_base, width: width)
27
+ end.join
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kettle
4
+ module Sponsors
5
+ # kettle-jem plugin: renders locally-hosted badges after template files exist.
6
+ # Central badges need no files; they are referenced by URL.
7
+ module Plugin
8
+ module_function
9
+
10
+ def register!(registrar)
11
+ registrar.after_phase(:remaining_files) do |context:, **|
12
+ render_local(context)
13
+ end
14
+ end
15
+
16
+ def render_local(context)
17
+ sponsors = Config.load(File.join(context.project_root, CONFIG_PATH))
18
+ local = sponsors.select(&:local?)
19
+ return if local.empty?
20
+
21
+ out = File.join(context.project_root, LOCAL_DIR)
22
+ Renderer.render(local, out: out).each do |path|
23
+ context.helpers.record_template_result(path, :replace)
24
+ end
25
+ context.out.report_detail("[kettle-sponsors] #{local.size} local sponsor badge(s) up to date")
26
+ rescue Error => e
27
+ context.out.warning("[kettle-sponsors] #{e.message}")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "fileutils"
5
+
6
+ module Kettle
7
+ module Sponsors
8
+ # A registry is a YAML file with a top-level +sponsors:+ list, one entry per
9
+ # slug. It is the input of the central badge host (logos.galtzo.com).
10
+ module Registry
11
+ HEADER = "# Sponsor registry. Merged by `kettle-sponsors publish`; badges are rendered from it.\n"
12
+
13
+ module_function
14
+
15
+ # Merge +incoming+ into +existing+ by slug. Incoming wins; registry-only
16
+ # entries are kept. Output is sorted by slug so it is deterministic.
17
+ def merge(existing, incoming)
18
+ by_slug = existing.to_h { |s| [s.slug, s] }
19
+ incoming.each { |s| by_slug[s.slug] = s }
20
+ by_slug.sort.map(&:last)
21
+ end
22
+
23
+ def dump(sponsors)
24
+ HEADER + {"sponsors" => sponsors.map(&:to_registry_h)}.to_yaml.sub(/\A---\s*\n/, "")
25
+ end
26
+
27
+ # @return [Boolean] true when the registry file changed
28
+ def publish(from:, registry:)
29
+ incoming = Config.load(from)
30
+ raise Error, "no sponsors found in #{from}" if incoming.empty?
31
+
32
+ output = dump(merge(Config.load(registry), incoming))
33
+ return false if File.file?(registry) && File.read(registry) == output
34
+
35
+ FileUtils.mkdir_p(File.dirname(registry))
36
+ File.write(registry, output)
37
+ true
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module Kettle
6
+ module Sponsors
7
+ # Writes desktop and phone SVGs for a set of sponsors into a directory,
8
+ # skipping all work when the active-sponsor checksum is unchanged.
9
+ module Renderer
10
+ module_function
11
+
12
+ # @return [Array<String>] paths written (empty when up to date)
13
+ def render(sponsors, out:, today: Date.today, force: false)
14
+ active = sponsors.select { |s| s.active?(today) }
15
+ sum = Checksum.compute(sponsors, today: today)
16
+ return [] if !force && Checksum.current?(out, sum)
17
+
18
+ FileUtils.mkdir_p(File.join(out, "phone"))
19
+ written = []
20
+ # Stale files (expired or removed sponsors) are removed so output matches config.
21
+ Dir[File.join(out, "{,phone/}*.svg")].each { |f| File.delete(f) }
22
+ active.each do |s|
23
+ {"" => :desktop, "phone" => :phone}.each do |sub, variant|
24
+ path = File.join(out, sub, "#{s.slug}.svg")
25
+ File.write(path, BadgeRenderer.svg(s, variant: variant))
26
+ written << path
27
+ end
28
+ end
29
+ Checksum.write(out, sum)
30
+ written
31
+ end
32
+
33
+ def stale?(sponsors, out:, today: Date.today)
34
+ !Checksum.current?(out, Checksum.compute(sponsors, today: today))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Kettle
6
+ module Sponsors
7
+ # One sponsor entry, normalized from a +corporate_sponsors+ hash.
8
+ class Sponsor
9
+ BADGE_MODES = %w[central local].freeze
10
+
11
+ attr_reader :name, :href, :since, :until_date, :description, :badge
12
+
13
+ def self.from_h(hash)
14
+ new(
15
+ name: hash["name"],
16
+ href: hash["target_href"] || hash["url"],
17
+ since: hash["since"],
18
+ until_date: hash["until"],
19
+ description: hash["description"],
20
+ badge: hash["badge"]
21
+ )
22
+ end
23
+
24
+ def initialize(name:, href:, since: nil, until_date: nil, description: nil, badge: nil)
25
+ @name = name.to_s.strip
26
+ raise Error, "sponsor requires a name" if @name.empty?
27
+
28
+ @href = href.to_s.strip
29
+ raise Error, "sponsor #{@name.inspect} has a non-http(s) link: #{@href.inspect}" unless @href.empty? || @href.match?(%r{\Ahttps?://}i)
30
+
31
+ @since = parse_date(since)
32
+ @until_date = parse_date(until_date)
33
+ @description = description.to_s.strip
34
+ @badge = (badge || "central").to_s
35
+ raise Error, "sponsor #{@name.inspect} has unknown badge mode #{@badge.inspect}" unless BADGE_MODES.include?(@badge)
36
+ end
37
+
38
+ def slug
39
+ name.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-|-\z/, "")
40
+ end
41
+
42
+ def local?
43
+ badge == "local"
44
+ end
45
+
46
+ # Active when today is within the inclusive since/until window.
47
+ def active?(today = Date.today)
48
+ (since.nil? || since <= today) && (until_date.nil? || until_date >= today)
49
+ end
50
+
51
+ # Stable representation used for checksumming.
52
+ def to_h
53
+ {
54
+ "name" => name, "href" => href, "since" => since&.iso8601,
55
+ "until" => until_date&.iso8601, "description" => description, "badge" => badge
56
+ }
57
+ end
58
+
59
+ # Registry form: the keys Config reads back, nils and blanks omitted.
60
+ def to_registry_h
61
+ {
62
+ "name" => name, "target_href" => href, "since" => since&.iso8601,
63
+ "until" => until_date&.iso8601, "description" => description,
64
+ "badge" => (badge unless badge == "central")
65
+ }.reject { |_, v| v.nil? || v == "" }
66
+ end
67
+
68
+ private
69
+
70
+ def parse_date(value)
71
+ return if value.nil? || value.to_s.strip.empty?
72
+ return value if value.is_a?(Date)
73
+
74
+ Date.parse(value.to_s)
75
+ rescue Date::Error
76
+ raise Error, "sponsor #{name.inspect} has an invalid date: #{value.inspect}"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+
6
+ module Kettle
7
+ module Sponsors
8
+ # Opt-in check that central badges exist. Never runs during templating.
9
+ module Verifier
10
+ module_function
11
+
12
+ # @return [Hash{String=>Boolean}] central URL => reachable
13
+ def verify(sponsors, base: CENTRAL_BASE, today: Date.today, http: method(:head))
14
+ sponsors.select { |s| s.active?(today) && !s.local? }.each_with_object({}) do |s, result|
15
+ %W[#{base}/#{s.slug}.svg #{base}/phone/#{s.slug}.svg].each { |url| result[url] = http.call(url) }
16
+ end
17
+ end
18
+
19
+ def head(url)
20
+ uri = URI(url)
21
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 5, read_timeout: 5) do |h|
22
+ h.head(uri.path).is_a?(Net::HTTPSuccess)
23
+ end
24
+ rescue SystemCallError, Net::OpenTimeout, Net::ReadTimeout, SocketError
25
+ false
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kettle
4
+ module Sponsors
5
+ # Version namespace for this gem.
6
+ module Version
7
+ # Current gem version.
8
+ VERSION = "0.1.0"
9
+ end
10
+ # Current gem version exposed at the traditional constant location.
11
+ VERSION = Version::VERSION # Traditional Constant Location
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "version_gem"
4
+ require_relative "sponsors/version"
5
+
6
+ module Kettle
7
+ # Sponsor badges for kettle-jem managed projects.
8
+ #
9
+ # Sponsors are read from +readme.corporate_sponsors+ in
10
+ # +.structuredmerge/kettle-jem.yml+. By default each badge is served from the
11
+ # central logos site; sponsors marked +badge: local+ are rendered into the
12
+ # project instead.
13
+ module Sponsors
14
+ class Error < StandardError; end
15
+
16
+ # Base URL of the central badge host (https://logos.galtzo.com).
17
+ CENTRAL_BASE = "https://logos.galtzo.com/assets/images/sponsors"
18
+ # Default directory (relative to the project root) for locally rendered badges.
19
+ LOCAL_DIR = "docs/assets/images/sponsors"
20
+ CONFIG_PATH = ".structuredmerge/kettle-jem.yml"
21
+
22
+ autoload :Sponsor, "kettle/sponsors/sponsor"
23
+ autoload :Config, "kettle/sponsors/config"
24
+ autoload :Checksum, "kettle/sponsors/checksum"
25
+ autoload :BadgeRenderer, "kettle/sponsors/badge_renderer"
26
+ autoload :Markup, "kettle/sponsors/markup"
27
+ autoload :Renderer, "kettle/sponsors/renderer"
28
+ autoload :Registry, "kettle/sponsors/registry"
29
+ autoload :Verifier, "kettle/sponsors/verifier"
30
+ autoload :CLI, "kettle/sponsors/cli"
31
+ autoload :Plugin, "kettle/sponsors/plugin"
32
+
33
+ class << self
34
+ # Entry point called by kettle-jem when this gem is listed under +plugins:+.
35
+ def register_kettle_jem_plugin(registrar)
36
+ Plugin.register!(registrar)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ Kettle::Sponsors::Version.class_eval do
43
+ extend VersionGem::Basic
44
+ end
@@ -0,0 +1,8 @@
1
+ module Kettle
2
+ module Sponsors
3
+ module Version
4
+ VERSION: String
5
+ end
6
+ VERSION: String
7
+ end
8
+ end
data.tar.gz.sig ADDED
@@ -0,0 +1,4 @@
1
+ x�5K4oV|f�u�J^���߷Y9vz83ܒK�6.FI��]��)�~�S\�Fu㎶�x�s�H�v�����T���gNd���G��V�Q(_�'|�FWj����<���P�L�|������HN,�=�j���
2
+ oxx篥rF��'�W�v9�L���Hq����3�LC%�3Yz�Y���SM��LKf�LY�\@��X4�f��>iA xҩ����.2ԮrI o�G�?�Td�x�9?�zn��r�;� ��1!jl�[!�%�nN���%�^��ށm�j���5��#�PΌ[~��K��F �v���1
3
+ ql��˖ZR��V�Wbq�4�
4
+ @��5�I����4S�ure�*i�6��E]�]�/� ���e9m�_�m��᪇1��R�