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

Special Variable for Current Working Directory #525

Closed
is-jonreeves opened this issue Jul 18, 2021 · 3 comments
Closed

Special Variable for Current Working Directory #525

is-jonreeves opened this issue Jul 18, 2021 · 3 comments
Labels
state: duplicate Duplicates another issue.

Comments

@is-jonreeves
Copy link

is-jonreeves commented Jul 18, 2021

It would be great to have a "special variable" that reflects the Current Working Directory for its context. I am finding myself trying to use the sh: echo "$PWD" approach lots and this has some issues when running more than once. Right now I find myself working around this issue alot having to use a combination of the list | join and osClean functions to construct the correct Abs Path for my given context.

Example

If a "special variable" called .CWD was implement then, the following would be possible:

version: '3'

tasks:
  example1:
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects\taskfile-test
  example2:
    dir: ../
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects
  example3:
    dir: ../../
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon

Workarounds

There are a few ways I've found of doing this today without a built-in special variable, but they feel unnecessary:

Approach 1: Dynamic Variables (glitched right now)

Note the need to append pairs of '' to each sh: usage.

version: '3'
silent: true

vars:
  CWD: { sh: echo $PWD }

tasks:
  example1:
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects\taskfile-test

  example2:
    dir: ../
    vars:
      CWD: { sh: echo $PWD'' }
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects

  example3:
    dir: ../../
    vars:
      CWD: { sh: echo $PWD'''' }
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon

Approach 2: String Processing

Really Repetitive and wouldn't support non-relative paths.

version: '3'
silent: true

vars:
  DIR: './'
  CWD: { sh: pwd }

tasks:
  example1:
    dir: '{{.DIR}}'
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects\taskfile-test

  example2:
    dir: '{{.DIR}}'
    vars:
      DIR: ../
      CWD: '{{list .CWD "/" .DIR | join "" | osClean}}'
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon\Projects

  example3:
    dir: '{{.DIR}}'
    vars:
      DIR: ../../
      CWD: '{{list .CWD "/" .DIR | join "" | osClean}}'
    cmds:
      - echo "{{.CWD}}"    # ---> C:\Users\Jon
@is-jonreeves is-jonreeves added the type: feature A new feature or functionality. label Jul 18, 2021
@weeping-somnambulist
Copy link

I wholeheartedly agree with this request. I also believe there should be a special variable for $HOME and $USERNAME.

@andreynering I'm happy to look into this as a first PR if you're interested in adding the functionality. I appreciate any thoughts you have on the matter.

@andreynering
Copy link
Member

Hi @is-jonreeves and @SwitchedToGitlab,

Yep, I this this makes sense to have! 👍


I think we don't need variables for username and home, though. These are available as environment variables, and do not change between task runs.

@andreynering andreynering added the state: duplicate Duplicates another issue. label Aug 1, 2021
@andreynering
Copy link
Member

I just realized this is proposed before.

Closed as a duplicate of #215.

@pd93 pd93 removed the type: feature A new feature or functionality. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: duplicate Duplicates another issue.
Projects
None yet
Development

No branches or pull requests

4 participants