diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 8189e0b2b..bfaebaa77 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -88,30 +88,60 @@ func runCmd() *cobra.Command { for _, task := range tasks { block := task.CodeBlock + // Check if to run all and if the block should be excluded if runAll && len(categories) == 0 && block.ExcludeFromRunAll() { + // Skip the task if it should be excluded from run all continue } - if len(categories) > 0 { - if block.ExcludeFromRunAll() { - continue - } + // Check if categories are specified and if the block should be excluded + if len(categories) > 0 && block.ExcludeFromRunAll() { + // Skip the task if it should be excluded based on categories + continue + } + // Check if the block matches any of the specified categories + if len(categories) > 0 { bcats := block.Categories() + fm, _ := block.Document().Frontmatter() match := false - for _, bcat := range bcats { - for _, cat := range categories { - if bcat == cat { + + if fm != nil && fm.Category != "" { + // Check if the frontmatter category matches any block category + for _, bcat := range bcats { + if fm.Category == bcat { match = true + break + } + } + if !match { + // Check if the frontmatter category matches any specified category + for _, cat := range categories { + if fm.Category == cat { + match = true + break + } + } + } + } else { + // Check if any block category matches any specified category + for _, bcat := range bcats { + for _, cat := range categories { + if bcat == cat { + match = true + break + } } } } if !match { + // Skip the task if it doesn't match any specified category continue } } + // If none of the exclusion conditions met, add the task to runTasks runTasks = append(runTasks, task) } diff --git a/testdata/categories/basic.txtar b/testdata/categories/basic.txtar index 6f178dc5d..031fc1b49 100644 --- a/testdata/categories/basic.txtar +++ b/testdata/categories/basic.txtar @@ -13,6 +13,11 @@ exec runme run -c buzz -c bar --filename=CATEGORIES.md cmp stdout buzz-bar-list.txt ! stderr . +env SHELL=/bin/bash +exec runme run --all --skip-prompts --category solution-2 +cmp stdout doc-category.txt +! stderr . + -- CATEGORIES.md -- ```bash {"category":"foo","name":"set-env"} @@ -39,6 +44,56 @@ $ stty -opost $ echo "buzz!" ``` +-- install-manual-solution-1.md -- +--- +shell: bash +cwd: /tmp +category: solution-1 +--- + +Installation steps for Solution 1: + +```sh {"name":"install-solution1"} +$ stty -opost +$ echo "Install solution1" +``` + +Deployment steps for Solution 1: + +```sh {"name":"deploy-solution1"} +$ stty -opost +$ echo "Deploy solution1" +``` + +```sh {"category":"delete-solution"} +echo "Delete solution" +``` + +-- install-manual-solution-2.md -- +--- +shell: bash +cwd: /tmp +category: solution-2 +--- + +Installation steps for Solution 2: + +```sh {"name":"install-solution2"} +$ stty -opost +$ echo "Install solution2" +``` + +Deployment steps for Solution 2: + +```sh {"name":"deploy-solution2"} +$ stty -opost +$ echo "Deploy solution2" +``` + +```sh {"category":"delete-solution"} +$ echo "Delete solution" +``` + -- foo-bar-list.txt -- ► Running task set-env... ► ✓ Task set-env exited with code 0 @@ -57,3 +112,10 @@ bar! ► Running task print-buzz... buzz! ► ✓ Task print-buzz exited with code 0 +-- doc-category.txt -- + ► Running task install-solution2... +Install solution2 + ► ✓ Task install-solution2 exited with code 0 + ► Running task deploy-solution2... +Deploy solution2 + ► ✓ Task deploy-solution2 exited with code 0