refactor: adapt docusaurus to use generic site module

- Introduce a new generic `site` module for web generation
- Update `herocmds` to use the new site creation flow
- Simplify docusaurus playbook logic with a `docusaurus.play` fn
- Refactor site generation to act on `Site` struct directly
- Fix playbook find filter to use wildcard `*`
This commit is contained in:
Mahmoud-Emad
2025-07-31 14:42:36 +03:00
parent e837912363
commit 697c500e35
7 changed files with 35 additions and 69 deletions

View File

@@ -1,6 +1,7 @@
module herocmds module herocmds
import freeflowuniverse.herolib.web.docusaurus import freeflowuniverse.herolib.web.docusaurus
import freeflowuniverse.herolib.web.site
import freeflowuniverse.herolib.core.pathlib import freeflowuniverse.herolib.core.pathlib
import os import os
import cli { Command, Flag } import cli { Command, Flag }
@@ -154,32 +155,31 @@ fn cmd_docusaurus_execute(cmd Command) ! {
mut builddevpublish := cmd.flags.get_bool('builddevpublish') or { false } mut builddevpublish := cmd.flags.get_bool('builddevpublish') or { false }
mut dev := cmd.flags.get_bool('dev') or { false } mut dev := cmd.flags.get_bool('dev') or { false }
mut docs := docusaurus.new( // Create a site first using the new API
template_update: update // Changed 'update' to 'template_update' mut generic_site := site.new(name: 'cli_site')!
path_build: build_path
heroscript_path: heroscript_config_dir // Pass the directory path
)!
mut site := docs.add( // Add docusaurus site
git_url: url // Map CLI 'url' flag to DSiteGetArgs 'git_url' mut dsite := docusaurus.add(
update: update site: generic_site
path_publish: publish_path // Map CLI 'publish' flag to DSiteGetArgs 'path_publish' path_src: url // Use URL as source path for now
init: init path_build: build_path
open: open path_publish: publish_path
// Removed build_path and deploykey as they are not in DSiteGetArgs reset: false
template_update: update
install: init
)! )!
// Conditional site actions based on flags // Conditional site actions based on flags
if buildpublish { if buildpublish {
site.build_publish()! dsite.build_publish()!
} else if builddevpublish { } else if builddevpublish {
site.build_dev_publish()! dsite.build_dev_publish()!
} else if dev { } else if dev {
site.dev(host: 'localhost', port: 3000)! dsite.dev(host: 'localhost', port: 3000, open: open)!
} else if open { } else if open {
site.open()! dsite.open('localhost', 3000)!
} else { } else {
// If no specific action (build/dev/open) is requested, just generate the site // If no specific action (build/dev/open) is requested, just generate the site
site.generate()! dsite.generate()!
} }
} }

View File

@@ -1,44 +1,10 @@
module playcmds module playcmds
import freeflowuniverse.herolib.core.playbook { PlayBook } import freeflowuniverse.herolib.core.playbook { PlayBook }
// import freeflowuniverse.herolib.ui.console // import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.web.docusaurus import freeflowuniverse.herolib.web.docusaurus
fn play(mut plbook PlayBook) ! { fn play(mut plbook PlayBook) ! {
// Use the new docusaurus.play() function which handles the new API structure
mut ds := docusaurus.new()! docusaurus.play(mut plbook)!
mut action0 := plbook.get(filter: 'docusaurus.define')!
mut p0 := action0.params
path_publish := p0.get_default('path_publish', '')!
path_build := p0.get_default('path_build', '')! // don't do heroscript here because this could already be done before
ds = docusaurus.new(
path_publish: path_publish
path_build: path_build
install: plbook.exists(filter: 'docusaurus.reset') || plbook.exists(filter: 'docusaurus.update')
reset: plbook.exists(filter: 'docusaurus.reset')
template_update: plbook.exists(filter: 'docusaurus.reset') || plbook.exists(filter: 'docusaurus.update')
)!
actions := plbook.find(filter: 'docusaurus.generate')!
for action in actions {
mut p := action.params
mut site := ds.add(
name: p.get('name') or {return error("can't find name in params for docusaurus.add in action:\n${action.heroscript}")}
nameshort: p.get_default('nameshort', p.get('name')!)!
path: p.get_default('path', '')!
git_url: p.get_default('git_url', '')!
git_reset: p.get_default_false('git_reset')
git_root: p.get_default('git_root', '')!
git_pull: p.get_default_false('git_pull')
)!
}
} }

View File

@@ -14,7 +14,7 @@ pub fn (params Params) decode_struct[T](start T) !T {
mut t := T{} mut t := T{}
$for field in T.fields { $for field in T.fields {
$if field.is_enum { $if field.is_enum {
t.$(field.name) = params.get_int(field.name) or { t.$(field.name) } t.$(field.name) = params.get_int(field.name) or { int(t.$(field.name)) }
} $else { } $else {
// super annoying didn't find other way, then to ignore options // super annoying didn't find other way, then to ignore options
$if field.is_option { $if field.is_option {

View File

@@ -110,7 +110,7 @@ pub fn (mut s Simulator) play(mut plbook PlayBook) ! {
} }
// now do the simulation, run it // now do the simulation, run it
mut actions4 := plbook.filter(find: 'tfgridsimulation_farming.')! mut actions4 := plbook.find(filter: 'tfgridsimulation_farming.*')!
for action_ri in actions4 { for action_ri in actions4 {
if action_ri.name == 'regional_internet_add' { if action_ri.name == 'regional_internet_add' {
mut iname := action_ri.params.get('name')! mut iname := action_ri.params.get('name')!

View File

@@ -5,7 +5,7 @@ import freeflowuniverse.herolib.threefold.grid4.cloudslices
pub fn play(mut plbook PlayBook) ! { pub fn play(mut plbook PlayBook) ! {
// first make sure we find a run action to know the name // first make sure we find a run action to know the name
mut my_actions := plbook.find(filter: 'tfgrid_simulator.')! mut my_actions := plbook.find(filter: 'tfgrid_simulator.*')!
if my_actions.len == 0 { if my_actions.len == 0 {
return return
@@ -33,7 +33,7 @@ pub fn play(mut plbook PlayBook) ! {
pub fn (mut self Simulator) play(mut plbook PlayBook) ! { pub fn (mut self Simulator) play(mut plbook PlayBook) ! {
// make sure we know the inca price // make sure we know the inca price
mut actions4 := plbook.find(filter: 'tfgrid_simulator.')! mut actions4 := plbook.find(filter: 'tfgrid_simulator.*')!
if actions4.len == 0 { if actions4.len == 0 {
return return
@@ -61,7 +61,7 @@ pub fn (mut self Simulator) play(mut plbook PlayBook) ! {
return error("can't find incaprice_define action for tfgrid_simulator, needs to define INCA price.") return error("can't find incaprice_define action for tfgrid_simulator, needs to define INCA price.")
} }
mut actions2 := plbook.find(filter: 'tfgrid_simulator.')! mut actions2 := plbook.find(filter: 'tfgrid_simulator.*')!
for action in actions2 { for action in actions2 {
if action.name == 'node_growth_define' { if action.name == 'node_growth_define' {
mut node_name := action.params.get_default('node_name', '')! mut node_name := action.params.get_default('node_name', '')!

View File

@@ -50,9 +50,9 @@ pub fn (mut self DocSite) generate() ! {
osal.dir_ensure(docs_dest)! osal.dir_ensure(docs_dest)!
// Generate pages defined in site.heroscript (!!site.page ...) // Generate pages defined in site.heroscript (!!site.page ...)
self.site.siteconfig.generate( self.site.generate(
path: docs_dest, path: docs_dest
flat: true, flat: true
)! )!
self.process_imports()! self.process_imports()!
@@ -76,7 +76,7 @@ pub fn (mut self DocSite) process_imports() ! {
for key, val in item.replace { for key, val in item.replace {
ri.add_item('\{${key}\}', val)! ri.add_item('\{${key}\}', val)!
} }
ri.replace_in_dir( ri.replace_in_dir(
path: dest_path path: dest_path
extensions: ['md'] extensions: ['md']

View File

@@ -8,10 +8,10 @@ import os
pub struct SiteGenerator { pub struct SiteGenerator {
pub mut: pub mut:
siteconfig_name string siteconfig_name string
path pathlib.Path path pathlib.Path
client &doctreeclient.DocTreeClient client &doctreeclient.DocTreeClient
flat bool // if flat then won't use sitenames as subdir's flat bool // if flat then won't use sitenames as subdir's
} }
@[params] @[params]
@@ -22,7 +22,7 @@ pub mut:
} }
// new creates a new siteconfig and stores it in redis, or gets an existing one // new creates a new siteconfig and stores it in redis, or gets an existing one
pub fn (siteconfig SiteConfig)generate(args SiteGeneratorArgs) ! { pub fn (site Site) generate(args SiteGeneratorArgs) ! {
mut path := args.path mut path := args.path
if path == '' { if path == '' {
path = '${os.home_dir()}/hero/var/sitegen' path = '${os.home_dir()}/hero/var/sitegen'
@@ -33,11 +33,11 @@ pub fn (siteconfig SiteConfig)generate(args SiteGeneratorArgs) ! {
flat: args.flat flat: args.flat
} }
for section in siteconfig.sections { for section in site.sections {
factory.section_generate(section)! factory.section_generate(section)!
} }
for page in siteconfig.pages { for page in site.pages {
factory.page_generate(page)! factory.page_generate(page)!
} }
} }