Skip to content

PWA Manifest Generator — Kotlin 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 Kotlin 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: Kotlin (1.9+), standard library only (this manifest builder is
// pure string work — no external packages exist to link)
// 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. 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. */
data class ManifestInput(
    val name: String = "",
    val shortName: String = "",
    val description: String = "",
    val startUrl: String = "",
    val display: String = "",         // "" means absent
    val orientation: String = "",     // "" means absent
    val backgroundColor: String = "",
    val themeColor: String = "",
    /** Null means "not provided" → default [192, 512]; an explicit empty
     *  list emits an empty icons array — the same distinction the TS lib
     *  draws with `??`. */
    val iconSizes: List<Long>? = null,
    /** Marks every icon purpose "maskable" instead of "any". */
    val maskable: Boolean = false,
)

/** Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts. */
data class ManifestResult(
    val ok: Boolean,
    val errors: List<String> = emptyList(),
    val warnings: List<String> = emptyList(),
    val json: String = "",
)

private val hexRe = Regex("^#[0-9a-fA-F]{6}$")

/** Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/. */
private fun isHexColor(s: String): Boolean = hexRe.matches(s)

/**
 * 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.
 */
fun jsonString(s: String): String {
    val out = StringBuilder(s.length + 2)
    out.append('"')
    for (c in s) {
        when (c) {
            '"' -> out.append("\\\"")
            '\\' -> out.append("\\\\")
            '\b' -> out.append("\\b")
            '\u000C' -> out.append("\\f")
            '\n' -> out.append("\\n")
            '\r' -> out.append("\\r")
            '\t' -> out.append("\\t")
            else ->
                if (c < ' ') out.append("\\u%04x".format(c.code))
                else out.append(c)
        }
    }
    out.append('"')
    return out.toString()
}

/** The Kotlin twin of buildManifest() in src/lib/pwa-manifest.ts. */
fun buildManifest(input: ManifestInput): ManifestResult {
    val errors = mutableListOf<String>()
    val warnings = mutableListOf<String>()

    val name = input.name.trim()
    val shortName = input.shortName.trim()
    val description = input.description.trim()
    val startUrl = input.startUrl.trim()
    val backgroundColor = input.backgroundColor.trim()
    val themeColor = input.themeColor.trim()
    val sizes = input.iconSizes ?: listOf(192L, 512L)

    if (name.isEmpty()) errors.add("name is required.")
    if (backgroundColor.isNotEmpty() && !isHexColor(backgroundColor))
        errors.add("background_color must be a #rrggbb hex value.")
    if (themeColor.isNotEmpty() && !isHexColor(themeColor))
        errors.add("theme_color must be a #rrggbb hex value.")
    if (sizes.any { it <= 0 }) errors.add("icon sizes must be positive integers.")
    val seenSizes = mutableSetOf<Long>()
    for (s in sizes) {
        if (!seenSizes.add(s)) errors.add("icon size $s is listed twice.")
    }
    if (errors.isNotEmpty()) {
        return ManifestResult(ok = false, errors = errors, warnings = emptyList(), json = "")
    }

    val shortNameLen = shortName.codePointCount(0, shortName.length)
    if (shortNameLen > 12) {
        warnings.add("short_name is $shortNameLen characters — home-screen labels truncate around 12.")
    }

    val lines = mutableListOf<String>()
    val add = { key: String, value: String ->
        lines.add("  " + jsonString(key) + ": " + jsonString(value))
    }
    add("name", name)
    if (shortName.isNotEmpty()) add("short_name", shortName)
    if (description.isNotEmpty()) add("description", description)
    if (startUrl.isNotEmpty()) add("start_url", startUrl)
    if (input.display.isNotEmpty()) add("display", input.display)
    if (input.orientation.isNotEmpty()) add("orientation", input.orientation)
    if (backgroundColor.isNotEmpty()) add("background_color", backgroundColor)
    if (themeColor.isNotEmpty()) add("theme_color", themeColor)

    val purpose = if (input.maskable) "maskable" else "any"
    val iconEntries = sizes.map { 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" +
            "    }"
    }

    val doc = StringBuilder()
    doc.append("{\n")
    for (line in lines) doc.append(line).append(",\n")
    if (iconEntries.isEmpty()) {
        doc.append("  \"icons\": []") // JSON.stringify renders an empty array inline
    } else {
        doc.append("  \"icons\": [\n")
        doc.append(iconEntries.joinToString(",\n"))
        doc.append("\n  ]")
    }
    doc.append("\n}\n")

    return ManifestResult(ok = true, errors = errors, warnings = warnings, json = doc.toString())
}

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 →