refactor: Generalize playbook find method and restructure site module

- Replace `actions_find` with a more generic `find(filter:)`
- Rename `siteconfig` module and related types to `site`
- Introduce a `Site` object to encapsulate configuration
- Update site generation to accept a playbook object directly
- Remove redundant blank lines and format code
This commit is contained in:
Mahmoud-Emad
2025-07-31 14:02:46 +03:00
parent d3d7eccf3d
commit e6c1d84836
10 changed files with 58 additions and 63 deletions

View File

@@ -7,15 +7,13 @@ import freeflowuniverse.herolib.ui.console
const action_priorities = { const action_priorities = {
0: ['department_define', 'costcenter_define'] 0: ['department_define', 'costcenter_define']
1: ['revenue_define', 'funding_define'] 1: ['revenue_define', 'funding_define']
2: ['cost_define','employee_define'] 2: ['cost_define', 'employee_define']
3: ['sheet_wiki', 'graph_bar_row', 'graph_pie_row', 'graph_line_row', 'row_overview'] 3: ['sheet_wiki', 'graph_bar_row', 'graph_pie_row', 'graph_line_row', 'row_overview']
} }
pub fn play(mut plbook PlayBook) ! { pub fn play(mut plbook PlayBook) ! {
// group actions by which bizmodel they belong to // group actions by which bizmodel they belong to
actions_by_biz := arrays.group_by[string, &Action](plbook.actions_find(actor: 'bizmodel')!, actions_by_biz := arrays.group_by[string, &Action](plbook.find(filter: 'bizmodel.*')!,
fn (a &Action) string { fn (a &Action) string {
return a.params.get('bizname') or { 'default' } return a.params.get('bizname') or { 'default' }
}) })
@@ -25,11 +23,10 @@ pub fn play(mut plbook PlayBook) ! {
mut model := getset(biz)! mut model := getset(biz)!
model.play(mut plbook)! model.play(mut plbook)!
} }
} }
pub fn (mut m BizModel) play(mut plbook PlayBook) ! { pub fn (mut m BizModel) play(mut plbook PlayBook) ! {
mut actions := plbook.actions_find(actor: 'bizmodel')! mut actions := plbook.find(filter: 'bizmodel.*')!
for action in actions.filter(it.name in action_priorities[0]) { for action in actions.filter(it.name in action_priorities[0]) {
m.act(*action)! m.act(*action)!
@@ -51,9 +48,7 @@ pub fn (mut m BizModel) play(mut plbook PlayBook) ! {
// m.sheet.pprint(nr_columns: 10)! // m.sheet.pprint(nr_columns: 10)!
for action in actions.filter(it.name in action_priorities[3]) { for action in actions.filter(it.name in action_priorities[3]) {
m.act(*action)! m.act(*action)!
} }
} }

View File

@@ -39,9 +39,11 @@ fn args_get(path string) !GeneratorArgs {
return error("can't find path with .heroscript in ${path}, is a bug") return error("can't find path with .heroscript in ${path}, is a bug")
} }
mut plbook := playbook.new(text: config_path.read()!) or { return error('failed to create playbook: ${err}') } mut plbook := playbook.new(text: config_path.read()!) or {
return error('failed to create playbook: ${err}')
}
mut install_actions := plbook.actions_find(actor:'hero_code', name: 'generate_installer')! mut install_actions := plbook.find(filter: 'hero_code.generate_installer')!
if install_actions.len > 0 { if install_actions.len > 0 {
for install_action in install_actions { for install_action in install_actions {
mut p := install_action.params mut p := install_action.params
@@ -65,7 +67,7 @@ fn args_get(path string) !GeneratorArgs {
} }
} }
mut client_actions := plbook.actions_find(actor:'hero_code', name: 'generate_client')! mut client_actions := plbook.find(filter: 'hero_code.generate_client')!
if client_actions.len > 0 { if client_actions.len > 0 {
for client_action in client_actions { for client_action in client_actions {
mut p := client_action.params mut p := client_action.params

View File

@@ -4,7 +4,7 @@ import freeflowuniverse.herolib.core.playbook { PlayBook }
// this play script should never be called from hero directly its called by gridsimulator // this play script should never be called from hero directly its called by gridsimulator
pub fn play(mut plbook PlayBook) !map[string]&Node { pub fn play(mut plbook PlayBook) !map[string]&Node {
mut actions2 := plbook.actions_find(actor: 'tfgrid_simulator')! mut actions2 := plbook.find(filter: 'tfgrid_simulator.*')!
mut nodesdict := map[string]&Node{} mut nodesdict := map[string]&Node{}
for action in actions2 { for action in actions2 {

View File

@@ -8,7 +8,7 @@ pub fn play(mut plbook PlayBook) ! {
// mut sheet_name := '' // mut sheet_name := ''
// 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.actions_find(actor: 'tfgridsimulation_farming')! mut my_actions := plbook.find(filter: 'tfgridsimulation_farming.*')!
if my_actions.len == 0 { if my_actions.len == 0 {
return return
@@ -37,7 +37,7 @@ pub fn play(mut plbook PlayBook) ! {
} }
pub fn (mut s Simulator) play(mut plbook PlayBook) ! { pub fn (mut s Simulator) play(mut plbook PlayBook) ! {
mut actions2 := plbook.actions_find(actor: 'tfgridsimulation_farming')! mut actions2 := plbook.find(filter: 'tfgridsimulation_farming.*')!
if actions2.len == 0 { if actions2.len == 0 {
// means nothing to do return quickly // means nothing to do return quickly
@@ -93,7 +93,7 @@ pub fn (mut s Simulator) play(mut plbook PlayBook) ! {
} }
// NOW ADD THE REGIONAL INTERNETS // NOW ADD THE REGIONAL INTERNETS
mut actions3 := plbook.actions_find(actor: 'tfgridsimulation_farming')! mut actions3 := plbook.find(filter: 'tfgridsimulation_farming.*')!
for action_ri in actions3 { for action_ri in actions3 {
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')!
@@ -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.actions_find(actor: '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.actions_find(actor: '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.actions_find(actor: '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.actions_find(actor: '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

@@ -3,7 +3,7 @@ module docusaurus
import freeflowuniverse.herolib.osal.screen import freeflowuniverse.herolib.osal.screen
import os import os
import freeflowuniverse.herolib.core.pathlib import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.web.siteconfig import freeflowuniverse.herolib.web.site as sitemodule
import freeflowuniverse.herolib.develop.gittools import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.osal.core as osal import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
@@ -12,32 +12,32 @@ import time
@[heap] @[heap]
pub struct DocSite { pub struct DocSite {
pub mut: pub mut:
name string name string
url string url string
path_src pathlib.Path path_src pathlib.Path
path_publish pathlib.Path path_publish pathlib.Path
args DSiteGetArgs args DSiteGetArgs
errors []SiteError errors []SiteError
config Configuration config Configuration
siteconfig siteconfig.SiteConfig siteconfig sitemodule.SiteConfig
factory &DocusaurusFactory @[skip; str: skip] // Reference to the parent factory &DocusaurusFactory @[skip; str: skip] // Reference to the parent
} }
@[params] @[params]
pub struct DSiteGetArgs { pub struct DSiteGetArgs {
pub mut: pub mut:
name string name string
nameshort string nameshort string
path string path string
git_url string git_url string
git_reset bool git_reset bool
git_root string git_root string
git_pull bool git_pull bool
open bool // Added open bool // Added
watch_changes bool // Added watch_changes bool // Added
path_publish string // Added path_publish string // Added
init bool // Added init bool // Added
update bool // Added (maps to template_update in DocusaurusArgs) update bool // Added (maps to template_update in DocusaurusArgs)
} }
pub fn (mut s DocSite) build() ! { pub fn (mut s DocSite) build() ! {

View File

@@ -4,12 +4,11 @@ import os
import freeflowuniverse.herolib.core.pathlib import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.develop.gittools import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.web.siteconfig import freeflowuniverse.herolib.web.site
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.osal.core as osal import freeflowuniverse.herolib.osal.core as osal
// import freeflowuniverse.herolib.data.doctree // import freeflowuniverse.herolib.data.doctree
pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite { pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
console.print_header(' Docusaurus: ${args_.name}') console.print_header(' Docusaurus: ${args_.name}')
mut args := args_ mut args := args_
@@ -53,7 +52,7 @@ pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
if args.nameshort.len == 0 { if args.nameshort.len == 0 {
args.nameshort = args.name args.nameshort = args.name
} }
args.name = texttools.name_fix(args.name) args.name = texttools.name_fix(args.name)
args.nameshort = texttools.name_fix(args.nameshort) args.nameshort = texttools.name_fix(args.nameshort)
@@ -61,8 +60,9 @@ pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
args.path_publish = '${f.path_publish}/${args.name}' args.path_publish = '${f.path_publish}/${args.name}'
} }
//this will get us the siteconfig run through plbook // this will get us the siteconfig run through plbook
mut mysiteconfig := *siteconfig.new(configpath)! mut mysite := site.new(name: args.name)!
mut mysiteconfig := mysite.siteconfig
// NOT NEEDED IS DONE FROM HEROSCRIPT BEFORE // NOT NEEDED IS DONE FROM HEROSCRIPT BEFORE
// //now run the plbook to get all relevant for the site, {SITENAME} has been set in the context.session // //now run the plbook to get all relevant for the site, {SITENAME} has been set in the context.session
@@ -71,9 +71,8 @@ pub fn (mut f DocusaurusFactory) add(args_ DSiteGetArgs) !&DocSite {
// reset: args.update // reset: args.update
// )! // )!
mut ds := DocSite{ mut ds := DocSite{
name: args.name name: args.name
path_src: pathlib.get_dir(path: args.path, create: false)! path_src: pathlib.get_dir(path: args.path, create: false)!
path_publish: pathlib.get_dir(path: args.path_publish)! path_publish: pathlib.get_dir(path: args.path_publish)!
args: args args: args

View File

@@ -3,7 +3,7 @@ module docusaurus
// import os // import os
// import json // import json
// import freeflowuniverse.herolib.core.pathlib // import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.web.siteconfig // For siteconfig.SiteConfig and siteconfig.new import freeflowuniverse.herolib.web.site // For site.SiteConfig and site.new
// import strings // No longer needed as we are not concatenating // import strings // No longer needed as we are not concatenating
// import freeflowuniverse.herolib.core.playbook // No longer directly needed here // import freeflowuniverse.herolib.core.playbook // No longer directly needed here
@@ -79,9 +79,9 @@ pub mut:
} }
fn config_load(path string) !Configuration { fn config_load(path string) !Configuration {
// Use siteconfig.new from factory.v. This function handles PlayBook creation, playing, and Redis interaction. // Use site.new from factory.v. This function handles PlayBook creation, playing, and Redis interaction.
site_cfg_ref := siteconfig.new(path)! site_ref := site.new(name: 'default')!
site_cfg_from_heroscript := *site_cfg_ref // Dereference to get the actual SiteConfig struct site_cfg_from_heroscript := site_ref.siteconfig // Get the actual SiteConfig struct
// Transform siteconfig.SiteConfig to docusaurus.Configuration // Transform siteconfig.SiteConfig to docusaurus.Configuration
mut nav_items := []NavbarItem{} mut nav_items := []NavbarItem{}

View File

@@ -2,13 +2,14 @@ module docusaurus
import freeflowuniverse.herolib.develop.gittools import freeflowuniverse.herolib.develop.gittools
import freeflowuniverse.herolib.core.pathlib import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.playbook
import json import json
import os import os
import freeflowuniverse.herolib.osal.core as osal import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.ui.console import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools.regext import freeflowuniverse.herolib.core.texttools.regext
// import freeflowuniverse.herolib.data.doctree // import freeflowuniverse.herolib.data.doctree
import freeflowuniverse.herolib.web.site import freeflowuniverse.herolib.web.site as sitegen
pub fn (mut site DocSite) generate() ! { pub fn (mut site DocSite) generate() ! {
console.print_header(' site generate: ${site.name} on ${site.factory.path_build.path}') console.print_header(' site generate: ${site.name} on ${site.factory.path_build.path}')
@@ -46,9 +47,9 @@ pub fn (mut site DocSite) generate() ! {
mut footer_file := pathlib.get_file(path: '${cfg_path}/footer.json', create: true)! mut footer_file := pathlib.get_file(path: '${cfg_path}/footer.json', create: true)!
footer_file.write(json.encode_pretty(site.config.footer))! footer_file.write(json.encode_pretty(site.config.footer))!
osal.rm("${site.factory.path_build.path}/docs")! osal.rm('${site.factory.path_build.path}/docs')!
if os.exists("${site.path_src.path}/docs"){ if os.exists('${site.path_src.path}/docs') {
mut aa := site.path_src.dir_get('docs')! mut aa := site.path_src.dir_get('docs')!
aa.copy(dest: '${site.factory.path_build.path}/docs', delete: true)! aa.copy(dest: '${site.factory.path_build.path}/docs', delete: true)!
} }
@@ -62,12 +63,10 @@ pub fn (mut site DocSite) generate() ! {
// draft:1 hide_title:1 // draft:1 hide_title:1
configpath := '${site.path_src.path}/cfg' configpath := '${site.path_src.path}/cfg'
sitegen.play(mut sitegen.Args{
heroscript_path: configpath // Create a playbook from the config path and run site processing
dest: '${site.factory.path_build.path}/docs' mut plbook := playbook.new(path: configpath)!
flat: true sitegen.play(mut plbook)!
sitename: site.name
})!
site.process_imports()! site.process_imports()!
} }

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)!
} }
} }