deadc0de6 / dotdrop

Save your dotfiles once, deploy them everywhere
https://dotdrop.readthedocs.io
GNU General Public License v3.0
1.77k stars 105 forks source link

[feature] use instant value of a dynvariable when define another dynvariable #399

Closed roachsinai closed 1 year ago

roachsinai commented 1 year ago

I wanna know current windows user in wsl as dynvariable: win_user, then define vim_home according to value of win_user:

    dynvariables:
      win_user: powershell.exe -command "[Environment]::UserName"
      vim_home: echo C:/Users/{{@@ win_user @@}}/vimfiles

when run dotdrop value of vim_home echo to be C:/Users/powershell.exe -command "[Environment]::UserName":

If i comment definition of vim_home, then {{@@ win_user @@}} will eval to the Windows username in lines the refer win_user in dotfiles.

So seems in block of yaml, win_user is just like a macro, so what happened in define of vim_home is just text substitution.

Is there a way to get instant value of win_user in dynvariables block?

Thanks!

deadc0de6 commented 1 year ago

Dotdrop supports getting environment variable directly in variables.

Try with something like this:

variables:
  vim_home: "C:/Users/{{@@ env['USERNAME'] @@}}/vimfiles"

You can also enable logging (--verbose) which will show you how the variables are interpreted, it's quite useful when using dynvariables.

roachsinai commented 1 year ago

Thanks for remind me that wsl has Windows environment variables.

image

Does dotdrop using multithread? Check second line and last line in above image that vim_home was evaluated before win_user with below config:

    dynvariables:
      win_user: powershell.exe -command "[Environment]::UserName" | tr -d '\r\n'
      vim_home: echo 'C:/Users/{{@@ win_user @@}}/vimfiles'
deadc0de6 commented 1 year ago

Again, I wouldn't use dynvariables for what you're trying to do but go straight with variables and using the ability to get environment variable in them directly.

Regarding your question, what you see is just a debug log of entries and is not related to the order in which the different variables were evaluated. The variables (variables and dynvariables) are internally transformed in a dictionary and then recursively evaluated until none is left. It seems from the above output that vim_home failed to be evaluated. I guess if you scroll up a bit you should have some logs on the error that occured.

Still for a thing as simple as inserting username into a variable I wouldn't use a dynvariables with some powershell oneliner, that's a bit overkill.

roachsinai commented 1 year ago

Yeah, I will use environment variable, too. By the way, didn't find keyword like failed, error.

Thanks for your reply!