rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.72k stars 2.42k forks source link

Extend aliases: allow arbitrary commands #6575

Open theduke opened 5 years ago

theduke commented 5 years ago

The [alias] configuration is very useful.

It would be even more useful if there was a way to not only define aliases for cargo, but to run arbitrary scripts. It's a shame that alias doesn't allow this already. I'm therefore proposing to add a new feature: scripts.

I'm imagining something like how npm works, where you can define arbitrary scripts in package.json that can be executed with npm NAME or npm run NAME (for conflicts). This is very widely used in the JS ecosystem and a great convenience utility.

It would allow a unified way to execute project-related convenience scripts, etc.

EG:

.cargo/config:

[script]
generate-keys = "ssh-keygen ....."
check = "cargo fmt && cargo test && cargo clippy"
deploy = "./bin/deploy.sh"
# Conflicting name, see below
publish = "./bin/publish.sh"
...

Disambiguation could be enabled by the cargo script command, eg : cargo script publish && cargo deploy.

Scripts would have the lowest priority, after built in commands, aliases and external binaries. I'd prefer them to have higher priority than global aliases and extension binaries, but that would be backwards incompatible.

djmitche commented 3 years ago

Git's approach is

    gist = "!/bin/sh -c 'git add --all . && git commit -m x && git push'"

I think the special-case is a string beginning with !. We could do the same with aliases.

epage commented 12 months ago

The challenge is what syntax do you support. Bonus points for cross platform.

deno_task_shell and duckscript would be two options.;

rmnilin commented 6 months ago

I see the integration of an additional scripting language for this functionality as too costly for the benefits provided. Despite the cross-platform bonuses, this solution increases bulkiness, reduces maintainability, and is less flexible and straightforward compared to alternatives. One of the more flexible and simple alternatives is passing values in a [script] object as script arguments to sh, cmd.exe, and other shells.

The cross-platform issue can be addressed by utilizing nested objects combined with shell settings:

[alias]
# Keeping `alias` and `script` separate avoids the need for less obvious conventions like `!`

[script]
greet = "echo -n 'Hello, ' && echo 'developer!'"

[script.powershell]
# Acts as a fallback for systems where the default shell is not found,
# and when the script was launched in PowerShell
greet = "Write-Host -NoNewline 'Hello, '; if ($?) { Write-Host 'developer!' }"

[shell]
default = "sh"