mateodelnorte / meta

tool for turning many repos into a meta repo. why choose many repos or a monolithic repo, when you can have both with a meta repo?
MIT License
2.03k stars 95 forks source link

Provide utility environment variables to `meta-exec` #239

Closed vviikk closed 3 years ago

vviikk commented 3 years ago

🚀 Feature Proposal

Have things like current folder name (just the name, not the full path) as an environment variable you can use in commands

Motivation

Take this for example. I have three repos (two python repos & one node) image

And I run the following command:

$ meta exec "[ -f requirements.txt ] && echo dir:`pwd`" --include-only=python-project-1,python-project-2,node-project-2

image

The context or current working directory is still where I ran the command from. It would be good to have access to such things when running meta-exec. But I don't know if this should be in loop or meta-exec. An environment variable like META_CWD. See example below.

Example

Say I want to create virtual environments for all my python projects with just the folder name as the venv name.

$ meta exec "[ -f requirements.txt ] && python3 -m venv --prompt $META_CWD   .venv"

Currently there is no easy way to do this so I just manually create my virtual environments. Hope I am making sense.

patrickleet commented 3 years ago

Makes sense

try xargs though

meta exec "pwd | xargs basename | xargs echo"

for venv

meta exec "pwd | xargs basename | xargs python3 -m venv .venv --prompt"

For example:

meta exec "pwd | xargs basename | xargs python3 -m venv .venv --prompt" --include-only=plugins/meta-loop

image

vviikk commented 3 years ago

What manner of sorcery is this? And why does this work lol?!

patrickleet commented 3 years ago

You were having trouble with when the expression is run - the command pwd always worked directly.

Seems that when you use in as an expression, wrapped in ` or $(), it gets evaluated before being executed in each directory.

So, in this example, pwd returns the directory path.. then it is piped to xargs which, as a simple explanation, basically puts it as an argument to the given command..

basename gets the directory name, and the result of that is piped to the next xargs command.

patrickleet commented 3 years ago

I didn't try this before, but escaping the `'s also works:

➜  meta exec "echo `pwd`" --include-only=plugins/meta-loop

plugins/meta-loop:
/Users/patrickleet/dev/mateodelnorte/meta
plugins/meta-loop ✓
➜  meta exec "echo \`pwd\`" --include-only=plugins/meta-loop

plugins/meta-loop:
/Users/patrickleet/dev/mateodelnorte/meta/plugins/meta-loop
plugins/meta-loop ✓
➜  meta exec "echo \$(pwd)" --include-only=plugins/meta-loop 

plugins/meta-loop:
/Users/patrickleet/dev/mateodelnorte/meta/plugins/meta-loop
plugins/meta-loop ✓
patrickleet commented 3 years ago

I added a note to the README: https://github.com/mateodelnorte/meta#how-to-escape-expressions

vviikk commented 3 years ago

Legend, thanks.