This commit is contained in:
2025-07-19 13:49:10 +02:00
parent 45c64b8184
commit f092095e7b
13 changed files with 158 additions and 51 deletions

View File

@@ -12,6 +12,7 @@ docusaurus.new(heroscript:'
git_url:"https://git.threefold.info/tfgrid/docs_tfgrid4/src/branch/main/ebooks/tech"
git_root:"/tmp/code"
git_reset:1
git_pull:1
!!docusaurus.dev

View File

@@ -35,20 +35,21 @@ pub fn play(args_ PlayArgs) ! {
git_reset:= p.get_default_false('git_reset')
git_pull:= p.get_default_false('git_pull')
doctree.scan(path: path, git_url: git_url, git_reset: git_reset, git_pull: git_pull)!
}
if collection_actions.len==0 {
return error("No collections configured, use !!doctree.collection...")
}
export_actions := plbook.find(filter: 'doctree.export')!
if export_actions.len == 0 {
name0:="main"
mut doctree0 := doctrees[name0] or { panic("can't find doctree with name ${name0}") }
doctree0.export()!
}
if export_actions.len > 0 {
if collection_actions.len==0 {
println(plbook)
return error("No collections configured, use !!doctree.collection..., otherwise cannot export")
}
}
for action in export_actions {
mut p := action.params
name := p.get_default('name',"main")!

View File

@@ -130,6 +130,7 @@ pub fn path(args_ GitPathGetArgs) !pathlib.Path {
if args.path.trim_space() == '' && args.currentdir{
args.path = os.getwd()
}
if args.git_url.len > 0 {
mut gs := gittools.get(coderoot: args.git_root)!
mut repo := gs.get_repo(

View File

@@ -17,7 +17,7 @@ pub mut:
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// Get GitLocation from a path within the Git repository
// Get GitLocation from a path within the Git repository, doesn't read from fs, is just from path missing branch...
pub fn (mut gs GitStructure) gitlocation_from_path(path string) !GitLocation {
mut full_path := pathlib.get(path)
rel_path := full_path.path_relative(gs.coderoot.path)!
@@ -42,7 +42,7 @@ pub fn (mut gs GitStructure) gitlocation_from_path(path string) !GitLocation {
}
}
// Get GitLocation from a URL
// Get GitLocation from a URL, doesn't go on filesystem just tries to figure out branch, ...
pub fn (mut gs GitStructure) gitlocation_from_url(url string) !GitLocation {
mut urllower := url.trim_space()
if urllower == '' {
@@ -60,7 +60,15 @@ pub fn (mut gs GitStructure) gitlocation_from_url(url string) !GitLocation {
// Extract branch if available
for i := 0; i < parts.len; i++ {
if parts[i] == 'refs' || parts[i] == 'tree' {
if parts[i] == 'src' && i + 1 < parts.len && parts[i + 1] == 'branch' {
if i + 2 < parts.len {
branch_or_tag = parts[i + 2]
}
if i + 3 < parts.len {
path = parts[(i + 3)..].join('/')
}
break
} else if parts[i] == 'tree' {
if i + 1 < parts.len {
branch_or_tag = parts[i + 1]
}
@@ -68,6 +76,21 @@ pub fn (mut gs GitStructure) gitlocation_from_url(url string) !GitLocation {
path = parts[(i + 2)..].join('/')
}
break
} else if parts[i] == 'refs' {
if i + 1 < parts.len && (parts[i + 1] == 'heads' || parts[i + 1] == 'tags') {
if i + 2 < parts.len {
branch_or_tag = parts[i + 2]
}
if i + 3 < parts.len {
path = parts[(i + 3)..].join('/')
}
} else if i + 1 < parts.len { // Fallback if no heads/tags
branch_or_tag = parts[i + 1]
if i + 2 < parts.len {
path = parts[(i + 2)..].join('/')
}
}
break
}
}
@@ -102,15 +125,6 @@ pub fn (mut gs GitStructure) gitlocation_from_url(url string) !GitLocation {
}
}
// Return a herolib path object on the filesystem pointing to the locator
pub fn (l GitLocation) patho() !pathlib.Path {
mut addrpath := pathlib.get_dir(path: '${l.provider}/${l.account}/${l.name}', create: false)!
if l.path.len > 0 {
return pathlib.get('${addrpath.path}/${l.path}')
}
return addrpath
}
// Normalize the URL for consistent parsing
fn normalize_url(url string) string {
// Remove common URL prefixes

View File

@@ -0,0 +1,59 @@
module gittools
import os
import freeflowuniverse.herolib.core.pathlib
fn test_gitlocation_from_url() {
mut gs := GitStructure{}
// Test case 1: URL with src/branch/main
url1 := 'https://git.threefold.info/tfgrid/docs_tfgrid4/src/branch/main/ebooks/tech'
loc1 := gs.gitlocation_from_url(url1)!
assert loc1.provider == 'git.threefold.info'
assert loc1.account == 'tfgrid'
assert loc1.name == 'docs_tfgrid4'
assert loc1.branch_or_tag == 'main'
assert loc1.path == 'ebooks/tech'
assert loc1.anker == ''
// Test case 2: URL with tree for branch
url2 := 'https://github.com/vlang/v/tree/master/vlib'
loc2 := gs.gitlocation_from_url(url2)!
assert loc2.provider == 'github'
assert loc2.account == 'vlang'
assert loc2.name == 'v'
assert loc2.branch_or_tag == 'master'
assert loc2.path == 'vlib'
assert loc2.anker == ''
// Test case 3: URL with refs for branch
url3 := 'https://github.com/vlang/v/refs/heads/master/vlib'
loc3 := gs.gitlocation_from_url(url3)!
assert loc3.provider == 'github'
assert loc3.account == 'vlang'
assert loc3.name == 'v'
assert loc3.branch_or_tag == 'master'
assert loc3.path == 'vlib'
assert loc3.anker == ''
// Test case 4: Simple URL without branch or path
url4 := 'https://github.com/vlang/v'
loc4 := gs.gitlocation_from_url(url4)!
assert loc4.provider == 'github'
assert loc4.account == 'vlang'
assert loc4.name == 'v'
assert loc4.branch_or_tag == ''
assert loc4.path == ''
assert loc4.anker == ''
// Test case 5: URL with an anchor
url5 := 'https://github.com/vlang/v/tree/master/vlib#L10'
loc5 := gs.gitlocation_from_url(url5)!
assert loc5.provider == 'github'
assert loc5.account == 'vlang'
assert loc5.name == 'v'
assert loc5.branch_or_tag == 'master'
assert loc5.path == 'vlib'
assert loc5.anker == 'L10'
}

View File

@@ -113,7 +113,16 @@ pub fn (mut gitstructure GitStructure) get_repo(args_ ReposGetArgs) !&GitRepo {
return error('Found more than one repository for \n${args}\n${repos}')
}
return repositories[0]
//the pull & reset was not used, now re-inserted
mut repo := repositories[0] or { panic('bug get_repo') }
if args.pull {
repo.pull()!
} else if args.reset {
repo.reset()!
}
return repo
}
// Helper function to check if a repository matches the criteria (name, account, provider).

View File

@@ -96,7 +96,7 @@ pub fn (mut repo GitRepo) commit(msg string) ! {
pub fn (mut repo GitRepo) push() ! {
repo.status_update()!
if repo.need_push_or_pull()! {
url := repo.get_repo_url()!
url := repo.get_repo_url_for_clone()!
console.print_header('Pushing changes to ${url}')
// We may need to push the locally created branches
repo.exec('git push --set-upstream origin ${repo.status_local.branch}')!

View File

@@ -6,6 +6,7 @@ import os
@[params]
pub struct GitCloneArgs {
pub mut:
//only url needed because is a clone
url string
sshkey string
}
@@ -17,11 +18,20 @@ pub fn (mut gitstructure GitStructure) clone(args GitCloneArgs) !&GitRepo {
}
console.print_header('Git clone from the URL: ${args.url}.')
//gitlocatin comes just from the url, not from fs of whats already there
git_location := gitstructure.gitlocation_from_url(args.url)!
mut repo := gitstructure.repo_new_from_gitlocation(git_location)!
repo.status_wanted.url = args.url
repo.status_wanted.branch = git_location.branch_or_tag
//TODO: this seems to be wrong, we should not set the url here
// repo.status_wanted.url = args.url
// repo.status_wanted.branch = git_location.branch_or_tag
mut repopath := repo.patho()!
if repopath.exists(){
return error("can't clone on existing path, came from url, path found is ${repopath.path}.\n")
}
if args.sshkey.len > 0 {
repo.set_sshkey(args.sshkey)!
@@ -29,14 +39,17 @@ pub fn (mut gitstructure GitStructure) clone(args GitCloneArgs) !&GitRepo {
parent_dir := repo.get_parent_dir(create: true)!
cfg := gitstructure.config()!
mut extra := ''
if gitstructure.config()!.light {
if cfg.light {
extra = '--depth 1 --no-single-branch '
}
cfg := gitstructure.config()!
//the url needs to be http if no agent, otherwise its ssh, the following code will do this
mut cmd := 'cd ${parent_dir} && git clone ${extra} ${repo.get_repo_url_for_clone()!} ${repo.name}'
mut cmd := 'cd ${parent_dir} && git clone ${extra} ${repo.get_http_url()!} ${repo.name}'
mut sshkey_include := ''
if cfg.ssh_key_path.len > 0 {

View File

@@ -87,27 +87,32 @@ pub fn (mut repo GitRepo) get_parent_dir(args GetParentDir) !string {
return parent_dir
}
@[params]
pub struct GetRepoUrlArgs {
pub mut:
with_branch bool // // If true, return the repo URL for an exact branch.
}
//DONT THINK ITS GOOD TO GIVE THE BRANCH
// @[params]
// pub struct GetRepoUrlArgs {
// pub mut:
// with_branch bool // // If true, return the repo URL for an exact branch.
// }
// url_get returns the URL of a git address
fn (self GitRepo) get_repo_url(args GetRepoUrlArgs) !string {
url := self.status_wanted.url
if url.len != 0 {
if args.with_branch {
return '${url}/tree/${self.status_local.branch}'
}
return url
}
fn (self GitRepo) get_repo_url_for_clone() !string {
//WHY do we do following, now uncommented, the following code dispisses the ssh url part
// url := self.status_wanted.url
// if true{panic(url)}
// if url.len != 0 {
// if args.with_branch {
// return '${url}/tree/${self.status_local.branch}'
// }
// return url
// }
if sshagent.loaded() {
return self.get_ssh_url()!
} else {
return self.get_http_url()!
}
}
fn (self GitRepo) get_ssh_url() !string {

View File

@@ -36,13 +36,14 @@ pub fn (mut f DocusaurusFactory) get(args_ DSiteGetArgs) !&DocSite {
mut args := args_
mut path := gittools.path(path: args.path, git_url: args.git_url, git_reset: args.git_reset, git_root: args.git_root, git_pull: args.git_pull,currentdir:true)!
args.path = path.path
if !path.is_dir() {
return error('path is not a directory')
}
configpath:='${args.path}/cfg'
if ! os.exists(configpath) {
return error("can't find config file in ${configpath}")
return error("can't find config file for docusaurus in ${configpath}")
}
osal.rm("${args.path}/cfg/main.json")!
@@ -63,7 +64,6 @@ pub fn (mut f DocusaurusFactory) get(args_ DSiteGetArgs) !&DocSite {
}
}
mut myconfig:=config_load(configpath)!
if myconfig.main.name.len == 0 {

View File

@@ -2,7 +2,6 @@ module docusaurus
import os
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.data.doctree
@[heap]
pub struct DocusaurusFactory {
@@ -42,5 +41,10 @@ pub fn new(args_ DocusaurusArgs) !&DocusaurusFactory {
}
f.template_install(install: args.update, template_update: args.update)!
if args.heroscript != '' {
play(heroscript: args.heroscript, heroscript_path: args.heroscript_path)!
}
return f
}

View File

@@ -42,7 +42,7 @@ fn (mut self DocusaurusFactory) template_install(args_ TemplateInstallArgs) ! {
//always stay in the context of the build directory
cmd: '
${osal.profile_path_source_and()!}
export PATH=${self.path_build.path}/node_modules/.bin::??{HOME}/.bun/bin/:??PATH
export PATH=${self.path_build.path}/node_modules/.bin::${os.home_dir()}/.bun/bin/:\$PATH
cd ${self.path_build.path}
bun install
'

View File

@@ -18,27 +18,27 @@ pub fn play(args_ PlayArgs) ! {
mut args := args_
mut plbook := args.plbook or { playbook.new(text: args.heroscript,path:args.heroscript_path)! }
mut ds := docusaurus.new()!
if plbook.exists_once(filter: 'docusaurus.define'){
mut action:=plbook.action_get(actor:'docusaurus',name:'define')!
actions_define := plbook.find(filter: 'docusaurus.define')!
if actions_define.len >1 {
return error("found multiple docusaurus.play actions, only one is allowed")
}
for action in actions_define{
mut p := action.params
path_publish := p.get_default('path_publish',"")!
path_build := p.get_default('path_build',"")!
production := p.get_default_false('production')
update := p.get_default_false('update')
//don't do heroscript here because this could already be done before
ds = docusaurus.new(
path_publish: path_publish
path_build: path_build
production: production
update: update
// heroscript: p.get_default('heroscript', "")!
// heroscript_path: p.get_default('heroscript_path', "")!
)!
}
actions := plbook.find(filter: 'docusaurus.add')!
@@ -68,7 +68,7 @@ pub fn play(args_ PlayArgs) ! {
if plbook.exists_once(filter: 'docusaurus.dev'){
// site.dev()!
site.dev()!
}