Skip to content

Commit

Permalink
watch: added new command (#1263)
Browse files Browse the repository at this point in the history
* watch: added new command

* Commit from GitHub Actions (test)

---------

Co-authored-by: rtx[bot] <[email protected]>
  • Loading branch information
jdx and mise-en-dev authored Dec 24, 2023
1 parent 31ddbe2 commit be6a73c
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 23 deletions.
19 changes: 16 additions & 3 deletions completions/_rtx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ _rtx() {
(up|upgrade) __rtx_upgrade_cmd && ret=0 ;;
(u|use) __rtx_use_cmd && ret=0 ;;
(v|version) __rtx_version_cmd && ret=0 ;;
(w|watch) __rtx_watch_cmd && ret=0 ;;
(where) __rtx_where_cmd && ret=0 ;;
(which) __rtx_which_cmd && ret=0 ;;
esac
Expand Down Expand Up @@ -530,7 +531,7 @@ __rtx_reshim_cmd() {
(( $+functions[__rtx_run_cmd] )) ||
__rtx_run_cmd() {
_arguments -s -S \
':task:__rtx_tasks' \
'::task:__rtx_tasks' \
'*::args:' \
'(-C --cd)'{-C,--cd}'=[Change to this directory before executing the command]:cd:_directories' \
'(-n --dry-run)'{-n,--dry-run}'[Don'\''t actually run the task(s), just print them in order of execution]' \
Expand Down Expand Up @@ -687,6 +688,7 @@ return ret
__rtx_task_edit_cmd() {
_arguments -s -S \
':task:__rtx_tasks' \
'(-p --path)'{-p,--path}'[Display the path to the task instead of editing it]' \
'(-q --quiet)'{-q,--quiet}'[Suppress non-error messages]' \
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all prompts]'
Expand All @@ -703,7 +705,7 @@ __rtx_task_ls_cmd() {
(( $+functions[__rtx_task_run_cmd] )) ||
__rtx_task_run_cmd() {
_arguments -s -S \
':task:__rtx_tasks' \
'::task:__rtx_tasks' \
'*::args:' \
'(-C --cd)'{-C,--cd}'=[Change to this directory before executing the command]:cd:_directories' \
'(-n --dry-run)'{-n,--dry-run}'[Don'\''t actually run the task(s), just print them in order of execution]' \
Expand Down Expand Up @@ -773,6 +775,16 @@ __rtx_version_cmd() {
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all prompts]'
}
(( $+functions[__rtx_watch_cmd] )) ||
__rtx_watch_cmd() {
_arguments -s -S \
'*'{-t,--task}'=[Task to run]:task:__rtx_tasks' \
'*::args:' \
'*'{-g,--glob}'=[Files to watch]:glob:' \
'(-q --quiet)'{-q,--quiet}'[Suppress non-error messages]' \
'*'{-v,--verbose}'[Show extra output (use -vv for even more)]' \
'(-y --yes)'{-y,--yes}'[Answer yes to all prompts]'
}
(( $+functions[__rtx_where_cmd] )) ||
__rtx_where_cmd() {
_arguments -s -S \
Expand Down Expand Up @@ -829,6 +841,7 @@ __rtx_cmds() {
{up,upgrade}':Upgrades outdated tool versions'
{u,use}':Change the active version of a tool locally or globally.'
'version:Show rtx version'
{w,watch}':\[experimental\] Run a task watching for changes'
'where:Display the installation path for a runtime'
'which:Shows the path that a bin name points to'
)
Expand Down Expand Up @@ -900,7 +913,7 @@ __rtx_sync_cmds() {
__rtx_task_cmds() {
local commands; commands=(
'edit:\[experimental\] Edit a task with \$EDITOR'
'ls:\[experimental\] List config files currently in use'
'ls:\[experimental\] List available tasks to execute'
{r,run}':\[experimental\] Run a task'
)
_describe -t commands 'command' commands "$@"
Expand Down
67 changes: 62 additions & 5 deletions completions/rtx.bash
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ _rtx() {
rtx,version)
cmd="rtx__version"
;;
rtx,w)
cmd="rtx__watch"
;;
rtx,watch)
cmd="rtx__watch"
;;
rtx,where)
cmd="rtx__where"
;;
Expand Down Expand Up @@ -426,6 +432,9 @@ _rtx() {
rtx__help,version)
cmd="rtx__help__version"
;;
rtx__help,watch)
cmd="rtx__help__watch"
;;
rtx__help,where)
cmd="rtx__help__where"
;;
Expand Down Expand Up @@ -682,7 +691,7 @@ _rtx() {

case "${cmd}" in
rtx)
opts="-q -v -y -h -V --debug --log-level --trace --quiet --verbose --yes --help --version activate alias asdf bin-paths cache completion config current deactivate direnv doctor env env-vars exec global hook-env implode install latest link local ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version where which render-completion render-help render-mangen help"
opts="-q -v -y -h -V --debug --log-level --trace --quiet --verbose --yes --help --version activate alias asdf bin-paths cache completion config current deactivate direnv doctor env env-vars exec global hook-env implode install latest link local ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version watch where which render-completion render-help render-mangen help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -1478,7 +1487,7 @@ _rtx() {
return 0
;;
rtx__help)
opts="activate alias asdf bin-paths cache completion config current deactivate direnv doctor env env-vars exec global hook-env implode install latest link local ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version where which render-completion render-help render-mangen help"
opts="activate alias asdf bin-paths cache completion config current deactivate direnv doctor env env-vars exec global hook-env implode install latest link local ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version watch where which render-completion render-help render-mangen help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -2429,6 +2438,20 @@ _rtx() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
rtx__help__watch)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
rtx__help__where)
opts=""
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
Expand Down Expand Up @@ -2996,7 +3019,7 @@ _rtx() {
return 0
;;
rtx__run)
opts="-C -n -f -p -i -t -j -r -q -v -y -h --cd --dry-run --force --prefix --interleave --tool --jobs --raw --debug --log-level --trace --quiet --verbose --yes --help <TASK> [ARGS]..."
opts="-C -n -f -p -i -t -j -r -q -v -y -h --cd --dry-run --force --prefix --interleave --tool --jobs --raw --debug --log-level --trace --quiet --verbose --yes --help [TASK] [ARGS]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -3384,7 +3407,7 @@ _rtx() {
return 0
;;
rtx__task__edit)
opts="-q -v -y -h --debug --log-level --trace --quiet --verbose --yes --help <TASK>"
opts="-p -q -v -y -h --path --debug --log-level --trace --quiet --verbose --yes --help <TASK>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -3490,7 +3513,7 @@ _rtx() {
return 0
;;
rtx__task__run)
opts="-C -n -f -p -i -t -j -r -q -v -y -h --cd --dry-run --force --prefix --interleave --tool --jobs --raw --debug --log-level --trace --quiet --verbose --yes --help <TASK> [ARGS]..."
opts="-C -n -f -p -i -t -j -r -q -v -y -h --cd --dry-run --force --prefix --interleave --tool --jobs --raw --debug --log-level --trace --quiet --verbose --yes --help [TASK] [ARGS]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -3657,6 +3680,40 @@ _rtx() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
rtx__watch)
opts="-t -g -q -v -y -h --task --glob --debug --log-level --trace --quiet --verbose --yes --help [ARGS]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
--task)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-t)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--glob)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-g)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--log-level)
COMPREPLY=($(compgen -W "error warn info debug trace" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
rtx__where)
opts="-q -v -y -h --debug --log-level --trace --quiet --verbose --yes --help <TOOL@VERSION> [ASDF_VERSION]"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
Expand Down
19 changes: 13 additions & 6 deletions completions/rtx.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -l fssf "__fish_seen_subcommand_from"
complete -kxc rtx -s q -l quiet -d 'Suppress non-error messages'
complete -kxc rtx -s v -l verbose -d 'Show extra output (use -vv for even more)'
complete -kxc rtx -s y -l yes -d 'Answer yes to all prompts'
set -l others activate alias bin-paths cache completion config current deactivate direnv doctor env env-vars exec implode install latest link ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version where which
set -l others activate alias bin-paths cache completion config current deactivate direnv doctor env env-vars exec implode install latest link ls ls-remote outdated plugins prune reshim run self-update settings shell sync task trust uninstall upgrade use version watch where which
complete -xc rtx -n "not $fssf $others" -a activate -d 'Initializes rtx in the current shell session'
complete -xc rtx -n "not $fssf $others" -a alias -d 'Manage aliases'
complete -xc rtx -n "not $fssf $others" -a bin-paths -d 'List all the active runtime bin paths'
Expand Down Expand Up @@ -39,6 +39,7 @@ complete -xc rtx -n "not $fssf $others" -a uninstall -d 'Removes runtime version
complete -xc rtx -n "not $fssf $others" -a upgrade -d 'Upgrades outdated tool versions'
complete -xc rtx -n "not $fssf $others" -a use -d 'Change the active version of a tool locally or globally.'
complete -xc rtx -n "not $fssf $others" -a version -d 'Show rtx version'
complete -xc rtx -n "not $fssf $others" -a watch -d '[experimental] Run a task watching for changes'
complete -xc rtx -n "not $fssf $others" -a where -d 'Display the installation path for a runtime'
complete -xc rtx -n "not $fssf $others" -a which -d 'Shows the path that a bin name points to'

Expand Down Expand Up @@ -220,15 +221,15 @@ complete -kxc rtx -n "$fssf prune" -a "(__rtx_plugins)" -d 'Prune only versions
# reshim
# run
complete -kxc rtx -n "$fssf run" -d 'Arguments to pass to the task'
complete -kxc rtx -n "$fssf run" -d 'Arguments to pass to the task. Use ":::" to separate tasks'
complete -kxc rtx -n "$fssf run" -s C -l cd -a "(__fish_complete_directories)" -d 'Change to this directory before executing the command'
complete -kxc rtx -n "$fssf run" -s n -l dry-run -d 'Don'\''t actually run the task(s), just print them in order of execution'
complete -kxc rtx -n "$fssf run" -s f -l force -d 'Force the task to run even if outputs are up to date'
complete -kxc rtx -n "$fssf run" -s i -l interleave -d 'Print directly to stdout/stderr instead of by line'
complete -kxc rtx -n "$fssf run" -s j -l jobs -d 'Number of tasks to run in parallel'
complete -kxc rtx -n "$fssf run" -s p -l prefix -d 'Print stdout/stderr by line, prefixed with the task'\''s label'
complete -kxc rtx -n "$fssf run" -s r -l raw -d 'Read/write directly to stdin/stdout/stderr instead of by line'
complete -kxc rtx -n "$fssf run" -a "(__rtx_tasks)" -d 'Task to run Can specify multiple tasks by separating with `:::` e.g.: rtx run task1 arg1 arg2 ::: task2 arg1 arg2'
complete -kxc rtx -n "$fssf run" -a "(__rtx_tasks)" -d 'Task to run'
complete -kxc rtx -n "$fssf run" -s t -l tool -a "(__rtx_tool_versions)" -d 'Tool(s) to also add e.g.: node@20 [email protected]'
# self-update
Expand Down Expand Up @@ -282,26 +283,27 @@ complete -kxc rtx -n "$fssf task" -l hidden -d 'Show hidden tasks'
complete -kxc rtx -n "$fssf task" -l no-header -d 'Do not print table header'
set -l others edit ls run
complete -xc rtx -n "$fssf task; and not $fssf $others" -a edit -d '[experimental] Edit a task with $EDITOR'
complete -xc rtx -n "$fssf task; and not $fssf $others" -a ls -d '[experimental] List config files currently in use'
complete -xc rtx -n "$fssf task; and not $fssf $others" -a ls -d '[experimental] List available tasks to execute'
complete -xc rtx -n "$fssf task; and not $fssf $others" -a run -d '[experimental] Run a task'
# task edit
complete -kxc rtx -n "$fssf task; and $fssf edit" -s p -l path -d 'Display the path to the task instead of editing it'
complete -kxc rtx -n "$fssf task; and $fssf edit" -a "(__rtx_tasks)" -d 'Task to edit'
# task ls
complete -kxc rtx -n "$fssf task; and $fssf ls" -l hidden -d 'Show hidden tasks'
complete -kxc rtx -n "$fssf task; and $fssf ls" -l no-header -d 'Do not print table header'
# task run
complete -kxc rtx -n "$fssf task; and $fssf run" -d 'Arguments to pass to the task'
complete -kxc rtx -n "$fssf task; and $fssf run" -d 'Arguments to pass to the task. Use ":::" to separate tasks'
complete -kxc rtx -n "$fssf task; and $fssf run" -s C -l cd -a "(__fish_complete_directories)" -d 'Change to this directory before executing the command'
complete -kxc rtx -n "$fssf task; and $fssf run" -s n -l dry-run -d 'Don'\''t actually run the task(s), just print them in order of execution'
complete -kxc rtx -n "$fssf task; and $fssf run" -s f -l force -d 'Force the task to run even if outputs are up to date'
complete -kxc rtx -n "$fssf task; and $fssf run" -s i -l interleave -d 'Print directly to stdout/stderr instead of by line'
complete -kxc rtx -n "$fssf task; and $fssf run" -s j -l jobs -d 'Number of tasks to run in parallel'
complete -kxc rtx -n "$fssf task; and $fssf run" -s p -l prefix -d 'Print stdout/stderr by line, prefixed with the task'\''s label'
complete -kxc rtx -n "$fssf task; and $fssf run" -s r -l raw -d 'Read/write directly to stdin/stdout/stderr instead of by line'
complete -kxc rtx -n "$fssf task; and $fssf run" -a "(__rtx_tasks)" -d 'Task to run Can specify multiple tasks by separating with `:::` e.g.: rtx run task1 arg1 arg2 ::: task2 arg1 arg2'
complete -kxc rtx -n "$fssf task; and $fssf run" -a "(__rtx_tasks)" -d 'Task to run'
complete -kxc rtx -n "$fssf task; and $fssf run" -s t -l tool -a "(__rtx_tool_versions)" -d 'Tool(s) to also add e.g.: node@20 [email protected]'
Expand Down Expand Up @@ -336,6 +338,11 @@ complete -kxc rtx -n "$fssf use" -a "(__rtx_tool_versions)" -d 'Tool(s) to add t
# version
# watch
complete -kxc rtx -n "$fssf watch" -d 'Extra arguments'
complete -kxc rtx -n "$fssf watch" -s g -l glob -d 'Files to watch'
complete -kxc rtx -n "$fssf watch" -s t -l task -a "(__rtx_tasks)" -d 'Task to run'
# where
complete -kxc rtx -n "$fssf where" -a "(__rtx_tool_versions)" -d 'Tool(s) to look up'
Expand Down
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod uninstall;
mod upgrade;
mod r#use;
pub mod version;
mod watch;
mod r#where;
mod r#which;

Expand Down Expand Up @@ -95,6 +96,7 @@ pub enum Commands {
Upgrade(upgrade::Upgrade),
Use(r#use::Use),
Version(version::Version),
Watch(watch::Watch),
Where(r#where::Where),
Which(which::Which),

Expand Down Expand Up @@ -149,6 +151,7 @@ impl Commands {
Self::Upgrade(cmd) => cmd.run(),
Self::Use(cmd) => cmd.run(),
Self::Version(cmd) => cmd.run(),
Self::Watch(cmd) => cmd.run(),
Self::Where(cmd) => cmd.run(),
Self::Which(cmd) => cmd.run(),

Expand Down
48 changes: 43 additions & 5 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,39 @@ use crate::ui::style;
use crate::{env, file, ui};

/// [experimental] Run a task
///
/// This command will run a task, or multiple tasks in parallel.
/// Tasks may have dependencies on other tasks or on source files.
/// If source is configured on a task, it will only run if the source
/// files have changed.
///
/// Tasks can be defined in .rtx.toml or as standalone scripts.
/// In .rtx.toml, tasks take this form:
///
/// [tasks.build]
/// run = "npm run build"
/// sources = ["src/**/*.ts"]
/// outputs = ["dist/**/*.js"]
///
/// Alternatively, tasks can be defined as standalone scripts.
/// These must be located in the `.rtx/tasks` directory.
/// The name of the script will be the name of the task.
///
/// $ cat .rtx/tasks/build<<EOF
/// #!/usr/bin/env bash
/// npm run build
/// EOF
/// $ rtx run build
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "r", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Run {
/// Task to run
/// Can specify multiple tasks by separating with `:::`
/// e.g.: rtx run task1 arg1 arg2 ::: task2 arg1 arg2
#[clap(verbatim_doc_comment, default_value = "default")]
pub task: String,

/// Arguments to pass to the task
/// Arguments to pass to the task. Use ":::" to separate tasks.
#[clap()]
pub args: Vec<String>,

Expand Down Expand Up @@ -156,9 +180,6 @@ impl Run {
}

fn run_task(&self, config: &Config, env: &BTreeMap<String, String>, task: &Task) -> Result<()> {
if self.dry_run {
return Ok(());
}
let prefix = style::estyle(task.prefix()).fg(get_color()).to_string();
if !self.force && self.sources_are_fresh(config, task) {
info_unprefix_trunc!("{prefix} sources up-to-date, skipping");
Expand Down Expand Up @@ -250,6 +271,9 @@ impl Run {
if let Some(cd) = &self.cd.as_ref().or(task.dir.as_ref()) {
cmd = cmd.current_dir(cd);
}
if self.dry_run {
return Ok(());
}
if let Err(err) = cmd.execute() {
if let Some(ScriptFailed(_, Some(status))) = err.downcast_ref::<Error>() {
if let Some(code) = status.code() {
Expand Down Expand Up @@ -370,8 +394,22 @@ impl Run {

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>rtx run lint</bold>
Runs the "lint" task. This needs to either be defined in .rtx.toml
or as a standalone script. See the project README for more information.
$ <bold>rtx run build --force</bold>
Forces the "build" task to run even if its sources are up-to-date.
$ <bold>rtx run test --raw</bold>
Runs "test" with stdin/stdout/stderr all connected to the current terminal.
This forces `--jobs=1` to prevent interleaving of output.
$ <bold>rtx run lint ::: test ::: check</bold>
Runs the "lint", "test", and "check" tasks in parallel.
$ <bold>rtx task cmd1 arg1 arg2 ::: cmd2 arg1 arg2</bold>
TODO
Execute multiple tasks each with their own arguments.
"#
);

Expand Down
Loading

0 comments on commit be6a73c

Please sign in to comment.