Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cells filtered by category at the document level #516

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading