Skip to content

Commit

Permalink
Run cells filtered by category at the document level (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso authored Feb 29, 2024
1 parent 7b6da2b commit c15d119
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 7 deletions.
44 changes: 37 additions & 7 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
62 changes: 62 additions & 0 deletions testdata/categories/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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
Expand All @@ -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

0 comments on commit c15d119

Please sign in to comment.