From 24eb709293fb8af68aef5d3335fa888471fd4bc5 Mon Sep 17 00:00:00 2001 From: despiegk Date: Wed, 19 Feb 2025 09:44:03 +0300 Subject: [PATCH] fixes docusaurus --- lib/biz/bizmodel/export.v | 5 +- lib/core/herocmds/docusaurus.v | 59 +++--- lib/core/pathlib/path_rsync.v | 6 +- lib/core/pathlib/template.v | 2 +- lib/osal/rsync/rsync.v | 2 +- lib/web/docusaurus/config.v | 2 +- lib/web/docusaurus/dsite.v | 173 ++++-------------- lib/web/docusaurus/dsite_get.v | 110 +++++++++++ lib/web/docusaurus/factory.v | 2 +- lib/web/docusaurus/template.v | 52 ++++-- lib/web/docusaurus/templates/build.sh | 2 +- .../docusaurus/templates/build_dev_publish.sh | 2 +- lib/web/docusaurus/templates/build_publish.sh | 2 +- lib/web/docusaurus/templates/develop.sh | 2 +- 14 files changed, 233 insertions(+), 188 deletions(-) create mode 100644 lib/web/docusaurus/dsite_get.v diff --git a/lib/biz/bizmodel/export.v b/lib/biz/bizmodel/export.v index 5d4d64a9..90cfe9c7 100644 --- a/lib/biz/bizmodel/export.v +++ b/lib/biz/bizmodel/export.v @@ -56,12 +56,13 @@ pub fn (r Report) export(export Export) ! { match export.format { .docusaurus { mut factory := docusaurus.new()! - factory.build( + mut site := factory.get( name: r.name path: r.path publish_path: export.path - config: docusaurus.Config {} + config: docusaurus.Config {} //TODO: is this needed )! + site.build()! } .mdbook {panic('MDBook export not fully implemented')} } diff --git a/lib/core/herocmds/docusaurus.v b/lib/core/herocmds/docusaurus.v index 41b04e70..9706ced2 100644 --- a/lib/core/herocmds/docusaurus.v +++ b/lib/core/herocmds/docusaurus.v @@ -29,6 +29,16 @@ pub fn cmd_docusaurus(mut cmdroot Command) { description: 'Url where docusaurus source is.' }) + cmd_run.add_flag(Flag{ + flag: .string + required: false + name: 'path' + abbrev: 'p' + // default: '' + description: 'Path where docusaurus source is.' + }) + + cmd_run.add_flag(Flag{ flag: .string required: false @@ -77,20 +87,27 @@ pub fn cmd_docusaurus(mut cmdroot Command) { description: 'Run your dev environment on local browser.' }) + + cmd_run.add_flag(Flag{ + flag: .bool + required: false + name: 'new' + abbrev: 'n' + description: 'create a new docusaurus site.' + }) + + cmdroot.add_command(cmd_run) } fn cmd_docusaurus_execute(cmd Command) ! { mut update := cmd.flags.get_bool('update') or { false } + mut init := cmd.flags.get_bool('new') or { false } mut url := cmd.flags.get_string('url') or { '' } mut publish_path := cmd.flags.get_string('publish') or { '' } mut deploykey := cmd.flags.get_string('deploykey') or { '' } - // mut path := cmd.flags.get_string('path') or { '' } - // if path == '' { - // path = os.getwd() - // } - // path = path.replace('~', os.home_dir()) + mut path := cmd.flags.get_string('path') or { '' } mut buildpublish := cmd.flags.get_bool('buildpublish') or { false } mut builddevpublish := cmd.flags.get_bool('builddevpublish') or { false } @@ -102,40 +119,28 @@ fn cmd_docusaurus_execute(cmd Command) ! { // } mut docs := docusaurus.new(update: update)! - - if publish_path.len > 0 { - _ := docs.build( + mut site := docs.get( url: url + path: path update: update publish_path: publish_path deploykey: deploykey - )! + init:init + )! + + if publish_path.len > 0 { + site.build()! } if buildpublish { - // Create a new docusaurus site - _ := docs.build_publish( - url: url - update: update - deploykey: deploykey - )! + site.build_publish()! } if builddevpublish { - // Create a new docusaurus site - _ := docs.build_dev_publish( - url: url - update: update - deploykey: deploykey - )! + site.build_dev_publish()! } if dev { - // Create a new docusaurus site - _ := docs.dev( - url: url - update: update - deploykey: deploykey - )! + site.dev()! } } diff --git a/lib/core/pathlib/path_rsync.v b/lib/core/pathlib/path_rsync.v index 8c38b456..d88707a7 100644 --- a/lib/core/pathlib/path_rsync.v +++ b/lib/core/pathlib/path_rsync.v @@ -13,7 +13,7 @@ pub mut: delete bool // do we want to delete the destination ignore []string // arguments to ignore e.g. ['*.pyc','*.bak'] ignore_default bool = true // if set will ignore a common set - debug bool = true + debug bool fast_rsync bool sshkey string } @@ -37,8 +37,8 @@ pub fn rsync(args_ RsyncArgs) ! { get(args.source) } cmdoptions := rsync_cmd_options(args)! - $if debug { - console.print_debug(' rsync command:\nrsync ${cmdoptions}') + if args.debug { + console.print_debug('rsync ${cmdoptions}') } r := os.execute('which rsync') if r.exit_code > 0 { diff --git a/lib/core/pathlib/template.v b/lib/core/pathlib/template.v index b382a42b..84dad150 100644 --- a/lib/core/pathlib/template.v +++ b/lib/core/pathlib/template.v @@ -10,7 +10,7 @@ pub fn template_write(template_ string, dest string, overwrite bool) ! { if overwrite || !(os.exists(dest)) { mut p := get_file(path: dest, create: true)! $if debug { - console.print_header(" write template to '${dest}'") + console.print_debug(" write template to '${dest}'") } p.write(template)! } diff --git a/lib/osal/rsync/rsync.v b/lib/osal/rsync/rsync.v index 28cc3753..d4def37d 100644 --- a/lib/osal/rsync/rsync.v +++ b/lib/osal/rsync/rsync.v @@ -14,7 +14,7 @@ pub mut: delete bool // do we want to delete the destination ignore []string // arguments to ignore e.g. ['*.pyc','*.bak'] ignore_default bool = true // if set will ignore a common set - stdout bool = true + stdout bool fast_rsync bool sshkey string } diff --git a/lib/web/docusaurus/config.v b/lib/web/docusaurus/config.v index 6885d93b..2e8bc03e 100644 --- a/lib/web/docusaurus/config.v +++ b/lib/web/docusaurus/config.v @@ -84,7 +84,7 @@ pub fn load_config(cfg_dir string) !Config { main_config_path := os.join_path(cfg_dir, 'main.json') main_content := os.read_file(main_config_path)! main := json.decode(Main, main_content) or { - eprintln('${main_config_path} is not in the right format please fix.\nError: ${err}') + eprintln('main.json in ${cfg_dir} is not in the right format please fix.\nError: ${err}') println(' ## EXAMPLE OF A GOOD ONE: diff --git a/lib/web/docusaurus/dsite.v b/lib/web/docusaurus/dsite.v index 634a91d3..4446ac01 100644 --- a/lib/web/docusaurus/dsite.v +++ b/lib/web/docusaurus/dsite.v @@ -18,29 +18,14 @@ pub mut: path_src pathlib.Path path_build pathlib.Path // path_publish pathlib.Path - args DSiteNewArgs + args DSiteGetArgs errors []SiteError config Config + factory &DocusaurusFactory @[skip; str: skip] // Reference to the parent } -@[params] -pub struct DSiteNewArgs { -pub mut: - name string - nameshort string - path string - url string - publish_path string - build_path string - production bool - watch_changes bool = true - update bool - deploykey string - config ?Config -} -pub fn (mut f DocusaurusFactory) build(args_ DSiteNewArgs) !&DocSite { - mut s := f.add(args_)! +pub fn (mut s DocSite) build() ! { s.generate()! osal.exec( cmd: ' @@ -49,11 +34,9 @@ pub fn (mut f DocusaurusFactory) build(args_ DSiteNewArgs) !&DocSite { ' retry: 0 )! - return s } -pub fn (mut f DocusaurusFactory) build_dev_publish(args_ DSiteNewArgs) !&DocSite { - mut s := f.add(args_)! +pub fn (mut s DocSite) build_dev_publish() ! { s.generate()! osal.exec( cmd: ' @@ -62,13 +45,10 @@ pub fn (mut f DocusaurusFactory) build_dev_publish(args_ DSiteNewArgs) !&DocSite ' retry: 0 )! - return s } -pub fn (mut f DocusaurusFactory) build_publish(args_ DSiteNewArgs) !&DocSite { - mut s := f.add(args_)! +pub fn (mut s DocSite) build_publish()! { s.generate()! - osal.exec( cmd: ' cd ${s.path_build.path} @@ -76,12 +56,9 @@ pub fn (mut f DocusaurusFactory) build_publish(args_ DSiteNewArgs) !&DocSite { ' retry: 0 )! - return s } -pub fn (mut f DocusaurusFactory) dev(args_ DSiteNewArgs) !&DocSite { - mut s := f.add(args_)! - +pub fn (mut s DocSite) dev()! { s.clean()! s.generate()! @@ -121,90 +98,11 @@ pub fn (mut f DocusaurusFactory) dev(args_ DSiteNewArgs) !&DocSite { // tf.wait()! println('\n') - if args_.watch_changes { + if s.args.watch_changes { docs_path := '${s.path_src.path}/docs' watch_docs(docs_path, s.path_src.path, s.path_build.path)! } - return s -} - -///////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////// - -pub fn (mut f DocusaurusFactory) add(args_ DSiteNewArgs) !&DocSite { - console.print_header(' Docusaurus: ${args_.name}') - mut args := args_ - - if args.build_path.len == 0 { - args.build_path = '${f.path_build.path}' - } - // if args.publish_path.len == 0 { - // args.publish_path = '${f.path_publish.path}/${args.name}' - - // coderoot:"${os.home_dir()}/hero/var/publishcode" - mut gs := gittools.new(ssh_key_path: args.deploykey)! - - if args.url.len > 0 { - args.path = gs.get_path(url: args.url)! - } - - if args.path.len == 0 { - return error("Can't get path from docusaurus site, its not specified.") - } - - mut r := gs.get_repo( - url: 'https://github.com/freeflowuniverse/docusaurus_template.git' - pull: args.update - )! - mut template_path := r.patho()! - - - // First, check if the new site args provides a configuration that can be written instead of template cfg dir - if cfg := args.config { - cfg.write('${args.path}/cfg')! - } else { - // Then ensure cfg directory exists in src, - if !os.exists('${args.path}/cfg') { - // else copy config from template - mut template_cfg := template_path.dir_get('cfg')! - template_cfg.copy(dest: '${args.path}/cfg')! - } - } - - if !os.exists('${args.path}/docs') { - mut template_cfg := template_path.dir_get('docs')! - template_cfg.copy(dest: '${args.path}/docs')! - } - - mut myconfig := load_config('${args.path}/cfg')! - - if myconfig.main.name.len == 0 { - myconfig.main.name = myconfig.main.base_url.trim_space().trim('/').trim_space() - } - - if args.name == '' { - args.name = myconfig.main.name - } - - if args.nameshort.len == 0 { - args.nameshort = args.name - } - args.nameshort = texttools.name_fix(args.nameshort) - - mut ds := DocSite{ - name: args.name - url: args.url - path_src: pathlib.get_dir(path: args.path, create: false)! - path_build: f.path_build - // path_publish: pathlib.get_dir(path: args.publish_path, create: true)! - args: args - config: myconfig - } - - f.sites << &ds - - return &ds } @[params] @@ -226,9 +124,27 @@ pub fn (mut site DocSite) error(args ErrorArgs) { console.print_stderr(args.msg) } +fn check_item(item string)!{ + item2:=item.trim_space().trim("/").trim_space().all_after_last("/") + if ["internal","infodev","info","dev","friends","dd","web"].contains(item2){ + return error("destination path is wrong, cannot be: ${item}") + } + +} + +fn (mut site DocSite) check() ! { + for item in site.config.main.build_dest{ + check_item(item)! + } + for item in site.config.main.build_dest_dev{ + check_item(item)! + } +} + pub fn (mut site DocSite) generate() ! { console.print_header(' site generate: ${site.name} on ${site.path_build.path}') console.print_header(' site source on ${site.path_src.path}') + site.check()! site.template_install()! // osal.exec( // cmd: ' @@ -256,34 +172,22 @@ pub fn (mut site DocSite) generate() ! { fn (mut site DocSite) template_install() ! { mut gs := gittools.new()! - mut r := gs.get_repo(url: 'https://github.com/freeflowuniverse/docusaurus_template.git')! - mut template_path := r.patho()! - - // always start from template first - for item in ['src', 'static', 'cfg'] { - mut aa := template_path.dir_get(item)! - aa.copy(dest: '${site.path_build.path}/${item}', delete: true)! - } - - for item in ['package.json', 'sidebars.ts', 'tsconfig.json', 'docusaurus.config.ts'] { - src_path := os.join_path(template_path.path, item) - dest_path := os.join_path(site.path_build.path, item) - os.cp(src_path, dest_path) or { - return error('Failed to copy ${item} to build path: ${err}') - } - } - - for item in ['.gitignore'] { - src_path := os.join_path(template_path.path, item) - dest_path := os.join_path(site.path_src.path, item) - os.cp(src_path, dest_path) or { - return error('Failed to copy ${item} to source path: ${err}') - } - } + site.factory.template_install(template_update:false, install:false, delete:false)! cfg := site.config - profile_include := osal.profile_path_source()! + mut myhome:="\$\{HOME\}" //for usage in bash + + profile_include := osal.profile_path_source()!.replace(os.home_dir(),myhome) + + mydir:=site.path_build.path.replace(os.home_dir(),myhome) + + for item in ['src', 'static'] { + mut aa := site.path_src.dir_get(item) or {continue} + aa.copy(dest: '${site.factory.path_build.path}/${item}', delete:false)! + + } + develop := $tmpl('templates/develop.sh') build := $tmpl('templates/build.sh') @@ -313,4 +217,5 @@ fn (mut site DocSite) template_install() ! { mut build2_ := site.path_src.file_get_new('build.sh')! build2_.template_write(build, true)! build2_.chmod(0o700)! + } diff --git a/lib/web/docusaurus/dsite_get.v b/lib/web/docusaurus/dsite_get.v new file mode 100644 index 00000000..c4f0705a --- /dev/null +++ b/lib/web/docusaurus/dsite_get.v @@ -0,0 +1,110 @@ +module docusaurus + +import os +import freeflowuniverse.herolib.core.pathlib +import freeflowuniverse.herolib.core.texttools +import freeflowuniverse.herolib.develop.gittools +import freeflowuniverse.herolib.ui.console + +@[params] +pub struct DSiteGetArgs { +pub mut: + name string + nameshort string + path string + url string + publish_path string + build_path string + production bool + watch_changes bool = true + update bool + init bool //means create new one if needed + deploykey string + config ?Config +} + +pub fn (mut f DocusaurusFactory) get(args_ DSiteGetArgs) !&DocSite { + console.print_header(' Docusaurus: ${args_.name}') + mut args := args_ + + if args.build_path.len == 0 { + args.build_path = '${f.path_build.path}' + } + // if args.publish_path.len == 0 { + // args.publish_path = '${f.path_publish.path}/${args.name}' + + // coderoot:"${os.home_dir()}/hero/var/publishcode" + mut gs := gittools.new(ssh_key_path: args.deploykey)! + + if args.url.len > 0 { + args.path = gs.get_path(url: args.url)! + } + + if args.path.trim_space() == "" { + args.path = os.getwd() + } + args.path = args.path.replace('~', os.home_dir()) + + mut r := gs.get_repo( + url: 'https://github.com/freeflowuniverse/docusaurus_template.git' + )! + mut template_path := r.patho()! + + // First, check if the new site args provides a configuration that can be written instead of template cfg dir + if cfg := args.config { + panic("not implemented") + // cfg.write('${args.path}/cfg')! + } else { + // Then ensure cfg directory exists in src, + if !os.exists('${args.path}/cfg') { + if args.init{ + // else copy config from template + mut template_cfg := template_path.dir_get('cfg')! + template_cfg.copy(dest: '${args.path}/cfg')! + }else{ + return error("Can't find cfg dir in chosen docusaurus location: ${args.path}") + } + } + } + + if !os.exists('${args.path}/docs') { + if args.init{ + mut template_cfg := template_path.dir_get('docs')! + template_cfg.copy(dest: '${args.path}/docs')! + }else{ + return error("Can't find docs dir in chosen docusaurus location: ${args.path}") + } + } + + mut myconfig := load_config('${args.path}/cfg')! + + if myconfig.main.name.len == 0 { + myconfig.main.name = myconfig.main.base_url.trim_space().trim('/').trim_space() + } + + if args.name == '' { + args.name = myconfig.main.name + } + + if args.nameshort.len == 0 { + args.nameshort = args.name + } + args.nameshort = texttools.name_fix(args.nameshort) + + mut ds := DocSite{ + name: args.name + url: args.url + path_src: pathlib.get_dir(path: args.path, create: false)! + path_build: f.path_build + // path_publish: pathlib.get_dir(path: args.publish_path, create: true)! + args: args + config: myconfig + factory: &f + } + + ds.check()! + + f.sites << &ds + + return &ds +} diff --git a/lib/web/docusaurus/factory.v b/lib/web/docusaurus/factory.v index 61691721..39f42f9e 100644 --- a/lib/web/docusaurus/factory.v +++ b/lib/web/docusaurus/factory.v @@ -39,7 +39,7 @@ pub fn new(args_ DocusaurusArgs) !&DocusaurusFactory { // path_publish: pathlib.get_dir(path: args_.publish_path, create: true)! } - ds.template_install(args.update)! + ds.template_install(install:true,template_update:args.update,delete:true)! return ds } \ No newline at end of file diff --git a/lib/web/docusaurus/template.v b/lib/web/docusaurus/template.v index d35b4018..15238153 100644 --- a/lib/web/docusaurus/template.v +++ b/lib/web/docusaurus/template.v @@ -5,30 +5,54 @@ import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.installers.web.bun import os -fn (mut site DocusaurusFactory) template_install(update bool) ! { +@[params] +struct TemplateInstallArgs{ + template_update bool = true + install bool + delete bool = true +} + +fn (mut self DocusaurusFactory) template_install(args TemplateInstallArgs) ! { mut gs := gittools.new()! mut r := gs.get_repo( url: 'https://github.com/freeflowuniverse/docusaurus_template.git' - pull: update + pull: args.template_update )! mut template_path := r.patho()! for item in ['package.json', 'sidebars.ts', 'tsconfig.json'] { mut aa := template_path.file_get(item)! - aa.copy(dest: '${site.path_build.path}/${item}')! + aa.copy(dest: '${self.path_build.path}/${item}')! } - // install bun - mut installer := bun.get()! - installer.install()! + // always start from template first + for item in ['src', 'static'] { + mut aa := template_path.dir_get(item)! + aa.copy(dest: '${self.path_build.path}/${item}', delete: args.delete)! + } + + for item in ['package.json', 'sidebars.ts', 'tsconfig.json', 'docusaurus.config.ts'] { + src_path := os.join_path(template_path.path, item) + dest_path := os.join_path(self.path_build.path, item) + os.cp(src_path, dest_path) or { + return error('Failed to copy ${item} to build path: ${err}') + } + } + + if args.install{ + // install bun + mut installer := bun.get()! + installer.install()! + + osal.exec( + cmd: ' + ${osal.profile_path_source_and()!} + export PATH=/tmp/docusaurus_build/node_modules/.bin:${os.home_dir()}/.bun/bin/:??PATH + cd ${self.path_build.path} + bun install + ' + )! + } - osal.exec( - cmd: ' - ${osal.profile_path_source_and()!} - export PATH=/tmp/docusaurus_build/node_modules/.bin:${os.home_dir()}/.bun/bin/:??PATH - cd ${site.path_build.path} - bun install - ' - )! } diff --git a/lib/web/docusaurus/templates/build.sh b/lib/web/docusaurus/templates/build.sh index 64552e56..28b7f5ea 100755 --- a/lib/web/docusaurus/templates/build.sh +++ b/lib/web/docusaurus/templates/build.sh @@ -7,7 +7,7 @@ cd "??{script_dir}" echo "Docs directory: ??script_dir" -cd ${site.path_build.path} +cd "${mydir}" export PATH=/tmp/docusaurus_build/node_modules/.bin:??{HOME}/.bun/bin/:??PATH diff --git a/lib/web/docusaurus/templates/build_dev_publish.sh b/lib/web/docusaurus/templates/build_dev_publish.sh index 13a2b107..e66c4741 100755 --- a/lib/web/docusaurus/templates/build_dev_publish.sh +++ b/lib/web/docusaurus/templates/build_dev_publish.sh @@ -8,7 +8,7 @@ cd "??{script_dir}" echo "Docs directory: ??script_dir" -cd ${site.path_build.path} +cd "${mydir}" export PATH=/tmp/docusaurus_build/node_modules/.bin:??{HOME}/.bun/bin/:??PATH diff --git a/lib/web/docusaurus/templates/build_publish.sh b/lib/web/docusaurus/templates/build_publish.sh index a3c994b9..8105e500 100755 --- a/lib/web/docusaurus/templates/build_publish.sh +++ b/lib/web/docusaurus/templates/build_publish.sh @@ -7,7 +7,7 @@ cd "??{script_dir}" echo "Docs directory: ??script_dir" -cd ${site.path_build.path} +cd "${mydir}" export PATH=/tmp/docusaurus_build/node_modules/.bin:??{HOME}/.bun/bin/:??PATH diff --git a/lib/web/docusaurus/templates/develop.sh b/lib/web/docusaurus/templates/develop.sh index aee47516..efa085e6 100755 --- a/lib/web/docusaurus/templates/develop.sh +++ b/lib/web/docusaurus/templates/develop.sh @@ -7,7 +7,7 @@ cd "??{script_dir}" echo "Docs directory: ??script_dir" -cd ${site.path_build.path} +cd "${mydir}" export PATH=/tmp/docusaurus_build/node_modules/.bin:??{HOME}/.bun/bin/:??PATH