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

feat(fingreprinting): add .taskignore #1932

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (e *Executor) Setup() error {
}
e.setupDefaults()
e.setupConcurrencyState()
e.readTaskignore()

return nil
}

Expand All @@ -63,6 +65,28 @@ func (e *Executor) getRootNode() (taskfile.Node, error) {
return node, err
}

func (e *Executor) readTaskignore() {
// get only the tasks that have the sources defined
tasksWithSources := taskfile.GetTasksWithSources(e.Taskfile)

if tasksWithSources == nil {
return
}

ignoreGlobs := taskfile.ReadTaskignore(e.Logger, e.Dir, e.Timeout)

if ignoreGlobs == nil {
return
}

// apply .taskignore globs to each task
for _, t := range tasksWithSources {
for _, g := range ignoreGlobs {
t.Sources = append(t.Sources, &ast.Glob{Glob: g, Negate: true})
}
}
}

func (e *Executor) readTaskfile(node taskfile.Node) error {
reader := taskfile.NewReader(
node,
Expand Down
17 changes: 17 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,23 @@ func TestReference(t *testing.T) {
}
}

func TestTaskignore(t *testing.T) {
var buff bytes.Buffer
e := task.Executor{
Dir: "testdata/taskignore",
Stdout: &buff,
Stderr: &buff,
Force: true,
Silent: true,
}

require.NoError(t, e.Setup())

err := e.Run(context.Background(), &ast.Call{Task: "txt"})
assert.Equal(t, "dont_ignore.txt\n", buff.String())
require.NoError(t, err)
}

// enableExperimentForTest enables the experiment behind pointer e for the duration of test t and sub-tests,
// with the experiment being restored to its previous state when tests complete.
//
Expand Down
67 changes: 67 additions & 0 deletions taskfile/taskignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package taskfile

import (
"context"
"path"
"strings"
"time"

"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/ast"
)

const taskignore = ".taskignore"

func GetTasksWithSources(t *ast.Taskfile) []*ast.Task {
var tasksWithSources []*ast.Task

for _, task := range t.Tasks.Values() {
if len(task.Sources) > 0 {
tasksWithSources = append(tasksWithSources, task)
}
}

return tasksWithSources
}

func ReadTaskignore(l *logger.Logger, dir string, timeout time.Duration) []string {
bytes := read(l, dir, timeout)
globs := filterGlobs(bytes)

return globs
}

func read(l *logger.Logger, dir string, timeout time.Duration) []byte {
fileNode, err := NewFileNode(l, "", path.Join(dir, taskignore))
if err != nil {
return nil
}

ctx, cf := context.WithTimeout(context.Background(), timeout)
defer cf()

bytes, err := fileNode.Read(ctx)
if err != nil {
return nil
}

return bytes
}

func filterGlobs(bytes []byte) []string {
if len(bytes) == 0 {
return nil
}

lines := strings.Split(string(bytes), "\n")
var validGlobs []string

for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed != "" && !strings.HasPrefix(trimmed, "#") {
validGlobs = append(validGlobs, trimmed)
}
}

return validGlobs
}
1 change: 1 addition & 0 deletions testdata/taskignore/.taskignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./**/*.json
12 changes: 12 additions & 0 deletions testdata/taskignore/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'

tasks:
txt:
desc: Echo file names
sources:
- ./nested/ignore_me_too.json
- ./ignore_me.json
- ./dont_ignore.txt
cmds:
- for: sources
cmd: echo {{.ITEM}}
Empty file.
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions website/docs/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,37 @@ tasks:
- public/bundle.css
```

It is also possible to exclude files from fingerprinting globally by creating `.taskignore`
file in the same directory where the `Taskfile` is located.

<Tabs defaultValue="2"
values={[
{ label: 'Taskfile', value: '1' },
{ label: '.taskignore', value: '2' }
]}>

<TabItem value="1">

```yaml
version: '3'

tasks:
css:
sources:
- mysources/**/*.css
generates:
- public/bundle.css
```

</TabItem>
<TabItem value="2">

```yaml
mysources/ignoreme.css
```

</TabItem></Tabs>

If you prefer these check to be made by the modification timestamp of the files,
instead of its checksum (content), just set the `method` property to
`timestamp`.
Expand Down