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

Add sources special var #1146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hernandanielg
Copy link

@hernandanielg hernandanielg commented May 2, 2023

Fixes #948

This is my attempt to implement the sources special variable, it does the trick but I am unsure if it should be this way... please provide some feedback ;)

Example test

$ cat ./testdata/special_vars/Taskfile.yml
tasks:
  default:
    cmds:
      - task: print
      - task: included:print

  print:
    cmds:
      - echo root/TASK={{.TASK}}
      - echo root/ROOT_DIR={{.ROOT_DIR}}
      - echo root/TASKFILE_DIR={{.TASKFILE_DIR}}
      - echo root/TASK_VERSION="{{.TASK_VERSION}}"
      - echo root/SOURCES="{{.SOURCES}}"
    sources:
     - '*.sh'

$ go run ./cmd/task --dir ./testdata/special_vars/ -f print
task: [print] echo root/TASK=print
root/TASK=print
task: [print] echo root/ROOT_DIR=/Users/hernandanielg/src/task/testdata/special_vars
root/ROOT_DIR=/Users/hernandanielg/src/task/testdata/special_vars
task: [print] echo root/TASKFILE_DIR=/Users/hernandanielg/src/task/testdata/special_vars
root/TASKFILE_DIR=/Users/hernandanielg/src/task/testdata/special_vars
task: [print] echo root/TASK_VERSION="(devel)"
root/TASK_VERSION=(devel)
task: [print] echo root/SOURCES="install-task.sh"
root/SOURCES=install-task.sh

$ go test -run ^TestSpecialVars$ github.com/go-task/task/v3

=== RUN   TestSpecialVars
--- PASS: TestSpecialVars (0.02s)
PASS
ok      github.com/go-task/task/v3      0.206s


> Test run finished at 5/2/2023, 7:03:21 AM <

@hernandanielg hernandanielg changed the title add sources special var Add sources special var May 2, 2023
@hernandanielg hernandanielg force-pushed the sources-special-var branch from 16af88d to 8b27568 Compare May 2, 2023 12:04
@andreynering andreynering added type: feature A new feature or functionality. area: fingerprinting Changes related to checksums and caching. labels May 7, 2023
@andreynering
Copy link
Member

Hi @hernandanielg, thanks for this PR!

As I mentioned on this comment #948 (comment) I think this variable should probably be a slice/array of strings, instead of a string.

This way, the user can use slim-sprig to manipulate the list however they want, adding more flexibility to this variable.

@andreynering andreynering removed the area: fingerprinting Changes related to checksums and caching. label May 7, 2023
@hernandanielg
Copy link
Author

Hey @andreynering

Thanks a lot for the review, would you please help me a bit giving me some insights (first tim contributor here)

I am confused because function func (c *CompilerV3) getSpecialVars(t *taskfile.Task) returns a (map[string]string, error) so that's why I couldn't return a slice of strings, Globs function returns a slice of strings already

The way slim-sprig works seems I should return a space-separated string, am I wrong?

So is this going in a good direction or I should do something different?

Cheers!

@andreynering
Copy link
Member

For reference, as said on Discord, this may need some refactor work to support types other than string on the variables map.

There's this old issue about it: #140. In theory it would allow even users themselves to declare array, maps, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A new feature or functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use sources as variable
2 participants