Skip to content

PWA Manifest Generator — Ruby source

Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.

This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.

# frozen_string_literal: true

# pwa-manifest — build a fixed-key-order manifest.json from form fields.
# Authored snippet mirroring src/lib/pwa-manifest.ts: same fixed key order
# (name, short_name, description, start_url, display, orientation,
# background_color, theme_color, icons), blank optional fields omitted, icon
# sizes defaulting to [192, 512], hard errors suppressing emission, and the
# short_name >12 warning never blocking. Same logic as this dir's
# typescript.ts and the live Go twin at
# cli/pwa-manifest-generator/pwa-manifest-generator.go.

# Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts. icon_sizes
# nil falls back to [192, 512]; an explicit empty array emits an empty icons
# array — the same distinction the TS lib draws with `??`.
ManifestInput = Struct.new(
  :name, :short_name, :description, :start_url, :display, :orientation,
  :background_color, :theme_color, :icon_sizes, :maskable,
  keyword_init: true
)

# Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts.
ManifestResult = Struct.new(:ok, :errors, :warnings, :json, keyword_init: true)

HEX = /\A#[0-9a-fA-F]{6}\Z/
DEFAULT_ICON_SIZES = [192, 512].freeze

# Double-quote s exactly like JavaScript's JSON.stringify: escape ", \, and
# the control characters (short escapes for \b \f \n \r \t, \u00xx for the
# rest), leave everything else — including non-ASCII — untouched.
def json_string(s)
  out = +'"'
  s.each_char do |ch|
    case ch
    when '"' then out << '\\"'
    when '\\' then out << '\\\\'
    when "\b" then out << '\\b'
    when "\f" then out << '\\f'
    when "\n" then out << '\\n'
    when "\r" then out << '\\r'
    when "\t" then out << '\\t'
    else
      code = ch.ord
      out << format('\\u%04x', code) if code < 0x20
      out << ch if code >= 0x20
    end
  end
  out << '"'
  out
end

# The Ruby twin of buildManifest() in src/lib/pwa-manifest.ts.
def build_manifest(input)
  errors = []
  warnings = []

  name = input[:name].to_s.strip
  short_name = input[:short_name].to_s.strip
  description = input[:description].to_s.strip
  start_url = input[:start_url].to_s.strip
  background_color = input[:background_color].to_s.strip
  theme_color = input[:theme_color].to_s.strip
  sizes = input[:icon_sizes].nil? ? DEFAULT_ICON_SIZES : input[:icon_sizes]

  errors << 'name is required.' if name.empty?
  errors << 'background_color must be a #rrggbb hex value.' if !background_color.empty? && background_color !~ HEX
  errors << 'theme_color must be a #rrggbb hex value.' if !theme_color.empty? && theme_color !~ HEX
  errors << 'icon sizes must be positive integers.' if sizes.any? { |s| !s.is_a?(Integer) || s <= 0 }
  seen = {}
  sizes.each do |s|
    errors << "icon size #{s} is listed twice." if seen[s]
    seen[s] = true
  end
  return ManifestResult.new(ok: false, errors: errors, warnings: [], json: '') unless errors.empty?

  warnings << "short_name is #{short_name.length} characters — home-screen labels truncate around 12." if short_name.length > 12

  lines = []
  add = ->(key, value) { lines << '  ' + json_string(key) + ': ' + json_string(value) }
  add.call('name', name)
  add.call('short_name', short_name) unless short_name.empty?
  add.call('description', description) unless description.empty?
  add.call('start_url', start_url) unless start_url.empty?
  add.call('display', input[:display]) unless input[:display].to_s.empty?
  add.call('orientation', input[:orientation]) unless input[:orientation].to_s.empty?
  add.call('background_color', background_color) unless background_color.empty?
  add.call('theme_color', theme_color) unless theme_color.empty?

  purpose = input[:maskable] ? 'maskable' : 'any'
  icon_entries = sizes.map do |s|
    '    {' + "\n" +
      '      "src": ' + json_string("icons/icon-#{s}x#{s}.png") + ",\n" +
      '      "sizes": ' + json_string("#{s}x#{s}") + ",\n" +
      '      "type": "image/png",' + "\n" +
      '      "purpose": ' + json_string(purpose) + "\n" +
      '    }'
  end
  # JSON.stringify renders an empty array inline.
  lines << (icon_entries.empty? ? '  "icons": []' : '  "icons": [' + "\n" + icon_entries.join(",\n") + "\n  ]")

  ManifestResult.new(ok: true, errors: errors, warnings: warnings, json: "{\n" + lines.join(",\n") + "\n}\n")
end

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →