Skip to content

Commit ec5193e

Browse files
authored
Merge pull request #204 from tenstorrent/info-print
Add more helpful information printed in subcommands
2 parents a06e1d0 + 2aec50e commit ec5193e

File tree

5 files changed

+161
-9
lines changed

5 files changed

+161
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1818
- Print dependency updates executed.
1919
- Enable updating of individual dependencies (and recursive dependencies if desired).
2020
- Add support for glob in manifest source files.
21+
- `parents`: Print currently selected version.
22+
- `packages`: Add `--version` flag to print currently used versions.
23+
- `packages`: Add `--targets` flag to print targets used in the corresponding manifest.
2124

2225
### Changed
2326
- `update`: Clean up alignment of manual resolution output.
2427
- `checkout`: When using `checkout_dir`, overwrite existing dependencies if not changed, warning if not checked out, flag to force checkout.
2528
- `update`: Update `checkout_dir` if no internal changes.
2629
- Execute checkout instead of clone to checkout correct dependency versions when reasonable.
30+
- `packages --graph`: Clean up alignment of output.
2731

2832
## 0.28.2 - 2025-03-31
2933
### Fixed

src/cmd/packages.rs

Lines changed: 115 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
//! The `packages` subcommand.
55
6+
use std::io::Write;
7+
68
use clap::{Arg, ArgAction, ArgMatches, Command};
9+
use indexmap::IndexSet;
10+
use tabwriter::TabWriter;
11+
use tokio::runtime::Runtime;
712

813
use crate::error::*;
9-
use crate::sess::Session;
14+
use crate::sess::{DependencySource, Session, SessionIo};
15+
use crate::src::SourceGroup;
16+
use crate::target::TargetSpec;
1017

1118
/// Assemble the `packages` subcommand.
1219
pub fn new() -> Command {
@@ -27,38 +34,132 @@ pub fn new() -> Command {
2734
.help("Do not group packages by topological rank")
2835
.long_help("Do not group packages by topological rank. If the `--graph` option is specified, print multiple lines per package, one for each dependency.")
2936
)
37+
.arg(Arg::new("version")
38+
.long("version")
39+
.num_args(0)
40+
.action(ArgAction::SetTrue)
41+
.help("Print the version of each package")
42+
.long_help("Print the version of each package. Implies --flat. More detailed information is available per dependency using the `parents` subcommand.")
43+
)
44+
.arg(Arg::new("targets")
45+
.long("targets")
46+
.num_args(0)
47+
.action(ArgAction::SetTrue)
48+
.help("Print the targets available for each package")
49+
)
3050
}
3151

3252
/// Execute the `packages` subcommand.
3353
pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
3454
let graph = matches.get_flag("graph");
3555
let flat = matches.get_flag("flat");
36-
if graph {
56+
let version = matches.get_flag("version");
57+
if graph && version {
58+
return Err(Error::new("cannot specify both --graph and --version"));
59+
}
60+
let targets = matches.get_flag("targets");
61+
if targets {
62+
if graph {
63+
return Err(Error::new("cannot specify both --graph and --targets"));
64+
}
65+
let rt = Runtime::new()?;
66+
let io = SessionIo::new(sess);
67+
let srcs = rt.block_on(io.sources(false, &[]))?;
68+
let mut target_str = String::from("");
69+
for pkgs in sess.packages().iter() {
70+
let pkg_names = pkgs.iter().map(|&id| sess.dependency_name(id));
71+
for pkg_name in pkg_names {
72+
target_str.push_str(&format!(
73+
"{}:\t{:?}\n",
74+
pkg_name,
75+
srcs.filter_packages(&IndexSet::from([pkg_name.into()]))
76+
.unwrap_or_else(|| SourceGroup {
77+
package: Default::default(),
78+
independent: true,
79+
target: TargetSpec::Wildcard,
80+
include_dirs: Default::default(),
81+
export_incdirs: Default::default(),
82+
defines: Default::default(),
83+
files: Default::default(),
84+
dependencies: Default::default(),
85+
version: None,
86+
})
87+
.get_avail_targets()
88+
));
89+
}
90+
}
91+
target_str.push_str(&format!(
92+
"{}:\t{:?}\n",
93+
&sess.manifest.package.name,
94+
srcs.filter_packages(&IndexSet::from([sess.manifest.package.name.clone()]))
95+
.unwrap_or_else(|| SourceGroup {
96+
package: Default::default(),
97+
independent: true,
98+
target: TargetSpec::Wildcard,
99+
include_dirs: Default::default(),
100+
export_incdirs: Default::default(),
101+
defines: Default::default(),
102+
files: Default::default(),
103+
dependencies: Default::default(),
104+
version: None,
105+
})
106+
.get_avail_targets()
107+
));
108+
let mut tw = TabWriter::new(vec![]);
109+
write!(&mut tw, "{}", target_str).unwrap();
110+
tw.flush().unwrap();
111+
print!("{}", String::from_utf8(tw.into_inner().unwrap()).unwrap());
112+
} else if graph {
113+
let mut graph_str = String::from("");
37114
for (&pkg, deps) in sess.graph().iter() {
38115
let pkg_name = sess.dependency_name(pkg);
39116
let dep_names = deps.iter().map(|&id| sess.dependency_name(id));
40117
if flat {
41118
// Print one line per dependency.
42119
for dep_name in dep_names {
43-
println!("{}\t{}", pkg_name, dep_name);
120+
graph_str.push_str(&format!("{}\t{}\n", pkg_name, dep_name));
44121
}
45122
} else {
46123
// Print all dependencies on one line.
47-
print!("{}\t", pkg_name);
124+
graph_str.push_str(&format!("{}\t", pkg_name));
48125
for (i, dep_name) in dep_names.enumerate() {
49126
if i > 0 {
50-
print!(" {}", dep_name);
127+
graph_str.push_str(&format!(" {}", dep_name));
51128
} else {
52-
print!("{}", dep_name);
129+
graph_str.push_str(dep_name);
53130
}
54131
}
55-
println!();
132+
graph_str.push('\n');
56133
}
57134
}
135+
let mut tw = TabWriter::new(vec![]);
136+
write!(&mut tw, "{}", graph_str).unwrap();
137+
tw.flush().unwrap();
138+
print!("{}", String::from_utf8(tw.into_inner().unwrap()).unwrap());
58139
} else {
140+
let mut version_str = String::from("");
59141
for pkgs in sess.packages().iter() {
60142
let pkg_names = pkgs.iter().map(|&id| sess.dependency_name(id));
61-
if flat {
143+
let pkg_sources = pkgs.iter().map(|&id| sess.dependency(id));
144+
if version {
145+
for pkg_source in pkg_sources {
146+
version_str.push_str(&format!(
147+
"{}:\t{}\tat {}\t{}\n",
148+
pkg_source.name,
149+
match pkg_source.version {
150+
Some(ref v) => format!("v{}", v),
151+
None => "".to_string(),
152+
},
153+
pkg_source.source,
154+
match pkg_source.source {
155+
DependencySource::Path { .. } => " as path".to_string(),
156+
DependencySource::Git(_) =>
157+
format!(" with hash {}", pkg_source.version()),
158+
_ => "".to_string(),
159+
}
160+
));
161+
}
162+
} else if flat {
62163
// Print one line per package.
63164
for pkg_name in pkg_names {
64165
println!("{}", pkg_name);
@@ -75,6 +176,12 @@ pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
75176
println!();
76177
}
77178
}
179+
if version {
180+
let mut tw = TabWriter::new(vec![]);
181+
write!(&mut tw, "{}", version_str).unwrap();
182+
tw.flush().unwrap();
183+
print!("{}", String::from_utf8(tw.into_inner().unwrap()).unwrap());
184+
}
78185
}
79186
Ok(())
80187
}

src/cmd/parents.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn new() -> Command {
2929
/// Execute the `parents` subcommand.
3030
pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
3131
let dep = &matches.get_one::<String>("name").unwrap().to_lowercase();
32-
sess.dependency_with_name(dep)?;
32+
let mydep = sess.dependency_with_name(dep)?;
3333
let rt = Runtime::new()?;
3434
let io = SessionIo::new(sess);
3535

@@ -115,6 +115,21 @@ pub fn run(sess: &Session, matches: &ArgMatches) -> Result<()> {
115115
print!("{}", String::from_utf8(tw.into_inner().unwrap()).unwrap());
116116
}
117117

118+
println!(
119+
"{} used version: {} at {}{}",
120+
sess.dependency(mydep).name,
121+
match sess.dependency(mydep).version.clone() {
122+
Some(ver) => ver.to_string(),
123+
None => "".to_string(),
124+
},
125+
sess.dependency(mydep).source,
126+
match sess.dependency(mydep).source {
127+
DependencySource::Path { .. } => " as path".to_string(),
128+
DependencySource::Git(_) => format!(" with hash {}", sess.dependency(mydep).version()),
129+
_ => "".to_string(),
130+
}
131+
);
132+
118133
if sess.config.overrides.contains_key(dep) {
119134
warnln!(
120135
"An override is configured for {} to {:?}",

src/src.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ impl<'ctx> SourceGroup<'ctx> {
268268
}
269269
flush_files(&mut files, into);
270270
}
271+
272+
/// Get available targets in sourcegroup.
273+
pub fn get_avail_targets(&self) -> IndexSet<String> {
274+
let mut targets = self.target.get_avail();
275+
for file in &self.files {
276+
match file {
277+
SourceFile::File(_) => {}
278+
SourceFile::Group(group) => {
279+
targets.extend(group.get_avail_targets());
280+
}
281+
}
282+
}
283+
targets
284+
}
271285
}
272286

273287
/// A source file.

src/target.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ impl TargetSpec {
122122
pub fn is_wildcard(&self) -> bool {
123123
matches!(*self, TargetSpec::Wildcard)
124124
}
125+
126+
/// Get list of available targets.
127+
pub fn get_avail(&self) -> IndexSet<String> {
128+
match *self {
129+
TargetSpec::Wildcard => IndexSet::new(),
130+
TargetSpec::Name(ref name) => IndexSet::from([name.clone()]),
131+
TargetSpec::All(ref specs) | TargetSpec::Any(ref specs) => {
132+
specs.iter().flat_map(TargetSpec::get_avail).collect()
133+
}
134+
TargetSpec::Not(ref spec) => spec.get_avail(),
135+
}
136+
}
125137
}
126138

127139
#[derive(Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)