Guidedog मैनुअल 0.6.3
भाषा
इस पृष्ठ पर
Guidedog / दस्तावेज़ 0.6.3

Native plugin लिखें

Plugin, Guidedog में एक सेवा जोड़ता है। Build plugin मूल पाठ या तैयार HTML पेज को बदल सकता है और स्थिर फ़ाइलें दे सकता है। Publisher तैयार साइट को उसके गंतव्य पर भेजता है। ये सेवाएँ Odin की सामान्य procedure tables का उपयोग करती हैं।

इस अध्याय में उत्पाद का badge बनाएँगे। यह मूल पाठ में {{product}} की जगह नाम रखता है, HTML पेज में लेबल जोड़ता है और उसका CSS देता है। फिर GitHub Pages से समझेंगे कि यही आधार बाहरी सिस्टम पर असर डालने वाली सेवा को कैसे संभालता है।

1. इंटरफ़ेस की सीमा समझें

plugins.Plugin plugin का विवरण देता है। source, html और assets procedures वैकल्पिक हैं। nil procedure का अर्थ है कि वह सेवा उपलब्ध नहीं है। load निजी स्थिति तैयार करता है और unload संसाधन बंद करता है। बिल्ड का नियंत्रण host के पास रहता है।

Source hooks parsing से पहले चलते हैं। HTML hooks और बने हुए assets staging में जाते हैं। प्रकाशन सफल बिल्ड के बाद होता है।
चित्र 19 बिल्ड सेवाएँ प्रकाशन से पहले चलती हैं।

सोर्स हुक हर मुख्य RST या Markdown दस्तावेज़ के पार्स होने से पहले चलता है, PDF और टेक्स्ट में भी। include इंजन पढ़ता है। HTML हुक को इंडेक्स और अतिरिक्त पृष्ठ समेत पूरा HTML मिलता है। बने एसेट केवल HTML के लिए _static/plugins/ID/ में हैं। होस्ट दस्तावेज़ पढ़ने से पहले प्लगइन सेटअप में इन्हें एक बार लेता है। वे उस बिल्ड में तय रहते हैं; बाद के हुक और एसेट नहीं जोड़ सकते।

Plugin table में build hooks या publisher में से एक होता है, दोनों नहीं। सामान्य बिल्ड publishing service को नहीं बुला सकता। स्थापित plugins भरोसेमंद native programs होते हैं। उन्हें स्पष्ट रूप से register करने पर executable का SHA-256 तय होता है। यह sandbox नहीं है।

2. चलने वाला उदाहरण compile करें

Checkout के मूल फ़ोल्डर से शुरू करें। Repository के build निर्देशों से Guidedog बनाएँ और उसी host का उपयोग करें। उदाहरण का package examples/build-plugin/badge में है। Executable का entry point उससे एक फ़ोल्डर ऊपर है।

macOS या Linux पर चलाएँ:

odin build examples/build-plugin -vet -strict-style -out:build/product-badge
build/guidedog quickstart build/plugin-tutorial -q -p Orbit -a Team

Windows पर PowerShell में ये commands चलाएँ:

odin build examples/build-plugin -vet -strict-style -out:build/product-badge.exe
build/guidedog.exe quickstart build/plugin-tutorial -q -p Orbit -a Team

पहला command executable और दूसरा documentation project बनाता है। Configuration के बाद plugin register करें। पैकेज किए गए plugin के उपयोगकर्ता को Odin compiler नहीं चाहिए।

3. प्रोजेक्ट के लिए इसे चुनें

build/plugin-tutorial/conf.toml को इससे बदलें:

project = "Orbit"
plugins = ["product-badge"]
plugin_timeout = 60

[plugin_configs]
product-badge = "badge.toml"

build/plugin-tutorial/badge.toml बनाएँ:

product = "Orbit"

build/plugin-tutorial/index.rst को इससे बदलें:

Welcome
=======

Hello {{product}}.

Configuration paths का आधार प्रोजेक्ट का configuration फ़ोल्डर है। Plugins, plugins में दिए क्रम से चलते हैं। केवल register करने से program नहीं चलता।

Configuration लिखने के बाद register करें। Registration checksum दर्ज करने से पहले child processes में build plugin जाँचता है। macOS या Linux पर:

build/guidedog plugin register product-badge ./build/product-badge \
  --project=build/plugin-tutorial --config=badge.toml

Windows पर एक PowerShell command इस्तेमाल करें:

build/guidedog.exe plugin register product-badge ./build/product-badge.exe --project=build/plugin-tutorial --config=badge.toml

4. बिल्ड करें और परिणाम देखें

macOS या Linux पर चलाएँ:

build/guidedog build html build/plugin-tutorial -W
build/guidedog serve build/plugin-tutorial

Windows पर चलाएँ:

build/guidedog.exe build html build/plugin-tutorial -W
build/guidedog.exe serve build/plugin-tutorial

सर्वर का पता खोलें। “Hello Orbit.” और Orbit बैज दिखेंगे; CSS _static/plugins/product-badge/banner.css है। नाम Comet करके दोबारा बिल्ड करें: टेक्स्ट और बैज बदलेंगे। बिना बदलाव अगला बिल्ड फ़ाइलें नहीं लिखेगा।

5. Implementation पढ़ें

नीचे पूरा package वही source है जिसे चरण 2 में compile किया था। स्थानीय प्रतिलिपि के लिए badge.odin और main.odin डाउनलोड करें। फ़ोल्डर संरचना बनाए रखें या अपने प्रोजेक्ट के लिए relative imports बदलें।

// A build plugin: typed source and HTML hooks, plus a generated CSS asset.
// Compile from the checkout root:
//     odin build examples/build-plugin -out:build/product-badge
package product_badge

import "core:fmt"
import "core:strings"
import "../../../lib/plugins"
import "../../../lib/toml"

Settings :: struct { product: string }

// plugin describes services. Nil procedures mean services this plugin does not offer.
plugin :: proc() -> plugins.Plugin {
    return {id = "product-badge", name = "Product badge", version = "1.0.0",
        api = plugins.PLUGIN_API_VERSION, load = load, source = source,
        html = html, assets = assets}
}

load :: proc(ctx: ^plugins.Plugin_Load_Context) -> plugins.Problem {
    text, problem := plugins.read_plugin_config(ctx)
    if problem.exit != 0 do return problem
    root, error := toml.parse(text)
    path := ctx.session.options.config
    if error.line != 0 do return {"badge.toml", "Cannot read the badge settings",
        fmt.aprintf("%s:%d", path, error.line), error.message,
        "Write product = \"Orbit\" in this TOML file.", 2}
    product := toml.get(&root, "product")
    if product == nil || product.kind != .String || len(product.text) == 0 ||
       len(product.text) > 256 || strings.contains_any(product.text, "\r\n\x00") {
        return {"badge.product", "The product name is missing or invalid", path,
            "product must be a nonempty string on one line, at most 256 bytes long.",
            "Set product = \"Orbit\" in this file.", 2}
    }
    settings, err := new(Settings, ctx.allocator)
    if err != nil do return plugins.memory()
    settings.product = product.text // the session owns the configuration's parsed text.
    ctx.session.data = settings
    return {}
}

source :: proc(ctx: ^plugins.Source_Context) -> plugins.Problem {
    settings := (^Settings)(ctx.session.data)
    marker :: "{{product}}"
    count := strings.count(ctx.input.text, marker)
    growth := len(settings.product) - len(marker)
    if growth > 0 && count > (plugins.MAX_PLUGIN_TEXT - len(ctx.input.text)) / growth {
        return {"badge.source-size", "The expanded source would be too large", ctx.input.path,
            "Replacing every product marker would exceed the 8 MiB source limit.",
            "Use fewer markers, a shorter product name, or split the document.", 2}
    }
    text, _ := strings.replace_all(ctx.input.text, "{{product}}", settings.product,
        ctx.allocator)
    if plugins.memory_refused(ctx.allocator) do return plugins.memory()
    ctx.text = text
    return {}
}

html :: proc(ctx: ^plugins.Html_Context) -> plugins.Problem {
    settings := (^Settings)(ctx.session.data)
    if !strings.contains(ctx.input.text, "</head>") ||
       !strings.contains(ctx.input.text, "</body>") {
        return {"badge.html", "The badge needs a complete HTML page", ctx.input.path,
            "The selected template has no closing head or body tag.",
            "Add </head> and </body> to the template, or disable product-badge for this build.", 2}
    }
    label := escape_html(settings.product)
    css := fmt.aprintf("<link rel=\"stylesheet\" href=\"%sbanner.css\">\n</head>",
        ctx.input.assets_url)
    badge := fmt.aprintf("<aside class=\"product-badge\" aria-label=\"Product\">%s</aside>" +
        "\n</body>", label)
    growth := len(css) + len(badge) - len("</head></body>")
    if growth > plugins.MAX_PLUGIN_TEXT - len(ctx.input.text) do return {
        "badge.html-size", "The badge would exceed the HTML limit", ctx.input.path,
        "This page has no room for the generated label and stylesheet link.",
        "Split this page or shorten the configured product name.", 2}
    head := replace_once(ctx.input.text, "</head>", css)
    ctx.text = replace_once(head, "</body>", badge)
    if plugins.memory_refused(ctx.allocator) do return plugins.memory()
    return {}
}

replace_once :: proc(text, marker, replacement: string) -> string {
    at := strings.last_index(text, marker)
    return strings.concatenate({text[:at], replacement, text[at + len(marker):]})
}

CSS :: ".product-badge { border-inline-start: 3px solid #216553; " +
    "padding: .5rem 1rem; margin: 1rem; font-weight: 700; }\n"

assets :: proc(ctx: ^plugins.Assets_Context) -> plugins.Problem {
    files, err := make([]plugins.Plugin_Asset, 1, ctx.allocator)
    if err != nil do return plugins.memory()
    files[0] = {path = "banner.css", data = transmute([]u8)string(CSS)}
    ctx.assets = files // collect_assets returns its own copy, including the static bytes.
    return {}
}

escape_html :: proc(text: string) -> string {
    escaped, _ := strings.replace_all(text, "&", "&amp;")
    escaped, _ = strings.replace_all(escaped, "<", "&lt;")
    escaped, _ = strings.replace_all(escaped, ">", "&gt;")
    return escaped
}

plugin() सार्वजनिक descriptor है। load सीमित आकार की UTF-8 configuration पढ़ता है, type और आकार जाँचता है और session में Settings के लिए memory लेता है। Source hook memory लेने से पहले विस्तार का आकार जाँचता है। यह code blocks के भीतर के markers भी बदलता है। यह पाठ का रूपांतरण है, markup parser नहीं।

HTML hook लेबल को escape करके आख़िरी closing body tag से पहले डालता है। Host का दिया assets_url उपयोग करने से nested pages में भी सही CSS link बनता है। यह छोटा उदाहरण head और body के closing tags को ठीक इसी lowercase रूप में चाहता है। मनमाना HTML बदलने वाला plugin उचित parser इस्तेमाल करे और अपना contract बताए।

Assets hook, ctx.allocator से result array के लिए memory लेता है। CSS bytes स्थिर हैं। SDK paths और bytes को result की अपनी memory में copy करता है। Callback के local array को दिखाने वाला slice कभी न लौटाएँ। Session state में hook inputs या hook results की memory न रखें।

Executable wrapper उसी descriptor को standard streams पर उपलब्ध कराता है:

// Compile from the checkout root. This entry point adapts an ordinary Odin package
// to the installed-plugin protocol; embedded applications import badge directly.
package main

import "core:fmt"
import "core:os"
import "../../lib/plugins"
import "badge"

run :: proc() -> int {
    if len(os.args) == 2 && os.args[1] == plugins.PROTOCOL_FLAG {
        return plugins.serve_plugin(badge.plugin(), os.stdin, os.stdout)
    }
    fmt.eprintln("Run this plugin through guidedog build with plugins = [\"product-badge\"].")
    return 2
}

main :: proc() { os.exit(run()) }

serve_plugin negotiation और dispatch संभालता है। Protocol output stdout पर और लोगों के लिए logs stderr पर लिखें। os.exit से पहले main में लौटें ताकि deferred cleanup चले। Embedded host descriptor को project.Load_Options.plugins में देता है और प्रोजेक्ट configuration में उसका ID चुनता है।

6. Ownership और caching स्पष्ट रखें

तालिका 30 Build plugin के डेटा के owners
Owner बनाएँ मुक्त करें
Plugin_Session load_plugin unload_plugin
Text_Result process_source या process_html release_text
Assets_Result collect_assets release_assets

Cleanup, zero, failed और पहले से मुक्त owners स्वीकार करता है। Returned data के लिए callback allocator इस्तेमाल करें। Allocation errors जाँचें। उन्हें छिपाने वाले helpers के बाद plugins.memory_refused(ctx.allocator) जाँचें। Allocation मना हो तो plugins.memory() लौटाएँ। Unload को आधा लोड हुआ session भी संभालना चाहिए।

Load के दौरान read_plugin_config और read_plugin_file इस्तेमाल करें। वे वास्तव में पढ़े गए bytes की fingerprint दर्ज करते हैं और dependencies घोषित करते हैं। लोड की गई स्थिति पूरे बिल्ड में न बदलें। अगर दूसरी फ़ाइलें खुद पढ़ते हैं तो उनके paths session.dependencies में दें और पढ़े गए मूल्यों की fingerprint session.cache_key में जोड़ें। उपयोग किए गए environment values भी जोड़ें। SDK की fingerprints न मिटाएँ और बिना घोषित बदलते डेटा पर निर्भर न रहें।

Host cache invalidation में पहचान, configuration, dependencies, cache key, builder, भाषा और generated assets शामिल करता है। हर बिल्ड के निजी sessions होते हैं। Reading workers callbacks को क्रम से चलाते हैं, इसलिए callbacks का thread-safe होना आवश्यक नहीं। Process deadline से native callback को रोका नहीं जा सकता। स्थापित programs के पूरे request पर समय सीमा होती है, जिसमें pipe writes भी शामिल हैं।

Guidedog प्लगइन लोड करके HTML एसेट लेता है, फिर सोर्स पढ़ता है। पढ़ने और रेंडरिंग में हुक चलते हैं। सफलता या असफलता, दोनों में सफ़ाई होती है।
चित्र 20 इंस्टॉल किए बिल्ड प्लगइन का जीवनचक्र। एसेट सेटअप के समय लिए जाते हैं।

हर hook में text की सीमा 8 MiB है। प्रत्येक plugin अधिकतम 256 assets और कुल 16 MiB bytes दे सकता है। Paths portable और relative होने चाहिए। Duplicate names, केवल case में अंतर, path traversal और प्रोजेक्ट फ़ाइलों से टकराव मना हैं। --untrusted build plugins बंद करता है। Child-process memory, host के build budget के बाहर है। Native plugin को allocations और अपनी सीमाएँ फिर भी जाँचनी होंगी।

7. त्रुटियाँ समझाएँ और उनका परीक्षण करें

Problem में code, title, संबंधित path, कारण, ठोस सुधार और nonzero exit status दें। जैसे product = 42 मान्य नहीं है। Badge बताता है कि string चाहिए और product = "Orbit" सुझाता है। विस्तार के बाद markup diagnostics बताती हैं कि input को plugin ने विस्तृत किया है और उसका असली अंश दिखाती हैं। विस्तृत line numbers को मूल फ़ाइल की lines नहीं बतातीं।

Package को सीधे और उसके executable से भी जाँचें। बदली settings, बिना बदलाव के rebuild, nested links, अमान्य markup, asset conflicts, cancellation, timeouts और allocation refusal को जाँचें। असफल बिल्ड पिछली साइट को सुरक्षित रखे। Repository का integration test यही उदाहरण compile करता है और HTML, directory HTML, single-page HTML, text तथा असफल generations की जाँच करता है।

8. Publishing service जोड़ें

GitHub Pages भी explicit descriptor के इसी आधार का उपयोग करता है:

package main

import "core:fmt"
import "guidedog:pages"
import "guidedog:plugins"

main :: proc() {
    descriptor := plugins.publisher_plugin(pages.publisher())
    fmt.println(descriptor.id)
}

उसका Publisher, load, plan, publish, status और recover देता है। Session configuration और credentials का owner है। Snapshot जाँची हुई साइट फ़ाइलों का owner है। Plan snapshot की तुलना target की baseline से करता है। Result सत्यापित परिणाम बताता है। हर owner को उसकी संबंधित SDK procedure से मुक्त करें।

examples/pages-publisher/main.odin से lib/pages तक implementation देखें। Configuration और machine binding अधिकार तय करते हैं। Planning target पढ़ती है। Publishing एक conditional Git push से पहले टिकाऊ journal लिखती है। Status और recovery परिणाम स्पष्ट करते हैं। Source या HTML callback को publishing का अधिकार नहीं मिलता।

Remote acceptance का अर्थ live deployment नहीं है। Accepted और Pending का exit status 6 है। अनिश्चित write में Unknown और 7 आते हैं तथा status या recovery के निर्देश मिलते हैं। अनिश्चित write को बिना जाँच दोबारा न भेजें। पूरे workflow के लिए GitHub Pages पर प्रकाशित करें और API reference में Publisher देखें।

9. Contract का version तय करें और वितरित करें

Build-plugin API 1.0, guidedog-build-plugin 1.0 protocol उपयोग करता है। Publisher SDK 1.1, guidedog-publisher 1.0 बनाए रखता है और SDK 1.0 publishers स्वीकार करता है। ये अलग contracts हैं। पुराने hosts build plugins को publishers समझने के बजाय मना करते हैं। Product version और interface version भी अलग हैं।

हर दोबारा बने executable को सोच-समझकर register करें। guidedog plugin inspect ID उसका प्रकार और घोषित hooks दिखाता है। सार्वजनिक catalog plugins का विवरण देता है, उन्हें डाउनलोड, स्थापित या चलाता नहीं। Custom markup readers और renderers अब भी compiled core.Registry API का उपयोग करते हैं। Install किए जाने वाले custom directives, roles और builders इस build-hook interface का हिस्सा नहीं हैं।

10. Register करने से पहले plugin जाँचें

guidedog plugin check PATH --project=DIR --config=FILE बिना registry बदले build plugin जाँचता है। Registration भी यही करता है: दो नए loads, दोहराए source और HTML results, assets का validation और cleanup। --timeout=SECONDS का default हर process request के लिए 60 है। ज़रूरत हो तो असली configuration दें। Relative configuration paths का आधार --project है, जिसका default वर्तमान directory है।

जाँच पास होना साथ काम करने का प्रमाण है, पूरी सुरक्षा का नहीं। हर रनटाइम उत्तर फिर भी जाँचा जाता है और इंस्टॉल किए प्लगइन अलग प्रक्रिया में चलते हैं। क्रैश, सीमा उल्लंघन या रुकना डायग्नोस्टिक बनते हैं; अधूरा संस्करण प्रकाशित नहीं होता। पुराने पब्लिशर की संगतता अब शर्त नहीं; सीधे जुड़े नेटिव कॉलबैक अब भी भरोसेमंद कोड हैं।

Generic checks publisher नहीं बुलाते। Publishers को उनके Odin tests और स्पष्ट configuration वाले dry run से जाँचें। Parser, node model या builder स्वयं बदलना अलग design का विषय है। Modular libraries और source, application या fork के लिए उपलब्ध रहते हैं।