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

Allow listing of groups (also recursively) #2298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ pub(crate) enum Error<'src> {
UnknownSubmodule {
path: String,
},
UnknownSubmoduleGroup {
path: String,
},
UnknownOverrides {
overrides: Vec<String>,
},
Expand Down Expand Up @@ -456,6 +459,9 @@ impl<'src> ColorDisplay for Error<'src> {
UnknownSubmodule { path } => {
write!(f, "Justfile does not contain submodule `{path}`")?;
}
UnknownSubmoduleGroup { path } => {
write!(f, "Justfile does not contain submodule nor group `{path}`")?;
}
UnknownOverrides { overrides } => {
let count = Count("Variable", overrides.len());
let overrides = List::and_ticked(overrides);
Expand Down
46 changes: 46 additions & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,38 @@ impl<'src> Justfile<'src> {
pub(crate) fn groups(&self) -> &[String] {
&self.groups
}
pub(crate) fn public_group_map(
&self,
config: &Config,
) -> BTreeMap<Option<String>, Vec<ListEntry>> {
let mut groups = BTreeMap::<Option<String>, Vec<ListEntry>>::new();
let aliases = self.aliases(config);
for recipe in self.public_recipes(config) {
let recipe_groups = recipe.groups();
let entry = ListEntry::from_recipe(
recipe,
self.name().to_string(),
aliases.get(recipe.name()).unwrap_or(&Vec::new()).clone(),
);
if recipe_groups.is_empty() {
groups.entry(None).or_default().push(entry);
} else {
for group in recipe_groups {
groups.entry(Some(group)).or_default().push(entry.clone());
}
}
}
groups
}

pub(crate) fn find_public_group(&self, group: &str, config: &Config) -> Vec<&Recipe> {
self
.public_recipes(config)
.iter()
.filter(|recipe| recipe.groups().contains(group))
.copied()
.collect()
}

pub(crate) fn public_groups(&self, config: &Config) -> Vec<String> {
let mut groups = Vec::new();
Expand Down Expand Up @@ -431,6 +463,20 @@ impl<'src> Justfile<'src> {

groups.into_iter().map(|(_, _, group)| group).collect()
}

pub(crate) fn aliases(&self, config: &Config) -> BTreeMap<&str, Vec<&str>> {
if config.no_aliases {
return BTreeMap::new();
}
let mut aliases = BTreeMap::<&str, Vec<&str>>::new();
for alias in self.aliases.values().filter(|alias| !alias.is_private()) {
aliases
.entry(alias.target.name.lexeme())
.or_default()
.push(alias.name.lexeme());
}
aliases
}
}

impl<'src> ColorDisplay for Justfile<'src> {
Expand Down
21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ pub(crate) use {
fragment::Fragment, function::Function, interpreter::Interpreter,
interrupt_guard::InterruptGuard, interrupt_handler::InterruptHandler, item::Item,
justfile::Justfile, keyed::Keyed, keyword::Keyword, lexer::Lexer, line::Line, list::List,
load_dotenv::load_dotenv, loader::Loader, module_path::ModulePath, name::Name,
namepath::Namepath, ordinal::Ordinal, output::output, output_error::OutputError,
list_entry::ListEntry, load_dotenv::load_dotenv, loader::Loader, module_path::ModulePath,
name::Name, namepath::Namepath, ordinal::Ordinal, output::output, output_error::OutputError,
parameter::Parameter, parameter_kind::ParameterKind, parser::Parser, platform::Platform,
platform_interface::PlatformInterface, position::Position, positional::Positional, ran::Ran,
range_ext::RangeExt, recipe::Recipe, recipe_resolver::RecipeResolver,
recipe_signature::RecipeSignature, scope::Scope, search::Search, search_config::SearchConfig,
search_error::SearchError, set::Set, setting::Setting, settings::Settings, shebang::Shebang,
show_whitespace::ShowWhitespace, source::Source, string_delimiter::StringDelimiter,
string_kind::StringKind, string_literal::StringLiteral, subcommand::Subcommand,
suggestion::Suggestion, table::Table, thunk::Thunk, token::Token, token_kind::TokenKind,
unresolved_dependency::UnresolvedDependency, unresolved_recipe::UnresolvedRecipe,
unstable_feature::UnstableFeature, use_color::UseColor, variables::Variables,
verbosity::Verbosity, warning::Warning,
recipe_signature::RecipeSignature, recipe_signature::SignatureWidths, scope::Scope,
search::Search, search_config::SearchConfig, search_error::SearchError, set::Set,
setting::Setting, settings::Settings, shebang::Shebang, show_whitespace::ShowWhitespace,
source::Source, string_delimiter::StringDelimiter, string_kind::StringKind,
string_literal::StringLiteral, subcommand::Subcommand, suggestion::Suggestion, table::Table,
thunk::Thunk, token::Token, token_kind::TokenKind, unresolved_dependency::UnresolvedDependency,
unresolved_recipe::UnresolvedRecipe, unstable_feature::UnstableFeature, use_color::UseColor,
variables::Variables, verbosity::Verbosity, warning::Warning,
},
camino::Utf8Path,
clap::ValueEnum,
Expand Down Expand Up @@ -151,6 +151,7 @@ mod keyword;
mod lexer;
mod line;
mod list;
mod list_entry;
mod load_dotenv;
mod loader;
mod module_path;
Expand Down
22 changes: 22 additions & 0 deletions src/list_entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::*;

#[derive(Debug, Clone)]
pub(crate) struct ListEntry<'src, 'outer> {
pub(crate) prefix: String,
pub(crate) recipe: &'outer Recipe<'src>,
pub(crate) aliases: Vec<&'src str>,
}

impl<'src, 'outer> ListEntry<'src, 'outer> {
pub(crate) fn from_recipe(
recipe: &'outer Recipe<'src>,
prefix: String,
aliases: Vec<&'src str>,
) -> Self {
Self {
prefix,
recipe,
aliases,
}
}
}
55 changes: 55 additions & 0 deletions src/recipe_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,58 @@ impl<'a> ColorDisplay for RecipeSignature<'a> {
Ok(())
}
}

#[derive(Debug)]
pub(crate) struct SignatureWidths<'a> {
pub(crate) widths: BTreeMap<&'a str, usize>,
pub(crate) threshold: usize,
pub(crate) max_width: usize,
}

impl<'a> SignatureWidths<'a> {
pub fn empty() -> Self {
Self {
widths: BTreeMap::new(),
threshold: 50,
max_width: 0,
}
}

pub fn add_string_custom_width(&mut self, string: &'a str, width: usize) {
self.widths.insert(string, width);
self.max_width = self.max_width.max(width).min(self.threshold);
}

pub fn add_entries<'outer>(&mut self, entries: &Vec<ListEntry<'a, 'outer>>) {
for entry in entries {
self.add_entry(entry);
}
}

pub fn add_entry<'file>(&mut self, entry: &ListEntry<'a, 'file>) {
if !entry.recipe.is_public() {
return;
}

for name in iter::once(entry.recipe.name()).chain(entry.aliases.iter().copied()) {
let format = if entry.prefix.is_empty() {
Cow::Borrowed(name)
} else {
Cow::Owned(format!("{}{}", entry.prefix, name))
};
let width = UnicodeWidthStr::width(
RecipeSignature {
name: &format,
recipe: entry.recipe,
}
.color_display(Color::never())
.to_string()
.as_str(),
);
self.widths.insert(name, width);
if width <= self.threshold {
self.max_width = self.max_width.max(width);
}
}
}
}
Loading