PWA Manifest Generator — C# 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 C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// pwa-manifest — build a fixed-key-order manifest.json from form fields.
//
// Language: C# (.NET 8), standard library only (this manifest builder is pure
// string work — no external packages exist to reference)
// CosmoDev polyglot showcase port of the `pwa-manifest-generator` tool.
// Ported from src/lib/pwa-manifest.ts — display source, part of CosmoDev's
// polyglot tool pages. Mirrors this dir's java.java structure 1:1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CosmoDev.PwaManifestGenerator;
/// <summary>Fixed-key-order manifest.json builder ported from src/lib/pwa-manifest.ts.</summary>
public static class PwaManifest
{
/// <summary>Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts.</summary>
public sealed class ManifestInput
{
public string Name { get; set; } = "";
public string ShortName { get; set; } = "";
public string Description { get; set; } = "";
public string StartUrl { get; set; } = "";
public string Display { get; set; } = ""; // "" means absent
public string Orientation { get; set; } = ""; // "" means absent
public string BackgroundColor { get; set; } = "";
public string ThemeColor { get; set; } = "";
/// <summary>Null means "not provided" → default [192, 512]; an explicit
/// empty list emits an empty icons array — the same distinction the TS
/// lib draws with `??`.</summary>
public IReadOnlyList<long>? IconSizes { get; set; } = null;
/// <summary>Marks every icon purpose "maskable" instead of "any".</summary>
public bool Maskable { get; set; }
}
/// <summary>Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts.</summary>
public sealed class ManifestResult
{
public bool Ok { get; set; }
public List<string> Errors { get; } = new();
public List<string> Warnings { get; } = new();
public string Json { get; set; } = "";
}
// Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/.
private static readonly Regex Hex = new("^#[0-9a-fA-F]{6}$", RegexOptions.Compiled);
/// <summary>
/// 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.
/// </summary>
internal static string JsonString(string s)
{
var out_ = new StringBuilder(s.Length + 2);
out_.Append('"');
foreach (var c in s)
{
switch (c)
{
case '"': out_.Append("\\\""); break;
case '\\': out_.Append("\\\\"); break;
case '\b': out_.Append("\\b"); break;
case '\f': out_.Append("\\f"); break;
case '\n': out_.Append("\\n"); break;
case '\r': out_.Append("\\r"); break;
case '\t': out_.Append("\\t"); break;
default:
if (c < 0x20) out_.AppendFormat("\\u{0:x4}", (int)c);
else out_.Append(c);
break;
}
}
out_.Append('"');
return out_.ToString();
}
/// <summary>The C# twin of buildManifest() in src/lib/pwa-manifest.ts.</summary>
public static ManifestResult BuildManifest(ManifestInput input)
{
var result = new ManifestResult();
static string Trim(string? s) => (s ?? "").Trim();
var name = Trim(input.Name);
var shortName = Trim(input.ShortName);
var description = Trim(input.Description);
var startUrl = Trim(input.StartUrl);
var backgroundColor = Trim(input.BackgroundColor);
var themeColor = Trim(input.ThemeColor);
IReadOnlyList<long> sizes = input.IconSizes ?? new long[] { 192, 512 };
if (name.Length == 0) result.Errors.Add("name is required.");
if (backgroundColor.Length != 0 && !Hex.IsMatch(backgroundColor))
result.Errors.Add("background_color must be a #rrggbb hex value.");
if (themeColor.Length != 0 && !Hex.IsMatch(themeColor))
result.Errors.Add("theme_color must be a #rrggbb hex value.");
if (sizes.Any(s => s <= 0)) result.Errors.Add("icon sizes must be positive integers.");
var seenSizes = new HashSet<long>();
foreach (var s in sizes)
{
if (!seenSizes.Add(s)) result.Errors.Add($"icon size {s} is listed twice.");
}
if (result.Errors.Count > 0)
{
result.Ok = false;
result.Warnings.Clear();
result.Json = "";
return result;
}
var shortNameLen = shortName.EnumerateRunes().Count();
if (shortNameLen > 12)
{
result.Warnings.Add($"short_name is {shortNameLen} characters — home-screen labels truncate around 12.");
}
var lines = new List<string>();
void Add(string key, string value) => lines.Add(" " + JsonString(key) + ": " + JsonString(value));
Add("name", name);
if (shortName.Length != 0) Add("short_name", shortName);
if (description.Length != 0) Add("description", description);
if (startUrl.Length != 0) Add("start_url", startUrl);
if (input.Display.Length != 0) Add("display", input.Display);
if (input.Orientation.Length != 0) Add("orientation", input.Orientation);
if (backgroundColor.Length != 0) Add("background_color", backgroundColor);
if (themeColor.Length != 0) Add("theme_color", themeColor);
var purpose = input.Maskable ? "maskable" : "any";
var iconEntries = sizes.Select(s => " {\n"
+ " \"src\": " + JsonString($"icons/icon-{s}x{s}.png") + ",\n"
+ " \"sizes\": " + JsonString($"{s}x{s}") + ",\n"
+ " \"type\": \"image/png\",\n"
+ " \"purpose\": " + JsonString(purpose) + "\n"
+ " }").ToList();
var doc = new StringBuilder();
doc.Append("{\n");
foreach (var line in lines) doc.Append(line).Append(",\n");
if (iconEntries.Count == 0)
{
doc.Append(" \"icons\": []"); // JSON.stringify renders an empty array inline
}
else
{
doc.Append(" \"icons\": [\n");
doc.Append(string.Join(",\n", iconEntries));
doc.Append("\n ]");
}
doc.Append("\n}\n");
result.Ok = true;
result.Json = doc.ToString();
return result;
}
}
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 →