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

feat: Show all available task with task list #1286

Merged
merged 26 commits into from
May 1, 2024
Merged
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 55 additions & 12 deletions src/cli/task.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::project::manifest::EnvironmentName;
use crate::project::manifest::FeatureName;
use crate::project::virtual_packages::verify_current_platform_has_required_virtual_packages;
use crate::task::{quote, Alias, CmdArgs, Execute, Task, TaskName};
use crate::Project;
use clap::Parser;
use indexmap::IndexMap;
use itertools::Itertools;
use rattler_conda_types::Platform;
use std::collections::HashSet;
use std::error::Error;
use std::path::PathBuf;
use std::str::FromStr;
use toml_edit::{Array, Item, Table, Value};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -135,17 +139,22 @@ impl From<AddArgs> for Task {
Self::Plain(cmd_args)
} else {
let cwd = value.cwd;
let mut env = IndexMap::new();
for (key, value) in value.env {
env.insert(key, value);
}
let env = if value.env.is_empty() {
None
} else {
let mut env = IndexMap::new();
for (key, value) in value.env {
env.insert(key, value);
}
Some(env)
};
Self::Execute(Execute {
cmd: CmdArgs::Single(cmd_args),
depends_on,
inputs: None,
outputs: None,
cwd,
env: Some(env),
env,
})
}
}
Expand Down Expand Up @@ -273,15 +282,49 @@ pub fn execute(args: Args) -> miette::Result<()> {
);
}
Operation::List(args) => {
let environment = project.environment_from_name_or_env_var(args.environment)?;
let tasks = environment
.tasks(Some(Platform::current()))?
.into_keys()
.collect_vec();
if tasks.is_empty() {
let explicit_environment = args
.environment
.map(|n| EnvironmentName::from_str(n.as_str()))
.transpose()?
.map(|n| {
project
.environment(&n)
.ok_or_else(|| miette::miette!("unknown environment '{n}'"))
})
.transpose()?;
let available_tasks: HashSet<TaskName> =
if let Some(explicit_environment) = explicit_environment {
explicit_environment
.tasks(Some(Platform::current()))
.into_iter()
.flat_map(|tasks| tasks.into_keys())
.map(ToOwned::to_owned)
.collect()
} else {
project
.environments()
.into_iter()
.filter(|env| {
verify_current_platform_has_required_virtual_packages(env).is_ok()
})
.flat_map(|env| {
env.tasks(Some(Platform::current()))
.into_iter()
.flat_map(|tasks| {
tasks
.into_iter()
.filter(|&(key, _)| !key.as_str().starts_with('_'))
.map(|(key, _)| key)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use filter_map here as this seems to be the correct thing to do. Just need some more time to get it right.

no spoilers 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sold on if filter_map is better than the filter and map combination.

})
.map(ToOwned::to_owned)
})
.collect()
};

if available_tasks.is_empty() {
eprintln!("No tasks found",);
} else {
let formatted: String = tasks
let formatted: String = available_tasks
.iter()
.sorted()
.map(|name| {
Expand Down
Loading