provisioning-outreach/presentations/rust-laspalmas-250926/auroraframe/framework/optimizers/optimize-kcl.nu

383 lines
9.8 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env nu
# KCL configuration processor for the Web Builder Framework
# Integrates KCL files for structured data and configuration
# Main KCL processing function
export def process_kcl_data [page_dir: string, config: record, verbose: bool] {
if $verbose { print "🔧 Processing KCL data files..." }
mut kcl_data = {}
# Check for KCL plugin availability
let has_kcl_plugin = (check_kcl_plugin_available $verbose)
if not $has_kcl_plugin {
if $verbose { print " ⚠️ KCL plugin not available, skipping KCL processing" }
return $kcl_data
}
# Find KCL files in page directory
let kcl_files = (find_kcl_files $page_dir $verbose)
if ($kcl_files | length) == 0 {
if $verbose { print " ⚠️ No KCL files found" }
return $kcl_data
}
# Process each KCL file
for kcl_file in $kcl_files {
let file_data = (process_kcl_file $kcl_file $verbose)
let file_key = ($kcl_file | path basename | str replace '.k' '')
$kcl_data = ($kcl_data | insert $file_key $file_data)
}
if $verbose {
print $" ✅ Processed ($kcl_files | length) KCL files"
print $" 📊 KCL data keys: ($kcl_data | columns | str join ', ')"
}
$kcl_data
}
# Check if KCL plugin is available
def check_kcl_plugin_available [verbose: bool] {
try {
# Try to run a simple KCL command
kcl version | complete | get exit_code == 0
} catch {
if $verbose { print " 💡 KCL plugin not available, install with: cargo install nu_plugin_kcl" }
false
}
}
# Find KCL files in page directory
def find_kcl_files [page_dir: string, verbose: bool] {
mut kcl_files = []
# Look for KCL files in common locations
let search_paths = [
$"($page_dir)/data",
$"($page_dir)/config",
$"($page_dir)/kcl",
$"($page_dir)"
]
for search_path in $search_paths {
if ($search_path | path exists) {
let found_files = (ls $"($search_path)/*.k" | get name | default [])
$kcl_files = ($kcl_files | append $found_files)
}
}
$kcl_files | uniq
}
# Process individual KCL file
def process_kcl_file [kcl_file: string, verbose: bool] {
if $verbose {
let relative_path = ($kcl_file | str replace (pwd) ".")
print $" 📄 Processing KCL file: ($relative_path)"
}
try {
# Use KCL plugin to compile and get JSON output
let kcl_output = (kcl run $kcl_file --format json)
$kcl_output | from json
} catch {
if $verbose { print $" ❌ Failed to process KCL file: ($kcl_file)" }
{}
}
}
# Generate example KCL files for a page
export def generate_kcl_examples [page_dir: string, page_name: string] {
let kcl_dir = $"($page_dir)/data"
mkdir $kcl_dir
# Generate site configuration KCL
let site_kcl = generate_site_kcl_example $page_name
$site_kcl | save $"($kcl_dir)/site.k"
# Generate content data KCL
let content_kcl = generate_content_kcl_example $page_name
$content_kcl | save $"($kcl_dir)/content.k"
# Generate navigation KCL
let nav_kcl = generate_navigation_kcl_example
$nav_kcl | save $"($kcl_dir)/navigation.k"
print $"✅ Generated KCL example files in ($kcl_dir)"
}
# Generate site configuration KCL example
def generate_site_kcl_example [page_name: string] {
$'# Site Configuration Schema
schema SiteConfig:
title: str
description: str
author?: str
lang: str = "en"
base_url?: str
meta: {
keywords: [str]
og_image?: str
twitter_card?: str
}
analytics?: {
google_analytics?: str
plausible?: str
}
# Site data for ($page_name)
site: SiteConfig = {
title = "($page_name | str title-case) - Web Builder Framework"
description = "Built with the Web Builder Framework using KCL for configuration"
author = "Web Builder Framework"
lang = "en"
base_url = "https://example.com"
meta = {
keywords = ["web", "framework", "kcl", "rust", "nushell"]
og_image = "/images/og-image.jpg"
twitter_card = "summary_large_image"
}
analytics = {
google_analytics = "UA-XXXXXXXX-X"
}
}'
}
# Generate content data KCL example
def generate_content_kcl_example [page_name: string] {
$'# Content Data Schema
schema ContentItem:
title: str
description?: str
image?: str
url?: str
tags?: [str]
featured?: bool = False
schema ContentSection:
heading: str
subtitle?: str
items: [ContentItem]
layout: str = "grid"
# Content data for ($page_name)
hero_section = {
title = "Welcome to ($page_name | str title-case)"
subtitle = "Powered by KCL configuration and Tera templates"
background_image = "/images/hero-bg.jpg"
cta = {
text = "Get Started"
url = "/getting-started"
style = "primary"
}
}
features: ContentSection = {
heading = "Framework Features"
subtitle = "Built with modern web technologies"
layout = "grid"
items = [
{
title = "KCL Configuration"
description = "Type-safe configuration with KCL"
image = "/icons/config.svg"
tags = ["config", "type-safe"]
featured = True
}
{
title = "Tera Templates"
description = "Powerful Jinja2-inspired templating"
image = "/icons/template.svg"
tags = ["template", "tera"]
}
{
title = "Rust Processing"
description = "Fast Markdown processing with Rust"
image = "/icons/rust.svg"
tags = ["rust", "performance"]
featured = True
}
]
}
testimonials = [
{
quote = "The Web Builder Framework makes building static sites a joy!"
author = "Jane Developer"
role = "Frontend Engineer"
avatar = "/images/avatars/jane.jpg"
}
{
quote = "KCL configuration gives me confidence in my site structure."
author = "John Architect"
role = "Solutions Architect"
avatar = "/images/avatars/john.jpg"
}
]'
}
# Generate navigation KCL example
def generate_navigation_kcl_example [] {
$'# Navigation Schema
schema NavItem:
title: str
url: str
icon?: str
external?: bool = False
children?: [NavItem]
schema Navigation:
primary: [NavItem]
secondary?: [NavItem]
footer?: [NavItem]
# Navigation configuration
navigation: Navigation = {
primary = [
{
title = "Home"
url = "/"
icon = "home"
}
{
title = "Features"
url = "/features"
icon = "star"
children = [
{ title = "KCL Integration", url = "/features/kcl" }
{ title = "Tera Templates", url = "/features/templates" }
{ title = "Rust Processing", url = "/features/rust" }
]
}
{
title = "Documentation"
url = "/docs"
icon = "book"
}
{
title = "GitHub"
url = "https://github.com/web-builder-framework"
icon = "github"
external = True
}
]
footer = [
{ title = "Privacy Policy", url = "/privacy" }
{ title = "Terms of Service", url = "/terms" }
{ title = "Contact", url = "/contact" }
]
}'
}
# Integration with template system
export def merge_kcl_with_template_data [template_data: record, kcl_data: record, verbose: bool] {
if $verbose { print "🔗 Merging KCL data with template context..." }
mut merged_data = $template_data
# Merge each KCL file data
for key in ($kcl_data | columns) {
let kcl_file_data = ($kcl_data | get $key)
# Merge at top level, KCL data takes precedence
for kcl_key in ($kcl_file_data | columns) {
$merged_data = ($merged_data | insert $kcl_key ($kcl_file_data | get $kcl_key))
}
}
if $verbose {
print $" ✅ Merged data keys: ($merged_data | columns | str join ', ')"
}
$merged_data
}'
}
# Generate KCL schema files for validation
export def generate_kcl_schemas [framework_dir: string] {
let schema_dir = $"($framework_dir)/schemas"
mkdir $schema_dir
# Page configuration schema
let page_schema = generate_page_schema
$page_schema | save $"($schema_dir)/page.k"
# Site configuration schema
let site_schema = generate_site_schema
$site_schema | save $"($schema_dir)/site.k"
print $"✅ Generated KCL schemas in ($schema_dir)"
}
def generate_page_schema [] {
$'# Page Configuration Schema for Web Builder Framework
schema PageMeta:
title: str
description?: str
author?: str
keywords?: [str]
lang?: str = "en"
published_date?: str
modified_date?: str
tags?: [str]
category?: str
reading_time?: int
schema PageTemplate:
template: str = "default.html"
layout?: str = "single-column"
theme?: str = "default"
schema PageFeatures:
toc?: bool = False
comments?: bool = False
sharing?: bool = False
related_posts?: bool = False
schema PageConfig:
meta: PageMeta
template: PageTemplate
features?: PageFeatures
custom?: {str:} # Allow custom fields'
}
def generate_site_schema [] {
$'# Site Configuration Schema for Web Builder Framework
schema SiteIdentity:
title: str
tagline?: str
description: str
logo?: str
favicon?: str
schema SiteUrls:
base_url: str
canonical_url?: str
feed_url?: str
sitemap_url?: str
schema SiteOwner:
name: str
email?: str
bio?: str
avatar?: str
social?: {str: str}
schema SiteFeatures:
search?: bool = False
analytics?: bool = False
comments?: bool = False
newsletter?: bool = False
schema SiteConfig:
identity: SiteIdentity
urls: SiteUrls
owner?: SiteOwner
features?: SiteFeatures'
}